@@ -13,6 +13,7 import smtplib | |||
|
13 | 13 | import ConfigParser |
|
14 | 14 | import StringIO |
|
15 | 15 | from threading import Thread |
|
16 | from multiprocessing import Process | |
|
16 | 17 | from email.mime.text import MIMEText |
|
17 | 18 | from email.mime.application import MIMEApplication |
|
18 | 19 | from email.mime.multipart import MIMEMultipart |
@@ -34,7 +35,7 def get_path(): | |||
|
34 | 35 | except: |
|
35 | 36 | log.error('I am sorry, but something is wrong... __file__ not found') |
|
36 | 37 | |
|
37 | def alarm(modes=[1], **kwargs): | |
|
38 | class Alarm(Process): | |
|
38 | 39 | ''' |
|
39 | 40 | modes: |
|
40 | 41 | 0 - All |
@@ -44,6 +45,12 def alarm(modes=[1], **kwargs): | |||
|
44 | 45 | 4 - Send to alarm system TODO |
|
45 | 46 | ''' |
|
46 | 47 | |
|
48 | def __init__(self, modes=[1], **kwargs): | |
|
49 | Process.__init__(self) | |
|
50 | self.modes = modes | |
|
51 | self.kwargs = kwargs | |
|
52 | ||
|
53 | @staticmethod | |
|
47 | 54 | def play_sound(): |
|
48 | 55 | sound = os.path.join(get_path(), 'alarm1.oga') |
|
49 | 56 | if os.path.exists(sound): |
@@ -53,45 +60,54 def alarm(modes=[1], **kwargs): | |||
|
53 | 60 | else: |
|
54 | 61 | log.warning('Unable to play alarm, sound file not found', 'ADMIN') |
|
55 | 62 | |
|
63 | @staticmethod | |
|
56 | 64 | def send_email(**kwargs): |
|
57 | 65 | notifier = SchainNotify() |
|
58 | 66 | notifier.notify(**kwargs) |
|
59 | 67 | |
|
68 | @staticmethod | |
|
60 | 69 | def show_popup(message='Error'): |
|
61 | 70 | popup(message) |
|
62 | 71 | |
|
72 | @staticmethod | |
|
63 | 73 | def send_alarm(): |
|
64 | 74 | pass |
|
65 | 75 | |
|
76 | @staticmethod | |
|
66 | 77 | def get_kwargs(kwargs, keys): |
|
67 | 78 | ret = {} |
|
68 | 79 | for key in keys: |
|
69 | 80 | ret[key] = kwargs[key] |
|
70 | 81 | return ret |
|
71 | 82 | |
|
72 | tasks = { | |
|
73 | 1 : send_email, | |
|
74 | 2 : play_sound, | |
|
75 | 3 : show_popup, | |
|
76 | 4 : send_alarm, | |
|
77 | } | |
|
78 | ||
|
79 | tasks_args = { | |
|
80 | 1: ['email', 'message', 'subject', 'subtitle', 'filename'], | |
|
81 | 2: [], | |
|
82 |
|
|
|
83 |
|
|
|
84 | } | |
|
85 | ||
|
86 | for mode in modes: | |
|
87 |
|
|
|
88 |
f |
|
|
89 | t = Thread(target=tasks[x], kwargs=get_kwargs(kwargs, tasks_args[x])) | |
|
83 | def run(self): | |
|
84 | tasks = { | |
|
85 | 1 : self.send_email, | |
|
86 | 2 : self.play_sound, | |
|
87 | 3 : self.show_popup, | |
|
88 | 4 : self.send_alarm, | |
|
89 | } | |
|
90 | ||
|
91 | tasks_args = { | |
|
92 | 1: ['email', 'message', 'subject', 'subtitle', 'filename'], | |
|
93 | 2: [], | |
|
94 | 3: ['message'], | |
|
95 | 4: [], | |
|
96 | } | |
|
97 | procs = [] | |
|
98 | for mode in self.modes: | |
|
99 | if 0 in self.modes: | |
|
100 | for x in tasks: | |
|
101 | t = Thread(target=tasks[x], kwargs=self.get_kwargs(self.kwargs, tasks_args[x])) | |
|
102 | t.start() | |
|
103 | procs.append(t) | |
|
104 | break | |
|
105 | else: | |
|
106 | t = Thread(target=tasks[mode], kwargs=self.get_kwargs(self.kwargs, tasks_args[mode])) | |
|
90 | 107 | t.start() |
|
91 | break | |
|
92 | else: | |
|
93 | t = Thread(target=tasks[mode], kwargs=get_kwargs(kwargs, tasks_args[x])) | |
|
94 | t.start() | |
|
108 | procs.append(t) | |
|
109 | for t in procs: | |
|
110 | t.join() | |
|
95 | 111 | |
|
96 | 112 | |
|
97 | 113 | class SchainConfigure(): |
@@ -255,13 +271,15 class SchainNotify: | |||
|
255 | 271 | self.__emailServer = confObj.getEmailServer() |
|
256 | 272 | |
|
257 | 273 | def sendEmail(self, email_from, email_to, subject='Error running ...', message="", subtitle="", filename="", html_format=True): |
|
258 | ||
|
274 | ||
|
259 | 275 | if not email_to: |
|
260 | 276 | return 0 |
|
261 | 277 | |
|
262 | 278 | if not self.__emailServer: |
|
263 | 279 | return 0 |
|
264 | 280 | |
|
281 | log.success('Sending email to {}...'.format(email_to), 'System') | |
|
282 | ||
|
265 | 283 | msg = MIMEMultipart() |
|
266 | 284 | msg['Subject'] = subject |
|
267 | 285 | msg['From'] = "(Python SChain API): " + email_from |
@@ -295,7 +313,7 class SchainNotify: | |||
|
295 | 313 | try: |
|
296 | 314 | smtp = smtplib.SMTP(self.__emailServer) |
|
297 | 315 | except: |
|
298 |
|
|
|
316 | log.error('Could not connect to server {}'.format(self.__emailServer), 'System') | |
|
299 | 317 | return 0 |
|
300 | 318 | |
|
301 | 319 | # Start the server: |
@@ -307,11 +325,13 class SchainNotify: | |||
|
307 | 325 | try: |
|
308 | 326 | smtp.sendmail(msg['From'], msg['To'], msg.as_string()) |
|
309 | 327 | except: |
|
310 |
|
|
|
328 | log.error('Could not send the email to {}'.format(msg['To']), 'System') | |
|
311 | 329 | smtp.quit() |
|
312 | 330 | return 0 |
|
313 | 331 | |
|
314 | 332 | smtp.quit() |
|
333 | ||
|
334 | log.success('Email sent ', 'System') | |
|
315 | 335 | |
|
316 | 336 | return 1 |
|
317 | 337 | |
@@ -343,8 +363,6 class SchainNotify: | |||
|
343 | 363 | if not sent: |
|
344 | 364 | return 0 |
|
345 | 365 | |
|
346 | print "***** Your system administrator has been notified *****" | |
|
347 | ||
|
348 | 366 | return 1 |
|
349 | 367 | |
|
350 | 368 | def notify(self, email, message, subject = "", subtitle="", filename=""): |
@@ -361,17 +379,16 class SchainNotify: | |||
|
361 | 379 | |
|
362 | 380 | if email is None: |
|
363 | 381 | email = self.__emailToAddress |
|
364 | ||
|
365 | log.success('Notifying to %s ...'.format(email), 'ADMIN') | |
|
366 | ||
|
367 | self.sendEmail(email_from=self.__emailFromAddress, | |
|
368 | email_to=email, | |
|
369 | subject=subject, | |
|
370 | message=message, | |
|
371 | subtitle=subtitle, | |
|
372 | filename=filename) | |
|
373 | 382 | |
|
374 | log.success('Email sent', 'ADMIN') | |
|
383 | self.sendEmail( | |
|
384 | email_from=self.__emailFromAddress, | |
|
385 | email_to=email, | |
|
386 | subject=subject, | |
|
387 | message=message, | |
|
388 | subtitle=subtitle, | |
|
389 | filename=filename | |
|
390 | ) | |
|
391 | ||
|
375 | 392 | |
|
376 | 393 | class SchainError(Exception): |
|
377 | 394 | """SchainError is an exception class that is thrown for all known errors using Schain Py lib. |
@@ -468,6 +485,10 class SchainError(Exception): | |||
|
468 | 485 | |
|
469 | 486 | return excStr |
|
470 | 487 | |
|
488 | class SchainWarning(Exception): | |
|
489 | pass | |
|
490 | ||
|
491 | ||
|
471 | 492 | if __name__ == '__main__': |
|
472 | 493 | |
|
473 | 494 | test = SchainNotify() |
@@ -15,7 +15,7 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring | |||
|
15 | 15 | from xml.dom import minidom |
|
16 | 16 | |
|
17 | 17 | import schainpy |
|
18 | import schainpy.admin | |
|
18 | from schainpy.admin import Alarm, SchainWarning | |
|
19 | 19 | from schainpy.model import * |
|
20 | 20 | from schainpy.utils import log |
|
21 | 21 | |
@@ -1180,10 +1180,13 class Project(Process): | |||
|
1180 | 1180 | |
|
1181 | 1181 | self.__connect(puObjIN, thisPUObj) |
|
1182 | 1182 | |
|
1183 | def __handleError(self, procUnitConfObj): | |
|
1183 | def __handleError(self, procUnitConfObj, modes=None): | |
|
1184 | 1184 | |
|
1185 | 1185 | import socket |
|
1186 | 1186 | |
|
1187 | if modes is None: | |
|
1188 | modes = self.alarm | |
|
1189 | ||
|
1187 | 1190 | err = traceback.format_exception(sys.exc_info()[0], |
|
1188 | 1191 | sys.exc_info()[1], |
|
1189 | 1192 | sys.exc_info()[2]) |
@@ -1215,8 +1218,8 class Project(Process): | |||
|
1215 | 1218 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime |
|
1216 | 1219 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime |
|
1217 | 1220 | |
|
1218 | schainpy.admin.alarm( | |
|
1219 |
modes=s |
|
|
1221 | a = Alarm( | |
|
1222 | modes=modes, | |
|
1220 | 1223 | email=self.email, |
|
1221 | 1224 | message=message, |
|
1222 | 1225 | subject=subject, |
@@ -1224,6 +1227,8 class Project(Process): | |||
|
1224 | 1227 | filename=self.filename |
|
1225 | 1228 | ) |
|
1226 | 1229 | |
|
1230 | a.start() | |
|
1231 | ||
|
1227 | 1232 | def isPaused(self): |
|
1228 | 1233 | return 0 |
|
1229 | 1234 | |
@@ -1292,7 +1297,8 class Project(Process): | |||
|
1292 | 1297 | try: |
|
1293 | 1298 | sts = procUnitConfObj.run() |
|
1294 | 1299 | is_ok = is_ok or sts |
|
1295 | j | |
|
1300 | except SchainWarning: | |
|
1301 | self.__handleError(procUnitConfObj, modes=[2, 3]) | |
|
1296 | 1302 | except KeyboardInterrupt: |
|
1297 | 1303 | is_ok = False |
|
1298 | 1304 | break |
@@ -37,13 +37,12 def figpause(interval): | |||
|
37 | 37 | return |
|
38 | 38 | |
|
39 | 39 | def popup(message): |
|
40 |
fig = plt.figure(figsize=(12, |
|
|
40 | fig = plt.figure(figsize=(12, 8), facecolor='r') | |
|
41 | 41 | fig.text(0.5, 0.5, message, ha='center', va='center', size='20', weight='heavy', color='w') |
|
42 | 42 | fig.show() |
|
43 | 43 | figpause(1000) |
|
44 | 44 | |
|
45 | 45 | |
|
46 | ||
|
47 | 46 | class PlotData(Operation, Process): |
|
48 | 47 | ''' |
|
49 | 48 | Base class for Schain plotting operations |
General Comments 0
You need to be logged in to leave comments.
Login now