|
9 | 9 | Code Repository: https://github.com/rasbt/pyprind |
10 | 10 | PyPI: https://pypi.python.org/pypi/PyPrind |
11 | 11 | """ |
12 | | - |
13 | | - |
| 12 | +import smtplib |
| 13 | +import socket |
14 | 14 | import time |
15 | 15 | import sys |
16 | 16 | import os |
17 | 17 | from io import UnsupportedOperation |
| 18 | +import ConfigParser |
| 19 | +from email.mime.text import MIMEText |
| 20 | +from email_notification import AESCipher |
| 21 | + |
| 22 | +try: |
| 23 | + from StringIO import StringIO |
| 24 | +except ImportError: |
| 25 | + from io import StringIO |
18 | 26 |
|
19 | 27 | try: |
20 | 28 | import psutil |
|
25 | 33 |
|
26 | 34 | class Prog(): |
27 | 35 | def __init__(self, iterations, track_time, stream, title, |
28 | | - monitor, update_interval=None): |
| 36 | + monitor, update_interval=None, email=False): |
29 | 37 | """ Initializes tracking object. """ |
30 | 38 | self.cnt = 0 |
31 | 39 | self.title = title |
@@ -54,6 +62,49 @@ def __init__(self, iterations, track_time, stream, title, |
54 | 62 | self.process = psutil.Process() |
55 | 63 | if self.track: |
56 | 64 | self.eta = 1 |
| 65 | + self.config = self.load_email_config() if email else False |
| 66 | + |
| 67 | + def load_email_config(self): |
| 68 | + dir_path = os.path.dirname(os.path.abspath(__file__)) |
| 69 | + file_path = os.path.join(dir_path, 'email_settings.ini.enc') |
| 70 | + if not os.path.exists(file_path): |
| 71 | + print('The email config cannot be found, please call' |
| 72 | + ' pyprind.setup_email function') |
| 73 | + return False |
| 74 | + return self.parse_email_config() |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def parse_email_config(): |
| 78 | + buf = StringIO.StringIO() |
| 79 | + cipher = AESCipher() |
| 80 | + raw_data = cipher.decrypt() |
| 81 | + buf.write(raw_data) |
| 82 | + buf.seek(0, os.SEEK_SET) |
| 83 | + config = ConfigParser.ConfigParser() |
| 84 | + config.readfp(buf) |
| 85 | + return config |
| 86 | + |
| 87 | + def send_email(self, message): |
| 88 | + email_address = self.config.get('Email', 'username') |
| 89 | + msg = MIMEText(message, 'plain') |
| 90 | + msg['From'] = email_address |
| 91 | + msg['To'] = email_address |
| 92 | + msg['Subject'] = 'Your task has finished' |
| 93 | + password = self.config.get('Email', 'password') |
| 94 | + self.config.get('Email', 'smtp_port') |
| 95 | + s = smtplib.SMTP_SSL() |
| 96 | + s.connect(self.config.get('Email', 'smtp_server'), |
| 97 | + self.config.get('Email', 'smtp_port')) |
| 98 | + try: |
| 99 | + s.login(email_address, password) |
| 100 | + except smtplib.SMTPAuthenticationError as e: |
| 101 | + print('Error occurred while sending email: %s' % e) |
| 102 | + return False |
| 103 | + try: |
| 104 | + s.sendmail(email_address, [email_address], msg.as_string()) |
| 105 | + s.quit() |
| 106 | + except socket.error as e: |
| 107 | + print('Error occurred while sending email: %s' % e) |
57 | 108 |
|
58 | 109 | def update(self, iterations=1, item_id=None, force_flush=False): |
59 | 110 | """ |
@@ -145,8 +196,9 @@ def _finish(self): |
145 | 196 | self.last_progress -= 1 # to force a refreshed _print() |
146 | 197 | self._print() |
147 | 198 | if self.track: |
148 | | - self._stream_out('\nTotal time elapsed: ' + |
149 | | - self._get_time(self.total_time)) |
| 199 | + message = '\nTotal time elapsed: ' + \ |
| 200 | + self._get_time(self.total_time) |
| 201 | + self._stream_out(message) |
150 | 202 | self._stream_out('\n') |
151 | 203 | self.active = False |
152 | 204 |
|
@@ -191,8 +243,12 @@ def __repr__(self): |
191 | 243 |
|
192 | 244 | cpu_mem_info = ' CPU %: {:.2f}\n'\ |
193 | 245 | ' Memory %: {:.2f}'.format(cpu_total, mem_total) |
194 | | - |
195 | | - return time_info + '\n' + cpu_mem_info |
| 246 | + time_elapsed = '\nTotal time elapsed: ' + \ |
| 247 | + self._get_time(self.total_time) |
| 248 | + body_message = time_info + '\n' + cpu_mem_info |
| 249 | + if self.config: |
| 250 | + self.send_email("{}\n{}".format(time_elapsed, body_message)) |
| 251 | + return body_message |
196 | 252 | else: |
197 | 253 | return time_info |
198 | 254 |
|
|
0 commit comments