creating global context and refactoring tmp_dir logic

This commit is contained in:
msramalho
2023-03-23 11:17:38 +00:00
parent 39818e648a
commit 906ed0f6e0
16 changed files with 88 additions and 34 deletions

View File

@@ -0,0 +1,38 @@
class ArchivingContext:
"""
Singleton context class.
ArchivingContext._get_instance() to retrieve it if needed
otherwise just
ArchivingContext.set(key, value)
and
ArchivingContext.get(key, default)
"""
_instance = None
def __init__(self):
self.configs = {}
@staticmethod
def get_instance():
if ArchivingContext._instance is None:
ArchivingContext._instance = ArchivingContext()
return ArchivingContext._instance
@staticmethod
def set(key, value):
ArchivingContext.get_instance().configs[key] = value
@staticmethod
def get(key: str, default=None):
return ArchivingContext.get_instance().configs.get(key, default)
# ---- custom getters/setters for widely used context values
@staticmethod
def set_tmp_dir(tmp_dir: str):
ArchivingContext.get_instance().configs["tmp_dir"] = tmp_dir
@staticmethod
def get_tmp_dir() -> str:
return ArchivingContext.get_instance().configs.get("tmp_dir")