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