def execute(self): if not super().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.label) except (ValueError, KeyError): self.error('No backup was found for the current state of ' + config().todotxt()) last_change.close()
def test_revert06(self): """ Test attempt of deletion with non-existing backup key""" backup = ChangeSet(self.todolist, self.archive, ['add One']) backup.timestamp = '1' command1 = AddCommand(["One"], self.todolist, self.out, self.error, None) command1.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Two']) backup.timestamp = '2' command2 = AddCommand(["Two"], self.todolist, self.out, self.error, None) command2.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Three']) backup.timestamp = '3' command3 = AddCommand(["Three"], self.todolist, self.out, self.error, None) command3.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['delete Three']) backup.timestamp = '4' command4 = DeleteCommand(["Three"], self.todolist, self.out, self.error, None) command4.execute() backup.save(self.todolist) backup = ChangeSet() backup.delete('Foo') changesets = list(backup.backup_dict.keys()) changesets.remove('index') index_timestamps = [change[0] for change in backup._get_index()] result = list(set(index_timestamps) - set(changesets)) self.assertEqual(len(changesets), 4) self.assertEqual(result, []) self.assertEqual(self.errors, "")
class RevertCommand(Command): 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) self._backup = None self._archive_file = None self._archive = None def execute(self): if not super().execute(): return False self._backup = ChangeSet() archive_path = config().archive() if archive_path: self._archive_file = TodoFile.TodoFile(config().archive()) self._archive = TodoList.TodoList(self._archive_file.read()) if len(self.args) > 1: self.error(self.usage()) else: try: arg = self.argument(0) self._handle_args(arg) except InvalidCommandArgument: try: self._revert_last() except (ValueError, KeyError): self.error( 'No backup was found for the current state of ' + config().todotxt()) self._backup.close() def _revert(self, p_timestamp=None): self._backup.read_backup(self.todolist, p_timestamp) self._backup.apply(self.todolist, self._archive) if self._archive: self._archive_file.write(self._archive.print_todos()) self.out("Reverted to state before: " + self._backup.label) def _revert_last(self): self._revert() self._backup.delete() def _revert_to_specific(self, p_position): timestamps = [timestamp for timestamp, _ in self._backup] position = int(p_position) - 1 # numbering in UI starts with 1 try: timestamp = timestamps[position] self._revert(timestamp) for timestamp in timestamps[:position + 1]: self._backup.read_backup(p_timestamp=timestamp) self._backup.delete() except IndexError: self.error('Specified index is out range') def _handle_args(self, p_arg): try: if p_arg == 'ls': self._handle_ls() elif p_arg.isdigit(): self._revert_to_specific(p_arg) else: raise InvalidCommandArgument except InvalidCommandArgument: self.error(self.usage()) def _handle_ls(self): num = 1 for timestamp, change in self._backup: label = change[2] time = arrow.get(float(timestamp)).format('YYYY-MM-DD HH:mm:ss') self.out('{0: >3}| {1} | {2}'.format(str(num), time, label)) num += 1 def usage(self): return """Synopsis: revert [ls] revert [NUMBER]""" def help(self): return """\
class RevertCommand(Command): 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) self._backup = None self._archive_file = None self._archive = None def execute(self): if not super().execute(): return False self._backup = ChangeSet() archive_path = config().archive() if archive_path: self._archive_file = TodoFile.TodoFile(config().archive()) self._archive = TodoList.TodoList(self._archive_file.read()) if len(self.args) > 1: self.error(self.usage()) else: try: arg = self.argument(0) self._handle_args(arg) except InvalidCommandArgument: try: self._revert_last() except (ValueError, KeyError): self.error('No backup was found for the current state of ' + config().todotxt()) self._backup.close() def _revert(self, p_timestamp=None): self._backup.read_backup(self.todolist, p_timestamp) self._backup.apply(self.todolist, self._archive) if self._archive: self._archive_file.write(self._archive.print_todos()) self.out("Reverted to state before: " + self._backup.label) def _revert_last(self): self._revert() self._backup.delete() def _revert_to_specific(self, p_position): timestamps = [timestamp for timestamp, _ in self._backup] position = int(p_position) - 1 # numbering in UI starts with 1 try: timestamp = timestamps[position] self._revert(timestamp) for timestamp in timestamps[:position + 1]: self._backup.read_backup(p_timestamp=timestamp) self._backup.delete() except IndexError: self.error('Specified index is out range') def _handle_args(self, p_arg): try: if p_arg == 'ls': self._handle_ls() elif p_arg.isdigit(): self._revert_to_specific(p_arg) else: raise InvalidCommandArgument except InvalidCommandArgument: self.error(self.usage()) def _handle_ls(self): num = 1 for timestamp, change in self._backup: label = change[2] time = arrow.get(timestamp).format('YYYY-MM-DD HH:mm:ss') self.out('{0: >3}| {1} | {2}'.format(str(num), time, label)) num += 1 def usage(self): return """Synopsis: revert [ls] revert [NUMBER]""" def help(self): return """\