Commit 79de0979 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Let caller ship functions to show output, errors and prompt for input.

parent 063e623f
......@@ -7,8 +7,11 @@ from PrettyPrinter import pretty_print, pp_number
from RelativeDate import relative_date_to_date
class AddCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(AddCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(AddCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.text = ' '.join(p_args)
def _preprocess_input_todo(self):
......
......@@ -3,8 +3,11 @@ from PrettyPrinter import pretty_print, pp_number
from Utils import convert_todo_number
class AppendCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(AppendCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(AppendCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None)
def execute(self):
number = convert_todo_number(self.argument(0))
......
class Command(object):
def __init__(self, p_args, p_todolist):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
"""
Sets up the basic properties for executing a subcommand.
p_args is a list of arguments that can be passed to this subcommand.
These can be retrieved with argument(), or the existence of an argument
using argumentShift().
p_todolist is a reference to the todolist instance to operate on.
p_out is a function to be called to print (standard) output. Defaults
to a noop.
p_err is a function to be called to print errors. Defaults to a noop.
p_prompt is a function that accepts a prompt string as its own argument
and returns the answer to that prompt (normally entered by the user in
some way). The default is a noop prompt.
"""
self.args = p_args
self.todolist = p_todolist
self.output = []
self.errors = []
# inputs and outputs
self.out = p_out
self.error = p_err
self.prompt = p_prompt
def execute(self):
""" The command to execute. """
return (False, None, None)
"""
Execute the command.
Returns True when the command succeeded or False on failure.
"""
return False
def argument(self, p_number):
""" Retrieves a value from the argument list. """
""" Retrieves a value from the argument list at the given position. """
try:
value = self.args[p_number]
except IndexError:
......@@ -32,8 +59,3 @@ class Command(object):
def usage(self):
return "No usage text defined for this command."
def out(self, p_text): # TODO: make private
self.output.append(p_text)
def error(self, p_text): # TODO: make private
self.errors.append(p_text)
......@@ -5,8 +5,11 @@ 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)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(DepCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.subsubcommand = self.argument(0)
def _handle_add(self):
......
......@@ -6,17 +6,20 @@ from Recurrence import advance_recurring_todo
from Utils import convert_todo_number
class DoCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(DoCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(DoCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
def _complete_children(self):
children = [t.attributes['number'] for t in self.todolist.children(self.number) if not t.is_completed()]
children = [t for t in self.todolist.children(self.number) if not t.is_completed()]
if children:
pretty_print_list(children, [pp_number])
self.out("\n".join(pretty_print_list(children, [pp_number])))
confirmation = raw_input("Also mark subtasks as done? [n] "); # FIXME
confirmation = self.prompt("Also mark subtasks as done? [n] ")
if re.match('^y(es)?$', confirmation, re.I):
for child in children:
......@@ -34,3 +37,4 @@ class DoCommand(Command.Command):
self._complete_children()
self._handle_recurrence()
self.todolist.set_todo_completed(self.number)
self.out(pretty_print(self.todo))
......@@ -4,8 +4,11 @@ import Filter
import Sorter
class ListCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(ListCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(ListCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def execute(self):
showAll = self.argumentShift("-x")
......
import Command
class ListContextCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(ListContextCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(ListContextCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def execute(self):
for context in sorted(self.todolist.contexts()):
......
import Command
class ListProjectCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(ListProjectCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(ListProjectCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def execute(self):
for project in sorted(self.todolist.projects()):
......
......@@ -66,16 +66,13 @@ class Application(object): # TODO: rename to CLIApplication
}
if subcommand in subcommand_map:
command = subcommand_map[subcommand](arguments(), self.todolist)
command.execute()
command = subcommand_map[subcommand](arguments(), self.todolist,
lambda o: sys.stdout.write(o + "\n"),
lambda e: sys.stderr.write(e + "\n"),
raw_input)
if len(command.errors):
text = "\n".join(command.errors)
sys.stderr.write(text + '\n')
exit(1)
elif len(command.output):
text = "\n".join(command.output)
sys.stdout.write(text + '\n')
if not command.execute():
exit(1)
else:
usage()
......
......@@ -3,8 +3,11 @@ from PrettyPrinter import pretty_print
from Utils import convert_todo_number, is_valid_priority
class PriorityCommand(Command.Command):
def __init__(self, p_args, p_todolist):
super(PriorityCommand, self).__init__(p_args, p_todolist)
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(PriorityCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
self.priority = self.argument(1)
......
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