I have written a function that launches the default editor set in git config
, right now I have managed to get it working for Sublime, nano and Vim.
def launchEditor(editor):
""" this function launches the default editor
for user to compose message to be sent
along with git diff.
Args:
editor(str): name or path of editor
Returns:
msg(str): html formatted message
"""
filePath = os.path.join(os.getcwd(), "compose.txt")
wfh = open(filePath, 'w')
wfh.close()
if os.path.exists(filePath):
# using sublime
if re.search(r'ubl', editor):
diff = subprocess.Popen(['cat',filePath], stdout=subprocess.PIPE)
pr = subprocess.Popen(
editor,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
stdin=diff.stdout
)
pr.wait()
if pr.returncode == 0:
msg = pr.stdout.read()
else:
# using vim or nano
pr = subprocess.Popen([editor, filePath], stdin=open('/dev/tty', 'r'))
pr.wait()
if pr.returncode == 0:
with open(filePath, 'r') as fh:
msg = fh.readlines()
os.remove(filePath)
return "".join(msg).replace("\n","<br>")
Any suggestion on improvement and adding support to other text editors is welcome!!
Revision update:
Traceback (most recent call last):
File "/Users/san/Development/executables//git-ipush", line 268, in <module>
sys.exit(main())
File "/Users/san/Development/executables//git-ipush", line 46, in main
preCheck(args)
File "/Users/sanjeevkumar/Development/executables//git-ipush", line 156, in preCheck
message = launchEditor(editor)
File "/Users/san/Development/executables//git-ipush", line 77, in launchEditor
if subprocess.call([editor, f.name]) != 0:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory