creados los script ejecutables
[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
12 sqlhub = db.get_hub()
13
14 def parse_time(time_seconds):
15     hour = 0
16     min = 0
17     sec = time_seconds
18     while sec >= 60:
19         min += 1
20         sec -= 60
21     while min >= 60:
22         hour += 1
23         min -= 60
24     time_str = '%02d:%02d:%02d' % (hour, min, sec)
25     return time_str
26
27 def track(task, project='', ticket=0):
28     today = datetime.datetime.now()
29     tick = time.time()
30     time_pass = 0
31     try:
32         while True:
33             tick2 = time.time()
34             time_pass = tick2 - tick
35             cadena = '\rtracking: %(task)s %(project)s %(ticket)s %(time)s' % \
36                 {'time':parse_time(time_pass),
37                  'task':task,
38                  'project':project,
39                  'ticket':ticket}
40             sys.stdout.write(cadena)
41     except:
42         print '\n', 'Finalizando'
43         end = datetime.datetime.now()
44         db.Tracker(project=project,
45                 task=task,
46                 ticket=ticket,
47                 start=today,
48                 end=end)
49
50 def show(trackers):
51     grouped = {}
52     total = 0
53     for i in trackers:
54         diff = i.end - i.start
55         total += diff
56         time_passed = parse_time(diff.seconds)
57
58         key = (i.task, i.project)
59         if grouped.has_key(key):
60             grouped[key] += diff.seconds
61             
62         else:
63             grouped[key] = diff.seconds
64         
65         to_show = '%-20s | %-40s | #%-6d | %s | %s | %s'
66         print to_show % (i.project, i.task, i.ticket, i.start, i.end,
67                                                             time_passed)
68     
69     print ''
70     for k,v in grouped.items():
71         task, project = k
72         time_passed = parse_time(v)
73         print 'Tiempo para "%s": %s' % (task, time_passed)
74
75     print '\nTotal para hoy: %s' % parse_time(total)
76
77 def show_today(delta=1):
78     # TODO yesterday no es real, hay que mirar desde las 00 del mismo dia
79     yesterday = datetime.datetime.now() - datetime.timedelta(delta)
80     today_trackers = Tracker.select(Tracker.q.start > yesterday)
81     show(today_trackers)
82
83 def show_project(project):
84     project_trackers = Tracker.select(Tracker.q.project == project)
85     sum = 0
86     for i in project_trackers:
87         sum += (i.end - i.start).seconds
88
89     print 'Tiempo para %s: %s' % (project, parse_time(sum))
90
91 def show_sem():
92     show_today(7)
93
94 def show_today_grouped(delta=1):
95     yesterday = datetime.datetime.now() - datetime.timedelta(delta)
96     today_trackers = Tracker.select(Tracker.q.start > yesterday)
97     show_grouped(today_trackers)
98
99 def show_grouped(trackers):
100     projects = {}
101     total = 0
102     for i in trackers:
103         diff = (i.end - i.start).seconds
104         total += diff
105         if projects.has_key(i.project):
106             projects[i.project] += diff
107         else:
108             projects[i.project] = diff
109
110     for k,v in projects.items():
111         print 'Tiempo para %-50s: %s' % (k, parse_time(v))
112
113     print '\nTotal para hoy: %s' % parse_time(total)
114
115 if __name__ == '__main__':
116     task, project, ticket = '','',0
117     args = sys.argv[1:]
118     if len(args) >= 1:
119         task = args[0]
120     if len(args) >= 2:
121         project = args[1]
122     if len(args) >= 3:
123         ticket = int(args[2])
124
125     track(task, project, ticket)