Commit e6d8ab54 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Maintain dirtiness in the todolist.

parent 1ba155b9
......@@ -55,7 +55,6 @@ def arguments():
class Application(object): # TODO: rename to CLIApplication
def __init__(self):
self.todolist = TodoList.TodoList([])
self.dirty = False
def print_todo(self, p_number):
""" Prints a single todo item to the standard output. """
......@@ -67,7 +66,6 @@ class Application(object): # TODO: rename to CLIApplication
command = AddCommand(arguments(), self.todolist)
if command.execute():
self.print_todo(self.todolist.count())
self.dirty = True
def append(self):
""" Appends a text to a todo item. """
......@@ -77,7 +75,6 @@ class Application(object): # TODO: rename to CLIApplication
self.todolist.append(number, text)
self.print_todo(number)
self.dirty = True
def dep(self):
""" Handles dependencies between todos. """
......@@ -96,8 +93,6 @@ class Application(object): # TODO: rename to CLIApplication
else:
self.todolist.remove_dependency(from_todonumber, to_todonumber)
self.dirty = True
def handle_ls():
""" Handles the ls subsubcommand. """
arg1 = argument(3)
......@@ -125,7 +120,6 @@ class Application(object): # TODO: rename to CLIApplication
handle_add_rm(subsubcommand)
elif subsubcommand == 'clean' or subsubcommand == 'gc':
self.todolist.clean_dependencies()
self.dirty = True
elif subsubcommand == 'ls':
handle_ls()
else:
......@@ -160,7 +154,6 @@ class Application(object): # TODO: rename to CLIApplication
todo.set_completed()
self.print_todo(number)
self.dirty = True
def pri(self):
number = convert_todo_number(argument(2))
......@@ -176,7 +169,6 @@ class Application(object): # TODO: rename to CLIApplication
print "Priority changed from %s to %s" \
% (old_priority, priority)
self.print_todo(number)
self.dirty = True
else:
error("Invalid priority given.")
......@@ -223,7 +215,7 @@ class Application(object): # TODO: rename to CLIApplication
else:
usage()
if self.dirty:
if self.todolist.is_dirty():
todofile.write(str(self.todolist))
if __name__ == '__main__':
......
......@@ -29,6 +29,8 @@ class TodoList(object):
for string in p_todostrings:
self.add(string)
self.dirty = False
def todo(self, p_number):
"""
The _todos list has the same order as in the backend store (usually
......@@ -107,6 +109,7 @@ class TodoList(object):
self._maintain_dep_graph(p_todo)
self._update_parent_cache()
self.dirty = True
def delete(self, p_number):
""" Deletes a todo item from the list. """
......@@ -121,6 +124,8 @@ class TodoList(object):
del self._todos[p_number - 1]
self.dirty = True
def count(self):
""" Returns the number of todos on this list. """
return len(self._todos)
......@@ -137,6 +142,7 @@ class TodoList(object):
if todo:
new_text = todo.source() + ' ' + p_string
todo.set_source_text(new_text)
self.dirty = True
def projects(self):
""" Returns a set of all projects in this list. """
......@@ -198,6 +204,7 @@ class TodoList(object):
to_todo.add_tag('p', dep_id)
self._depgraph.add_edge(p_number1, p_number2, dep_id)
self._update_parent_cache()
self.dirty = True
def remove_dependency(self, p_number1, p_number2):
""" Removes a dependency between two todos. """
......@@ -217,6 +224,8 @@ class TodoList(object):
if not self.children(p_number1, True):
from_todo.remove_tag('id')
self.dirty = True
def parents(self, p_number, p_only_direct=False):
"""
Returns a list of parent todos that (in)directly depend on the
......@@ -248,6 +257,7 @@ class TodoList(object):
value = todo.tag_value(tag_name)
if not self._depgraph.has_edge_id(value):
todo.remove_tag(tag_name, value)
self.dirty = True
self._depgraph.transitively_reduce()
clean_by_tag('p')
......@@ -264,6 +274,9 @@ class TodoList(object):
for todo in self._todos:
todo.attributes['parents'] = self.parents(todo.attributes['number'])
def is_dirty(self):
return self.dirty
def todos(self):
return self._todos
......
......@@ -18,10 +18,12 @@ class TodoListTester(unittest.TestCase):
def test_contexts(self):
self.assertEquals(set(['Context1', 'Context2']), \
self.todolist.contexts())
self.assertFalse(self.todolist.is_dirty())
def test_projects(self):
self.assertEquals(set(['Project1', 'Project2']), \
self.todolist.projects())
self.assertFalse(self.todolist.is_dirty())
def test_add1(self):
text = "(C) Adding a new task @Context3 +Project3"
......@@ -33,6 +35,7 @@ class TodoListTester(unittest.TestCase):
self.todolist.projects())
self.assertEquals(set(['Context1', 'Context2', 'Context3']), \
self.todolist.contexts())
self.assertTrue(self.todolist.is_dirty())
def test_add2(self):
text = str(self.todolist)
......@@ -72,12 +75,14 @@ class TodoListTester(unittest.TestCase):
self.assertEquals(self.todolist.todo(2).source(), \
"(C) Baz @Context1 +Project1 key:value")
self.assertEquals(self.todolist.count(), count - 1)
self.assertTrue(self.todolist.is_dirty())
def test_delete2(self):
count = self.todolist.count()
self.todolist.delete(count + 1)
self.assertEquals(self.todolist.count(), count)
self.assertFalse(self.todolist.is_dirty())
def test_append1(self):
self.todolist.append(3, "@Context3")
......@@ -86,6 +91,7 @@ class TodoListTester(unittest.TestCase):
"(C) Baz @Context1 +Project1 key:value @Context3")
self.assertEquals(set(['Context1', 'Context2', 'Context3']), \
self.todolist.contexts())
self.assertTrue(self.todolist.is_dirty())
def test_append2(self):
text = self.todolist.todo(3).text()
......@@ -103,12 +109,14 @@ class TodoListTester(unittest.TestCase):
def test_append4(self):
self.todolist.append(999, 'foo')
self.assertFalse(self.todolist.is_dirty())
def test_todo(self):
count = self.todolist.count()
todo = self.todolist.todo(count+100)
self.assertEquals(todo, None)
self.assertFalse(self.todolist.is_dirty())
def test_string(self):
# readlines() always ends a string with \n, but join() in str(todolist)
......
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