bench_executor.notifier
This module holds the Notifier class which is responsible for notifying the user remotely when the execution is finished, interrupted or failed.
1#!/usr/bin/env python3 2 3""" 4This module holds the Notifier class which is responsible for notifying the 5user remotely when the execution is finished, interrupted or failed. 6""" 7 8import smtplib 9from email.mime.text import MIMEText 10from bench_executor.logger import Logger 11 12 13class Notifier(): 14 """Notify user via e-mail when execution finished, or failed.""" 15 16 def __init__(self, server: str, port: int, username: str, password: str, 17 sender: str, receiver: str, directory: str, verbose: bool): 18 """Creates an instance of the Notifier class. 19 20 Parameters 21 ---------- 22 server : str 23 The SMTP server domain name. 24 port : int 25 The SMTP server port. 26 username : str 27 The SMTP server username for sending an e-mail. 28 password : str 29 The SMTP server password for sending an e-mail. 30 sender : str 31 The e-mailaddress of the sender. 32 receiver : str 33 The e-mailaddress of the receiver. 34 directory : str 35 The path to the directory where the logs must be stored. 36 verbose : bool 37 Enable verbose logs. 38 """ 39 self._server = server 40 self._port = port 41 self._username = username 42 self._password = password 43 self._sender = sender 44 self._receiver = receiver 45 self._logger = Logger(__name__, directory, verbose) 46 47 def send(self, title: str, message: str) -> bool: 48 """Send a notification via e-mail with a title and message. 49 50 Parameters 51 ---------- 52 title : str 53 E-mail's subject 54 message : str 55 E-mail's body 56 57 Returns 58 ------- 59 success : bool 60 Whether sending the notification was successfull or not. 61 """ 62 if self._server is not None and self._port is not None \ 63 and self._username is not None and self._password is not None \ 64 and self._sender is not None and self._password is not None: 65 msg = MIMEText(message) 66 msg['Subject'] = title 67 msg['From'] = self._sender 68 msg['To'] = self._receiver 69 70 with smtplib.SMTP(self._server, self._port) as server: 71 server.starttls() 72 server.login(self._username, self._password) 73 server.sendmail(self._sender, [self._receiver], 74 msg.as_string()) 75 self._logger.info(f'Notification send to {self._receiver}') 76 else: 77 self._logger.debug('Parameters are missing to send an e-mail ' 78 'notification') 79 return False 80 81 return True
class
Notifier:
14class Notifier(): 15 """Notify user via e-mail when execution finished, or failed.""" 16 17 def __init__(self, server: str, port: int, username: str, password: str, 18 sender: str, receiver: str, directory: str, verbose: bool): 19 """Creates an instance of the Notifier class. 20 21 Parameters 22 ---------- 23 server : str 24 The SMTP server domain name. 25 port : int 26 The SMTP server port. 27 username : str 28 The SMTP server username for sending an e-mail. 29 password : str 30 The SMTP server password for sending an e-mail. 31 sender : str 32 The e-mailaddress of the sender. 33 receiver : str 34 The e-mailaddress of the receiver. 35 directory : str 36 The path to the directory where the logs must be stored. 37 verbose : bool 38 Enable verbose logs. 39 """ 40 self._server = server 41 self._port = port 42 self._username = username 43 self._password = password 44 self._sender = sender 45 self._receiver = receiver 46 self._logger = Logger(__name__, directory, verbose) 47 48 def send(self, title: str, message: str) -> bool: 49 """Send a notification via e-mail with a title and message. 50 51 Parameters 52 ---------- 53 title : str 54 E-mail's subject 55 message : str 56 E-mail's body 57 58 Returns 59 ------- 60 success : bool 61 Whether sending the notification was successfull or not. 62 """ 63 if self._server is not None and self._port is not None \ 64 and self._username is not None and self._password is not None \ 65 and self._sender is not None and self._password is not None: 66 msg = MIMEText(message) 67 msg['Subject'] = title 68 msg['From'] = self._sender 69 msg['To'] = self._receiver 70 71 with smtplib.SMTP(self._server, self._port) as server: 72 server.starttls() 73 server.login(self._username, self._password) 74 server.sendmail(self._sender, [self._receiver], 75 msg.as_string()) 76 self._logger.info(f'Notification send to {self._receiver}') 77 else: 78 self._logger.debug('Parameters are missing to send an e-mail ' 79 'notification') 80 return False 81 82 return True
Notify user via e-mail when execution finished, or failed.
Notifier( server: str, port: int, username: str, password: str, sender: str, receiver: str, directory: str, verbose: bool)
17 def __init__(self, server: str, port: int, username: str, password: str, 18 sender: str, receiver: str, directory: str, verbose: bool): 19 """Creates an instance of the Notifier class. 20 21 Parameters 22 ---------- 23 server : str 24 The SMTP server domain name. 25 port : int 26 The SMTP server port. 27 username : str 28 The SMTP server username for sending an e-mail. 29 password : str 30 The SMTP server password for sending an e-mail. 31 sender : str 32 The e-mailaddress of the sender. 33 receiver : str 34 The e-mailaddress of the receiver. 35 directory : str 36 The path to the directory where the logs must be stored. 37 verbose : bool 38 Enable verbose logs. 39 """ 40 self._server = server 41 self._port = port 42 self._username = username 43 self._password = password 44 self._sender = sender 45 self._receiver = receiver 46 self._logger = Logger(__name__, directory, verbose)
Creates an instance of the Notifier class.
Parameters
- server (str): The SMTP server domain name.
- port (int): The SMTP server port.
- username (str): The SMTP server username for sending an e-mail.
- password (str): The SMTP server password for sending an e-mail.
- sender (str): The e-mailaddress of the sender.
- receiver (str): The e-mailaddress of the receiver.
- directory (str): The path to the directory where the logs must be stored.
- verbose (bool): Enable verbose logs.
def
send(self, title: str, message: str) -> bool:
48 def send(self, title: str, message: str) -> bool: 49 """Send a notification via e-mail with a title and message. 50 51 Parameters 52 ---------- 53 title : str 54 E-mail's subject 55 message : str 56 E-mail's body 57 58 Returns 59 ------- 60 success : bool 61 Whether sending the notification was successfull or not. 62 """ 63 if self._server is not None and self._port is not None \ 64 and self._username is not None and self._password is not None \ 65 and self._sender is not None and self._password is not None: 66 msg = MIMEText(message) 67 msg['Subject'] = title 68 msg['From'] = self._sender 69 msg['To'] = self._receiver 70 71 with smtplib.SMTP(self._server, self._port) as server: 72 server.starttls() 73 server.login(self._username, self._password) 74 server.sendmail(self._sender, [self._receiver], 75 msg.as_string()) 76 self._logger.info(f'Notification send to {self._receiver}') 77 else: 78 self._logger.debug('Parameters are missing to send an e-mail ' 79 'notification') 80 return False 81 82 return True
Send a notification via e-mail with a title and message.
Parameters
- title (str): E-mail's subject
- message (str): E-mail's body
Returns
- success (bool): Whether sending the notification was successfull or not.