AƱadido sistema de instalaciĆ³n
[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             sys.stdout.flush()
42             time.sleep(0.5)
43     except:
44         print '\n', 'Finalizando'
45         end = datetime.datetime.now()
46         db.Tracker(project=project,
47                 task=task,
48                 ticket=ticket,
49                 start=today,
50                 end=end)
51
52 def show(trackers):
53     grouped = {}
54     total = 0
55     prev = ''
56     for i in trackers:
57         diff = i.end - i.start
58         total += diff.seconds
59         time_passed = parse_time(diff.seconds)
60
61         key = (i.task, i.project)
62         if grouped.has_key(key):
63             grouped[key] += diff.seconds
64             
65         else:
66             grouped[key] = diff.seconds
67         
68         to_show = '%s | %-20s | %-40s | #%-6d | %s'
69         to_show = to_show % (i.start.ctime(), i.project, i.task, i.ticket, time_passed)
70         if prev != to_show[0:3]:
71             prev = to_show[0:3]
72             print ''
73         print to_show
74     
75     print ''
76     for k,v in grouped.items():
77         task, project = k
78         time_passed = parse_time(v)
79         print 'Tiempo para %-40s %s' % ('"'+task+'":', time_passed)
80
81     print '\nTotal: %s' % parse_time(total)
82
83 def show_today(delta=0):
84     today = datetime.datetime.now() - datetime.timedelta(delta)
85     yesterday = datetime.datetime(today.year, today.month, today.day)
86     today_trackers = Tracker.select(Tracker.q.start > yesterday)
87     show(today_trackers)
88
89 def show_project(project):
90     project_trackers = Tracker.select(Tracker.q.project == project)
91     sum = 0
92     for i in project_trackers:
93         sum += (i.end - i.start).seconds
94
95     print 'Tiempo para "%s": %s' % (project, parse_time(sum))
96
97 def show_all():
98     all = Tracker.select()
99     show(all)
100
101 def show_week():
102     show_today(6)
103
104 def show_week_grouped():
105     show_today_grouped(6)
106
107 def show_today_grouped(delta=0):
108     today = datetime.datetime.now() - datetime.timedelta(delta)
109     yesterday = datetime.datetime(today.year, today.month, today.day)
110     today_trackers = Tracker.select(Tracker.q.start > yesterday)
111     show_grouped(today_trackers)
112
113 def show_grouped(trackers):
114     projects = {}
115     total = 0
116     for i in trackers:
117         diff = (i.end - i.start).seconds
118         total += diff
119         if projects.has_key(i.project):
120             projects[i.project] += diff
121         else:
122             projects[i.project] = diff
123
124     print ''
125     for k,v in projects.items():
126         p = '"' + k + '":'
127         print 'Tiempo para %-20s %s' % (p, parse_time(v))
128
129     print '\nTotal: %s' % parse_time(total)
130
131 if __name__ == '__main__':
132     task, project, ticket = '','',0
133     args = sys.argv[1:]
134     if len(args) >= 1:
135         task = args[0]
136     if len(args) >= 2:
137         project = args[1]
138     if len(args) >= 3:
139         ticket = int(args[2])
140
141     track(task, project, ticket)