From e09c0e047e13e48b9983df0e80c2b3096baeb49d Mon Sep 17 00:00:00 2001 From: Ansgar Burchardt Date: Thu, 5 Jul 2012 10:23:28 -0600 Subject: [PATCH] utils.py: add call_editor function Signed-off-by: Ansgar Burchardt --- daklib/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/daklib/utils.py b/daklib/utils.py index 3e299abc..e5431c8b 100755 --- a/daklib/utils.py +++ b/daklib/utils.py @@ -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) -- 2.39.2