#!/usr/bin/env python
'''
A timeclock.
'''
import os, string, time
home = os.path.expanduser('~')
khronosHome = os.path.join(home, '.khronos/')
khronosHomeArchive = os.path.join(khronosHome, 'archive/')
khronosHomeTrash = os.path.join(khronosHome, 'trash/')
# Places a title at the top of each 'screen'
def title():
os.system('clear')
print 'Khronos 3000\n'
# Prompts to start a timer, or quit.
def chooser():
print '> home\n'
print '(t)imer'
print '(p)rojects'
print '(h)elp'
print ' -'
print '(q)uit\n'
chooserAction = raw_input('? ')
chooserAction = string.strip(chooserAction)
if chooserAction == 't':
setProject()
if chooserAction == 'p':
projects()
if chooserAction == 'h':
help()
if chooserAction == 'q':
quit()
else:
goHome()
def setProject():
title()
print '> Timer: Choose an Existing Project or Start Anew\n'
projectLister()
print '\nProject Name:'
myProjectName = raw_input('? ')
if myProjectName == 'q':
quit()
if myProjectName == 'h':
goHome()
else:
title()
myProjectName = myProjectName.replace(" ", "_")
timer(projectName=myProjectName)
# The timer, which can be started, stopped, and
# when prompted will give the elapsed time
def timer(timeElapsed=0, projectName='default'):
file = khronosHome + projectName
timeStart = time.time()
if timeElapsed == 0:
print 'Timer running...\n'
print '(s)top'
print '(e)lapsed time\n'
timerAction = raw_input('? ')
timerAction = string.strip(timerAction)
if timerAction == 's': # stop
timeStop = time.time()
timeTotal = (int(timeStop - timeStart) + timeElapsed)
title()
print 'Saving Session...\n'
print 'Time:'
secondsToHMS(timeTotal)
print 'Notes:'
sessionNote = raw_input('? ')
f = open(file, 'a')
session = ('%d' % timeTotal) + '\t' + string.strip(sessionNote) + '\n'
f.write(session)
f.close()
goHome()
if timerAction == 'e': # get time
timeStop = time.time()
timeElapsed = (timeStop - timeStart) + timeElapsed
title()
secondsToHMS(timeElapsed)
timer(timeElapsed, projectName)
else: # keep the timer going on bad input
title()
print 'Timer running...\n'
timeStop = time.time()
timeElapsed = (timeStop - timeStart) + timeElapsed
timer(timeElapsed)
def projects():
title()
print '> Projects\n'
projectLister()
sessionLister()
def projectLister(): # FIXME this should say if there are no projects.
# list all projects (files), but not folders (archive, trash)
print 'Projects:'
dirList = [f for f in os.listdir(khronosHome) if os.path.isfile(os.path.join(khronosHome, f))] # http://www.faqs.org/docs/diveintopython/apihelper_filter.html
for fname in dirList:
print " ", fname
def sessionLister():
# list sessions for a particular project
print '\nManage Project:'
sessionListerAction = raw_input('? ') # this can be a project name or a nav choice
title()
file = os.path.join(khronosHome, sessionListerAction)
if os.path.isfile(file):
f=open(file, 'r')
print '> Project:', sessionListerAction, '\n'
for line in f:
print line,
print '\n(a)rchive'
print '(t)rash'
print ' -'
print '(p)rojects\n'
sessionListAction = raw_input('? ')
sessionListAction = string.strip(sessionListAction)
if sessionListAction == 'a': # archive
os.system('mv ' + khronosHome + sessionListerAction + ' ' + khronosHome + 'archive/')
title()
print 'Project', sessionListerAction, 'archived'
time.sleep(2)
projects()
if sessionListAction == 't': # trash
os.system('mv ' + khronosHome + sessionListerAction + ' ' + khronosHome + 'trash/')
title()
print 'Project', sessionListerAction, 'trashed'
time.sleep(2)
projects()
if sessionListAction == 'p': # ok
projects()
if sessionListAction == 'h': # home
goHome()
if sessionListAction == 'q': # quit
quit()
else:
projects()
if sessionListerAction == 'h': #home
goHome()
if sessionListerAction == 'q': #
quit()
else:
projects()
def secondsToHMS(seconds):
# convert seconds into hours, minutes and seconds
mins, secs = divmod(seconds,60)
hours, mins = divmod(mins, 60)
print int(hours), 'h', int(mins), 'm', int(secs), 's\n'
def help():
title()
print '> Help\n'
print 'Global Commands:'
print '* (h)ome'
print '* (q)uit'
print '\nFiles'
print '* Projects are stored in ~/.khronos/<projectname>'
print '* Archived projects are stored in ~/.khronos/archive/<projectname>'
print '* Trashed projects are stored in ~/.khronos/archive/<projectname>'
print '\n(h)ome\n'
helpAction = raw_input('? ')
if helpAction == 'h': #home
goHome()
else:
help()
def quit():
title()
print 'Bye\n'
exit()
def goHome():
title()
chooser()
# Initialize ~/.khronos/ Storage Directories
def mkdir():
if os.path.isdir(khronosHome):
pass
elif os.path.isfile(khronosHome):
print "A file with the name", khronosHome, "exists. You'll need to fix this."
else:
os.system('mkdir ' + khronosHome)
os.system('mkdir ' + khronosHome + '/trash')
os.system('mkdir ' + khronosHome + '/archive')
title()
mkdir()
chooser()