Added editor_string utility function
authorAaron Bentley <abentley@panoramicfeedback.com>
Wed, 23 Mar 2005 18:30:11 +0000 (18:30 +0000)
committerAaron Bentley <abentley@panoramicfeedback.com>
Wed, 23 Mar 2005 18:30:11 +0000 (18:30 +0000)
libbe/utility.py

index a2774d3824faaf67f7d93113ff845a552d1491a1..3ed4bda015d88bcbbc84f7459d204d52afa3cb6d 100644 (file)
@@ -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
+