41 lines
934 B
Python
41 lines
934 B
Python
"""Implements a config object with dot-indexing syntax."""
|
|
|
|
|
|
from envyaml import EnvYAML
|
|
|
|
from image_prediction.locations import CONFIG_FILE
|
|
|
|
|
|
def _get_item_and_maybe_make_dotindexable(container, item):
|
|
ret = container[item]
|
|
return DotIndexable(ret) if isinstance(ret, dict) else ret
|
|
|
|
|
|
class DotIndexable:
|
|
def __init__(self, x):
|
|
self.x = x
|
|
|
|
def __getattr__(self, item):
|
|
return _get_item_and_maybe_make_dotindexable(self.x, item)
|
|
|
|
def __setitem__(self, key, value):
|
|
self.x[key] = value
|
|
|
|
def __repr__(self):
|
|
return self.x.__repr__()
|
|
|
|
|
|
class Config:
|
|
def __init__(self, config_path):
|
|
self.__config = EnvYAML(config_path)
|
|
|
|
def __getattr__(self, item):
|
|
if item in self.__config:
|
|
return _get_item_and_maybe_make_dotindexable(self.__config, item)
|
|
|
|
def __getitem__(self, item):
|
|
return self.__getattr__(item)
|
|
|
|
|
|
CONFIG = Config(CONFIG_FILE)
|