Commit 187e9ed8 authored by Jacek Sowiński's avatar Jacek Sowiński

Unify calling the editor in EditCommand

Now every call to editor is done by `EditCommand._open_in_editor()` and
whole process of getting editor name is also done there.
parent 9d33afef
...@@ -139,7 +139,7 @@ class EditCommandTest(CommandTest): ...@@ -139,7 +139,7 @@ class EditCommandTest(CommandTest):
self.assertEqual(self.output, expected) self.assertEqual(self.output, expected)
self.assertEqual(self.todolist.print_todos(), u("Foo id:1\nFo\u00f3B\u0105\u017a\nLazy Cat\nLazy Dog")) self.assertEqual(self.todolist.print_todos(), u("Foo id:1\nFo\u00f3B\u0105\u017a\nLazy Cat\nLazy Dog"))
@mock.patch('topydo.commands.EditCommand.call') @mock.patch('topydo.commands.EditCommand.check_call')
def test_edit_archive(self, mock_call): def test_edit_archive(self, mock_call):
""" Edit archive file. """ """ Edit archive file. """
mock_call.return_value = 0 mock_call.return_value = 0
......
...@@ -73,12 +73,20 @@ class EditCommand(MultiCommand, ExpressionCommand): ...@@ -73,12 +73,20 @@ class EditCommand(MultiCommand, ExpressionCommand):
return todo_objs return todo_objs
def _open_in_editor(self, p_temp_file, p_editor): def _open_in_editor(self, p_file):
try: try:
return check_call([p_editor, p_temp_file.name]) editor = os.environ['EDITOR'] or DEFAULT_EDITOR
except(KeyError):
editor = DEFAULT_EDITOR
try:
return check_call([editor, p_file])
except CalledProcessError: except CalledProcessError:
self.error('Something went wrong in the editor...') self.error('Something went wrong in the editor...')
return 1 return 1
except(OSError):
self.error('There is no such editor as: ' + editor + '. '
'Check your $EDITOR and/or $PATH')
def _catch_todo_errors(self): def _catch_todo_errors(self):
errors = [] errors = []
...@@ -99,54 +107,46 @@ class EditCommand(MultiCommand, ExpressionCommand): ...@@ -99,54 +107,46 @@ class EditCommand(MultiCommand, ExpressionCommand):
return False return False
self.printer.add_filter(PrettyPrinterNumbers(self.todolist)) self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
try:
editor = os.environ['EDITOR'] or DEFAULT_EDITOR
except(KeyError):
editor = DEFAULT_EDITOR
try: if len(self.args) < 1:
if len(self.args) < 1: todo = config().todotxt()
todo = config().todotxt()
return call([editor, todo]) == 0 return self._open_in_editor(todo) == 0
else: else:
self._process_flags() self._process_flags()
if self.edit_archive: if self.edit_archive:
archive = config().archive() archive = config().archive()
return call([editor, archive]) == 0 return self._open_in_editor(archive) == 0
if self.is_expression: if self.is_expression:
self.todos = self._view().todos self.todos = self._view().todos
else: else:
self.get_todos(self.args) self.get_todos(self.args)
todo_errors = self._catch_todo_errors() todo_errors = self._catch_todo_errors()
if not todo_errors: if not todo_errors:
temp_todos = self._todos_to_temp() temp_todos = self._todos_to_temp()
if not self._open_in_editor(temp_todos, editor): if not self._open_in_editor(temp_todos):
new_todos = self._todos_from_temp(temp_todos) new_todos = self._todos_from_temp(temp_todos)
if len(new_todos) == len(self.todos): if len(new_todos) == len(self.todos):
for todo in self.todos: for todo in self.todos:
BASE_TODOLIST(self.todolist).delete(todo) BASE_TODOLIST(self.todolist).delete(todo)
for todo in new_todos: for todo in new_todos:
self.todolist.add_todo(todo) self.todolist.add_todo(todo)
self.out(self.printer.print_todo(todo)) self.out(self.printer.print_todo(todo))
else:
self.error('Number of edited todos is not equal to '
'number of supplied todo IDs.')
else: else:
self.error(self.usage()) self.error('Number of edited todos is not equal to '
'number of supplied todo IDs.')
else: else:
for error in todo_errors: self.error(self.usage())
self.error(error) else:
except(OSError): for error in todo_errors:
self.error('There is no such editor as: ' + editor + '. ' self.error(error)
'Check your $EDITOR and/or $PATH')
def usage(self): def usage(self):
return """Synopsis: return """Synopsis:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment