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