Añadidas tres funciones más:
[ttt.git] / ttt.py
1 #!/usr/bin/python
2
3 # Author: Daniel Garcia <dani@danigm.net>
4 # License: GPLv3
5
6 import datetime
7 import time
8 import sys
9 import tttdb as db
10 from tttdb import Tracker
11 from sqlobject import AND
12
13 sqlhub = db.get_hub()
14
15 def parse_time(time_seconds):
16     hour = 0
17     min = 0
18     sec = time_seconds
19     while sec >= 60:
20         min += 1
21         sec -= 60
22     while min >= 60:
23         hour += 1
24         min -= 60
25     time_str = '%02d:%02d:%02d' % (hour, min, sec)
26     return time_str
27
28 def track(task, project='', ticket=0):
29     today = datetime.datetime.now()
30     tick = time.time()
31     time_pass = 0
32     try:
33         while True:
34             tick2 = time.time()
35             time_pass = tick2 - tick
36             cadena = '\rtracking: %(task)s %(project)s %(ticket)s %(time)s' % \
37                 {'time':parse_time(time_pass),
38                  'task':task,
39                  'project':project,
40                  'ticket':ticket}
41             sys.stdout.write(cadena)
42             sys.stdout.flush()
43             time.sleep(0.5)
44     except:
45         print '\n', 'Finalizando'
46         end = datetime.datetime.now()
47         db.Tracker(project=project,
48                 task=task,
49                 ticket=ticket,
50                 start=today,
51                 end=end)
52
53 def show(trackers):
54     grouped = {}
55     total = 0
56     prev = ''
57     for i in trackers:
58         diff = i.end - i.start
59         total += diff.seconds
60         time_passed = parse_time(diff.seconds)
61
62         key = (i.task, i.project)
63         if grouped.has_key(key):
64             grouped[key] += diff.seconds
65             
66         else:
67             grouped[key] = diff.seconds
68         
69         to_show = '%s | %-20s | %-40s | #%-6d | %s'
70         to_show = to_show % (i.start.ctime(), i.project, i.task, i.ticket, time_passed)
71         if prev != to_show[0:3]:
72             prev = to_show[0:3]
73             print ''
74         print to_show
75     
76     print ''
77     for k,v in grouped.items():
78         task, project = k
79         time_passed = parse_time(v)
80         print 'Tiempo para %-40s %s' % ('"'+task+'":', time_passed)
81
82     print '\nTotal: %s' % parse_time(total)
83
84 def show_today(delta=0, day=False, func=show):
85     today = datetime.datetime.now() - datetime.timedelta(delta)
86     yesterday = datetime.datetime(today.year, today.month, today.day)
87     tomorrow = datetime.datetime(today.year, today.month, today.day, 23, 59, 59)
88     if day:
89         today_trackers = Tracker.select(AND(Tracker.q.start > yesterday,
90                                             Tracker.q.start < tomorrow),
91                                             orderBy="start")
92     else:
93         today_trackers = Tracker.select(Tracker.q.start > yesterday,
94                                         orderBy="start")
95
96     print 'DAY %s' % yesterday.ctime()
97     func(today_trackers)
98
99 def show_project(project):
100     project_trackers = Tracker.select(Tracker.q.project == project,
101                                     orderBy="start")
102     sum = 0
103     for i in project_trackers:
104         sum += (i.end - i.start).seconds
105
106     print 'Tiempo para "%s": %s' % (project, parse_time(sum))
107
108 def show_all(func=show):
109     all = Tracker.select(orderBy="start")
110     func(all)
111
112 def show_all_grouped():
113     all = show_all(func=show_grouped)
114
115 def show_week():
116     d = datetime.datetime.now().weekday()
117     show_today(d)
118
119 def show_week_grouped():
120     d = datetime.datetime.now().weekday()
121     show_today_grouped(d)
122
123 def show_today_grouped(delta=0, day=False):
124     show_today(delta, day, func=show_grouped)
125
126 def show_grouped(trackers):
127     projects = {}
128     total = 0
129     for i in trackers:
130         diff = (i.end - i.start).seconds
131         total += diff
132         if projects.has_key(i.project):
133             projects[i.project] += diff
134         else:
135             projects[i.project] = diff
136
137     print ''
138     for k,v in projects.items():
139         p = '"' + k + '":'
140         print 'Tiempo para %-20s %s' % (p, parse_time(v))
141
142     print '\nTotal: %s' % parse_time(total)
143
144 if __name__ == '__main__':
145     help = '''
146     %s tarea [proyecto] [ticket]
147     '''
148
149     task, project, ticket = '','',0
150     args = sys.argv[1:]
151     if ('-h' in args) or ('--help' in args) or (len(args) == 0):
152         print help % sys.argv[0]
153         sys.exit()
154     if len(args) >= 1:
155         task = args[0]
156     if len(args) >= 2:
157         project = args[1]
158     if len(args) >= 3:
159         try:
160             ticket = int(args[2])
161         except:
162             ticket = 0
163
164     track(task, project, ticket)