def test_revert01(self): backup = ChangeSet(p_label=['do 1']) backup.add_todolist(self.todolist) backup.add_archive(self.archive) backup.timestamp = '1' command = DoCommand(["1"], self.todolist, self.out, self.error, None) command.execute() archive_command = ArchiveCommand(self.todolist, self.archive) archive_command.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: do 1\n")) self.assertEqual(result, "") self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz")
class CLIApplicationBase(object): """ Base class for a Command Line Interfaces (CLI) for topydo. Examples are the original CLI and the Prompt interface. Handles input/output of the various subcommands. """ def __init__(self): self.todolist = TodoList.TodoList([]) self.todofile = None self.do_archive = True self._post_archive_action = None self.backup = None @staticmethod def _usage(): usage() sys.exit(0) def _process_flags(self): args = sys.argv[1:] try: opts, args = getopt.getopt(args, MAIN_OPTS, MAIN_LONG_OPTS) except getopt.GetoptError as e: error(str(e)) sys.exit(1) alt_config_path = None overrides = {} for opt, value in opts: if opt == "-a": self.do_archive = False elif opt == "-c": alt_config_path = value elif opt == "-C": overrides[('topydo', 'force_colors')] = '1' overrides[('topydo', 'colors')] = value elif opt == "-t": overrides[('topydo', 'filename')] = value elif opt == "-d": overrides[('topydo', 'archive_filename')] = value elif opt in ("-v", "--version"): version() else: CLIApplicationBase._usage() if alt_config_path: config(alt_config_path, overrides) elif len(overrides): config(p_overrides=overrides) return args def _archive(self): """ Performs an archive action on the todolist. This means that all completed tasks are moved to the archive file (defaults to done.txt). """ archive, archive_file = _retrieve_archive() if self.backup: self.backup.add_archive(archive) if archive: command = ArchiveCommand(self.todolist, archive) command.execute() if archive.dirty: archive_file.write(archive.print_todos()) @staticmethod def is_read_only(p_command): """ Returns True when the given command class is read-only. """ read_only_commands = tuple( cmd for cmd in ('revert', ) + READ_ONLY_COMMANDS) return p_command.name() in read_only_commands def _backup(self, p_command, p_args=None, p_label=None): if config().backup_count( ) > 0 and p_command and not CLIApplicationBase.is_read_only(p_command): p_args = p_args if p_args else [] call = [p_command.name()] + p_args from topydo.lib.ChangeSet import ChangeSet label = p_label if p_label else call self.backup = ChangeSet(self.todolist, p_label=label) def _execute(self, p_command, p_args): """ Execute a subcommand with arguments. p_command is a class (not an object). """ self._backup(p_command, p_args) if p_command == None: usage() return False if p_command.name() != 'archive': command = p_command(p_args, self.todolist, output, error, input) if command.execute() != False: self._post_archive_action = command.execute_post_archive_actions return True else: self.todolist.dirty = True self.do_archive = True return True return False def _post_execute(self): """ Should be called when executing the user requested command has been completed. It will do some maintenance and write out the final result to the todo.txt file. """ if self.todolist.dirty: # do not archive when the value of the filename is an empty string # (i.e. explicitly left empty in the configuration if self.do_archive and config().archive(): self._archive() elif config().archive() and self.backup: archive = _retrieve_archive()[0] self.backup.add_archive(archive) try: self._post_archive_action() except TypeError: pass if config().keep_sorted(): from topydo.commands.SortCommand import SortCommand self._execute(SortCommand, []) if self.backup: self.backup.save(self.todolist) self.todofile.write(self.todolist.print_todos()) self.todolist.dirty = False self.backup = None def run(self): raise NotImplementedError
class CLIApplicationBase(object): """ Base class for a Command Line Interfaces (CLI) for topydo. Examples are the original CLI and the Prompt interface. Handles input/output of the various subcommands. """ def __init__(self): self.todolist = TodoList.TodoList([]) self.todofile = None self.do_archive = True self._post_archive_action = None self.backup = None @staticmethod def _usage(): usage() sys.exit(0) def _process_flags(self): args = sys.argv[1:] try: opts, args = getopt.getopt(args, MAIN_OPTS, MAIN_LONG_OPTS) except getopt.GetoptError as e: error(str(e)) sys.exit(1) alt_config_path = None overrides = {} for opt, value in opts: if opt == "-a": self.do_archive = False elif opt == "-c": alt_config_path = value elif opt == "-C": overrides[('topydo', 'force_colors')] = '1' overrides[('topydo', 'colors')] = value elif opt == "-t": overrides[('topydo', 'filename')] = value elif opt == "-d": overrides[('topydo', 'archive_filename')] = value elif opt in ("-v", "--version"): version() else: CLIApplicationBase._usage() if alt_config_path: config(alt_config_path, overrides) elif len(overrides): config(p_overrides=overrides) return args def _archive(self): """ Performs an archive action on the todolist. This means that all completed tasks are moved to the archive file (defaults to done.txt). """ archive, archive_file = _retrieve_archive() if self.backup: self.backup.add_archive(archive) if archive: from topydo.commands.ArchiveCommand import ArchiveCommand command = ArchiveCommand(self.todolist, archive) command.execute() if archive.dirty: archive_file.write(archive.print_todos()) @staticmethod def is_read_only(p_command): """ Returns True when the given command class is read-only. """ read_only_commands = tuple(cmd for cmd in ('revert', ) + READ_ONLY_COMMANDS) return p_command.name() in read_only_commands def _backup(self, p_command, p_args=None, p_label=None): if config().backup_count() > 0 and p_command and not CLIApplicationBase.is_read_only(p_command): p_args = p_args if p_args else [] call = [p_command.name()] + p_args from topydo.lib.ChangeSet import ChangeSet label = p_label if p_label else call self.backup = ChangeSet(self.todolist, p_label=label) def _execute(self, p_command, p_args): """ Execute a subcommand with arguments. p_command is a class (not an object). """ self._backup(p_command, p_args) command = p_command( p_args, self.todolist, output, error, input) if command.execute() != False: self._post_archive_action = command.execute_post_archive_actions return True return False def _post_execute(self): """ Should be called when executing the user requested command has been completed. It will do some maintenance and write out the final result to the todo.txt file. """ if self.todolist.dirty: # do not archive when the value of the filename is an empty string # (i.e. explicitly left empty in the configuration if self.do_archive and config().archive(): self._archive() elif config().archive() and self.backup: archive = _retrieve_archive()[0] self.backup.add_archive(archive) self._post_archive_action() if config().keep_sorted(): from topydo.commands.SortCommand import SortCommand self._execute(SortCommand, []) if self.backup: self.backup.save(self.todolist) self.todofile.write(self.todolist.print_todos()) self.todolist.dirty = False self.backup = None def run(self): raise NotImplementedError