Skip to content
Snippets Groups Projects
Commit 32818f49 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
class GenericImageFileAccessor(object):
pass
class CziImageFileAccessor(GenericImageFileAccessor):
def get_shape(self):
pass
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()
import os
def validate_directory_exists(path):
return os.path.exists(path)
class SharedImageDirectory(object):
def __init__(self, path): # create at session startup, validate
self.path = path
if not validate_directory_exists(path):
raise InvalidDirectoryError(f'Invalid directory:\n{path}')
class InvalidDirectoryError(Exception):
pass
from pathlib import Path
root = Path('c:/Users/rhodes/projects/proj0015-model-server/resources')
paths = {
'logs': {
'session': root / 'logs' / 'session',
},
'images': {
'inbound': root / 'images' / 'inbound',
'outbound': root / 'images' / 'outbound',
}
}
for gk in paths.keys():
for pk in paths[gk].keys():
paths[gk][pk].mkdir(parents=True, exist_ok=True)
\ No newline at end of file
import unittest
from base.session import Session
class TestGetSessionObject(unittest.TestCase):
def setUp(self) -> None:
pass
def test_single_session_instance(self):
sesh = Session()
self.assertIs(sesh, Session(), 'Re-initializing Session class returned a new object')
def test_restart_session(self):
sesh = Session()
logfile = sesh.logfile
self.assertIsNot(logfile, sesh.restart(), 'Restarting session does not generate new logfile')
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment