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 _retrieve_archive(): """ Returns a tuple with archive content: the first element is a TodoListBase and the second element is a TodoFile. """ archive_file = TodoFile.TodoFile(config().archive()) archive = TodoListBase.TodoListBase(archive_file.read()) return (archive, archive_file)
def project_list(todo_cfg=None): """ Provide a full (Python) list of all projects. Takes our todo list and our done list, and returns a (Python) list of all projects found. """ todofile = TodoFile.TodoFile(topydo_config(todo_cfg).todotxt()) # print('Loaded todo file from {}'.format(todofile.path)) todotodos = TodoList.TodoList(todofile.read()) todo_projects = todotodos.projects() donefile = TodoFile.TodoFile(topydo_config(todo_cfg).archive()) # print('Loaded done file from {}'.format(donefile.path)) donetodos = TodoList.TodoList(donefile.read()) done_projects = donetodos.projects() # operator called 'join' and gives the union of the two sets all_projects_list = list(todo_projects | done_projects) return sort_project_list(all_projects_list)
def run(self): """ Main entry function. """ args = self._process_flags() self.todofile = TodoFile.TodoFile(config().todotxt()) self.todolist = TodoList.TodoList(self.todofile.read()) (subcommand, args) = get_subcommand(args) if subcommand == None: self._usage() if self._execute(subcommand, args) == False: sys.exit(1) else: self._post_execute()
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_file = TodoFile.TodoFile(config().archive()) archive = TodoListBase.TodoListBase(archive_file.read()) if archive: command = ArchiveCommand(self.todolist, archive) command.execute() if archive.is_dirty(): archive_file.write(archive.print_todos())
def _load_file(self): """ Reads the configured todo.txt file and loads it into the todo list instance. If the modification time of the todo.txt file is equal to the last time it was checked, nothing will be done. """ current_mtime = _todotxt_mtime() if not self.todofile or self.mtime != current_mtime: self.todofile = TodoFile.TodoFile(config().todotxt()) self.todolist = TodoList.TodoList(self.todofile.read()) self.mtime = current_mtime # suppress upstream issue with Python 2.7 # pylint: disable=no-value-for-parameter self.completer = TopydoCompleter(self.todolist)
def run(self): """ Main entry function. """ args = self._process_flags() self.todofile = TodoFile.TodoFile(config().todotxt()) self.todolist = TodoList.TodoList(self.todofile.read()) try: (subcommand, args) = get_subcommand(args) except ConfigError as ce: error('Error: ' + str(ce) + '. Check your aliases configuration') sys.exit(1) if subcommand is None: self._usage() if self._execute(subcommand, args) == False: sys.exit(1) else: self._post_execute()
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 sorted_todos_by_project(cfg, todo_cfg=None): """ Takes our todo list, and returns two dictionaries of where the keys equal to the project name, and the value is a list of todo items under that project. - Note that a todo item may be appended to multiple second level HTML lists if the item is listed under multiple projects. - Note that todo items without a project are discarded. - Note that completed items beyond `completion_cutoff` (measured in days) are discarded. todo_cfg is called to topydo.config directs as the path of the test configuration. Setting this to None will use the normal configuration. """ ''' print(type(cfg)) print(cfg) print(type(cfg['todo']), cfg['todo']) print(type(cfg['todo']['completion_cutoff']), cfg['todo']['completion_cutoff']) ''' completion_range = timestring.Range('last {!s} days'.format(cfg['todo']['completion_cutoff'])) my_sorter = Sorter(p_sortstring=cfg['todo']['sort_string']) todofile = TodoFile.TodoFile(topydo_config(todo_cfg).todotxt()) # print('Loaded todo file from {}'.format(todofile.path)) todotodos = TodoList.TodoList(todofile.read()) # todolist = my_sorter.sort(todolist) # in topydo v0.10 # json_str = JsonPrinter().print_list(todolist) # in topydo v0.10 todolist = my_sorter.sort(todotodos.todos()) # sort before filters # filters return a list, so apply them all at once? todolist = HiddenTagFilter().filter(todolist) todo_json_str = JsonPrinter().print_list(todolist) todo_json = json.loads(todo_json_str) donefile = TodoFile.TodoFile(topydo_config(todo_cfg).archive()) # print('Loaded done file from {}'.format(donefile.path)) donetodos = TodoList.TodoList(donefile.read()) donelist = my_sorter.sort(donetodos.todos()) donelist = HiddenTagFilter().filter(donelist) done_json_str = JsonPrinter().print_list(donelist) done_json = json.loads(done_json_str) active_todos = {} completed_todos = {} for my_json in [todo_json, done_json]: for todo in my_json: if not todo['completed']: for project in todo['projects']: try: active_todos[project].append(todo['source']) except KeyError: active_todos[project] = [todo['source']] else: completion_date = timestring.Date(todo['completion_date']) if completion_date in completion_range: for project in todo['projects']: try: completed_todos[project].append(todo['source']) except KeyError: completed_todos[project] = [todo['source']] return active_todos, completed_todos