gist: 3856 Download_button fork
public
Public Clone URL: git://gist.github.com/3856.git
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/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()

Owner

Anonymous

Revisions

  • a6dae5 Sun Aug 03 21:32:45 -0700 2008