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
from topydo.lib import TodoList
from topydo.lib.Config import config
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_err=lambda a: None,
p_prompt=lambda a: None):
super().__init__(p_args, p_todolist, p_out, p_err,
p_prompt)
super().__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self._archive = None
self._archive_file = None
def execute(self):
if not super().execute():
return False
archive_file = TodoFile.TodoFile(config().archive())
archive = TodoList.TodoList(archive_file.read())
archive_path = config().archive()
if archive_path:
self._archive_file = TodoFile.TodoFile(archive_path)
self._archive = TodoList.TodoList(self._archive_file.read())
last_change = ChangeSet()
try:
last_change.get_backup(self.todolist)
last_change.apply(self.todolist, archive)
archive_file.write(archive.print_todos())
last_change.apply(self.todolist, self._archive)
if self._archive:
self._archive_file.write(self._archive.print_todos())
last_change.delete()
self.out("Successfully reverted: " + last_change.label)
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()
def usage(self):
return """Synopsis: revert"""
......
......@@ -176,10 +176,10 @@ class ChangeSet(object):
def apply(self, p_todolist, p_archive):
""" Applies backup on supplied p_todolist. """
if self.todolist:
if self.todolist and p_todolist:
p_todolist.replace(self.todolist.todos())
if self.archive:
if self.archive and p_archive:
p_archive.replace(self.archive.todos())
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