Use NamedTemporaryFile instead of mkstemp.

Also fixed EDITOR not being defined causing a crash.
This commit is contained in:
noah morrison
2015-03-23 22:31:58 -04:00
parent 08c78c54ec
commit c00a9fee3d

View File

@@ -1,9 +1,9 @@
import curses import curses
import sys import sys
import time import time
import tempfile
import os import os
import uuid from uuid import uuid4
from tempfile import NamedTemporaryFile
import praw.errors import praw.errors
@@ -249,25 +249,29 @@ class SubmissionPage(BasePage):
curses.endwin() curses.endwin()
fd, filename = tempfile.mkstemp(prefix="rtv-comment-", suffix=".txt") cid = str(uuid4())
cid = str(uuid.uuid4()) if data['type'] == 'Submission':
try: info = (data['author'], 'submission', data['text'])
with open(filename, 'w') as comment_file: else:
if data['type'] == 'Submission': info = (data['author'], 'comment', data['body'])
info = (data['author'], 'submission', data['text'])
else:
info = (data['author'], 'comment', data['body'])
comment_info = COMMENT_FILE.format(cid, *info) comment_info = COMMENT_FILE.format(cid, *info)
comment_file.write(comment_info)
os.system('%s %s' % (os.getenv('EDITOR'), filename)) with NamedTemporaryFile(prefix='rtv-comment-', mode='w+') as fp:
with open(filename, 'r') as comment_file: fp.write(comment_info)
comment_text = (comment_file.read() fp.flush()
.split('--% ' + cid + ' %--'))[0]
finally: editor = os.getenv('RTVEDITOR') or os.getenv('EDITOR')
os.remove(filename) if editor is None:
curses.doupdate() show_notification(self.stdscr, ['No EDITOR defined'])
return
os.system(editor + ' ' + fp.name)
fp.seek(0)
comment_text = fp.read().split('--% ' + cid + ' %--')[0]
curses.doupdate()
if comment_text is None or comment_text.isspace(): if comment_text is None or comment_text.isspace():
return return