Added docs for cpl.mailing

This commit is contained in:
Sven Heidemann 2021-05-17 17:54:57 +02:00
parent 6fc4cebbfe
commit fa5655414a
4 changed files with 74 additions and 51 deletions

View File

@ -2,16 +2,24 @@ import re
class EMail: class EMail:
r"""Represents an email
def __init__(self, header: list[str] = None, subject: str = None, body: str = None, transceiver: str = None, receiver: list[str] = None): Parameter
""" ---------
Represents an email header: list[:class:`str`]
:param header: Header of the E-Mail
:param subject: subject: :class:`str`
:param body: Subject of the E-Mail
:param transceiver: body: :class:`str`
:param receiver: Body of the E-Mail
transceiver: :class:`str`
Transceiver of the E-Mail
receiver: list[:class:`str`]
Receiver of the E-Mail
""" """
def __init__(self, header: list[str] = None, subject: str = None, body: str = None, transceiver: str = None,
receiver: list[str] = None):
self._header: list[str] = header self._header: list[str] = header
self._subject: str = subject self._subject: str = subject
@ -73,18 +81,26 @@ class EMail:
@staticmethod @staticmethod
def check_mail(address: str) -> bool: def check_mail(address: str) -> bool:
""" r"""Checks if an email is valid
Checks if an email is valid
:param address: Parameter
:return: ---------
address: :class:`str`
The address to check
Returns
-------
Result if E-Mail is valid or not
""" """
return bool(re.search('^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+$', address)) return bool(re.search('^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+$', address))
def add_header(self, header: str): def add_header(self, header: str):
""" r"""Adds header
Adds header
:param header: Parameter
:return: ---------
header: :class:`str`
The header of the E-Mail
""" """
if self._header is None: if self._header is None:
self._header = [] self._header = []
@ -92,10 +108,12 @@ class EMail:
self._header.append(header) self._header.append(header)
def add_receiver(self, receiver: str): def add_receiver(self, receiver: str):
""" r"""Adds receiver
Adds receiver
:param receiver: Parameter
:return: ---------
receiver: :class:`str`
The receiver of the E-Mail
""" """
if self._receiver is None: if self._receiver is None:
self._receiver = [] self._receiver = []
@ -106,9 +124,17 @@ class EMail:
raise Exception(f'Invalid email: {receiver}') raise Exception(f'Invalid email: {receiver}')
def get_content(self, transceiver: str): def get_content(self, transceiver: str):
r"""Returns the mail as string
Parameter
---------
transceiver: :class:`str`
The transceiver of the E-Mail
Returns
-------
E-Mail as string
""" """
Returns the mail as string return str(
:param transceiver: f'From: {transceiver}\r\nTo: {self.receiver}\r\n{self.header}\r\nSubject: {self.subject}\r\n{self.body}').encode(
:return: 'utf-8')
"""
return str(f'From: {transceiver}\r\nTo: {self.receiver}\r\n{self.header}\r\nSubject: {self.subject}\r\n{self.body}').encode('utf-8')

View File

@ -4,27 +4,24 @@ from cpl.mailing.email import EMail
class EMailClientABC(ABC): class EMailClientABC(ABC):
"""ABC of :class:`cpl.mailing.email_client_service.EMailClient`"""
@abstractmethod @abstractmethod
def __init__(self): def __init__(self):
"""
ABC to send emails
"""
ABC.__init__(self) ABC.__init__(self)
@abstractmethod @abstractmethod
def connect(self): def connect(self):
""" r"""Connects to server"""
Connects to server
:return:
"""
pass pass
@abstractmethod @abstractmethod
def send_mail(self, email: EMail): def send_mail(self, email: EMail):
""" r"""Sends email
Sends email
:param email: Parameter
:return: ---------
email: :class:`cpl.mailing.email.EMail`
Object of the E-Mail to send
""" """
pass pass

View File

@ -11,14 +11,19 @@ from cpl.utils.credential_manager import CredentialManager
class EMailClient(EMailClientABC): class EMailClient(EMailClientABC):
r"""Service to send emails
Parameter
---------
environment: :class:`cpl.environment.application_environment_abc.ApplicationEnvironmentABC`
Environment of the application
logger: :class:`cpl.logging.logger_abc.LoggerABC`
The logger to use
mail_settings: :class:`cpl.mailing.email_client_settings.EMailClientSettings`
Settings for mailing
"""
def __init__(self, environment: ApplicationEnvironmentABC, logger: LoggerABC, mail_settings: EMailClientSettings): def __init__(self, environment: ApplicationEnvironmentABC, logger: LoggerABC, mail_settings: EMailClientSettings):
"""
Service to send emails
:param environment:
:param logger:
:param mail_settings:
"""
EMailClientABC.__init__(self) EMailClientABC.__init__(self)
self._environment = environment self._environment = environment
@ -30,10 +35,7 @@ class EMailClient(EMailClientABC):
self.create() self.create()
def create(self): def create(self):
""" r"""Creates connection"""
Creates connection
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.create') self._logger.trace(__name__, f'Started {__name__}.create')
self.connect() self.connect()
self._logger.trace(__name__, f'Stopped {__name__}.create') self._logger.trace(__name__, f'Stopped {__name__}.create')
@ -54,10 +56,7 @@ class EMailClient(EMailClientABC):
self._logger.trace(__name__, f'Stopped {__name__}.connect') self._logger.trace(__name__, f'Stopped {__name__}.connect')
def login(self): def login(self):
""" r"""Login to server"""
Login to server
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.login') self._logger.trace(__name__, f'Started {__name__}.login')
try: try:
self._logger.debug(__name__, f'Try to login {self._mail_settings.user_name}@{self._mail_settings.host}:{self._mail_settings.port}') self._logger.debug(__name__, f'Try to login {self._mail_settings.user_name}@{self._mail_settings.host}:{self._mail_settings.port}')

View File

@ -6,6 +6,7 @@ from cpl.mailing.email_client_settings_name_enum import EMailClientSettingsNameE
class EMailClientSettings(ConfigurationModelABC): class EMailClientSettings(ConfigurationModelABC):
r"""Representation of mailing settings"""
def __init__(self): def __init__(self):
ConfigurationModelABC.__init__(self) ConfigurationModelABC.__init__(self)