"""Implements a config object with dot-indexing syntax.""" from envyaml import EnvYAML from pyinfra.locations import CONFIG_FILE def make_art(): return """ ______ _____ __ | ___ \ |_ _| / _| | |_/ / _ | | _ __ | |_ _ __ __ _ | __/ | | || || '_ \| _| '__/ _` | | | | |_| || || | | | | | | | (_| | \_| \__, \___/_| |_|_| |_| \__,_| __/ | |___/ """ 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__() def __getitem__(self, item): return self.__getattr__(item) 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)