import json
import os
from ccae_reporter.common.logging import Log
from ccae_reporter.common.util import safe_open
from ccae_reporter.common.util import PathCheck
class ConfigUtil:
config = None
logger = None
@classmethod
def log_warning(cls, msg: str):
if not cls.logger:
cls.logger = Log(__name__).getlog()
cls.logger.warning(msg)
@classmethod
def get_config(cls, key: str):
if not cls.config:
config_dir = os.getenv('CONFIG_PATH')
if config_dir is None:
raise RuntimeError("Environment variable $CONFIG_PATH is not set.")
config_dir = os.path.realpath(config_dir)
if not PathCheck.check_path_full(config_dir):
raise RuntimeError("Failed to check `CONFIG_PATH`")
config_file_path = os.path.join(config_dir, "user_config.json")
with safe_open(config_file_path) as f:
cls.config = json.loads(f.read())
keys = key.split('.')
value = cls.config
try:
for k in keys:
if value is None:
return value
value = value.get(k, None)
return value
except (KeyError, TypeError):
cls.log_warning(f"{key} is not in config of ms controller")
return None