added Result class to hold command result

This commit is contained in:
Josh Holtrop 2011-01-03 13:53:45 -05:00
parent b8d96c8b5a
commit 7a10dd335a

52
dwtt
View File

@ -54,40 +54,49 @@ def main(argv):
break
cmd = Command(cmdline)
res = processCommand(cmd, ds)
if type(res).__name__ == 'bool':
if not res:
if not res.keepwindow:
break
elif type(res).__name__ == 'str':
# todo: move to GUI
print "Error:", res
else:
print "Unknown return type '%s'" % type(res).__name__
class Result:
def __init__(self):
self.keepwindow = False
self.error = ''
self.message = ''
self.formatted = ''
def processStart(cmd, store):
res = Result()
processOut(cmd, store)
task = store.getTaskByShortName(cmd.argstr)
if task is None:
parent = store.getParentTaskByShortName(cmd.argstr)
if parent is None:
return 'Could not find task "%s"' % \
res.error = 'Could not find task "%s"' % \
(':'.join(cmd.argstr.split(':')[:-1]))
res.keepwindow = True
return res
taskid = store.createTask(cmd.argstr.split(':')[-1], '', parent.taskid)
else:
taskid = task.taskid
store.updateCurrentTask(TaskRef(taskid, cmd.time))
return False
return res
def processOut(cmd, store):
res = Result()
ct = store.getCurrentTask()
if ct is not None:
if ct is None:
res.error = 'No current task defined'
res.keepwindow = True
return res
seconds = (cmd.time - ct.time).seconds
if seconds > 0:
store.addTime(cmd.time.strftime('%Y-%m-%d'), ct.taskid, seconds)
store.clearCurrentTask()
return False
return 'No current task defined'
return res
def processTask(cmd, store):
res = Result()
res.keepwindow = True
parts = cmd.argstr.split(',', 1)
fullname = parts[0].strip()
longname = '' if len(parts) < 2 else parts[1].strip()
@ -96,17 +105,22 @@ def processTask(cmd, store):
if task is not None:
# the task already exists, update it
store.updateTask(task.taskid, nameparts[-1], longname)
return False
res.message = 'Task "%s" updated' % fullname
return res
if len(nameparts) > 1:
parenttask = store.getParentTaskByShortName(fullname)
if parenttask is None:
return 'Parent task not found'
res.error = 'Parent task of "%s" not found' % fullname
return res
store.createTask(nameparts[-1].strip(), longname, parenttask.taskid)
else:
store.createTask(nameparts[-1].strip(), longname, None)
return False
res.message = 'Task "%s" created' % fullname
return res
def processStatus(cmd, store):
res = Result()
res.keepwindow = True
now = datetime.now()
monday = now - timedelta(now.weekday())
sunday = monday + timedelta(6)
@ -119,7 +133,7 @@ def processStatus(cmd, store):
print store.getTaskPath(task) + ':'
hours = round(float(ent.seconds) / (60 * 60), 1)
print ' %s: %0.1f' % (ent.date, hours)
return False
return res
COMMAND_HANDLERS = {
'start' : processStart,
@ -132,8 +146,10 @@ COMMAND_HANDLERS = {
def processCommand(cmd, store):
if cmd.command in COMMAND_HANDLERS:
return COMMAND_HANDLERS[cmd.command](cmd, store)
# todo: error on command not found
return False
res = Result()
res.error = 'Unknown command: %s' % cmd.command
res.keepwindow = True
return res
if __name__ == "__main__":
main(sys.argv)