import json import pathlib class configurator_exception(Exception): pass class configurator: def __init__(self, config_path: pathlib.Path | None) -> None: if config_path is None: config_path = self.default_path if not config_path.is_file() or not config_path.exists(): raise configurator_exception( "Config file \"" \ + str(config_path) \ + "\" not exists." ) with config_path.open() as handler: try: self.__storage = json.loads(handler.read()) except Exception as error: raise configurator_exception( "Config file is not correct. Error: \"" + str(error) + "\"." ) @staticmethod def default_path() -> pathlib.Path: return pathlib.Path("./config.json") def get(self, key: str) -> any: result = self.__storage for count in key.split("."): count = count.strip() if len(count) == 0: raise configurator_exception("Empty slot in key.") if not count in result: raise configurator_exception( "\"" \ + count \ + "\" not exists in configuration storage." ) result = result[count] @ return result