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

Don't try to replace archive when archiving is off

When user disabled archive feature after he made some backups, don't try
to restore archive during `topydo revert`, as there is no file to write.
parent e276dcd5
...@@ -20,36 +20,42 @@ from topydo.lib import TodoFile ...@@ -20,36 +20,42 @@ from topydo.lib import TodoFile
from topydo.lib import TodoList from topydo.lib import TodoList
from topydo.lib.Config import config from topydo.lib.Config import config
class RevertCommand(Command): class RevertCommand(Command):
def __init__(self, p_args, p_todolist, #pragma: no branch def __init__(self, p_args, p_todolist, # pragma: no branch
p_out=lambda a: None, p_out=lambda a: None,
p_err=lambda a: None, p_err=lambda a: None,
p_prompt=lambda a: None): p_prompt=lambda a: None):
super().__init__(p_args, p_todolist, p_out, p_err, super().__init__(p_args, p_todolist, p_out, p_err, p_prompt)
p_prompt)
self._archive = None
self._archive_file = None
def execute(self): def execute(self):
if not super().execute(): if not super().execute():
return False return False
archive_file = TodoFile.TodoFile(config().archive()) archive_path = config().archive()
archive = TodoList.TodoList(archive_file.read()) if archive_path:
self._archive_file = TodoFile.TodoFile(archive_path)
self._archive = TodoList.TodoList(self._archive_file.read())
last_change = ChangeSet() last_change = ChangeSet()
try: try:
last_change.get_backup(self.todolist) last_change.get_backup(self.todolist)
last_change.apply(self.todolist, archive) last_change.apply(self.todolist, self._archive)
archive_file.write(archive.print_todos()) if self._archive:
self._archive_file.write(self._archive.print_todos())
last_change.delete() last_change.delete()
self.out("Successfully reverted: " + last_change.label) self.out("Successfully reverted: " + last_change.label)
except (ValueError, KeyError): except (ValueError, KeyError):
self.error('No backup was found for the current state of ' + config().todotxt()) self.error('No backup was found for the current state of '
+ config().todotxt())
last_change.close() last_change.close()
def usage(self): def usage(self):
return """Synopsis: revert""" return """Synopsis: revert"""
......
...@@ -176,10 +176,10 @@ class ChangeSet(object): ...@@ -176,10 +176,10 @@ class ChangeSet(object):
def apply(self, p_todolist, p_archive): def apply(self, p_todolist, p_archive):
""" Applies backup on supplied p_todolist. """ """ Applies backup on supplied p_todolist. """
if self.todolist: if self.todolist and p_todolist:
p_todolist.replace(self.todolist.todos()) p_todolist.replace(self.todolist.todos())
if self.archive: if self.archive and p_archive:
p_archive.replace(self.archive.todos()) p_archive.replace(self.archive.todos())
def close(self): def close(self):
......
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