예제 #1
0
    def sendEmails(self, receiverEmails: List[str], msg: str, subject: str) -> None:
        try:
            smtpServer = "smtp.gmail.com"
            senderEmail = environ.get('SENDEREMAIL')
            password = environ.get('EMAILPWD')

            if senderEmail and password:
                with smtplib.SMTP_SSL(smtpServer, 465) as server:
                    server.login(senderEmail, password)

                    for email in receiverEmails:
                        try:
                            emailMsg = EmailMessage()
                            emailMsg.set_content(msg)
                            emailMsg['Subject'] = subject
                            emailMsg['From'] = senderEmail
                            emailMsg['To'] = email
                            server.send_message(emailMsg)
                            info("{} Email sent to {}".format(subject, email))
                        except Exception as e:
                            # log error
                            error(str(e))
            else:
                raise Exception("Missing sender email and/or password")
        except Exception as e:
            error(str(e))
예제 #2
0
    def parseCvsTs(self, cvsRespData: Any) -> datetime:
        roundedParsed = datetime(1, 1, 1)
        ts = ""

        if "currentTime" in cvsRespData:
            ts = cvsRespData["currentTime"]

        if 'T' in ts:
            splitTs = ts.split('T')

            if len(splitTs) >= 2:
                formats = ['%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S']

                # try multiple different formats as it could
                # be either of them
                for format in formats:
                    try:
                        tsStr = '{} {}'.format(splitTs[0], splitTs[1])
                        parsed = datetime.strptime(tsStr, format)
                        # create new dt object that is rounded to nearest second
                        roundedParsed = datetime(parsed.year, parsed.month, parsed.day, 
                            parsed.hour, parsed.minute, parsed.second)
                        break
                    except Exception as e:
                        error(str(e))

        return roundedParsed
예제 #3
0
def sample_bottom_up(infile='presents_revorder.csv', outfile='sub_bottomup_1.csv', write=True, check=True):
    """
    Replicate the sample bottom-up approach
    """
    sleigh = classes.LayerSleigh()
    layer = classes.Layer()

    presents_file = os.path.join('data', infile)
    outfile = os.path.join('data', outfile)
    logger.info("Reading and placing presents")
    with open(presents_file, 'rb') as presents:
            presents.readline()  # skip header
            read = csv.reader(presents)
            for row in read:
                present = classes.Present(*row)
                if not layer.place_present(present):
                    # Can't place the present on the layer, so close the layer and start a new one
                    sleigh.add_layer(layer)
                    layer = classes.Layer(z=sleigh.max_z + 1)
                    res = layer.place_present(present)
            # Add the final layer
            sleigh.add_layer(layer)

    if check and not sleigh.check_all():
        logger.error('There is an error in the Sleigh')
        return sleigh

    if write:
        sleigh.write_to_file(outfile)
    return sleigh
예제 #4
0
    def getApptDataForStateCvs(self, state: str) -> Any:
        apptData = None

        try:
            reqUrl = baseURLs["CVS"].format(state)
            headers = {'Referer': 'https://www.cvs.com/immunizations/covid-19-vaccine'}
            req = get(url=reqUrl, headers=headers)
            apptData = req.json()['responsePayloadData']
        except Exception as e:
            error(str(e))

        return apptData
예제 #5
0
def sample_top_down(infile='presents_revorder.csv', outfile='sub_topdown_1.csv', write=True, check=True):
    """
    Replicate the MatLab top-down approach

    Strategy is basically the same as bottom_up, but before closing each layer,
    align all of the presents to the top of the layer.

    Actually this strategy is not quite the same, since it reads the present in a different order.
    Result is a slightly higher score than the benchmark
    """
    sleigh = classes.LayerSleigh()
    layer = classes.Layer()

    presents_file = os.path.join('data', infile)
    outfile = os.path.join('data', outfile)
    logger.info("Reading and placing presents")
    with open(presents_file, 'rb') as presents:
            presents.readline()  # skip header
            read = csv.reader(presents)
            for row in read:
                present = classes.Present(*row)
                if not layer.place_present(present):
                    # Can't place the present on the layer, so close the layer and start a new one
                    # Before closing, re-align all of the presents in the layer to the top of the layer
                    align_presents_to_layer_top(layer)
                    sleigh.add_layer(layer)
                    layer = classes.Layer(z=sleigh.max_z + 1)
                    res = layer.place_present(present)
            # Add the final layer
            align_presents_to_layer_top(layer)
            sleigh.add_layer(layer)

    if check and not sleigh.check_all():
        logger.error('There is an error in the Sleigh')
        return sleigh

    if write:
        sleigh.write_to_file(outfile)
    return sleigh
예제 #6
0
 def check(self):
     if not self.sleigh.check_all():
         logger.error('There is an error in the Sleigh')
예제 #7
0
from pathlib import Path
from classes.logger import error, info


if __name__ == "__main__":
    try:
        logFile = environ.get('LOGFILE')
        
        if logFile:
            # construct new file name, get current log directory, construct
            # new file absolute path
            curDate = datetime.strftime(datetime.now(), "%Y-%m-%d")
            newFileName = "app.logger.{}".format(curDate)
            logDir = path.dirname(logFile)
            newFile = path.join(logDir, newFileName)
            exist = path.isfile(newFile)

            if not exist:
                # if the new file doesn't already exist
                # then rename the old one to the new file 
                Path(logFile).rename(newFile)
                info("Logs successfully archived to file {}".format(newFileName))
            else:
                # if the file exists already don't archive
                info("Log file was not archived as {} already exists.".format(newFileName))
        else:
            info("Unable to archive log file. No log file environment variable found.")

    except Exception as e:
        error(str(e))