Merge branch 'master' into feat/dialogedit

main
Olivier Mangin 7 years ago committed by GitHub
commit 6957ae3cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -38,35 +38,9 @@ def _get_encoding(conf):
def _get_local_editor():
"""Get the editor from environment variables.
Use nano as a default.
Use vi as a default.
"""
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)
return os.environ.get('EDITOR', 'vi')
def get_ui():
@ -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)

@ -112,18 +112,12 @@ For *zsh* completion, the global activation is not supported but bash completion
You can access the self-documented configuration by using `pubs conf`, and all the commands' help is available with the `--help` option. Did not find an answer to your question? Drop us an issue. We may not answer right away (science comes first!) but we'll eventually look into it.
## Requirements
- python >= 2.7 or >= 3.3
- [bibtexparser](https://github.com/sciunto-org/python-bibtexparser)
- [beautifulsoup4](https://www.crummy.com/software/BeautifulSoup)
- [argcomplete](https://argcomplete.readthedocs.io) (optional, for autocompletion)
## Authors
- [Fabien Benureau](http://fabien.benureau.com)
- [Olivier Mangin](http://olivier.mangin.com)
### Contributors
- [Jonathan Grizou](https://github.com/jgrizou)

@ -0,0 +1,6 @@
pyyaml
bibtexparser>=1.0
python-dateutil
requests
configobj
beautifulsoup4

@ -26,10 +26,9 @@ setup(
],
},
install_requires=['pyyaml', 'bibtexparser>=1.0', 'python-dateutil', 'requests',
'configobj',
'beautifulsoup4'], # to be made optional?
tests_require=['pyfakefs>=2.7'],
install_requires=['pyyaml', 'bibtexparser>=1.0', 'python-dateutil',
'requests', 'configobj', 'beautifulsoup4'],
tests_require=['pyfakefs>=2.7', 'mock'],
extras_require={'autocompletion': ['argcomplete'],
},

@ -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):
@ -356,7 +358,7 @@ class TestList(DataCommandTestCase):
def test_list_several_no_date(self):
self.execute_cmds(['pubs init -p testrepo'])
os.chdir('/') # weird fix for shutil.rmtree invocation.
shutil.rmtree(self.rootpath + '/testrepo')
shutil.rmtree(os.path.join(self.rootpath, 'testrepo'))
os.chdir(self.rootpath)
self.fs.add_real_directory(os.path.join(self.rootpath, 'testrepo'), read_only=False)

Loading…
Cancel
Save