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):
self.todolist.set_priority(todo, 'B')
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):
def setUp(self):
super(TodoListDependencyTester, self).setUp()
......
......@@ -66,38 +66,65 @@ class TodoListBase(object):
"""
result = None
try:
def todo_by_uid(p_identifier):
""" Returns the todo that corresponds to the unique ID. """
result = None
if config().identifiers() == 'text':
result = self._id_todo_map[p_identifier]
else:
try:
if not re.match('[1-9]\d*', p_identifier):
raise ValueError # leading zeros, pass to regexp
result = self._id_todo_map[p_identifier]
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:
if re.match('[1-9]\d*', p_identifier):
# the expression is a string and no leading zeroes,
# treat it as an integer
raise TypeError
except TypeError:
# we're dealing with an integer
pass
try:
result = self._todos[int(p_identifier) - 1]
except IndexError:
raise InvalidTodoException
result = self._todos[int(p_identifier) - 1]
except IndexError:
raise InvalidTodoException
except (TypeError, ValueError, KeyError):
result = self.todo_by_regexp(p_identifier)
return result
return result
def todo_by_regexp(p_identifier):
"""
Returns the todo that is (uniquely) identified by the given regexp.
If the regexp matches more than one item, no result is returned.
"""
result = None
def todo_by_regexp(self, p_identifier):
"""
Returns the todo that is (uniquely) identified by the given regexp.
If the regexp matches more than one item, no result is returned.
"""
result = None
candidates = Filter.GrepFilter(p_identifier).filter(self._todos)
if len(candidates) == 1:
result = candidates[0]
else:
raise InvalidTodoException
candidates = Filter.GrepFilter(p_identifier).filter(self._todos)
return result
if len(candidates) == 1:
result = candidates[0]
else:
raise InvalidTodoException
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
......
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