Commit 3e938f33 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Refactor TodoListBase.todo().

Handle every identifier type in a separate function and try to obtain a
result from one of them.
parent 59452903
...@@ -212,6 +212,14 @@ class TodoListTester(TopydoTest.TopydoTest): ...@@ -212,6 +212,14 @@ class TodoListTester(TopydoTest.TopydoTest):
self.todolist.set_priority(todo, 'B') self.todolist.set_priority(todo, 'B')
self.assertEquals(self.todolist.todo('6iu').source(), "(B) Foo @Context2 Not@Context +Project1 Not+Project") self.assertEquals(self.todolist.todo('6iu').source(), "(B) Foo @Context2 Not@Context +Project1 Not+Project")
def test_uid3(self):
"""
Must be able to handle integers when text identifiers are enabled.
"""
config("test/data/todolist-uid.conf")
self.assertRaises(InvalidTodoException, self.todolist.todo, 1)
class TodoListDependencyTester(TopydoTest.TopydoTest): class TodoListDependencyTester(TopydoTest.TopydoTest):
def setUp(self): def setUp(self):
super(TodoListDependencyTester, self).setUp() super(TodoListDependencyTester, self).setUp()
......
...@@ -66,26 +66,43 @@ class TodoListBase(object): ...@@ -66,26 +66,43 @@ class TodoListBase(object):
""" """
result = None result = None
try: def todo_by_uid(p_identifier):
""" Returns the todo that corresponds to the unique ID. """
result = None
if config().identifiers() == 'text': if config().identifiers() == 'text':
try:
result = self._id_todo_map[p_identifier] result = self._id_todo_map[p_identifier]
else: except KeyError:
pass # we'll try something else
return result
def todo_by_linenumber(p_identifier):
"""
Attempts to find the todo on the given line number.
When the identifier is a number but has leading zeroes, the result
will be None.
"""
result = None
if config().identifiers() != 'text':
try: try:
if not re.match('[1-9]\d*', p_identifier): if re.match('[1-9]\d*', p_identifier):
raise ValueError # leading zeros, pass to regexp # the expression is a string and no leading zeroes,
# treat it as an integer
raise TypeError
except TypeError: except TypeError:
# we're dealing with an integer try:
pass
result = self._todos[int(p_identifier) - 1] result = self._todos[int(p_identifier) - 1]
except IndexError: except IndexError:
raise InvalidTodoException raise InvalidTodoException
except (TypeError, ValueError, KeyError):
result = self.todo_by_regexp(p_identifier)
return result return result
def todo_by_regexp(self, p_identifier): def todo_by_regexp(p_identifier):
""" """
Returns the todo that is (uniquely) identified by the given regexp. Returns the todo that is (uniquely) identified by the given regexp.
If the regexp matches more than one item, no result is returned. If the regexp matches more than one item, no result is returned.
...@@ -101,6 +118,16 @@ class TodoListBase(object): ...@@ -101,6 +118,16 @@ class TodoListBase(object):
return result return result
result = todo_by_uid(p_identifier)
if not result:
result = todo_by_linenumber(p_identifier)
if not result:
result = todo_by_regexp(p_identifier)
return result
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. """
todos = self.add_list([p_src]) todos = self.add_list([p_src])
......
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