Commit fea67492 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Make number not a class attribute but an entry in the attributes field.

parent c31aa9df
......@@ -69,7 +69,7 @@ class DependencyFilter(Filter):
"""
Returns True when there are no children that are uncompleted yet.
"""
children = self.todolist.children(p_todo.number)
children = self.todolist.children(p_todo.attributes['number'])
uncompleted = [todo for todo in children if not todo.is_completed()]
return not uncompleted
......@@ -163,7 +163,7 @@ class Application(object):
def do(self):
def complete_children(p_number):
children = [t.number for t in self.todolist.children(p_number) if not t.is_completed()]
children = [t.attributes['number'] for t in self.todolist.children(p_number) if not t.is_completed()]
if children:
for child in children:
self.print_todo(child)
......
......@@ -39,7 +39,7 @@ def pretty_print(p_todos, p_show_numbers=False, p_color=False):
todo_str = str(todo)
if p_show_numbers:
todo_str = "%3d %s" % (todo.number, todo_str)
todo_str = "%3d %s" % (todo.attributes['number'], todo_str)
if Config.COLORS and p_color:
todo_str = add_colors(todo_str, todo)
......
......@@ -14,9 +14,9 @@ class Todo(TodoBase.TodoBase):
base class, mainly by interpreting the start and due dates of task.
"""
def __init__(self, p_str, p_number=-1):
TodoBase.TodoBase.__init__(self, p_str, p_number)
self.attributes = {}
def __init__(self, p_str):
TodoBase.TodoBase.__init__(self, p_str)
self.attributes = { 'number': -1}
def get_date(self, p_tag):
""" Given a date tag, return a date object. """
......
......@@ -19,10 +19,9 @@ class TodoBase(object):
in a todo item.
"""
def __init__(self, p_src, p_number=-1):
def __init__(self, p_src):
self.src = ""
self.fields = {}
self.number = p_number
self.set_text(p_src)
......
......@@ -62,23 +62,32 @@ class TodoList(object):
dep_id = p_todo.tag_value('id')
# maintain dependency graph
if dep_id:
self._depgraph.add_node(p_todo.number)
self._depgraph.add_node(p_todo.attributes['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)
self._depgraph.add_edge(p_todo.attributes['number'], dep.attributes['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)
self._depgraph.add_edge(parent.attributes['number'], p_todo.attributes['number'], child)
def add(self, p_src):
""" Given a todo string, parse it and put it to the end of the list. """
todo = None
if re.search(r'\S', p_src):
todo = Todo.Todo(p_src)
self.add_todo(todo)
return todo
def add_todo(self, p_todo):
"""
Given a todo string, parse it and put it to the end of the list.
Add an Todo object to the list.
Also maintains the dependency graph to track the dependencies between
tasks.
......@@ -93,17 +102,7 @@ class TodoList(object):
Then there will be an edge 1 --> 2 with ID 4.
"""
todo = None
if re.search(r'\S', p_src):
number = len(self._todos) + 1
todo = Todo.Todo(p_src, number)
self.add_todo(todo)
return todo
def add_todo(self, p_todo):
""" Add an Todo object to the list. """
p_todo.attributes['number'] = len(self._todos) + 1
self._todos.append(p_todo)
self._maintain_dep_graph(p_todo)
......@@ -115,10 +114,10 @@ class TodoList(object):
if todo:
for child in self.children(p_number):
self.remove_dependency(todo.number, child.number)
self.remove_dependency(todo.attributes['number'], child.attributes['number'])
for parent in self.parents(p_number):
self.remove_dependency(parent.number, todo.number)
self.remove_dependency(parent.attributes['number'], todo.attributes['number'])
del self._todos[p_number - 1]
......@@ -263,7 +262,10 @@ class TodoList(object):
"""
for todo in self._todos:
todo.attributes['parents'] = self.parents(todo.number)
todo.attributes['parents'] = self.parents(todo.attributes['number'])
def todos(self):
return self._todos
def __str__(self):
return '\n'.join(pretty_print(self._todos))
......
......@@ -4,7 +4,7 @@ import datetime
import unittest
import Filter
from TestFacilities import load_file, load_file_to_raw_list, todolist_to_string
from TestFacilities import *
import Todo
import TodoList
......@@ -65,13 +65,10 @@ class FilterTest(unittest.TestCase):
def test_filter7(self):
""" Tests the dependency filter. """
todos_raw = load_file_to_raw_list('data/FilterTest2.txt')
todos = load_file('data/FilterTest2.txt')
todolist = TodoList.TodoList(todos_raw)
todolist = load_file_to_todolist('data/FilterTest2.txt')
depfilter = Filter.DependencyFilter(todolist)
filtered_todos = depfilter.filter(todos)
filtered_todos = depfilter.filter(todolist.todos())
reference = load_file('data/FilterTest2-result.txt')
self.assertEquals(todolist_to_string(filtered_todos), \
......
......@@ -7,7 +7,7 @@ def load_file(p_filename):
Loads a todo file from the given filename and returns a list of todos.
"""
todolist = load_file_to_raw_list(p_filename)
return [Todo.Todo(src, number + 1) for number, src in enumerate(todolist)]
return [Todo.Todo(src) for src in todolist]
def load_file_to_raw_list(p_filename):
"""
......
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