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,23 +107,18 @@ class EditCommand(MultiCommand, ExpressionCommand): ...@@ -99,23 +107,18 @@ 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
...@@ -127,7 +130,7 @@ class EditCommand(MultiCommand, ExpressionCommand): ...@@ -127,7 +130,7 @@ class EditCommand(MultiCommand, ExpressionCommand):
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:
...@@ -144,9 +147,6 @@ class EditCommand(MultiCommand, ExpressionCommand): ...@@ -144,9 +147,6 @@ class EditCommand(MultiCommand, ExpressionCommand):
else: else:
for error in todo_errors: for error in todo_errors:
self.error(error) self.error(error)
except(OSError):
self.error('There is no such editor as: ' + editor + '. '
'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