/
rt_notify.py
executable file
·251 lines (204 loc) · 8.61 KB
/
rt_notify.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
#!/usr/bin/env python2
# -*- coding: utf8 -*-
from ConfigParser import SafeConfigParser
import os
import logging
import webbrowser
from bs4 import BeautifulSoup
import keyring
import requests.exceptions
from pync import Notifier
import rumps
import xdg.BaseDirectory
import requests
import expiringdict
# noinspection PyUnresolvedReferences
from AppKit import NSSecureTextField
# noinspection PyUnresolvedReferences
from Foundation import NSMakeRect
__author__ = 'peter'
def setup_logging():
"""
Setup logging
"""
formatter = logging.Formatter('%(asctime)s (%(process)d:%(name)s) [ %(levelname)7s ] : %(message)s')
rootlogger = logging.getLogger()
consolehandler = logging.StreamHandler()
consolehandler.setFormatter(formatter)
rootlogger.addHandler(consolehandler)
rootlogger.setLevel(logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)
class RTNotifier(rumps.App):
CONFIG_NAME = 'rtnotifier.ini'
def __init__(self):
super(RTNotifier, self).__init__("RT Notifier", icon='57365.png')
self.config = None
# Set user/password/url, either by asking or by getting from the config
xdg_base = xdg.BaseDirectory.load_first_config(self.__class__.__name__)
config_path = os.path.join(xdg_base, RTNotifier.CONFIG_NAME) if xdg_base else ''
self.config = SafeConfigParser()
if os.path.isfile(config_path):
# Load config file
with open(config_path) as f:
self.config.readfp(f)
else:
self.config.add_section('main')
if self.set_user_pass(None) and self.set_url(None) and self.set_renotify_time(None):
self.save_config()
else:
rumps.quit_application()
renotify_time = self.config.getint('main', 'renotify_time') * 60
self.tickets = expiringdict.ExpiringDict(max_len=100, max_age_seconds=renotify_time)
self.debug = False
rumps.debug_mode(self.debug)
def set_user_pass(self, _):
w = self.ask('Please enter your username')
old_user = self.config.get('main', 'user')
if w.clicked and w.text:
user = w.text
self.config.set('main', 'user', w.text)
w = self.ask('Please enter your password', password=True)
if w.clicked and w.text:
keyring.set_password(self.__class__.__name__, user, w.text)
else:
self.config.set('main', 'user', old_user)
return False
else:
return False
self.save_config()
return True
def set_url(self, _):
w = self.ask('Please enter the RT url')
if w.clicked and w.text:
self.config.set('main', 'url', w.text)
return False
self.save_config()
return True
def set_renotify_time(self, sender):
w = self.ask('Please enter a new renotify time, in minutes')
if w.clicked and w.text:
try:
int(w.text)
self.config.set('main', 'renotify_time', w.text)
sender.title = 'Change renotify time (now {} minutes)'.format(w.text)
old = self.tickets
# Create a new expiring dict with the new time
self.tickets = expiringdict.ExpiringDict(max_len=100, max_age_seconds=int(w.text) * 60)
# Copy all old keys
for k, v in old.items():
self.tickets[k] = v
except ValueError:
rumps.alert('The given renotify time is not a valid integer!')
else:
return False
self.save_config()
return True
@rumps.timer(600)
def run_monitor(self, _):
try:
url = self.config.get('main', 'url')
user = self.config.get('main', 'user')
password = keyring.get_password(self.__class__.__name__, user)
r = requests.get(url, auth=(user, password), timeout=3)
if r.status_code == 401:
# Forbidden, user/pass combo is probably wrong. Reask.
rumps.alert('Could not login, please reenter username and password!')
self.set_user_pass(None)
user = self.config.get('main', 'user')
password = keyring.get_password(self.__class__.__name__, user)
r = requests.get(url, auth=(user, password), timeout=3)
soup = BeautifulSoup(r.text, "lxml")
tables = soup.find_all("table", {"class": "ticket-list collection-as-table"})
self.menu['Recent tickets'] = []
owned = []
unowned = None
if len(tables):
owned = [tables[0]]
if len(tables) > 1:
unowned = tables[1]
if len(tables) > 2:
owned += [tables[2]]
for t in owned:
self.process_table(self.tickets, t, url, user)
if unowned:
self.process_table(self.tickets, unowned, url, user, filter_owner=False)
except requests.exceptions.RequestException:
logging.warning("Could not connect to Request Tracker, trying again soon")
pass
def process_table(self, tickets, table, url, user, filter_owner=True):
logging.debug('Processing table')
last_update_idx, subject_idx = self.find_indexes(table)
if not last_update_idx:
return
logging.debug('Found valid entries in table')
for line in table.find_all("tr"):
tds = line.find_all("td")
if len(tds) > last_update_idx:
ticketnr = int(tds[0].a.contents[0])
subject = tds[subject_idx].a.contents[0]
last_updated_by = tds[last_update_idx].contents[0]
if filter_owner and last_updated_by == user:
continue
if ticketnr not in tickets:
self.notify(url, "Ticket {} is new: '{}'", ticketnr, subject)
tickets[ticketnr] = last_updated_by
elif filter_owner and last_updated_by != tickets[ticketnr]:
self.notify(url, "Ticket {} is updated: '{}'", ticketnr, subject)
elif self.debug:
self.notify(url, "Ticket {} is triggered by debug: '{}'", ticketnr, subject)
def get_config_path(self):
xdg_base = xdg.BaseDirectory.load_first_config(self.__class__.__name__)
return os.path.join(xdg_base, RTNotifier.CONFIG_NAME) if xdg_base else ''
def save_config(self):
config_path = self.get_config_path()
with open(config_path, 'w') as f:
self.config.write(f)
def update_renotify_menu_item(self, text):
for k, v in self.menu.items():
if v.title.startswith('Change renotify time'):
v.title = 'Change renotify time (now {} minutes)'.format(text)
return
def notify(self, url, msg, ticketnr, subject):
msg = msg.format(ticketnr, subject)
logging.info(msg)
url = url + '/Ticket/Display.html?id={}'.format(ticketnr)
self.menu['Recent tickets'].add(rumps.MenuItem(msg, callback=lambda x: webbrowser.open(url)))
Notifier.notify(msg, title="Request Tracker", open=url)
@staticmethod
def find_indexes(table):
last_update_idx = 0
subject_idx = 0
for i, th in enumerate(table.tr.find_all("th")):
if 'Last Updated By' in th.contents:
last_update_idx = i
if 'Subject' in th.contents:
subject_idx = i
return last_update_idx, subject_idx
# noinspection PyProtectedMember
@staticmethod
def ask(message, password=False):
dimensions = (320, 20)
w = rumps.Window(title=message, dimensions=dimensions, cancel=True)
if password:
w._textfield = NSSecureTextField.alloc().initWithFrame_(NSMakeRect(0, 0, *dimensions))
w._textfield.setSelectable_(True)
w._alert.setAccessoryView_(w._textfield)
return w.run()
def main():
setup_logging()
logging.info('Starting RT monitor')
app = RTNotifier()
app.menu = [
{'Recent tickets': [
]},
None,
rumps.MenuItem('Force recheck', callback=app.run_monitor),
rumps.MenuItem('Change user and password', callback=app.set_user_pass),
rumps.MenuItem('Change RequestTracker URL', callback=app.set_url),
rumps.MenuItem('Change renotify time (now {} minutes)'.format(app.config.getint('main', 'renotify_time')),
callback=app.set_renotify_time),
None
]
app.run()
if __name__ == '__main__':
main()