from Library.Json import json_decode from Library.Yaml import yaml_decode import os def _home_of_conf(): home = '%s/%s' % (os.path.dirname(os.path.dirname(__file__)), 'Config') if os.path.exists(home): return home else: raise FileNotFoundError('Directory "%s" does not exist.' % home) class TestingConfig(dict): """ Load the configuration file from the configuration directory. """ def __init__(self, name: str): home = _home_of_conf() json_extension = '.json' yaml_extension = '.yaml' pa_json = '%s/%s%s' % (home, name, json_extension) pa_yaml = '%s/%s%s' % (home, name, yaml_extension) data = None if os.path.exists(pa_json): file = pa_json data = json_decode(open(file, encoding='utf-8').read()) if os.path.exists(pa_yaml): file = pa_yaml data = yaml_decode(open(file, encoding='utf-8').read()) if data is not None: super().__init__(data) else: raise FileNotFoundError('No json or yaml configuration file "%s" in config directory.' % name) def __setattr__(self, key, value): raise AttributeError('Attribute is read only.') def __getattr__(self, item): try: return super().__getitem__(item) except KeyError: return None def __setitem__(self, key, value): return self.__setattr__(key, value) def __getitem__(self, item): return self.__getattr__(item)