-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtracker.py
executable file
·507 lines (416 loc) · 16.1 KB
/
gtracker.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/python
__appname__ = "Gtracker"
__author__ = "Eustaquio 'TaQ' Rangel"
__copyright__ = "2010 Eustaquio 'TaQ' Rangel"
__version__ = "0.0.1"
__license__ = "GPL"
__email__ = "[email protected]"
__website__ = "http://github.com/taq/gtracker"
__date__ = "$Date: 2010/04/12 12:00:00$"
import os
import sys
import gtk
import glob
import time
import gobject
import pygtk
import locale
import gettext
import datetime
import threading
pygtk.require('2.0')
from configwindow import *
from pivotal import *
from config import *
from story import *
from state import *
from task import *
try:
import pynotify
notify = 1
except:
notify = 0
try:
from dbus_server import *
dbus_enabled = 1
except:
dbus_enabled = 0
BASE_DIRS = [os.path.join(os.path.expanduser("~"), ".local", "share"),"/usr/share/pyshared","/usr/local/share", "/usr/share"]
DATA_DIRS = [os.path.abspath(sys.path[0])] + [os.path.join(d,__appname__.lower()) for d in BASE_DIRS]
gettext.bindtextdomain(__appname__.lower())
gettext.textdomain(__appname__.lower())
_ = gettext.gettext
gobject.threads_init()
class InitThread(threading.Thread):
def __init__(self,gui):
super(InitThread,self).__init__()
self.gui = gui
def run(self):
self.gui.init()
class Gtracker:
def __init__(self):
self.started = datetime.datetime.now()
self.username = None
self.password = None
self.pivotal = None
self.projects = {}
self.stories = {}
self.working = False
self.stats = {}
self.config = Config()
self.interval = self.config.interval
self.username = self.config.username
self.password = self.config.password
self.pivotal = Pivotal(self)
self.firstrun = True
self.hashes = {}
self.old_hashes= {}
self.menu = gtk.Menu()
self.config_menu = gtk.Menu()
self.configItem = None
self.checkItem = None
self.statItem = None
self.aboutItem = None
self.quitItem = None
self.statusIcon = gtk.StatusIcon()
self.statusIcon.set_from_file(self.get_icon("gtracker.png"))
self.statusIcon.set_visible(True)
self.statusIcon.connect('activate' , self.left_click , self.menu)
self.statusIcon.connect('popup-menu', self.right_click, self.config_menu)
self.statusIcon.set_visible(1)
self.make_control_menu()
self.set_tooltip("Gtracker - Control your Pivotal Tracker stories from the tray bar")
if notify>0:
pynotify.init("Gtracker")
t = InitThread(self)
t.start()
if dbus_enabled>0:
bus = dbus.SessionBus()
bus_name = dbus.service.BusName('com.Gtracker',bus=bus)
bus_obj = DbusServer(bus_name,'/')
bus_obj.set_manager(self)
gtk.main()
def make_config_item(self):
if self.configItem==None:
self.configItem = gtk.MenuItem(_("Configuration"))
self.configItem.connect('activate', self.config_from_menu)
self.config_menu.append(self.configItem)
return self.configItem
def make_check_item(self):
if self.checkItem==None:
self.checkItem = gtk.MenuItem(_("Check now!"))
self.checkItem.connect('activate', self.check_now)
self.config_menu.append(self.checkItem)
return self.checkItem
def make_stat_item(self):
if self.statItem==None:
self.statItem = gtk.MenuItem(_("Statistics"))
self.statItem.connect('activate', self.statistic)
self.config_menu.append(self.statItem)
return self.statItem
def make_about_item(self):
if self.aboutItem==None:
self.aboutItem = gtk.MenuItem(_("About"))
self.aboutItem.connect('activate', self.about)
self.config_menu.append(self.aboutItem)
return self.aboutItem
def make_quit_item(self):
if self.quitItem==None:
self.quitItem = gtk.MenuItem(_("Quit"))
self.quitItem.connect('activate', self.quit)
self.config_menu.append(self.quitItem)
return self.quitItem
def make_control_menu(self):
self.make_config_item()
self.make_check_item()
self.make_stat_item()
self.make_about_item()
self.make_quit_item()
def have_authentication_info(self):
return self.username!=None and self.password!=None and len(self.username)>0 and len(self.password)>0
def show_auth_message(self):
self.set_tooltip(_("Please configure your username and password and try again"))
self.blinking(True)
def init(self):
self.set_tooltip(_("Authenticating and verifying stories ..."))
self.checkItem.set_sensitive(False)
if not self.have_authentication_info():
self.show_auth_message()
return
self.check_stories()
self.checkItem.set_sensitive(True)
def check_stories(self):
if not self.have_authentication_info():
print "No authentication found."
self.show_auth_message()
self.show_error(_("Please configure your username and password and try again"))
return
self.working = True
self.blinking(True)
try:
if not self.pivotal.auth():
print "Could not authenticate."
self.set_tooltip(_("Could not authenticate. Please verify your username and password and try again."))
self.working = False
return
except Exception as exc:
self.set_tooltip(_("Error authenticating. Please try again."))
self.working = False
return
self.set_tooltip(_("Retrieving projects info ..."))
projs = self.pivotal.get_projects()
count = 0
self.old_hashes.update(self.hashes)
self.hashes = {}
for proj in projs:
proj_id, proj_name, proj_last = proj
proj_found = None
# if there's already a project, check the last activity time
if proj_id in self.projects:
proj_found = self.projects[proj_id]
if proj_found["last_activity"]==proj_last:
continue
# check if there are stories for this project
self.set_tooltip(_("Retrieving stories for project %s ...") % proj_name)
stories = self.pivotal.get_stories(proj_id)
# no stories, move on
if len(stories)<1:
# but if there is a menu item, remove it
if proj_found!=None and proj_found["menu_item"]!=None:
self.menu.remove(proj_found["menu_item"])
continue
# if there isn't a project, we need to create a dictionary entry
proj_item = None
if proj_found==None:
proj_item = gtk.MenuItem(proj_name,False)
self.menu.append(proj_item)
# if there is a project with some activity, clear the stories submenu
else:
proj_found["menu_item"].remove_submenu()
proj_item = proj_found["menu_item"]
# update project info
self.projects[proj_id] = {"name":proj_name,"menu_item":proj_item,"last_activity":proj_last}
submenu = gtk.Menu()
proj_item.set_submenu(submenu)
self.stories[proj_id] = {}
for story in stories:
self.stories[proj_id][story.id] = story
tasks = self.pivotal.get_tasks(proj_id,story.id,story.name,True)
story.tasks = tasks
task_size = len(tasks)
story.multiline = self.config.multiline
menu_item = gtk.MenuItem(("%s" % story),False)
story.menu_item = menu_item
self.hashes[story.hash()] = story.name
snew = False
print "checking for '%s' - %s in %s" % (story.name,story.hash(),",".join(self.old_hashes.keys()))
if not story.hash() in self.old_hashes:
print "didnt't find %s" % story.hash()
snew = True
if not self.firstrun and snew:
print "there were some changes on %s - %s" % (story.name,story.hash())
self.notify(("%s %s" % (proj_name,_("story alert"))),story.str(True))
if story.done or int(story.points)<0:
story.menu_item.set_sensitive(False)
if task_size>0:
task_submenu = gtk.Menu()
for task in tasks:
task_menuitem = gtk.MenuItem(("%s" % task),False)
task_menuitem.connect("activate",self.complete_task_from_menu,task)
task.menu_item = task_menuitem
task_submenu.append(task_menuitem)
menu_item.set_submenu(task_submenu)
else:
menu_item.connect("activate",self.update_story_state_from_menu,story)
submenu.append(menu_item)
if self.config.separator and story!=stories[-1]:
submenu.append(gtk.SeparatorMenuItem())
count += 1
self.set_tooltip(_("%d stories retrieved.") % count)
self.blinking(False)
self.working = False
self.firstrun = False
self.schedule_next_check()
def schedule_next_check(self):
gobject.timeout_add(1000*60*self.interval,self.check_thread)
def update_story_state_from_menu(self,widget,story):
self.update_story_state(story,False)
def choose_from_states(self,states):
image = gtk.Image()
image.set_from_file(self.get_icon("gtracker.png"))
dialog = gtk.Dialog(_("Please choose"),None,gtk.DIALOG_MODAL,(_(states[0].verb),1000,_(states[1].verb),1001,_("Cancel"),gtk.RESPONSE_CANCEL))
label = gtk.Label(_("Please choose what you want\nto do with your story."))
dialog.vbox.pack_start(label,padding=10,expand=True,fill=True)
dialog.vbox.pack_end(image,padding=10)
dialog.show_all()
resp = dialog.run()
dialog.destroy()
return resp
def notify(self,title,msg):
noti = pynotify.Notification(title,msg,self.get_icon("gtracker.png"))
noti.show()
def update_story_state(self,story,silent=False):
# choose state
next_states = story.next_states()
if len(next_states)>1:
states = [States.get_state(next_states[0]),States.get_state(next_states[1])]
rsp = self.choose_from_states(states)
if rsp==gtk.RESPONSE_CANCEL:
self.show_error(_("Story update canceled!"))
return False
next_state = next_states[rsp-1000]
else:
next_state = next_states[0]
state_obj = States.get_state(next_state)
present_verb = _(state_obj.verb)
past_verb = _(state_obj.past)
if not silent and self.ask(_("Are you sure you want to %(verb)s '%(name)s'?") % {"verb":present_verb.lower(),"name":story.name})!=gtk.RESPONSE_YES:
return False
rsp = self.pivotal.update_story_state(story,next_state)
if not silent and not rsp:
self.show_error(_("Could not start to work on '%s'") % story.name)
return False
story.menu_item.set_label(story.__str__())
if story.done:
story.menu_item.set_sensitive(False)
if not silent:
self.show_info(_("'%(name)s' %(verb)s.") % {"name":story.name,"verb":past_verb.lower()})
count = 0
key = past_verb.lower()
try:
count = self.stats[key]
count += 1
except:
count = 1
self.stats[key] = count
return True
def stats_str(self):
s = ""
for k,v in self.stats.items():
s += k.capitalize()+": "+str(v)+"\n"
return s
def statistic(self,widget):
self.show_info(_("About this session\n\nStarted on %(start)s\n\n%(str)s") % {"start":self.started.strftime(_("%m/%d/%Y %H:%M:%S")),"str":self.stats_str()})
def find_task_by_id(self,id):
for proj_id,stories in self.stories.items():
for story_id,story in stories.items():
for task in story.tasks:
if task.id==id:
return task
return None
def find_story_by_id(self,id):
for proj_id,stories in self.stories.items():
for story_id,story in stories.items():
if story.id==id:
return story
return None
def complete_task_from_menu(self,widget,task):
self.complete_task(task,False)
def complete_task(self,task,silent=False):
if not silent and self.ask(_("Are you sure you want to mark task '%s' as completed?") % task.description)!=gtk.RESPONSE_YES:
return
task_menu_item = task.menu_item
task_menu_parent = task.menu_item.parent
# extra checking
task = self.pivotal.get_task(task)
if task.complete!="false":
return False
# if not completed, try to complete now
rst = self.pivotal.complete_task(task)
# remove from menu
task_menu_parent.remove(task_menu_item)
story = self.stories[task.proj_id][task.story_id]
story.remove_task(task)
story.menu_item.set_label(story.__str__())
# remove submenu
if(len(story.tasks)<1):
story.menu_item.remove_submenu()
story.menu_item.connect("activate",self.update_story_state_from_menu,story)
# start story, if status different from started
state = States.get_state(story.state)
if not state.started:
self.pivotal.update_story_state(story,"started")
story.menu_item.set_label(story.__str__())
if rst and not silent:
self.show_info(_("Task '%s' marked as completed.") % task.description)
return rst
def blinking(self,blink):
self.statusIcon.set_blinking(blink)
def get_icon(self,icon):
for base in DATA_DIRS:
path = os.path.join(base,"images",icon)
if os.path.exists(path):
return path
return None
def right_click(self, widget, button, time, data = None):
if self.working:
return
self.blinking(False)
data.show_all()
data.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusIcon)
def set_tooltip(self,text):
self.statusIcon.set_tooltip(text)
def left_click(self,widget,data):
if self.working:
return
self.blinking(False)
data.show_all()
data.popup(None,None,None,3,0)
def check_now(self,widget,data=None):
self.check_thread()
def check_thread(self):
t = InitThread(self)
t.start()
def config_from_menu(self, widget, data = None):
ConfigWindow(self)
def about(self,widget,data=None):
self.about = gtk.AboutDialog()
self.about.set_name(__appname__)
self.about.set_program_name(__appname__)
self.about.set_version(__version__)
self.about.set_copyright(__copyright__)
self.about.set_license(__license__)
self.about.set_website(__website__)
self.about.set_website_label(__website__)
self.about.set_authors(["%s <%s>" % (__author__,__email__)])
self.about.set_logo(gtk.gdk.pixbuf_new_from_file(self.get_icon("gtracker.png")))
self.about.run()
self.about.destroy()
pass
def quit(self,widget,data=None):
gtk.main_quit()
def show_error(self,msg):
self.show_dialog(gtk.MESSAGE_ERROR,msg)
def show_info(self,msg):
self.show_dialog(gtk.MESSAGE_INFO,msg)
def show_dialog(self,msg_type,msg):
dialog = gtk.MessageDialog(None,gtk.DIALOG_MODAL,msg_type,gtk.BUTTONS_OK,msg)
dialog.run()
dialog.destroy()
def ask(self,msg):
dialog = gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_QUESTION,gtk.BUTTONS_YES_NO,msg)
rsp = dialog.run()
dialog.destroy()
return rsp
# dbus
def story_count(self):
count = 0
for proj_id in self.stories:
count += len(self.stories[proj_id])
return count
def get_story(self,pos):
all = []
for proj_id,stories in self.stories.items():
proj = self.projects[proj_id]["name"]
for story_id,story in stories.items():
tasks = story.tasks
if len(tasks)<1:
all.append([story.id,proj,story.name,story.points,"",""])
continue
for task in tasks:
all.append([story.id,proj,story.name,story.points,task.id,task.description])
if len(all)<pos:
return [None,None,None,None]
return all[pos]
if __name__ == "__main__":
gtracker = Gtracker()