Commit a2627d36 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Make separate command for dependencies.

parent e6d8ab54
......@@ -6,3 +6,15 @@ class Command(object):
def execute(self):
""" The command to execute. """
return False
def argument(self, p_number):
""" Retrieves a value from the argument list. """
try:
value = self.args[p_number]
except IndexError:
self.usage()
return value
def usage(self):
return ""
import Command
import Config
import Sorter
from Utils import convert_todo_number
import View
class DepCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(DepCommand, self).__init__(p_args, p_todolist)
self.subsubcommand = self.argument(0)
def _handle_add(self):
(from_todo, to_todo) = self._get_todo_numbers()
self.todolist.add_dependency(from_todo, to_todo)
def _handle_rm(self):
(from_todo, to_todo) = self._get_todo_numbers()
self.todolist.remove_dependency(from_todo, to_todo)
def _get_todo_numbers(self):
from_todonumber = convert_todo_number(self.argument(1))
to_todonumber = self.argument(2)
if to_todonumber == 'to':
to_todonumber = convert_todo_number(self.argument(3))
else:
to_todonumber = convert_todo_number(to_todonumber)
return (from_todonumber, to_todonumber)
def _handle_ls(self):
""" Handles the ls subsubcommand. """
arg1 = self.argument(1)
arg2 = self.argument(2)
todos = []
if arg2 == 'to':
# dep ls 1 to ...
todos = self.todolist.children(convert_todo_number(arg1))
elif arg1 == 'to':
# dep ls ... to 1
todos = self.todolist.parents(convert_todo_number(arg2))
else:
self.usage()
if todos:
sorter = Sorter.Sorter(Config.SORT_STRING)
view = View.View(sorter, [], todos)
print view.pretty_print() # FIXME
def execute(self):
dispatch = {
'add': self._handle_add,
'rm': self._handle_rm,
'del': self._handle_rm,
'ls': self._handle_ls,
'clean': self.todolist.clean_dependencies,
'gc': self.todolist.clean_dependencies,
}
dispatch[self.subsubcommand]() if self.subsubcommand in dispatch \
else self.usage()
return True
......@@ -5,6 +5,7 @@ import re
import sys
from AddCommand import AddCommand
from DepCommand import DepCommand
import Config
import Filter
from PrettyPrinter import pretty_print
......@@ -13,7 +14,6 @@ import Sorter
import TodoFile
import TodoList
from Utils import convert_todo_number
import View
def print_iterable(p_iter):
""" Prints an iterable to the standard output, one item per line. """
......@@ -77,53 +77,8 @@ class Application(object): # TODO: rename to CLIApplication
self.print_todo(number)
def dep(self):
""" Handles dependencies between todos. """
def handle_add_rm(operation):
""" Handles the add and rm subsubcommands. """
from_todonumber = convert_todo_number(argument(3))
to_todonumber = argument(4)
if to_todonumber == 'to':
to_todonumber = convert_todo_number(argument(5))
else:
to_todonumber = convert_todo_number(to_todonumber)
if operation == 'add':
self.todolist.add_dependency(from_todonumber, to_todonumber)
else:
self.todolist.remove_dependency(from_todonumber, to_todonumber)
def handle_ls():
""" Handles the ls subsubcommand. """
arg1 = argument(3)
arg2 = argument(4)
todos = []
if arg2 == 'to':
# dep ls 1 to ...
todos = self.todolist.children(convert_todo_number(arg1))
elif arg1 == 'to':
# dep ls ... to 1
todos = self.todolist.parents(convert_todo_number(arg2))
else:
usage()
if todos:
sorter = Sorter.Sorter(Config.SORT_STRING)
view = View.View(sorter, [], todos)
print view.pretty_print()
subsubcommand = argument(2)
if subsubcommand == 'add' or \
subsubcommand == 'rm' or subsubcommand == 'del':
handle_add_rm(subsubcommand)
elif subsubcommand == 'clean' or subsubcommand == 'gc':
self.todolist.clean_dependencies()
elif subsubcommand == 'ls':
handle_ls()
else:
usage()
command = DepCommand(arguments(), self.todolist)
command.execute()
def do(self):
def complete_children(p_number):
......
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