-
Notifications
You must be signed in to change notification settings - Fork 2
/
t
executable file
·409 lines (356 loc) · 13.6 KB
/
t
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
import os
import sys
import ConfigParser
from datetime import datetime
from datetime import timedelta
import lib.util as util
from lib.TimesheetCSV import TimesheetCSV
from lib.TimesheetState import TimesheetState
from lib.Importer import Importer
def print_usage(prog):
print 'Welcome to timesheet!'
print ''
print 'TIMER USAGE'
print ' ' + prog + ' start [-m msg] [date]\tstart timing'
print ' ' + prog + ' stop [-m msg] [date]\tstop timing'
print ' ' + prog + ' message msg\t\t\tdescribe this period'
print ' ' + prog + ' cancel\t\t\tcancel this period'
print ' ' + prog + ' status\t\t\tcurrent period\'s time'
print ' ' + prog + ' last\t\t\tthe last period\'s time'
print ' ' + prog + ' break\t\t\ttime since last period'
print ''
print ' "date" takes natural expressions like "3 hours ago" using GNU date'
print ' "msg" is a string describing the time period'
print ''
print 'REPORTING'
print ' ' + prog + ' week [date]\t\t\tbreakdown of this or a previous week by day'
print ' ' + prog + ' lastweek\t\t\tbreakdown of last week by day'
print ' ' + prog + ' today\t\t\tbreakdown of today\'s work'
print ' ' + prog + ' yesterday\t\t\tbreakdown of yesterday\'s work'
print ' ' + prog + ' day [date]\t\t\tbreakdown of a given day\'s work'
print ' ' + prog + ' done\t\t\thours done this week'
print ' ' + prog + ' left\t\t\thours left this week'
print ' ' + prog + ' since date [to date]\t\twork done since or between given dates'
print ''
print ' "date" takes natural expressions like "3 hours ago" using GNU date'
print ''
print 'EXAMPLES OF NATURAL DATE EXPRESSIONS'
print ' ' + prog + ' start 5 minutes ago'
print ' ' + prog + ' stop yesterday 3:25pm'
print ' ' + prog + ' week 2 weeks ago'
print ' ' + prog + ' day yesterday'
print ' ' + prog + ' since 3 days ago'
print ' ' + prog + ' from last thursday to yesterday'
def analyze(timesheet_log, timesheet_state, period_start, period_stop, breakdown=True, current=False, week_hours=None, left=False):
# sum the range
string_start = util.date2string(period_start)
string_stop = util.date2string(period_stop)
dur = timedelta(0)
last_day = None
for row in [ x for x in timesheet_log.entries if x[0] <= string_stop and x[1] >= string_start ]:
entry_start = util.string2date(row[0])
entry_stop = util.string2date(row[1])
entry_dur = min(entry_stop, period_stop) - max(entry_start, period_start)
entry_message = row[2]
entry_day = util.date2string(max(entry_start, period_start)).split(' ')[0]
entry_day = datetime.strptime(entry_day, '%Y/%m/%d').strftime('%a %Y/%m/%d')
if entry_day != last_day:
if last_day != None:
if breakdown:
print ' ' * len(entry_day) + ' Total ' + util.delta2string(day_dur, decimal=True, abbr=True)
print ''
display_day = entry_day
day_dur = timedelta(0)
else:
display_day = ' ' * len(entry_day)
last_day = entry_day
if len(entry_message) == 0:
entry_message = '-'
dur += entry_dur
day_dur += entry_dur
if breakdown:
print display_day, util.delta2string(entry_dur, show_days=True, decimal=True, abbr=True) + '\t' + entry_message
if last_day != None:
if breakdown:
print ' ' * len(entry_day) + ' Total ' + util.delta2string(day_dur, decimal=True, abbr=True)
print ''
if current:
# add the current timer if applicable
ret = timesheet_state.Get()
if ret:
logged_time, logged_message = ret
day_dur = period_stop - max(logged_time, period_start)
dur += day_dur
if breakdown:
print 'Current ' + util.delta2string(day_dur) + ' ' + logged_message
print ''
if left:
time_left = max(timedelta(hours=week_hours) - dur, timedelta(0), timedelta(0))
time_left = util.delta2string(time_left)
print time_left, 'left'
else:
print util.delta2string(dur)
def create_config(config_path):
config = ConfigParser.ConfigParser()
config.add_section('Week')
config.set('Week', 'start_day', 'Monday')
config.set('Week', 'start_time', '09:00')
config.set('Week', 'hours', '40')
config.add_section('Storage')
config.set('Storage', 'timesheet', '~/.timesheet')
config.set('Storage', 'state', '~/.timesheet.state')
config.add_section('Importing')
config.set('Importing', 'mbox', '~/.mail/incoming')
with open(config_path, 'wb+') as configfile:
config.write(configfile)
print 'I have created the configuration file ' + config_path + '.\n'
def main(argv):
prog = os.path.basename(argv[0]) # program name as called
argc = 1 # i like C
# get or create config file
config_path = os.path.expanduser('~/.timesheetrc')
if len(argv[argc:]) >= 2 and argv[argc] == '-c':
argc = argc + 1
config_path = argv[argc]
argc = argc + 1
start_day = None
start_time = None
week_hours = None
timesheet_logfile = None
timesheet_statefile = None
mbox_path = None
if not os.path.exists(config_path):
create_config(config_path)
try_config = 0
success = False
while try_config < 3 and not success:
try_config += 1
config = ConfigParser.ConfigParser()
config.read(config_path)
try:
start_day = config.get('Week', 'start_day')
start_time = config.get('Week', 'start_time')
week_hours = int(config.get('Week', 'hours'))
timesheet_logfile = config.get('Storage', 'timesheet')
timesheet_statefile = config.get('Storage', 'state')
mbox_path = config.get('Importing', 'mbox')
success = True
except ConfigParser.NoOptionError, err:
print 'There is an option missing in the configuration file.'
create_config(config_path)
except ConfigParser.NoSectionError, err:
print 'There is an option missing in the configuration file.'
create_config(config_path)
# get the timesheet command and its arguments
if argc >= len(argv):
print_usage(prog)
return
command = argv[argc]
argc += 1
args = argv[argc:]
# get the "-m message" parameter for the start and stop commands.
command_message = ''
if len(args) > 0 and command in ['start', 'stop'] and args[0] == '-m':
if len(args) == 1:
print '-m must take a message as a parameter'
return
else:
command_message = args[1]
args = args[2:]
argument = ' '.join(args)
# get the timesheet log and state files
timesheet_log = TimesheetCSV(os.path.expanduser(timesheet_logfile))
timesheet_state = TimesheetState(os.path.expanduser(timesheet_statefile))
# process commands
if command == 'start':
ret = timesheet_state.Get()
if argument != '':
# backdate the timer
starttime = util.interpretdate(argument)
if starttime == None:
print 'invalid date. try again.'
return
else:
starttime = datetime.today()
if ret:
# stop the old timer first
yn = raw_input('the timer is already going. start a new one? [Y/n] ')
if yn.lower() == 'n':
print 'aborted. did not cancel the timer'
return
print 'stopping old timer'
stoptime = starttime;
logged_time, logged_message = ret
while logged_message == '':
logged_message = raw_input('please enter a message: ')
added, msg = timesheet_log.AddEntry(logged_time, stoptime, logged_message)
if not added:
print msg
exit(1)
timesheet_state.Clear()
print util.date2string(logged_time), util.date2string(stoptime),
logged_message
print '\n', util.delta2string(stoptime - logged_time), '\n'
timesheet_state.Set(starttime, command_message)
print 'started timing'
if command_message != '':
print 'message set'
elif command == 'stop':
ret = timesheet_state.Get()
if not ret:
print 'cannot stop what has not been started.'
return
if argument != '':
# backdate the timer
stoptime = util.interpretdate(argument)
if stoptime == None:
print 'invalid date. try again.'
return
else:
stoptime = datetime.today();
logged_time, logged_message = ret
entry_message = ''
if command_message != '':
entry_message = command_message
else:
while logged_message == '':
logged_message = raw_input('please enter a message: ')
entry_message = logged_message
added, msg = timesheet_log.AddEntry(logged_time, stoptime, entry_message)
if not added:
print msg
exit(1)
timesheet_state.Clear()
print util.date2string(logged_time), util.date2string(stoptime), entry_message
print '\n', util.delta2string(stoptime - logged_time)
elif command == 'message':
ret = timesheet_state.Get()
if not ret:
print 'the timer is not going'
return
logged_time, logged_message = ret
if logged_message != '':
print 'the message is already set'
yn = raw_input('do you want to change the message? [y/N] ')
if yn.lower() != 'y':
print 'okay. leaving the existing message alone'
return
logged_message = ''
if argument != '':
entry_message = argument
else:
while logged_message == '':
logged_message = raw_input('please enter a message: ')
entry_message = logged_message
timesheet_state.Set(logged_time, entry_message)
print 'message set'
elif command == 'cancel':
ret = timesheet_state.Get()
if not ret:
print 'cannot stop what has not been started.'
return
logged_time, logged_message = ret
print 'started', util.date2string(logged_time)
yn = raw_input('are you sure you want to cancel the entry? [y/N] ')
if yn.lower() == 'y':
timesheet_state.Clear()
print 'cancelled timer'
else:
'aborted. did not cancel the timer'
elif command == 'last':
last = timesheet_log.entries[-1]
print last[0], last[1], last[2]
print util.delta2string(util.string2date(last[1]) - util.string2date(last[0]))
elif command == 'status':
ret = timesheet_state.Get()
if not ret:
print 'the timer is not going'
return
logged_time, logged_message = ret
stop_time = datetime.today();
print util.delta2string(stop_time - logged_time), logged_message
print 'started at', util.date2string(logged_time)
elif command == 'import':
importer = Importer(timesheet_log)
importer.eternity(mbox_path)
elif command == 'week':
if argument == '':
period_start = util.get_week_start(start_day, start_time)
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True)
else:
period_start = util.get_week_start(start_day, start_time, week=util.interpretdate(argument))
period_stop = period_start + timedelta(days=7)
analyze(timesheet_log, timesheet_state, period_start, period_stop)
elif command == 'thisweek':
period_start = util.get_week_start(start_day, start_time)
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True)
elif command == 'lastweek':
# set the range sum up
period_stop = util.get_week_start(start_day, start_time)
period_start = period_stop - timedelta(days=7)
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=False)
elif command == 'day':
if argument == '':
period_start = util.get_day_start()
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True)
else:
period_start = util.get_day_start(day=util.interpretdate(argument))
period_stop = period_start + timedelta(days=1)
analyze(timesheet_log, timesheet_state, period_start, period_stop)
elif command == 'today':
# set the range sum up
period_start = util.get_day_start()
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True)
elif command == 'yesterday':
# set the range sum up
period_start = util.get_day_start() - timedelta(days=1)
period_stop = util.get_day_start()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=False)
elif command == 'break':
ret = timesheet_state.Get()
if ret:
print 'the timer is going'
return
else:
last = timesheet_log.entries[-1]
print util.delta2string(datetime.today() - util.string2date(last[1]))
elif command == 'done':
# set the range sum up
period_start = util.get_week_start(start_day, start_time)
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True, breakdown=False)
elif command == 'left':
# set the range sum up
period_start = util.get_week_start(start_day, start_time)
period_stop = datetime.today()
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True, breakdown=False, week_hours=week_hours, left=True)
elif command == 'since':
if argument == '':
print 'please specify a date'
else:
date_range = argument.split(' to ')
if len(date_range) == 1:
# set the range sum up
period_start = util.interpretdate(argument)
period_stop = datetime.today()
# Current week
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=True)
elif len(date_range) == 2:
# set the range sum up
period_start = util.interpretdate(date_range[0])
period_stop = util.interpretdate(date_range[1])
# Current week
analyze(timesheet_log, timesheet_state, period_start, period_stop, current=False)
else:
print "invalid syntax"
exit(1)
else:
print 'invalid command'
return
if __name__=='__main__':
main(sys.argv)