-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_totals.py
executable file
·68 lines (57 loc) · 2.11 KB
/
quick_totals.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
#!/usr/bin/env python
import logging
import logging.config
import re
from collections import defaultdict
from datetime import date, timedelta
from optparse import OptionParser
from ConfigParser import ConfigParser
from models import get_session, TimeEntry, SlimtimerUser
def main():
parser = OptionParser('usage: %prog [options] task')
parser.add_option("-f", "--config-file", dest='config_file',
help="Configuration file")
options, args = parser.parse_args()
if not (options.config_file):
parser.error("Please specify a configuration file.")
logging.config.fileConfig(options.config_file)
config = ConfigParser()
config.read(options.config_file)
session = get_session(config)
today = date.today()
for task in args:
print 'Task: {0}'.format(task)
print
# Print today's totals
entries = session.query(TimeEntry).filter(
TimeEntry.task.op('~')(task)).filter(
TimeEntry.start_dt >= today).filter(
TimeEntry.start_dt <= (today + timedelta(days=1)))
grouped_entries = group_entries(entries)
print 'Time Today:'
print
print_user_totals(session, grouped_entries)
print
# Print month's totals
entries = session.query(TimeEntry).filter(
TimeEntry.task.op('~')(task)).filter(
TimeEntry.start_dt >= date(today.year, today.month, 1)).filter(
TimeEntry.start_dt <= (today + timedelta(days=1)))
grouped_entries = group_entries(entries)
print 'Time This Month:'
print
print_user_totals(session, grouped_entries)
print
print
def group_entries(entries):
grouped_entries = defaultdict(list)
for entry in entries:
grouped_entries[entry.user_id].append(entry)
return grouped_entries
def print_user_totals(session, user_entries):
for user_id in user_entries.keys():
user = session.query(SlimtimerUser).get(user_id)
hours = sum(map(lambda e: e.duration, user_entries[user_id]))
print '{0}: {1:0.2f}'.format(user.label, hours)
if __name__ == '__main__':
main()