aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/utility.py
diff options
context:
space:
mode:
authorW. Trevor King <wking@drexel.edu>2008-11-25 15:47:19 -0500
committerW. Trevor King <wking@drexel.edu>2008-11-25 15:47:19 -0500
commit5699aef2a5741c5ffc24d9cb12d6bc9b085d484a (patch)
tree84ac8783ce2d1f9a28ba0bd0c256ae7179c68507 /libbe/utility.py
parented4d971d1375a692fbd3a394237f56e851bb5d0e (diff)
downloadbugseverywhere-5699aef2a5741c5ffc24d9cb12d6bc9b085d484a.tar.gz
Added libbe/encoding.py to wrap input/output/file access appropriately.
I borrowed most of the code for this. get_encoding() is from Trac http://trac.edgewall.org/browser/trunk/trac/util/datefmt.py format_datetime() Trac has a BSD license http://trac.edgewall.org/wiki/TracLicense I don't know if such a small snippet requires us to "reproduce the above copyright" or where we need to reproduce it if it is needed. The stdout/stdin replacement code follows http://wiki.python.org/moin/ShellRedirectionFails Because of the stdout replacement, the doctests executes now need an optional 'test' argument to turn off replacement during the doctests, otherwise doctest flips out (since it had set up stdout to catch output, and then we clobbered it's setup). References: http://wiki.python.org/moin/Unicode http://www.amk.ca/python/howto/unicode http://www.python.org/dev/peps/pep-0100/ I also split libbe/editor.py off from libbe.utility.py and started explaining the motivation for the BugDir init flags in it's docstring.
Diffstat (limited to 'libbe/utility.py')
-rw-r--r--libbe/utility.py73
1 files changed, 3 insertions, 70 deletions
diff --git a/libbe/utility.py b/libbe/utility.py
index 2c77fcf..30240a9 100644
--- a/libbe/utility.py
+++ b/libbe/utility.py
@@ -15,10 +15,11 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import calendar
-import time
+import codecs
import os
-import tempfile
import shutil
+import tempfile
+import time
import doctest
@@ -87,73 +88,5 @@ def str_to_time(str_time):
def handy_time(time_val):
return time.strftime("%a, %d %b %Y %H:%M", time.localtime(time_val))
-class CantFindEditor(Exception):
- def __init__(self):
- Exception.__init__(self, "Can't find editor to get string from")
-
-def editor_string(comment=None):
-
- """Invokes the editor, and returns the user_produced text as a string
-
- >>> if "EDITOR" in os.environ:
- ... del os.environ["EDITOR"]
- >>> if "VISUAL" in os.environ:
- ... del os.environ["VISUAL"]
- >>> editor_string()
- Traceback (most recent call last):
- CantFindEditor: Can't find editor to get string from
- >>> os.environ["EDITOR"] = "echo bar > "
- >>> editor_string()
- u'bar\\n'
- >>> os.environ["VISUAL"] = "echo baz > "
- >>> editor_string()
- u'baz\\n'
- >>> del os.environ["EDITOR"]
- >>> del os.environ["VISUAL"]
- """
- for name in ('VISUAL', 'EDITOR'):
- try:
- editor = os.environ[name]
- break
- except KeyError:
- pass
- else:
- raise CantFindEditor()
- fhandle, fname = tempfile.mkstemp()
- try:
- if comment is not None:
- os.write(fhandle, '\n'+comment_string(comment))
- os.close(fhandle)
- oldmtime = os.path.getmtime(fname)
- os.system("%s %s" % (editor, fname))
- output = trimmed_string(file(fname, "rb").read().decode("utf-8"))
- if output.rstrip('\n') == "":
- output = None
- finally:
- os.unlink(fname)
- return output
-
-
-def comment_string(comment):
- """
- >>> comment_string('hello')
- '== Anything below this line will be ignored ==\\nhello'
- """
- return '== Anything below this line will be ignored ==\n' + comment
-
-
-def trimmed_string(instring):
- """
- >>> trimmed_string("hello\\n== Anything below this line will be ignored")
- 'hello\\n'
- >>> trimmed_string("hi!\\n" + comment_string('Booga'))
- 'hi!\\n'
- """
- out = []
- for line in instring.splitlines(True):
- if line.startswith('== Anything below this line will be ignored'):
- break
- out.append(line)
- return ''.join(out)
suite = doctest.DocTestSuite()