Skip to content
Snippets Groups Projects
session.py 1.25 KiB
Newer Older
import os

from pathlib import Path
from time import strftime, localtime

from conf.server import paths
from base.share import SharedImageDirectory

def create_session_log():
    """
    Create a log file with an autoincremented date-specific filename.

    :return: absolute path to log file
    """
    where_log = Path(paths['logs']['session'])

    yyyymmdd = strftime('%Y%m%d', localtime())

    idx = 0
    while os.path.exists(where_log / f'{yyyymmdd}-{idx:04d}.log'):
        idx += 1

    log_file = where_log / f'{yyyymmdd}-{idx:04d}.log'
    with open(log_file, 'w+') as fh:
        fh.write(f'{yyyymmdd} -- called session constructor')
    return log_file

class Session(object):
    """
    Singleton class for persisting data between API calls.
    """
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            cls.instance = super(Session, cls).__new__(cls)
            cls.instance.logfile = create_session_log()
        return cls.instance

    def __init__(self):
        print('Initializing session')
        self.inbound = SharedImageDirectory(paths['images']['inbound'])
        self.outbound = SharedImageDirectory(paths['images']['outbound'])

    def restart(self):
        self.__init__()
        self.logfile = create_session_log()