123 lines
3.5 KiB
Python
Executable File
123 lines
3.5 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import os
|
|
import sys
|
|
import getopt
|
|
|
|
from CmdWindow import CmdWindow
|
|
from Command import Command
|
|
from DataStore import DataStore, TaskRef
|
|
|
|
PROGRAM_NAME = 'dwtt'
|
|
|
|
def usage():
|
|
print '''Usage: dwtt [options] [dbfile]
|
|
Options:
|
|
--help Show this help
|
|
'''
|
|
|
|
def main(argv):
|
|
try:
|
|
opts, args = getopt.getopt(argv[1:], "", ["help"])
|
|
except getopt.GetoptError:
|
|
usage()
|
|
sys.exit(1)
|
|
|
|
for opt, arg in opts:
|
|
if opt == "--help":
|
|
usage()
|
|
sys.exit()
|
|
else:
|
|
usage()
|
|
sys.exit(3)
|
|
|
|
timedbfile = os.path.expanduser('~') + os.path.sep + '.dwtt.db'
|
|
if len(args) >= 1:
|
|
timedbfile = args[0]
|
|
|
|
ds = DataStore(timedbfile)
|
|
|
|
while True:
|
|
status = ''
|
|
starttime = None
|
|
ct = ds.getCurrentTask()
|
|
if ct is not None:
|
|
task = ds.getTaskByID(ct.taskid)
|
|
status = 'Task: ' + ds.getTaskPath(task)
|
|
if task.longname != '':
|
|
status += ' (%s)' % task.longname
|
|
starttime = ct.time
|
|
cw = CmdWindow(status, starttime)
|
|
cmdline = cw.main()
|
|
if cmdline.strip() == '':
|
|
break
|
|
cmd = Command(cmdline)
|
|
res = processCommand(cmd, ds)
|
|
if type(res).__name__ == 'bool':
|
|
if not res:
|
|
break
|
|
elif type(res).__name__ == 'str':
|
|
# todo: move to GUI
|
|
print "Error:", res
|
|
else:
|
|
print "Unknown return type '%s'" % type(res).__name__
|
|
|
|
def processStart(cmd, store):
|
|
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"' % \
|
|
(':'.join(cmd.argstr.split(':')[:-1]))
|
|
taskid = store.createTask(cmd.argstr.split(':')[-1], '', parent.taskid)
|
|
else:
|
|
taskid = task.taskid
|
|
store.updateCurrentTask(TaskRef(taskid, cmd.time))
|
|
return False
|
|
|
|
def processOut(cmd, store):
|
|
ct = store.getCurrentTask()
|
|
if ct is not None:
|
|
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'
|
|
|
|
def processTask(cmd, store):
|
|
parts = cmd.argstr.split(',', 1)
|
|
fullname = parts[0].strip()
|
|
longname = '' if len(parts) < 2 else parts[1].strip()
|
|
nameparts = fullname.split(':')
|
|
task = store.getTaskByName(fullname)
|
|
if task is not None:
|
|
# the task already exists, update it
|
|
store.updateTask(task.taskid, nameparts[-1], longname)
|
|
return False
|
|
if len(nameparts) > 1:
|
|
parenttask = store.getParentTaskByShortName(fullname)
|
|
if parenttask is None:
|
|
return 'Parent task not found'
|
|
store.createTask(nameparts[-1].strip(), longname, parenttask.taskid)
|
|
else:
|
|
store.createTask(nameparts[-1].strip(), longname, None)
|
|
return False
|
|
|
|
COMMAND_HANDLERS = {
|
|
'start' : processStart,
|
|
'out' : processOut,
|
|
'task' : processTask
|
|
}
|
|
|
|
# Returns boolean for whether the command prompt should be displayed again
|
|
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
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv)
|