"""Functions to get the parameters from the YAML files during the importing.""" from importlib.resources import files import logging import os import re import yaml logger = logging.getLogger(__name__) YAML_ENVVAR_RGX = re.compile(r'^ENV\[(.*)\]$') def _parse_envvar(token): """Parse a string containing an ENV variable. Args: token (str): String from the config file. Returns: str: Name of an environment variable. """ return YAML_ENVVAR_RGX.match(token).groups()[0] def _yaml_envvar_contructor(loader, node): """Get the variable value from the environment. Args: loader (Loader): An instance of a yaml.Loader class. node (ScalarNode): An instance of a node of a config. Returns: str: The value of an environment variable. """ token = loader.construct_scalar(node) env_var = _parse_envvar(token) if env_var in os.environ: return os.environ[env_var] return None yaml.add_implicit_resolver('!env_var', YAML_ENVVAR_RGX) yaml.add_constructor('!env_var', _yaml_envvar_contructor) def getconf(name, ext='yml'): """Get the configuration parameters from the YAML stream. Args: name (str): A name of a file. ext (str): An extension of the file. Returns: dict: The values of a config. """ filename = '.'.join((name, ext)) logger.debug('loading config file %s', filename) package = __name__ resource = files(package).joinpath(filename) with resource.open('rb') as conf_stream: return yaml.load(conf_stream, Loader=yaml.FullLoader) def flat_dict(dd, sep='.', _prefix=''): """Convert nested dictionary into a flat dictionary. Example: {'a': {'b': 'c'}} -> {'a.b': 'c'} Args: dd (dict): Nested dictionary. sep (str): Separator to use for joining nested keys. _prefix (str): Used for recursive buildup of composite keys. Returns: dict: Flat dictionary with composite keys. """ return {sep.join((_prefix, k)) if _prefix else k: v for kk, vv in dd.items() for k, v in flat_dict(vv, sep, kk).items() } if isinstance(dd, dict) else {_prefix: dd} def getctx(name): """Get the configuration parameters from the YAML file. Args: name (str): A name of a config section. Returns: dict: А flat dictionary with the config parameters. """ return flat_dict(getconf(name))