Commit 565d0203 authored by Jacek Sowiński's avatar Jacek Sowiński

Add "revert" command

`topydo revert` will search for backup corresponding with current state
of todo file, and use it to recover previous state. Output will show to
user results of which command had been reverted. If no suitable backup
is found (for example if user edited todo file recently with other
application), no action is taken and user is notified on the output
about lack of corresponding backup in backup file.
parent b8f99e6e
...@@ -47,6 +47,7 @@ _SUBCOMMAND_MAP = { ...@@ -47,6 +47,7 @@ _SUBCOMMAND_MAP = {
'postpone': 'PostponeCommand', 'postpone': 'PostponeCommand',
'pri': 'PriorityCommand', 'pri': 'PriorityCommand',
'quit': 'ExitCommand', 'quit': 'ExitCommand',
'revert': 'RevertCommand',
'rm': 'DeleteCommand', 'rm': 'DeleteCommand',
'sort': 'SortCommand', 'sort': 'SortCommand',
'tag': 'TagCommand', 'tag': 'TagCommand',
......
...@@ -57,6 +57,7 @@ Available commands: ...@@ -57,6 +57,7 @@ Available commands:
* listprojects (lsprj) * listprojects (lsprj)
* postpone * postpone
* pri * pri
* revert
* sort * sort
* tag * tag
...@@ -200,7 +201,7 @@ class CLIApplicationBase(object): ...@@ -200,7 +201,7 @@ class CLIApplicationBase(object):
Execute a subcommand with arguments. p_command is a class (not an Execute a subcommand with arguments. p_command is a class (not an
object). object).
""" """
cmds_wo_backup = tuple(cmd + 'Command' for cmd in READ_ONLY_COMMANDS cmds_wo_backup = tuple(cmd + 'Command' for cmd in ('Revert', ) + READ_ONLY_COMMANDS)
if config().backup_count() > 0 and p_command and not p_command.__module__.endswith(cmds_wo_backup): if config().backup_count() > 0 and p_command and not p_command.__module__.endswith(cmds_wo_backup):
call = [p_command.__module__.lower()[16:-7]] + p_args # strip "topydo.commands" and "Command" call = [p_command.__module__.lower()[16:-7]] + p_args # strip "topydo.commands" and "Command"
self.backup = ChangeSet(self.todolist, p_call=call) self.backup = ChangeSet(self.todolist, p_call=call)
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from topydo.lib.Command import Command
from topydo.lib.ChangeSet import ChangeSet
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,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(RevertCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None)
def execute(self):
if not super(RevertCommand, self).execute():
return False
archive_file = TodoFile.TodoFile(config().archive())
archive = TodoList.TodoList(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.delete()
self.out("Successfully reverted: " + last_change.call)
except (ValueError, KeyError):
self.error('No backup was found for the current state of ' + config().todotxt())
last_change.close()
def usage(self):
return """Synopsis: revert"""
def help(self):
return """\
Reverts the last command.
"""
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