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