Support nested configurations

This commit is contained in:
Matthias 2022-04-07 20:29:03 +02:00
parent aee0cfd17a
commit 1ea49ce864

View File

@ -75,18 +75,36 @@ def load_config_file(path: str) -> Dict[str, Any]:
return config return config
def load_from_files(files: List[str]) -> Dict[str, Any]: def load_from_files(files: List[str], base_path: Path = None, level: int = 0) -> Dict[str, Any]:
"""
Recursively load configuration files if specified.
Sub-files are assumed to be relative to the initial config.
"""
config: Dict[str, Any] = {} config: Dict[str, Any] = {}
if level > 5:
raise OperationalException("Config loop detected.")
if not files: if not files:
return deepcopy(MINIMAL_CONFIG) return deepcopy(MINIMAL_CONFIG)
# We expect here a list of config filenames # We expect here a list of config filenames
for path in files: for filename in files:
logger.info(f'Using config: {path} ...') logger.info(f'Using config: {filename} ...')
# Merge config options, overwriting old values if filename == '-':
config = deep_merge_dicts(load_config_file(path), config) # Immediately load stdin and return
return load_config_file(filename)
file = Path(filename)
if base_path:
# Prepend basepath to allow for relative assignments
file = base_path / file
config_tmp = load_config_file(str(file))
if 'files' in config_tmp:
config_sub = load_from_files(config_tmp['files'], file.resolve().parent, level + 1)
deep_merge_dicts(config_sub, config_tmp)
# Merge config options, overwriting prior values
config = deep_merge_dicts(config_tmp, config)
config['config_files'] = files config['config_files'] = files