forked from amperka/cube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodes.py
295 lines (234 loc) · 8.94 KB
/
modes.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
# -*- coding: utf-8; -*-
import imaplib
import json
import logging
import oerplib
import socket
import threading
import urllib2
import wx
from oerplib.error import RPCError
from time import sleep
from wx.lib.newevent import NewEvent
StatusChangedEvent, EVT_STATUS_CHANGED = NewEvent()
class Mode(object):
def __init__(self):
self._evt_transport = wx.Frame(wx.GetApp().GetTopWindow())
def bind(self, evt, handler):
self._evt_transport.Bind(evt, handler)
def unbind(self, evt, handler):
self._evt_transport.Unbind(evt, handler=handler)
def _post_event(self, event):
if isinstance(self._evt_transport, wx.EvtHandler):
wx.PostEvent(self._evt_transport, event)
class ImapMode(Mode):
def __init__(self, device, interval=20):
super(ImapMode, self).__init__()
self.device = device
self.interval = interval
self.status = u''
self._prev_count = 0
self._host = None
self._port = None
self._login = None
self._password = None
def set_host_port(self, host, port):
self._host = host
self._port = port
def set_credentials(self, login, password):
self._login = login
self._password = password
def loop(self):
self._stopped = False
while not self._stopped:
self.set_status(u"Проверка почты…")
count = 0
try:
count = self._fetch_unread_count()
message = u"Писем: {}".format(count)
except imaplib.IMAP4.error as e:
message = u"Неверные логин/пароль"
except socket.error:
message = u"Нет соединения с сервером"
self.set_status(message)
if self._stopped:
break
if count > self._prev_count:
self.device.blink()
if count:
self.device.go_green()
else:
self.device.go_red()
self._prev_count = count
countdown = self.interval
while countdown > 0 and not self._stopped:
sleep(0.1)
countdown -= 0.1
self.set_status(u"{} ~ {:.0f}".format(message, countdown))
def stop(self):
self._stopped = True
def set_status(self, status):
self.status = status
self._post_event(StatusChangedEvent(status=status))
def _fetch_unread_count(self):
connection = imaplib.IMAP4_SSL(self._host, self._port)
connection.login(self._login, self._password)
connection.select()
resp = connection.search(None, 'UnSeen')
return len(resp[1][0].split())
class GMailMode(ImapMode):
def __init__(self, device, interval=20):
super(GMailMode, self).__init__(device, interval)
self.set_host_port('imap.gmail.com', '993')
class MailruMode(ImapMode):
def __init__(self, device, interval=20):
super(MailruMode, self).__init__(device, interval)
self.set_host_port('imap.mail.ru', '993')
class SlackMode(ImapMode):
def __init__(self, device, interval=10):
self.UNREADS = []
self._init_logging()
return super(SlackMode, self).__init__(device, interval)
def _init_logging(self):
logger = logging.getLogger(SlackMode.__name__)
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - '
'%(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
self._logger = logger
def decorate(func):
base_url = 'https://slack.com/api/'
def wrapper(self, uri_part):
uri_part = func(self, uri_part)
token = self._login
return '%s%s?token=%s' % (base_url, uri_part, token)
return wrapper
@decorate
def get_url(self, uri_part):
return uri_part
def get_unreads_count(self, item_id, uri_part):
history_url = self.get_url('%s.history' % uri_part)
url = '%s&channel=%s&unreads=True' % (history_url, item_id)
try:
history = json.loads(urllib2.urlopen(url).read())
self.UNREADS.append(history['unread_count_display'])
except urllib2.URLError, e:
self.error(e)
def get_unreads(self, uri, key):
url = self.get_url(uri)
resp = json.loads(urllib2.urlopen(url).read())
if 'error' in resp:
self.error(str(resp['error']))
items = resp[key]
ids = map(lambda x: x['id'], items)
threads = []
for item_id in ids:
uri_part = uri.split('.')[0]
t = threading.Thread(target=self.get_unreads_count,
args=(item_id, uri_part))
threads.append(t)
map(lambda x: x.start(), threads)
map(lambda x: x.join(), threads)
def _fetch_threads(self):
self.get_unreads('channels.list', 'channels')
self.get_unreads('im.list', 'ims')
def error(self, message):
self._logger.error('Ошибка: %s', message)
self.set_status(message)
self.stop()
raise LoginError
def _fetch_unread_count(self):
self.UNREADS = []
if not self._login:
message = 'Отсутствует токен.'
self.error(message)
self._fetch_threads()
res = sum(self.UNREADS)
if res:
self._logger.info('Нових писем: %s', res)
return res
class OdooMode(ImapMode):
"""
Class for connection to the Odoo8
"""
def __init__(self, device, interval=90):
self._database = None
self._protocol = "xmlrpc+ssl"
self._prev_count_of_tasks = 0
self._prev_count_of_issues = 0
self._init_logging()
super(OdooMode, self).__init__(device, interval)
def _init_logging(self):
logger = logging.getLogger(OdooMode.__name__)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - - %(levelname)s: '
'%(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
self._logger = logger
def set_host_port_database_protocol(self, host, port, database, protocol="xmlrpc+ssl"):
"""Set host, port and database for connection"""
self._database = database
self._protocol = protocol
super(OdooMode, self).set_host_port(host, port)
def loop(self):
self._stopped = False
while not self._stopped:
self.set_status(u"Проверка почты…")
tasks, issues = 0, 0
try:
tasks, issues = self._fetch_unread_count()
message = u'Задачи: {0} | Вопросы: {1}'.format(
tasks,
issues,
)
except RPCError as e:
self._logger.error('Got a problem in function "loop"!', exc_info=True)
message = u"Ошибка соединения с базой данных"
self.set_status(message)
if self._stopped:
break
if tasks > self._prev_count_of_tasks:
self.device.blink_green()
if issues > self._prev_count_of_issues:
self.device.blink_red()
self.device.go_orange()
self._prev_count_of_tasks = tasks
self._prev_count_of_issues = issues
countdown = self.interval
while countdown > 0 and not self._stopped:
sleep(0.1)
countdown -= 0.1
self.set_status(u"{} ~ {:.0f}".format(message, countdown))
def _fetch_unread_count(self):
"""Connect to the database and count unseen messages"""
connection = oerplib.OERP(self._host, protocol=self._protocol, port=self._port)
self._logger.info('Success connection')
user = connection.login(self._login, self._password, self._database)
self._logger.info('Success logging')
try:
tasks = connection.search(
'project.task',
[('message_unread', '=', True)]
)
except RPCError as e:
self._logger.error('Got a problem with counting of tasks!', exc_info=True)
tasks = []
try:
issues = connection.search(
'project.issue',
[('message_unread', '=', True)]
)
except RPCError as e:
self._logger.error('Got a problem with counting of issues!', exc_info=True)
issues = []
unseen = (len(tasks), len(issues),)
return unseen
def stop(self):
self.device.disconnect()
super(OdooMode, self).stop()
class LoginError(Exception):
pass