Add Report
This commit is contained in:
parent
b6465f0ce8
commit
49ed5d7a82
1 changed files with 58 additions and 0 deletions
58
Report.py
Normal file
58
Report.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# coding: utf8
|
||||||
|
|
||||||
|
""" Report """
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
class Report(object): # pylint: disable=useless-object-inheritance
|
||||||
|
""" Logging report """
|
||||||
|
|
||||||
|
content = []
|
||||||
|
handler = None
|
||||||
|
formatter = None
|
||||||
|
subject = None
|
||||||
|
rcpt_to = None
|
||||||
|
email_client = None
|
||||||
|
|
||||||
|
def __init__(self, loglevel=logging.WARNING, logformat='%(asctime)s - %(levelname)s - %(message)s',
|
||||||
|
subject=None, rcpt_to=None, email_client=None):
|
||||||
|
self.handler = logging.StreamHandler(self)
|
||||||
|
self.handler.setLevel(loglevel)
|
||||||
|
self.formatter = logging.Formatter(logformat)
|
||||||
|
self.handler.setFormatter(self.formatter)
|
||||||
|
|
||||||
|
self.subject = subject
|
||||||
|
self.rcpt_to = rcpt_to
|
||||||
|
self.email_client = email_client
|
||||||
|
|
||||||
|
def get_handler(self):
|
||||||
|
""" Retreive logging handler """
|
||||||
|
return self.handler
|
||||||
|
|
||||||
|
def write(self, msg):
|
||||||
|
""" Write a message """
|
||||||
|
self.content.append(msg)
|
||||||
|
|
||||||
|
def get_content(self):
|
||||||
|
""" Read the report content """
|
||||||
|
return "".join(self.content)
|
||||||
|
|
||||||
|
def send(self, subject=None, rcpt_to=None, email_client=None, just_try=False):
|
||||||
|
""" Send report using an EmailClient """
|
||||||
|
if not self.rcpt_to and not rcpt_to:
|
||||||
|
logging.debug('No report recipient, do not send report')
|
||||||
|
return True
|
||||||
|
assert self.subject or subject, "You must provide report subject using Report.__init__ or Report.send"
|
||||||
|
assert self.email_client or email_client, "You must provide email client using Report.__init__ or Report.send"
|
||||||
|
content = self.get_content()
|
||||||
|
if not content:
|
||||||
|
logging.debug('Report is empty, do not send it')
|
||||||
|
return True
|
||||||
|
msg = email_client.forge_message(
|
||||||
|
self.rcpt_to or rcpt_to,
|
||||||
|
subject=self.subject or subject,
|
||||||
|
text_body=content
|
||||||
|
)
|
||||||
|
return email_client.send(self.rcpt_to or rcpt_to, msg=msg, just_try=just_try)
|
Loading…
Reference in a new issue