-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator.py
executable file
·104 lines (81 loc) · 2.89 KB
/
indicator.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
import datetime
from os.path import abspath
import sys
import signal
import gtk
import appindicator as ind
import productivity as prod
ICONS = {
prod.WORKING: abspath('working.svg'),
prod.PLAYING: abspath('playing.svg'),
prod.CRUNCH_MODE: abspath('crunch-mode.svg'),
prod.RED_ALERT: abspath('red-alert.svg'),
}
class ProductivityIndicator(object):
'''
Implementation based on AppIndicator tutorial from ConjureCode:
http://conjurecode.com/create-indicator-applet-for-ubuntu-unity-with-python/
'''
def __init__(self):
self.ind = ind.Indicator('productivity', 'media-seek-forward-symbolic',
ind.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(ind.STATUS_ACTIVE)
self.menu = self.build_menu()
self.ind.set_menu(self.menu)
self.prod = prod.Productivity()
def build_menu(self):
menu = gtk.Menu()
self.clockin_item = self.add_item(menu, prod.WORKING.label, self.clockin)
self.clockout_item = self.add_item(menu, prod.PLAYING.label, self.clockout)
self.add_item(menu, 'Quit', self.quit)
return menu
def add_item(self, menu, label, func):
item = gtk.MenuItem(label)
item.connect('activate', func)
item.show()
menu.append(item)
return item
def main(self):
gtk.timeout_add(1000, self.update)
gtk.main()
def clockin(self, _):
self.prod.clockin()
self.update()
def clockout(self, _):
self.prod.clockout()
self.update()
def quit(self, _):
self.prod.quit()
sys.exit(0)
def update(self):
uptime, percentile = self.prod.uptime_and_percentile()
uptime_str = format_timedelta(uptime)
percentile_str = format_percentile(percentile)
self.ind.set_label('%s [%s]' % (uptime_str, percentile_str))
self.ind.set_icon(ICONS[self.prod.status()])
self.clockin_item.set_label(prod.CRUNCH_MODE.label
if self.prod.late else
prod.WORKING.label)
self.clockout_item.set_label(prod.RED_ALERT.label
if self.prod.late else
prod.PLAYING.label)
self.clockin_item.set_sensitive(not self.prod.working)
self.clockout_item.set_sensitive(self.prod.working)
return True
def format_timedelta(td):
if td < datetime.timedelta(0):
td_str = '-' + str(-td)
else:
td_str = str(td)
point = td_str.find('.')
if point == -1:
return td_str
else:
return td_str[:point]
def format_percentile(p):
return '%d%%' % int(p)
if __name__ == '__main__':
pi = ProductivityIndicator()
signal.signal(signal.SIGTERM, lambda num, frame: pi.quit(None))
signal.signal(signal.SIGINT, lambda num, frame: pi.quit(None))
pi.main()