From 49a7771336ce09f6d42c7699ef32aecea0e83182 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 7 Dec 2009 20:07:55 -0500 Subject: Initial directory restructuring to clarify dependencies --- libbe/ui/util/editor.py | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 libbe/ui/util/editor.py (limited to 'libbe/ui/util/editor.py') diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py new file mode 100644 index 0000000..859cedc --- /dev/null +++ b/libbe/ui/util/editor.py @@ -0,0 +1,113 @@ +# Bugs Everywhere, a distributed bugtracker +# Copyright (C) 2008-2009 Gianluca Montecchi +# W. Trevor King +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" +Define editor_string(), a function that invokes an editor to accept +user-produced text as a string. +""" + +import codecs +import locale +import os +import sys +import tempfile + +import libbe +if libbe.TESTING == True: + import doctest + + +default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding() + +comment_marker = u"== Anything below this line will be ignored\n" + +class CantFindEditor(Exception): + def __init__(self): + Exception.__init__(self, "Can't find editor to get string from") + +def editor_string(comment=None, encoding=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"] + """ + if encoding == None: + encoding = default_encoding + 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: + cstring = u'\n'+comment_string(comment) + os.write(fhandle, cstring.encode(encoding)) + os.close(fhandle) + oldmtime = os.path.getmtime(fname) + os.system("%s %s" % (editor, fname)) + f = codecs.open(fname, "r", encoding) + output = trimmed_string(f.read()) + f.close() + if output.rstrip('\n') == "": + output = None + finally: + os.unlink(fname) + return output + + +def comment_string(comment): + """ + >>> comment_string('hello') == comment_marker+"hello" + True + """ + return comment_marker + comment + + +def trimmed_string(instring): + """ + >>> trimmed_string("hello\\n"+comment_marker) + u'hello\\n' + >>> trimmed_string("hi!\\n" + comment_string('Booga')) + u'hi!\\n' + """ + out = [] + for line in instring.splitlines(True): + if line.startswith(comment_marker): + break + out.append(line) + return ''.join(out) + +if libbe.TESTING == True: + suite = doctest.DocTestSuite() -- cgit From 19fe0817ba7c2cd04caea3adfa82d4490288a548 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 07:37:51 -0500 Subject: Transitioned comment to Command format --- libbe/ui/util/editor.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'libbe/ui/util/editor.py') diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py index 859cedc..83262e7 100644 --- a/libbe/ui/util/editor.py +++ b/libbe/ui/util/editor.py @@ -28,12 +28,12 @@ import sys import tempfile import libbe +import libbe.util.encoding + if libbe.TESTING == True: import doctest -default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding() - comment_marker = u"== Anything below this line will be ignored\n" class CantFindEditor(Exception): @@ -60,7 +60,7 @@ def editor_string(comment=None, encoding=None): >>> del os.environ["VISUAL"] """ if encoding == None: - encoding = default_encoding + encoding = libbe.util.encoding.get_filesystem_encoding() for name in ('VISUAL', 'EDITOR'): try: editor = os.environ[name] @@ -77,9 +77,8 @@ def editor_string(comment=None, encoding=None): os.close(fhandle) oldmtime = os.path.getmtime(fname) os.system("%s %s" % (editor, fname)) - f = codecs.open(fname, "r", encoding) - output = trimmed_string(f.read()) - f.close() + output = libbe.util.encoding.get_file_contents( + fname, encoding=encoding, decode=True) if output.rstrip('\n') == "": output = None finally: -- cgit From 4fbf5d1d222610b0775f95472fe1a60aaedea29f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 28 Dec 2009 10:40:48 -0500 Subject: Restore comment stripping to libbe.ui.util.editor.editor_string() --- libbe/ui/util/editor.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'libbe/ui/util/editor.py') diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py index 83262e7..1a10fa4 100644 --- a/libbe/ui/util/editor.py +++ b/libbe/ui/util/editor.py @@ -56,6 +56,9 @@ def editor_string(comment=None, encoding=None): >>> os.environ["VISUAL"] = "echo baz > " >>> editor_string() u'baz\\n' + >>> os.environ["VISUAL"] = "echo 'baz\\n== Anything below this line will be ignored\\nHi' > " + >>> editor_string() + u'baz\\n' >>> del os.environ["EDITOR"] >>> del os.environ["VISUAL"] """ @@ -79,6 +82,7 @@ def editor_string(comment=None, encoding=None): os.system("%s %s" % (editor, fname)) output = libbe.util.encoding.get_file_contents( fname, encoding=encoding, decode=True) + output = trimmed_string(output) if output.rstrip('\n') == "": output = None finally: -- cgit From 4d4283ecd654f1efb058cd7f7dba6be88b70ee92 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 1 Jan 2010 08:11:08 -0500 Subject: Updated copyright information --- libbe/ui/util/editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/ui/util/editor.py') diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py index 1a10fa4..ce14365 100644 --- a/libbe/ui/util/editor.py +++ b/libbe/ui/util/editor.py @@ -1,5 +1,5 @@ # Bugs Everywhere, a distributed bugtracker -# Copyright (C) 2008-2009 Gianluca Montecchi +# Copyright (C) 2008-2010 Gianluca Montecchi # W. Trevor King # # This program is free software; you can redistribute it and/or modify -- cgit From 12a948ddf009fb2510eda5be4f576dc3e7de401f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 29 Jan 2010 07:32:41 -0500 Subject: Fix editor spawning on null-string EDITOR and VISUAL. $ EDITOR= VISUAL= python -c 'import os; import sys; print os.environ' {..., 'EDITOR': '', ..., 'VISUAL': '', ...} --- libbe/ui/util/editor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libbe/ui/util/editor.py') diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py index ce14365..1a430c7 100644 --- a/libbe/ui/util/editor.py +++ b/libbe/ui/util/editor.py @@ -64,13 +64,12 @@ def editor_string(comment=None, encoding=None): """ if encoding == None: encoding = libbe.util.encoding.get_filesystem_encoding() + editor = None for name in ('VISUAL', 'EDITOR'): - try: + if name in os.environ and os.environ[name] != '': editor = os.environ[name] break - except KeyError: - pass - else: + if editor == None: raise CantFindEditor() fhandle, fname = tempfile.mkstemp() try: -- cgit