Moves editor commands to ui to avoid using with wrong editor values.

For now, the editor_input and edit_file functions do not make sense
outside of the context of an ui. Having them in the content module
was confusing. (See issue fixed by a600855.)
main
Olivier Mangin 8 years ago
parent a600855c31
commit 6c2f0f6f8d

@ -1,9 +1,6 @@
import os
import io
import subprocess
import tempfile
import shutil
import shlex
from .p3 import urlparse, HTTPConnection, urlopen
@ -166,29 +163,3 @@ def copy_content(source, target, overwrite=False):
_dump_byte_url_content(source, target)
else:
shutil.copy(source, target)
def editor_input(editor, initial=u'', suffix='.tmp'):
"""Use an editor to get input"""
str_initial = initial.encode('utf-8') # TODO: make it a configuration item
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file:
tfile_name = temp_file.name
temp_file.write(str_initial)
cmd = shlex.split(editor) # this enable editor command with option, e.g. gvim -f
cmd.append(tfile_name)
subprocess.call(cmd)
content = read_text_file(tfile_name)
os.remove(tfile_name)
return content
def edit_file(editor, path_to_file, temporary=True):
if temporary:
check_file(path_to_file, fail=True)
content = read_text_file(path_to_file)
content = editor_input(editor, content)
write_file(path_to_file, content)
else:
cmd = editor.split() # this enable editor command with option, e.g. gvim -f
cmd.append(path_to_file)
subprocess.call(cmd)

@ -2,13 +2,16 @@ from __future__ import print_function
import os
import sys
import shlex
import locale
import codecs
import tempfile
import subprocess
from .content import editor_input, edit_file
from . import color
from . import config
from .p3 import _get_raw_stdout, _get_raw_stderr, input, ustr
from .content import check_file, read_text_file, write_file
# package-shared ui that can be accessed using :
@ -38,6 +41,32 @@ def _get_local_editor():
return os.environ.get('EDITOR', 'nano')
def _editor_input(editor, initial=u'', suffix='.tmp'):
"""Use an editor to get input"""
str_initial = initial.encode('utf-8') # TODO: make it a configuration item
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file:
tfile_name = temp_file.name
temp_file.write(str_initial)
cmd = shlex.split(editor) # this enable editor command with option, e.g. gvim -f
cmd.append(tfile_name)
subprocess.call(cmd)
content = read_text_file(tfile_name)
os.remove(tfile_name)
return content
def _edit_file(editor, path_to_file, temporary=True):
if temporary:
check_file(path_to_file, fail=True)
content = read_text_file(path_to_file)
content = _editor_input(editor, content)
write_file(path_to_file, content)
else:
cmd = editor.split() # this enable editor command with option, e.g. gvim -f
cmd.append(path_to_file)
subprocess.call(cmd)
def get_ui():
if _ui is None:
return PrintUI(config.load_default_conf()) # no editor support. (#FIXME?)
@ -187,7 +216,7 @@ class InputUI(PrintUI):
return [True, False][answer]
def editor_input(self, initial="", suffix='.tmp'):
return editor_input(self.editor, initial=initial, suffix=suffix)
return _editor_input(self.editor, initial=initial, suffix=suffix)
def edit_file(self, path, temporary):
edit_file(self.editor, path, temporary=temporary)
_edit_file(self.editor, path, temporary=temporary)

@ -89,7 +89,7 @@ class FakeInput():
def as_global(self):
for md in self.module_list:
md.input = self
md.editor_input = self
md._editor_input = self
# if mdname.endswith('files'):
# md.editor_input = self

@ -37,6 +37,7 @@ class FakeSystemExit(Exception):
"""
pass
# code for fake fs
class TestFakeInput(unittest.TestCase):
@ -58,10 +59,10 @@ class TestFakeInput(unittest.TestCase):
def test_editor_input(self):
other_input = fake_env.FakeInput(['yes', 'no'],
module_list=[content, color])
module_list=[uis, color])
other_input.as_global()
self.assertEqual(content.editor_input(), 'yes')
self.assertEqual(content.editor_input(), 'no')
self.assertEqual(uis._editor_input(), 'yes')
self.assertEqual(uis._editor_input(), 'no')
with self.assertRaises(fake_env.FakeInput.UnexpectedInput):
color.input()

Loading…
Cancel
Save