From db039bf8987e6148bca5fc3bd417ad0d04dfa3ab Mon Sep 17 00:00:00 2001 From: Aaron Bentley Date: Wed, 23 Mar 2005 18:30:11 +0000 Subject: Added editor_string utility function --- libbe/utility.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'libbe/utility.py') diff --git a/libbe/utility.py b/libbe/utility.py index a2774d3..3ed4bda 100644 --- a/libbe/utility.py +++ b/libbe/utility.py @@ -1,5 +1,7 @@ import calendar import time +import os +import tempfile class FileString(object): """Bare-bones pseudo-file class @@ -76,4 +78,25 @@ def str_to_time(str_time): def handy_time(time_val): return time.strftime("%a, %d %b %Y %H:%M", time.localtime(time_val)) - + +def editor_string(): + """Invokes the editor, and returns the user_produced text as a string""" + try: + editor = os.environ["EDITOR"] + except KeyError: + raise cmdutil.UserError( + "No comment supplied, and EDITOR not specified.") + + fhandle, fname = tempfile.mkstemp() + try: + os.close(fhandle) + oldmtime = os.path.getmtime(fname) + os.system("%s %s" % (editor, fname)) + if oldmtime == os.path.getmtime(fname): + output = None + else: + output = file(fname, "rb").read() + finally: + os.unlink(fname) + return output + -- cgit