From 6c2f0f6f8dfaca4fc62c8751168c759b2f3c3398 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Wed, 24 May 2017 13:51:02 -0400 Subject: [PATCH] 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.) --- pubs/content.py | 29 ----------------------------- pubs/uis.py | 35 ++++++++++++++++++++++++++++++++--- tests/fake_env.py | 2 +- tests/test_usecase.py | 7 ++++--- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/pubs/content.py b/pubs/content.py index f17bad4..63c27aa 100644 --- a/pubs/content.py +++ b/pubs/content.py @@ -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) diff --git a/pubs/uis.py b/pubs/uis.py index 3409782..d1166f2 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -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) diff --git a/tests/fake_env.py b/tests/fake_env.py index 52b98ef..1e7c3bf 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -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 diff --git a/tests/test_usecase.py b/tests/test_usecase.py index a73d03f..1120141 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -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()