Fix for gedit.

This commit is contained in:
Michael Lazar
2015-03-29 16:57:10 -07:00
parent 5edc54f684
commit e2b0a27468
2 changed files with 27 additions and 25 deletions

View File

@@ -3,25 +3,36 @@ import os
import textwrap
import subprocess
from datetime import datetime
from tempfile import NamedTemporaryFile
from . import config
__all__ = ['open_browser', 'clean', 'wrap_text', 'strip_textpad',
'strip_subreddit_url', 'humanize_timestamp']
'strip_subreddit_url', 'humanize_timestamp', 'open_editor']
def open_editor(filename):
def open_editor(data=''):
"""
Open the given file using the system's default editor.
Open a temporary file using the system's default editor.
The data string will be written to the file before opening. This function
will block until the editor has closed. At that point the file will be
read and and lines starting with '#' will be stripped.
"""
editors = [os.getenv('RTV_EDITOR'), os.getenv('EDITOR'), 'nano', 'vi']
for program in editors:
if program:
try:
subprocess.Popen([program, filename]).wait()
break
except OSError:
pass
with NamedTemporaryFile(prefix='rtv-', suffix='.txt', mode='w') as fp:
fp.write(data)
fp.flush()
editor = os.getenv('RTV_EDITOR') or os.getenv('EDITOR') or 'nano'
subprocess.Popen([editor, fp.name]).wait()
# Open a second file object to read. This appears to be necessary in
# order to read the changes made by some editors (gedit). w+ mode does
# not work!
with open(fp.name) as fp2:
text = ''.join(line for line in fp2 if not line.startswith('#'))
text = text.rstrip()
return text
def open_browser(url):
"""

View File

@@ -1,7 +1,6 @@
import curses
import sys
import time
from tempfile import NamedTemporaryFile
import praw.errors
@@ -126,26 +125,18 @@ class SubmissionPage(BasePage):
# Comment out every line of the content
content = '\n'.join(['# |' + line for line in content.split('\n')])
info = {'author': data['author'],
'type': data['type'],
'content': content}
comment_info = COMMENT_FILE.format(**info)
comment_info = COMMENT_FILE.format(
author=data['author'],
type=data['type'].lower(),
content=content)
curses.endwin()
with NamedTemporaryFile(prefix='rtv-comment-', mode='w+') as fp:
fp.write(comment_info)
fp.flush()
open_editor(fp.name) # Open the default text editor and block
fp.seek(0)
comment_text = '\n'.join(line for line in fp.read().split('\n')
if not line.startswith("#"))
comment_text = comment_text.rstrip()
comment_text = open_editor(comment_info)
curses.doupdate()
if not comment_text:
curses.flash()
return
try:
if data['type'] == 'Submission':
data['object'].add_comment(comment_text)