def _log(self, string, sender=None, date_color="cyan", sender_color="white", color="white", on_color=None, p=False): """Internal logger function, should only be called be SimplerLogger-class methods. :param string: String to print :param sender: Current function the logger logs from :param date_color: Specifying color of the date-stamp :param sender_color: Specifying color of the sender :param color: Specifying color of the string :param on_color: Specifyinh on-color of the message :param p: If True, the logger prints the log-message """ if self.ll[self.level] > self.ll[str(gf().f_back.f_code.co_name)]: return now = datetime.now() date = str(now)[:19] message = str(string) if sender is None: sender = gf().f_back.f_back.f_code.co_name log_string = "[{d}][{s}] {m}".format(d=colored(date, date_color), m=colored(message, color, on_color), s=colored(sender, sender_color)) if p: print(log_string) if self.logfile is not None: self.logfile.write(log_string + "\n") self.logfile.flush()
def send(self): self.d.write(" Line {} - Entering send(..)\n".format(gf().f_lineno), "a") try: outlook = Dispatch("Outlook.Application") new_email = outlook.CreateItem(0) if self._cfg['SendOnBehalfOf']: new_email.SentOnBehalfOfName = self._cfg['SendOnBehalfOfName'] new_email.To = self._to new_email.CC = self._cc new_email.Subject = self._subj new_email.HTMLBody = self.report.__str__() # Added this on Apr 8th to monitor what type of email # has been sent, if any. ... Meh self._log.header("report", 65, "a") self._log.write([ "Email Sent.".ljust(25) + " < OK > ".ljust(15) + self.email_type ], "a") if not self._ok: new_email.Attachments.Add(abspath(self._attachment)) new_email.Send() self.d.write(" Line {} - Email sent.\n".format(gf().f_lineno), "a") except BaseException as e: print(f"Exception occured in send()..\n{e}") self.d.write( " Line {} - send(..), >> Exception \n {}".format( gf().f_lineno, e), "a")
def prepare_header_and_footer(self, **kargs): """ Iterates through dict() and substitutes placeholders inside base template. Final step in forming end report table. """ self._d.write( " Line {} - Entering prepare_header_and_footer(..)\n".format( gf().f_lineno), "a") for k, v in kargs.items(): if k == "head": if hasattr(self, "base"): self.base = self.base.replace("{EMAIL_HEADER_PLACEMENT}", "\n".join(v)) self._d.write( " Line {} - Replaced 'EMAIL_HEADER_PLACEMENT' placeholder\n" .format(gf().f_lineno), "a") else: self._d.write( " Line {} - Couldn't replace 'EMAIL_HEADER_PLACEMENT' placeholder. No 'base' attribute.\n" .format(gf().f_lineno), "a") else: if hasattr(self, "base"): self.base = self.base.replace("{EMAIL_FOOTER_PLACEMENT}", "\n".join(v)) self._d.write( " Line {} - Replaced 'EMAIL_FOOTER_PLACEMENT' placeholder\n" .format(gf().f_lineno), "a") else: self._d.write( " Line {} - Couldn't replace 'EMAIL_FOOTER_PLACEMENT' placeholder. No 'base' attribute.\n" .format(gf().f_lineno), "a")
def prepare_submission_table(self): """ Amend loaded base template by replacing 2 placeholders with corresponding values""" self._d.write( " Line {} - Entering prepare_submission_table(..)\n".format( gf().f_lineno), "a") self.base = self.base.replace("{CURRENT_DAY_QUEUE_NUM}", self.Qs["today"]) self._d.write( " Line {} - Replaced 'CURRENT_DAY_QUEUE_NUM' placeholder\n". format(gf().f_lineno), "a") self.base = self.base.replace("{PREV_DAY_QUEUE_NUM}", self.Qs["yesterday"]) self._d.write( " Line {} - Replaced 'PREV_DAY_QUEUE_NUM' placeholder\n".format( gf().f_lineno), "a")
def read_emails(self, emails): """ Processes a list of emails from the targeted folders, etc.. """ if emails is not None and len(emails) > 0: msg = emails.GetFirst() self._d.write( " Line {} - Entering read_emails(..)\n".format(gf().f_lineno), "a") while msg and self._is_of_today(msg.CreationTime, time=dt.now()): try: match = search(self._pattern, msg.Subject) match2 = search(self._prev_pattern, msg.Subject) # added on Sep 6 if match or match2: self._targeted_emails[msg.Subject] = 1 x = self._extract(msg) if x[0]: log_msg = x[1].ljust(25) + " < OK > ".ljust( 15) + msg.Subject self._log_buffer.append(log_msg + "\n") else: log_msg = x[1].ljust(25) + " < ERROR > ".ljust( 15) + msg.Subject self._log_buffer.append(log_msg + "\n") except BaseException as e: print( f" >> Exception occured in emails.py > read_emails(..)\n >> {e}" ) msg = emails.GetNext() self._dump_log()
def __init__(self, Qs, debug, with_footer): self._d = debug # Added on Apr 15th self._d.write( " Line {} - Initializing 'Partition' object.\n".format( gf().f_lineno), "a") self._with_footer = with_footer try: # Added 'fname' on Sep 9th to enable/diable footer signature fname = Path("COE_Report/report_template/base.html") \ if self._with_footer else Path("COE_Report/report_template/base_without_signature.html") with open((Path.home() / fname), "r") as f: self._d.write( " Line {} - Reading base template structure.\n".format( gf().f_lineno), "a") self.base = f.read() self._d.write( " Line {} - Loaded base template.\n".format( gf().f_lineno), "a") except Exception as e: self._d.write( " Line {} - Exception fired up in Partition.__init__.\n {}" .format(gf().f_lineno, str(e)), "a") self.Qs = Qs if hasattr(self, "base"): self._d.write( " Line {} - Partition.__init__() =>> 'base' attribute loaded.\n" .format(gf().f_lineno), "a") else: self._d.write( " Line {} - Partition.__init__() =>> has NO 'base' attribute. Using string.\n" .format(gf().f_lineno), "a") self.base = "<!doctype html><html lang=en><head><meta charset=utf-8><title>Report</title><style>table,th,td{border:1px solid black}.header_center{text-align:center}.centered_width{text-align:center;padding:2px 20px 2px 20px}.infr_col{text-align:left;padding:2px 20px 2px 2px}.centered{position:fixed;top:50%;left:50%;transform:translate(-50%,-50%)}.legend_table{border:1px solid black}</style></head><body><div class=centered><pre style=font-family:Calibri,Times,serif;font-size:16px>{EMAIL_HEADER_PLACEMENT}</pre><table style=width:650px><thead><tr><th class=header_center style=background-color:#cbd0d8> Canadian Order management System (COE)<br>Pre-Market Health Check </th><th colspan=2 class=header_center style=background-color:green><b>Green</b></th></tr></thead><tbody><tr><td style=width:325px;background-color:#59aff5 class=header_center><b>Infrastructure (5:00 AM)</b></td><td style=width:75px;background-color:#cbd0d8 class=header_center><b>Status</b></td><td style=width:200px;background-color:#cbd0d8 class=header_center><b>Comments</b></td></tr><tr><td style=background-color:#97cdfa class=infr_col>Windows - Hardware.</td><td class=centered_width style=background-color:green><b>OK</b></td><td class=centered_width>No issue to report.</td></tr><tr><td style=background-color:#97cdfa class=infr_col>Windows - OS.</td><td class=centered_width style=background-color:green><b>OK</b></td><td class=centered_width>No issue to report.</td></tr><tr><td style=background-color:#97cdfa class=infr_col>Database.</td><td class=centered_width style=background-color:green><b>OK</b></td><td class=centered_width>No issue to report.</td></tr><tr><td style=background-color:#97cdfa class=infr_col>Linux - Hardware.</td><td class=centered_width style=background-color:green><b>OK</b></td><td class=centered_width>No issue to report.</td></tr><tr><td style=background-color:#97cdfa class=infr_col>Linux - OS.</td><td class=centered_width style=background-color:green><b>OK</b></td><td class=centered_width>No issue to report.</td></tr></tbody></table><br/><table style=width:650px class=header_center><thead><tr><th style=width:325px;background-color:#e66f00>Canadian Equity Links Components</th><th style=width:75px;background-color:#cbd0d8>Status</th><th style=width:100px;background-color:#cbd0d8>Current day</th><th style=width:100px;background-color:#c0c4c9>Previous day</th></tr></thead><tbody><tr><td style=background-color:#ffb223 class=infr_col>TDSI SOR Primary started normally.</td><td style=background-color:green><b>OK</b></td><td><b>6:37 AM</b></td><td style=background-color:#c0c4c9><b>6:37 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>TDSI SOR Secondary started normally.</td><td style=background-color:green><b>OK</b></td><td><b>6:38 AM</b></td><td style=background-color:#c0c4c9><b>6:38 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>Wombat start of day completed - confirms market data consumption.</td><td style=background-color:green><b>OK</b></td><td><b>6:45 AM</b></td><td style=background-color:#c0c4c9><b>6:45 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>Retail subscription feed completed.</td><td style=background-color:green><b>OK</b></td><td><b>6:45 AM</b></td><td style=background-color:#c0c4c9><b>6:45 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>IBM /Raptor links are connected.</td><td style=background-color:green><b>OK</b></td><td><b>7:00 AM</b></td><td style=background-color:#c0c4c9><b>7:00 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>Morning queue release Successful.</td><td style=background-color:green><b>OK</b></td><td><b>7:06 AM</b></td><td style=background-color:#c0c4c9><b>7:06 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>All CDN Equities have been released to the markets successfully.</td><td style=background-color:green><b>OK</b></td><td><b>7:10 AM</b></td><td style=background-color:#c0c4c9><b>7:10 AM</b></td></tr><tr><td style=background-color:#ffb223 class=infr_col>All CDN Options & Complex Orders have been released to the markets successfully. </td><td style=background-color:green><b>OK</b></td><td><b>7:34 AM</b></td><td style=background-color:#c0c4c9><b>7:34 AM</b></td></tr><tr><td style=background-color:#e66f00 colspan=2 class=infr_col>Number of Released Orders in Queue:</td><td><b>{CURRENT_DAY_QUEUE_NUM}</b></td><td style=background-color:#c0c4c9><b>{PREV_DAY_QUEUE_NUM}</b></td></tr></tbody></table><br/><table class=legend_table><thead><tr><th colspan=2 style=background-color:#cbd0d8>Overall status of COE. </th></tr></thead><tbody><tr><td style=background-color:green>Green - </td><td>No issues.</td></tr><tr><td style=background-color:#ece906>Yellow - </td><td>Application is operating in degraded mode.</td></tr><tr><td style=background-color:#f00b0b>Red - </td><td>Application is UNAVAILABLE.</td></tr></tbody></table><br/><pre style=font-family:Calibri,Times,serif;font-size:16px;color:blue>{EMAIL_FOOTER_PLACEMENT}</pre></div></body></html>" # change to object
from config import ConfigHandler from logger_ import MyLogger, Debug from urls import URLHandler from report import Report, Partition from datetime import datetime as dt from sys import _getframe as gf from os import _exit if __name__ == "__main__": today = dt.now().strftime("%b %d") cfg = ConfigHandler().load() d = Debug(enable=cfg['debug_enabled']) d.write("Line {} - Entered main routine \n".format(gf().f_lineno), "w+") d.write("Line {} - Before loading logger \n".format(gf().f_lineno), "a") log = MyLogger() d.write("Line {} - After loading logger \n".format(gf().f_lineno), "a") d.write("Line {} - Checking for balcklist \n".format(gf().f_lineno), "a") # to track current day and check against black list if today in cfg['blacklist_days']: d.write("Line {} - entered blacklist routine \n".format(gf().f_lineno), "a") log._switch_to_blacklist() log.header("blacklist", 65, "w+") log.write([ " >>\n", " >> This date '{0}' is listed on the blacklist, which means:\n". format(today), " >> No data has been gathered.\n", " >> No data has been validated.\n",
def download(self, td=dt.now(), minus=1, limit=5, pfound=False): self._d.write( " Line {} - Entering download(..)\n".format(gf().f_lineno), "a") today = td.replace(hour=0, minute=0) yesterday = today - timedelta(days=minus) ol = Dispatch("Outlook.Application").GetNamespace( "MAPI") # added this on Apr 4 inbox = ol.DefaultStore.GetRootFolder() # # added this on Apr 4 # Arp 4: changed = self._inbox.Folders folders, found = inbox.Folders, pfound for f in folders: if f.Name in self._cfg["lookup_folder"] and len(f.Items) > 0: self._d.write( " Line {} - Seraching inside '{}' folder. Iteration No: {} \n" .format(gf().f_lineno, f.Name, limit), "a") messages = f.Items messages.Sort("[ReceivedTime]", True) messages = messages.Restrict( "[ReceivedTime] >='" + yesterday.strftime("%Y-%m-%d %H:%M %p") + "' and [ReceivedTime] <='" + today.strftime("%Y-%m-%d %H:%M %p") + "'") msg = messages.GetFirst() while msg: try: match = search(self._prev_pattern, msg.Subject) if match: match = search("is\s{0,3}\d{0,3}(,)?\d{1,3}", msg.Body) if match: tmp = str(match.group(0)[2:]).strip() if "." not in tmp: self._prev_day_q_num = tmp else: self._prev_day_q_num = tmp.replace(".", "") #self._prev_day_q_num = match.group(0)[9:] #str(match.group()[m.span()[0]:]) self._OK = found = True log_msg = "Load prev. Q num".ljust( 25) + " < OK > ".ljust(15) + str( msg.Subject) + " || Num of Qs: " + str( self._prev_day_q_num) self._log_buffer.append(log_msg + "\n") return True msg = messages.GetNext() except BaseException as e: print( f" Raised exception within preload(..)\n >> Occured at this email '{msg.Subject}'\n >> '{e}'." ) self._d.write( " Line {} - download(..), >> Exception \n {}" .format(gf().f_lineno, e), "a") today -= timedelta(days=minus) if (limit - 1) != 0 and not found: self.download(td=today, minus=1, limit=limit - 1, pfound=found) else: log_msg = "Load base template".ljust(25) + " < ERROR > ".ljust( 15 ) + "Couldn't find Previous 'Green' Report...Tried 4 days deep." self._log_buffer.append(log_msg + "\n") self._OK = False return False