Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()