-
Notifications
You must be signed in to change notification settings - Fork 0
/
watson-indicator.py
191 lines (171 loc) · 7.44 KB
/
watson-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
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
#!/usr/bin/env python3
import os
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
import time
from threading import Thread
from subprocess import getoutput, Popen, check_output
import json
from datetime import datetime
class Indicator():
def __init__(self):
self.app = "watson-indicator"
# icons
dirname = os.path.dirname(os.path.abspath(__file__))
self.icon_passive = os.path.join(dirname, "assets/project_passive.png")
self.icon_active = os.path.join(dirname, "assets/project_active.png")
self.indicator = AppIndicator3.Indicator.new(self.app, self.icon_passive, AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
# variables
self.old_project = ""
self.old_project_active = False # if a project is active
self.indicator.set_label(self.old_project, self.app)
self.last_project = "?" # last active project (important for restarting the last project)
self.issues = self.get_issues() # list of open issues
# the thread:
self.update = Thread(target=self.update)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
# get list of issues
def get_issues(self):
# get open firefox tab issues
## load data from firefox profile
data = json.loads(check_output(['/home/jschweig/misc/lz4json/lz4jsoncat', '/home/jschweig/.mozilla/firefox/o5ngtz92.default/sessionstore-backups/recovery.jsonlz4']))
## list of jira tickets in open firefox tabs
tickets = []
for tab_obj in data['windows'][0]['tabs']:
for entry in tab_obj['entries']:
# if tab is jira issue
if entry['url'].startswith('https://knime-com.atlassian.net/browse/') and not entry['title'].startswith('https://knime-com'):
ticket = entry['title'].replace(' - JIRA', '')
# AP-XXXX: 2 pull requests
if not ticket.startswith('['):
ticket = '[' + ticket
ticket = ticket.replace(': ', '] ', 1)
ticket = ticket.replace(':', '')
if len(ticket) > 30:
ticket = ticket[:30] + '...'
tickets.append(ticket)
tickets = list(dict.fromkeys(tickets))
return tickets
# start working on issue
# widget: reference to the widget where the function was triggered (Don't know how to avoid this)
# issue_code: issue code of the ticket to be started
def start_issue(self, widget, issue_code):
Popen(['watson', 'start', issue_code])
# get ticket submenu
# show: if there should be a separate show call for the menuitems
# returns None if no tickets or a GtkMenuItem with a submenu with the tickets
def get_tickets(self, show):
tickets = self.get_issues()
# return None if there are no tickets
if len(tickets) == 0:
return None
# create menu
item_start_ticket = Gtk.MenuItem('Start ticket')
menu_tickets = Gtk.Menu()
for ticket in tickets:
item = Gtk.MenuItem(ticket)
menu_tickets.append(item)
# get code of issue
i = ticket.find(']')
issue_code = ticket[1:i]
item.connect('activate', self.start_issue, issue_code)
if show:
item.show()
item_start_ticket.set_submenu(menu_tickets)
return item_start_ticket
# stops a project (if one is running) or restarts the last one
def stop_restart(self, source):
if self.old_project: # if project is running, stop it
Popen(["watson", "stop"])
else: # if no project is running, restart the last one
Popen(["watson", "restart"])
def test(self):
print('test')
# creates the top menu for the application indicator
def create_menu(self):
self.menu = Gtk.Menu()
# menu item "Stop/Restart project"
self.item_stop_restart = Gtk.MenuItem("Loading...")
self.item_stop_restart.connect('activate', self.stop_restart)
self.menu.append(self.item_stop_restart)
# menu item "Start ticket"
its = self.get_tickets(False)
if its is not None:
self.item_start_ticket = its
self.menu.append(self.item_start_ticket)
# menu item "Quit"
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.quit)
self.menu.append(item_quit)
self.menu.show_all()
return self.menu
def update_label(self, project):
# check if different project
if project != self.old_project:
self.old_project = project
# change label
GObject.idle_add(self.indicator.set_label, project, self.app, priority=GObject.PRIORITY_DEFAULT)
# update icon
def update_icon(self, project_active):
# check if different project_active
if project_active != self.old_project_active:
self.old_project_active = project_active
# change icon
GObject.idle_add(self.indicator.set_icon, self.icon_active if project_active else self.icon_passive)
# updates the label of the stop/restart menuitem
def update_stop_restart_label(self, project_active):
text = ""
if project_active: # stop project (time)
text = "Stop (" + getoutput("watson status -e").strip() + ")"
text = text.replace("minutes ago", "min")
else: # restart project
text = "Restart '" + self.last_project + "'"
# change item label in menu
GObject.idle_add(self.item_stop_restart.get_child().set_text, text)
# updates the start ticket menu
def update_tickets(self):
new_issues = self.get_issues()
if new_issues != self.issues:
self.issues = new_issues
# check if item_start_ticket has been defined
if hasattr(self, 'item_start_ticket'):
GObject.idle_add(self.menu.remove, self.item_start_ticket)
its = self.get_tickets(True)
if its is not None:
self.item_start_ticket = its
GObject.idle_add(self.menu.insert, self.item_start_ticket, 1)
self.item_start_ticket.show()
def update(self):
# updates the icon, indicator and menu item labels
while True:
time.sleep(1)
# "project" or "No project started"
project = getoutput("watson status -p").strip()
if "No project started" in project:
project = ""
project_active = False
else:
self.last_project = project
project = " " + self.last_project
project_active = True
# update indicator label
self.update_label(project)
# update indicator icon
self.update_icon(project_active)
# update time and project in stop/restart label
self.update_stop_restart_label(project_active)
# update tickets in 'Start ticket'
self.update_tickets()
def quit(self, source):
Gtk.main_quit()
Indicator()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()