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