]> git.decadent.org.uk Git - dak.git/commitdiff
utils.py: add call_editor function
authorAnsgar Burchardt <ansgar@debian.org>
Thu, 5 Jul 2012 16:23:28 +0000 (10:23 -0600)
committerAnsgar Burchardt <ansgar@debian.org>
Fri, 6 Jul 2012 16:37:19 +0000 (10:37 -0600)
Signed-off-by: Ansgar Burchardt <ansgar@debian.org>
daklib/utils.py

index 3e299abc51709a4b4b3b3c308039068048195cc9..e5431c8bc63854337b0b0d9b518f58ff547bf2a5 100755 (executable)
@@ -1595,3 +1595,25 @@ def mail_addresses_for_upload(maintainer, changed_by, fingerprint):
 
     encoded_addresses = [ fix_maintainer(e)[1] for e in addresses ]
     return encoded_addresses
+
+################################################################################
+
+def call_editor(text="", suffix=".txt"):
+    """Run editor and return the result as a string
+
+    Kwargs:
+       text (str): initial text
+       suffix (str): extension for temporary file
+
+    Returns:
+       string with the edited text
+    """
+    editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vi'))
+    tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
+    try:
+        print >>tmp, text,
+        tmp.close()
+        subprocess.check_call([editor, tmp.name])
+        return open(tmp.name, 'r').read()
+    finally:
+        os.unlink(tmp.name)