Commit 4ed21654 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add operators parents-of and children-of to 'dep add'

These operators copy the parents of one todo item to another:

    dep add 1 parents-of 2

This will add the parents of item 2 to item 1.

Likewise, a children-of operator adds the children of item 2 to item 1.

Along the way, the _get_todos() method was refactored to simplify things
a bit.
parent e9e5262a
......@@ -121,6 +121,67 @@ class DepCommandTest(CommandTest):
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def add_parentsof_helper(self, p_args):
command = DepCommand(p_args, self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertTrue(self.todolist.todo(4).has_tag('p', '1'))
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def test_add10(self):
self.add_parentsof_helper(["add", "4", "parents-of", "2"])
def test_add11(self):
self.add_parentsof_helper(["add", "4", "parent-of", "2"])
def test_add12(self):
self.add_parentsof_helper(["add", "4", "parentsof", "2"])
def test_add13(self):
self.add_parentsof_helper(["add", "4", "parentof", "2"])
def test_add14(self):
command = DepCommand(["add", "4", "parents-of", "5"], self.todolist,
self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def add_childrenof_helper(self, p_args):
command = DepCommand(p_args, self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertTrue(self.todolist.todo(2).has_tag('p', '2'))
self.assertTrue(self.todolist.todo(3).has_tag('p', '2'))
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def test_add15(self):
self.add_childrenof_helper(["add", "4", "children-of", "1"])
def test_add16(self):
self.add_childrenof_helper(["add", "4", "child-of", "1"])
def test_add17(self):
self.add_childrenof_helper(["add", "4", "childrenof", "1"])
def test_add18(self):
self.add_childrenof_helper(["add", "4", "childof", "1"])
def test_add19(self):
command = DepCommand(["add", "4", "children-of", "5"], self.todolist,
self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def rm_helper(self, p_args):
"""
Helper function that checks the removal of the dependency from todo 1
......
......@@ -39,44 +39,66 @@ class DepCommand(Command):
self.printer = pretty_printer_factory(self.todolist)
def _handle_add(self):
(from_todo, to_todo) = self._get_todos()
if from_todo and to_todo:
for from_todo, to_todo in self._get_todos():
self.todolist.add_dependency(from_todo, to_todo)
def _handle_rm(self):
(from_todo, to_todo) = self._get_todos()
if from_todo and to_todo:
for from_todo, to_todo in self._get_todos():
self.todolist.remove_dependency(from_todo, to_todo)
def _get_todos(self):
from_todo = None
to_todo = None
result = []
def get_parent_dependencies():
child_todo = first_todo
sibling_todo = second_todo
return [(parent, child_todo) for parent in self.todolist.parents(sibling_todo)]
def get_child_dependencies():
parent_todo = first_todo
sibling_todo = second_todo
return [(parent_todo, child) for child in self.todolist.children(sibling_todo)]
get_before_dependency = lambda: [(second_todo, first_todo)]
get_after_dependency = lambda: [(first_todo, second_todo)]
operators = {
"after": get_after_dependency,
"before": get_before_dependency,
"child-of": get_child_dependencies,
"childof": get_child_dependencies,
"children-of": get_child_dependencies,
"childrenof": get_child_dependencies,
"parent-of": get_parent_dependencies,
"parentof": get_parent_dependencies,
"parents-of": get_parent_dependencies,
"parentsof": get_parent_dependencies,
"partof": get_before_dependency,
"to": get_after_dependency,
}
try:
first_todo_nr = self.argument(1)
operator = self.argument(2)
if operator == 'before' or operator == 'partof':
from_todo_nr = self.argument(3)
to_todo_nr = self.argument(1)
elif operator == 'to' or operator == 'after':
from_todo_nr = self.argument(1)
to_todo_nr = self.argument(3)
if operator in operators:
second_todo_nr = self.argument(3)
else:
# the operator was omitted, assume 2nd argument is target task
# default to 'to' behavior
from_todo_nr = self.argument(1)
to_todo_nr = self.argument(2)
second_todo_nr = self.argument(2)
operator = "to"
first_todo = self.todolist.todo(first_todo_nr)
second_todo = self.todolist.todo(second_todo_nr)
from_todo = self.todolist.todo(from_todo_nr)
to_todo = self.todolist.todo(to_todo_nr)
result = operators[operator]()
except (InvalidTodoException):
self.error("Invalid todo number given.")
except InvalidCommandArgument:
self.error(self.usage())
return (from_todo, to_todo)
return result
def _handle_ls(self):
""" Handles the ls subsubcommand. """
......@@ -129,7 +151,7 @@ class DepCommand(Command):
def usage(self):
return """Synopsis:
dep <add|rm> <NUMBER> [to] <NUMBER>
dep add <NUMBER> <before|partof|after> <NUMBER>
dep add <NUMBER> <before|partof|after|parents-of|children-of> <NUMBER>
dep ls <NUMBER> to
dep ls to <NUMBER>
dep clean"""
......
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