Commit c1726616 authored by Bram Schoenmakers's avatar Bram Schoenmakers

First attempt at archiving todos to a separate file.

parent 22218f2f
import Command
import Config
class ArchiveCommand(Command.Command):
def __init__(self, p_todolist, p_archive_list):
super(ArchiveCommand, self).__init__([], p_todolist)
self.archive = p_archive_list
def execute(self):
for todo in [t for t in self.todolist.todos() if t.is_completed()]:
self.archive.add_todo(todo)
self.todolist.delete(todo.attributes['number'])
...@@ -7,7 +7,7 @@ COLORS = True ...@@ -7,7 +7,7 @@ COLORS = True
HIGHLIGHT_PROJECTS_CONTEXTS = True HIGHLIGHT_PROJECTS_CONTEXTS = True
FILENAME = 'todo.txt' FILENAME = 'todo.txt'
COMPLETED_FILENAME = 'done.txt' ARCHIVE_FILENAME = 'done.txt'
TAG_START = 't' TAG_START = 't'
TAG_DUE = 'due' TAG_DUE = 'due'
......
...@@ -5,6 +5,7 @@ import sys ...@@ -5,6 +5,7 @@ import sys
from AddCommand import AddCommand from AddCommand import AddCommand
from AppendCommand import AppendCommand from AppendCommand import AppendCommand
from ArchiveCommand import ArchiveCommand
from DepCommand import DepCommand from DepCommand import DepCommand
import Config import Config
from DoCommand import DoCommand from DoCommand import DoCommand
...@@ -40,14 +41,28 @@ class CLIApplication(object): ...@@ -40,14 +41,28 @@ class CLIApplication(object):
def __init__(self): def __init__(self):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
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_FILENAME)
archive = TodoList.TodoList(archive_file.read())
if archive:
command = ArchiveCommand(self.todolist, archive)
command.execute()
if archive.is_dirty():
archive_file.write(archive)
def run(self): def run(self):
""" Main entry function. """ """ Main entry function. """
todofile = TodoFile.TodoFile(Config.FILENAME) todofile = TodoFile.TodoFile(Config.FILENAME)
try:
self.todolist = TodoList.TodoList(todofile.read()) self.todolist = TodoList.TodoList(todofile.read())
except Exception:
pass # TODO
try: try:
subcommand = sys.argv[1] subcommand = sys.argv[1]
...@@ -82,10 +97,11 @@ class CLIApplication(object): ...@@ -82,10 +97,11 @@ class CLIApplication(object):
lambda e: sys.stderr.write(e + "\n"), lambda e: sys.stderr.write(e + "\n"),
raw_input) raw_input)
if not command.execute(): if command.execute() == False:
exit(1) exit(1)
if self.todolist.is_dirty(): if self.todolist.is_dirty():
# self.archive()
todofile.write(str(self.todolist)) todofile.write(str(self.todolist))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -13,10 +13,13 @@ class TodoFile(object): ...@@ -13,10 +13,13 @@ class TodoFile(object):
def read(self): def read(self):
""" Reads the todo.txt file and returns a list of todo items. """ """ Reads the todo.txt file and returns a list of todo items. """
todos = []
try:
todofile = open(self.path, 'r') todofile = open(self.path, 'r')
todos = todofile.readlines() todos = todofile.readlines()
todofile.close() todofile.close()
except IOError:
pass
return todos return todos
......
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