error message when editor is missing
simplified a bit the ui code, to remove unecessary out-of-class functions (needed anyway because of ui.error calls).
This commit is contained in:
parent
29aed39bf8
commit
6cd74a9966
64
pubs/uis.py
64
pubs/uis.py
@ -43,32 +43,6 @@ def _get_local_editor():
|
||||
return os.environ.get('EDITOR', 'nano')
|
||||
|
||||
|
||||
def _editor_input(editor, initial='', 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(system_path(path_to_file))
|
||||
subprocess.call(cmd)
|
||||
|
||||
|
||||
def get_ui():
|
||||
if _ui is None:
|
||||
return PrintUI(config.load_default_conf()) # no editor support. (#FIXME?)
|
||||
@ -223,7 +197,41 @@ class InputUI(PrintUI):
|
||||
return [True, False][answer]
|
||||
|
||||
def editor_input(self, initial="", suffix='.tmp'):
|
||||
return _editor_input(self.editor, initial=initial, suffix=suffix)
|
||||
"""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)
|
||||
self._call_editor(tfile_name)
|
||||
content = read_text_file(tfile_name)
|
||||
os.remove(tfile_name)
|
||||
return content
|
||||
|
||||
def edit_file(self, path, temporary):
|
||||
_edit_file(self.editor, path, temporary=temporary)
|
||||
if temporary:
|
||||
check_file(path, fail=True)
|
||||
content = read_text_file(path)
|
||||
content = self.editor_input(content)
|
||||
write_file(path, content)
|
||||
else:
|
||||
self._call_editor(path)
|
||||
|
||||
def _call_editor(self, path):
|
||||
"""Call the editor, and checks that no error were raised by the OS"""
|
||||
cmd = shlex.split(self.editor) # this enable editor command with option, e.g. gvim -f
|
||||
cmd.append(path)
|
||||
try:
|
||||
subprocess.call(cmd)
|
||||
except OSError as e:
|
||||
if e.errno == os.errno.ENOENT:
|
||||
self.error(("Error while calling editor '{}'. The editor may "
|
||||
"not be present. You can change the text editor "
|
||||
"that pubs uses by setting the $EDITOR environment "
|
||||
"variable, or by running `pubs conf` and setting "
|
||||
"the `edit_cmd` field."
|
||||
).format(self.editor))
|
||||
# handle file not found error.
|
||||
self.exit()
|
||||
else:
|
||||
# Something else went wrong while trying to run `wget`
|
||||
self.handle_exception(e)
|
||||
|
@ -74,12 +74,11 @@ class FakeInput():
|
||||
def as_global(self):
|
||||
for md in self.module_list:
|
||||
md.input = self
|
||||
md._editor_input = self
|
||||
md._edit_file = self.input_to_file
|
||||
# if mdname.endswith('files'):
|
||||
# md.editor_input = self
|
||||
if md.__name__ == 'pubs.uis':
|
||||
md.InputUI.editor_input = self
|
||||
md.InputUI.edit_file = self.input_to_file
|
||||
|
||||
def input_to_file(self, _, path_to_file, temporary=True):
|
||||
def input_to_file(self, path_to_file, temporary=True):
|
||||
content.write_file(path_to_file, self())
|
||||
|
||||
def add_input(self, inp):
|
||||
|
@ -45,9 +45,8 @@ class FakeSystemExit(Exception):
|
||||
"Exited with code: {}.".format(self.code), *args)
|
||||
|
||||
|
||||
# code for fake fs
|
||||
|
||||
class TestFakeInput(unittest.TestCase):
|
||||
class TestInput(unittest.TestCase):
|
||||
"""Test that the fake input mechanisms work correctly in the tests"""
|
||||
|
||||
def test_input(self):
|
||||
input = fake_env.FakeInput(['yes', 'no'])
|
||||
@ -65,13 +64,16 @@ class TestFakeInput(unittest.TestCase):
|
||||
color.input()
|
||||
|
||||
def test_editor_input(self):
|
||||
sample_conf = conf.load_default_conf()
|
||||
ui = uis.InputUI(sample_conf)
|
||||
|
||||
other_input = fake_env.FakeInput(['yes', 'no'],
|
||||
module_list=[uis, color])
|
||||
other_input.as_global()
|
||||
self.assertEqual(uis._editor_input(), 'yes')
|
||||
self.assertEqual(uis._editor_input(), 'no')
|
||||
self.assertEqual(ui.editor_input(), 'yes')
|
||||
self.assertEqual(ui.editor_input(), 'no')
|
||||
with self.assertRaises(fake_env.FakeInput.UnexpectedInput):
|
||||
color.input()
|
||||
ui.editor_input()
|
||||
|
||||
|
||||
class CommandTestCase(fake_env.TestFakeFs):
|
||||
|
Loading…
x
Reference in New Issue
Block a user