Commit 6c49fbdd authored by Jacek Sowiński's avatar Jacek Sowiński

Add possibility to trigger actions after archiving

Each command can now execute action defined in its own
Command.post_archive() method after archiving action is done. Hook for
`do` and `del` is also included in this commit.

This should fix #139
parent dea65c34
...@@ -18,7 +18,6 @@ from datetime import date ...@@ -18,7 +18,6 @@ from datetime import date
from topydo.lib.DCommand import DCommand from topydo.lib.DCommand import DCommand
from topydo.lib.printers.PrettyPrinter import PrettyPrinter from topydo.lib.printers.PrettyPrinter import PrettyPrinter
from topydo.lib.prettyprinters.Numbers import PrettyPrinterNumbers
from topydo.lib.Recurrence import NoRecurrenceException, advance_recurring_todo from topydo.lib.Recurrence import NoRecurrenceException, advance_recurring_todo
from topydo.lib.RelativeDate import relative_date_to_date from topydo.lib.RelativeDate import relative_date_to_date
from topydo.lib.Utils import date_string_to_date from topydo.lib.Utils import date_string_to_date
...@@ -67,9 +66,6 @@ class DoCommand(DCommand): ...@@ -67,9 +66,6 @@ class DoCommand(DCommand):
self.todolist.add_todo(new_todo) self.todolist.add_todo(new_todo)
printer = PrettyPrinter()
printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(printer.print_todo(new_todo))
except NoRecurrenceException: except NoRecurrenceException:
self.error("Warning: todo item has an invalid recurrence pattern.") self.error("Warning: todo item has an invalid recurrence pattern.")
......
...@@ -90,6 +90,10 @@ class Command(object): ...@@ -90,6 +90,10 @@ class Command(object):
"""" Returns short-name of the command. """ """" Returns short-name of the command. """
return cls.__name__[:-7].lower() # strip 'Command' return cls.__name__[:-7].lower() # strip 'Command'
def execute_post_archive_actions(self):
""" Runs various hooks that should take place after archiving. """
pass
def usage(self): def usage(self):
""" Returns a one-line synopsis for this command. """ """ Returns a one-line synopsis for this command. """
raise NotImplementedError raise NotImplementedError
......
...@@ -35,9 +35,8 @@ class DCommand(MultiCommand): ...@@ -35,9 +35,8 @@ class DCommand(MultiCommand):
p_args, p_todolist, p_out, p_err, p_prompt) p_args, p_todolist, p_out, p_err, p_prompt)
self.force = False self.force = False
self._delta = []
# to determine newly activated todos
self.length = len(self.todolist.todos())
def get_flags(self): def get_flags(self):
return ("f", ["force"]) return ("f", ["force"])
...@@ -76,11 +75,10 @@ class DCommand(MultiCommand): ...@@ -76,11 +75,10 @@ class DCommand(MultiCommand):
self.execute_specific_core(child) self.execute_specific_core(child)
self.out(self.prefix() + self.printer.print_todo(child)) self.out(self.prefix() + self.printer.print_todo(child))
def _print_unlocked_todos(self, p_old, p_new): def _print_unlocked_todos(self):
delta = [todo for todo in p_new if todo not in p_old] if self._delta:
if delta:
self.out("The following todo item(s) became active:") self.out("The following todo item(s) became active:")
self._print_list(delta) self._print_list(self._delta)
def _active_todos(self): def _active_todos(self):
""" """
...@@ -92,7 +90,7 @@ class DCommand(MultiCommand): ...@@ -92,7 +90,7 @@ class DCommand(MultiCommand):
Since these todos pop up at the end of the list, we cut off the list Since these todos pop up at the end of the list, we cut off the list
just before that point. just before that point.
""" """
return [todo for todo in self.todolist.todos()[:self.length] return [todo for todo in self.todolist.todos()
if not self._uncompleted_children(todo) and todo.is_active()] if not self._uncompleted_children(todo) and todo.is_active()]
def condition(self, _): def condition(self, _):
...@@ -125,4 +123,8 @@ class DCommand(MultiCommand): ...@@ -125,4 +123,8 @@ class DCommand(MultiCommand):
self.error(self.condition_failed_text()) self.error(self.condition_failed_text())
current_active = self._active_todos() current_active = self._active_todos()
self._print_unlocked_todos(old_active, current_active) self._delta = [todo for todo in current_active
if todo not in old_active]
def execute_post_archive_actions(self):
self._print_unlocked_todos()
...@@ -174,6 +174,7 @@ class CLIApplicationBase(object): ...@@ -174,6 +174,7 @@ class CLIApplicationBase(object):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
self.todofile = None self.todofile = None
self.do_archive = True self.do_archive = True
self._post_archive_action = None
self.backup = None self.backup = None
def _usage(self): def _usage(self):
...@@ -271,6 +272,7 @@ class CLIApplicationBase(object): ...@@ -271,6 +272,7 @@ class CLIApplicationBase(object):
input) input)
if command.execute() != False: if command.execute() != False:
self._post_archive_action = command.execute_post_archive_actions
return True return True
return False return False
...@@ -291,6 +293,8 @@ class CLIApplicationBase(object): ...@@ -291,6 +293,8 @@ class CLIApplicationBase(object):
archive = _retrieve_archive()[0] archive = _retrieve_archive()[0]
self.backup.add_archive(archive) self.backup.add_archive(archive)
self._post_archive_action()
if config().keep_sorted(): if config().keep_sorted():
from topydo.commands.SortCommand import SortCommand from topydo.commands.SortCommand import SortCommand
self._execute(SortCommand, []) self._execute(SortCommand, [])
......
...@@ -327,6 +327,8 @@ class UIApplication(CLIApplicationBase): ...@@ -327,6 +327,8 @@ class UIApplication(CLIApplicationBase):
try: try:
if transaction.execute(): if transaction.execute():
post_archive_action = transaction.execute_post_archive_actions
self._post_archive_action = post_archive_action
self._post_execute() self._post_execute()
else: else:
self._rollback() self._rollback()
......
...@@ -27,6 +27,7 @@ class Transaction(object): ...@@ -27,6 +27,7 @@ class Transaction(object):
self._cmd = lambda op: p_subcommand(op, *p_env_args) self._cmd = lambda op: p_subcommand(op, *p_env_args)
self._todo_ids = p_todo_ids self._todo_ids = p_todo_ids
self._operations = [] self._operations = []
self._post_archive_actions = []
self._cmd_name = p_subcommand.name() self._cmd_name = p_subcommand.name()
self.label = [] self.label = []
...@@ -72,5 +73,13 @@ class Transaction(object): ...@@ -72,5 +73,13 @@ class Transaction(object):
if command.execute() is False: if command.execute() is False:
return False return False
elif i == last_operation: else:
action = command.execute_post_archive_actions
self._post_archive_actions.append(action)
if i == last_operation:
return True return True
def execute_post_archive_actions(self):
for action in self._post_archive_actions:
action()
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