Commit 51f3c03a authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add function that maintains the dependency graph.

The function takes care that all edges are present in the dependency
graph.

This fixes a bug that when adding a todo with a parent which appears
later in the file, the edge wouldn't be drawn.
parent d11ab459
...@@ -53,6 +53,29 @@ class TodoList(object): ...@@ -53,6 +53,29 @@ class TodoList(object):
return hits[0] if len(hits) else None return hits[0] if len(hits) else None
def _maintain_dep_graph(self, p_todo):
"""
Makes sure that the dependency graph is consistent according to the
given todo.
"""
dep_id = p_todo.tag_value('id')
# maintain dependency graph
if dep_id:
self._depgraph.add_node(p_todo.number)
# connect all tasks we have in memory so far that refer to this
# task
for dep in \
[dep for dep in self._todos if dep.has_tag('p', dep_id)]:
self._depgraph.add_edge(p_todo.number, dep.number, dep_id)
for child in p_todo.tag_values('p'):
parent = self.todo_by_dep_id(child)
if parent:
self._depgraph.add_edge(parent.number, p_todo.number, child)
def add(self, p_src): def add(self, p_src):
""" """
Given a todo string, parse it and put it to the end of the list. Given a todo string, parse it and put it to the end of the list.
...@@ -76,14 +99,7 @@ class TodoList(object): ...@@ -76,14 +99,7 @@ class TodoList(object):
todo = Todo.Todo(p_src, number) todo = Todo.Todo(p_src, number)
self._todos.append(todo) self._todos.append(todo)
# maintain dependency graph self._maintain_dep_graph(todo)
if todo.has_tag('id'):
self._depgraph.add_node(todo.number)
for child in todo.tag_values('p'):
parent = self.todo_by_dep_id(child)
if parent:
self._depgraph.add_edge(parent.number, todo.number, child)
def delete(self, p_number): def delete(self, p_number):
""" Deletes a todo item from the list. """ """ Deletes a todo item from the list. """
......
...@@ -133,6 +133,7 @@ class TodoListDependencyTester(unittest.TestCase): ...@@ -133,6 +133,7 @@ class TodoListDependencyTester(unittest.TestCase):
self.todolist.add("Bar p:1") self.todolist.add("Bar p:1")
self.todolist.add("Baz p:1 id:2") self.todolist.add("Baz p:1 id:2")
self.todolist.add("Buzz p:2") self.todolist.add("Buzz p:2")
self.todolist.add("Fnord")
def test_check_dep(self): def test_check_dep(self):
children = self.todolist.children(1) children = self.todolist.children(1)
...@@ -158,6 +159,12 @@ class TodoListDependencyTester(unittest.TestCase): ...@@ -158,6 +159,12 @@ class TodoListDependencyTester(unittest.TestCase):
self.assertEqual(self.todolist.children(2), []) self.assertEqual(self.todolist.children(2), [])
self.assertEqual(self.todolist.parents(1), []) self.assertEqual(self.todolist.parents(1), [])
def test_add_dep(self):
self.todolist.add_dependency(5, 4)
self.assertTrue(self.todolist.todo(5).has_tag('id', '3'))
self.assertTrue(self.todolist.todo(4).has_tag('p', '3'))
def test_remove_dep1(self): def test_remove_dep1(self):
self.todolist.remove_dependency(3, 4) self.todolist.remove_dependency(3, 4)
...@@ -168,7 +175,7 @@ class TodoListDependencyTester(unittest.TestCase): ...@@ -168,7 +175,7 @@ class TodoListDependencyTester(unittest.TestCase):
old = str(self.todolist) old = str(self.todolist)
self.todolist.remove_dependency(1, 4) self.todolist.remove_dependency(1, 4)
self.assertEquals(str(self.todolist),old) self.assertEquals(str(self.todolist), old)
def test_remove_task(self): def test_remove_task(self):
self.todolist.delete(3) self.todolist.delete(3)
......
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