Commit 96cca8b6 authored by Jacek Sowiński's avatar Jacek Sowiński

Introduce MultiCommand.execute() and execute_multi_specific()

Commands operating on multiple todos will inherit common execute()
method from MultiCommand class, and part specific for each command will
be covered in execute_multi_specific()
parent 4218f1bb
...@@ -129,24 +129,15 @@ class DCommand(MultiCommand): ...@@ -129,24 +129,15 @@ class DCommand(MultiCommand):
""" """
pass pass
def execute(self): def execute_multi_specific(self):
if not super(DCommand, self).execute(): old_active = self._active_todos()
return False
for todo in self.todos:
todo_errors = self.catch_todo_errors() if todo and self.condition(todo):
self._process_subtasks(todo)
if not todo_errors: self.execute_specific(todo)
old_active = self._active_todos() else:
self.error(self.condition_failed_text())
for todo in self.todos:
if todo and self.condition(todo): current_active = self._active_todos()
self._process_subtasks(todo) self._print_unlocked_todos(old_active, current_active)
self.execute_specific(todo)
else:
self.error(self.condition_failed_text())
current_active = self._active_todos()
self._print_unlocked_todos(old_active, current_active)
else:
for error in todo_errors:
self.error(error)
...@@ -40,7 +40,7 @@ class MultiCommand(Command): ...@@ -40,7 +40,7 @@ class MultiCommand(Command):
except InvalidTodoException: except InvalidTodoException:
self.invalid_numbers.append(number) self.invalid_numbers.append(number)
def catch_todo_errors(self): def _catch_todo_errors(self):
""" """
Returns None or list of error messages depending on number of valid todo Returns None or list of error messages depending on number of valid todo
objects and number of invalid todo IDs. objects and number of invalid todo IDs.
...@@ -62,3 +62,22 @@ class MultiCommand(Command): ...@@ -62,3 +62,22 @@ class MultiCommand(Command):
return errors return errors
else: else:
return None return None
def execute_multi_specific(self):
"""
Operations specific for particular command dealing with multiple todo
IDs.
"""
pass
def execute(self):
if not super(MultiCommand, self).execute():
return False
todo_errors = self._catch_todo_errors()
if not todo_errors:
self.execute_multi_specific()
else:
for error in todo_errors:
self.error(error)
...@@ -45,7 +45,7 @@ class PostponeCommand(MultiCommand): ...@@ -45,7 +45,7 @@ class PostponeCommand(MultiCommand):
self.args = args self.args = args
def execute(self): def execute_multi_specific(self):
def _get_offset(p_todo): def _get_offset(p_todo):
offset = p_todo.tag_value( offset = p_todo.tag_value(
config().tag_due(), date.today().isoformat()) config().tag_due(), date.today().isoformat())
...@@ -56,38 +56,29 @@ class PostponeCommand(MultiCommand): ...@@ -56,38 +56,29 @@ class PostponeCommand(MultiCommand):
return offset_date return offset_date
if not super(PostponeCommand, self).execute(): try:
return False pattern = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
todo_errors = self.catch_todo_errors()
for todo in self.todos:
if not todo_errors: offset = _get_offset(todo)
try: new_due = relative_date_to_date(pattern, offset)
pattern = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist)) if new_due:
if self.move_start_date and todo.has_tag(config().tag_start()):
for todo in self.todos: length = todo.length()
offset = _get_offset(todo) new_start = new_due - timedelta(length)
new_due = relative_date_to_date(pattern, offset) todo.set_tag(config().tag_start(), new_start.isoformat())
if new_due: todo.set_tag(config().tag_due(), new_due.isoformat())
if self.move_start_date and todo.has_tag(config().tag_start()):
length = todo.length() self.todolist.set_dirty()
new_start = new_due - timedelta(length) self.out(self.printer.print_todo(todo))
todo.set_tag(config().tag_start(), new_start.isoformat()) else:
self.error("Invalid date pattern given.")
todo.set_tag(config().tag_due(), new_due.isoformat()) break
except (InvalidCommandArgument, IndexError):
self.todolist.set_dirty() self.error(self.usage())
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid date pattern given.")
break
except (InvalidCommandArgument, IndexError):
self.error(self.usage())
else:
for error in todo_errors:
self.error(error)
def usage(self): def usage(self):
return "Synopsis: postpone [-s] <NUMBER> [<NUMBER2> ...] <PATTERN>" return "Synopsis: postpone [-s] <NUMBER> [<NUMBER2> ...] <PATTERN>"
......
...@@ -29,37 +29,29 @@ class PriorityCommand(MultiCommand): ...@@ -29,37 +29,29 @@ class PriorityCommand(MultiCommand):
self.get_todos(self.args[:-1]) self.get_todos(self.args[:-1])
def execute(self): def execute_multi_specific(self):
if not super(PriorityCommand, self).execute():
return False
priority = None priority = None
todo_errors = self.catch_todo_errors()
if not todo_errors:
try:
priority = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
if is_valid_priority(priority):
for todo in self.todos:
old_priority = todo.priority()
self.todolist.set_priority(todo, priority)
if old_priority and priority and old_priority != priority:
self.out("Priority changed from {} to {}".format(
old_priority, priority))
elif not old_priority:
self.out("Priority set to {}.".format(priority))
self.out(self.printer.print_todo(todo)) try:
else: priority = self.args[-1]
self.error("Invalid priority given.") self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
except IndexError:
self.error(self.usage()) if is_valid_priority(priority):
else: for todo in self.todos:
for error in todo_errors: old_priority = todo.priority()
self.error(error) self.todolist.set_priority(todo, priority)
if old_priority and priority and old_priority != priority:
self.out("Priority changed from {} to {}".format(
old_priority, priority))
elif not old_priority:
self.out("Priority set to {}.".format(priority))
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid priority given.")
except IndexError:
self.error(self.usage())
def usage(self): def usage(self):
return """Synopsis: pri <NUMBER1> [<NUMBER2> ...] <PRIORITY>""" return """Synopsis: pri <NUMBER1> [<NUMBER2> ...] <PRIORITY>"""
......
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