#!/usr/bin/env python
import os
from tempfile import mkstemp
from cStringIO import StringIO
def select_editor( ):
editCmd = os.environ.get( "EDITOR" )
if editCmd != None:
return editCmd
else:
return None
def external_edit( content = None, editCmd = None ):
"Launches an external editor on a temporary file containing content, and returns the content when the editor exits."
if content is None:
content = ''
if editCmd is None:
editCmd = select_editor()
if editCmd is None: # Still?
return None
fileFd, fileName = mkstemp()
os.write( fileFd, content )
os.close( fileFd )
print editCmd + " " + fileName
if os.system( editCmd + " " + fileName ) == 0:
content = "".join( file( fileName ).readlines() )
else:
content = None
os.unlink( fileName )
return content
if __name__ == "__main__":
print external_edit( "Hello, world!" )
|