os_helper.config_utils module
Configuration Utilities
Load and validate configuration settings from multiple sources with a
deterministic fallback order:
1. an explicit JSON or YAML file (or the first valid one in a folder),
2. one or more .env files merged into os.environ,
3. plain environment variables.
The helpers verify that every required key is present and raise a clear
RuntimeError when none of the sources can satisfy the request.
- Author:
Warith HARCHAOUI, https://linkedin.com/in/warith-harchaoui
- os_helper.config_utils.get_config(keys, config_type, path=None, env_files=None)[source]
Load configuration settings using a fixed fallback order.
Precedence (first match wins): 1.
pathpointing to a JSON/YAML file (or, if a directory, the first.json,.yamlor.ymlfile in it that contains all keys);one or more
.envfiles merged intoos.environ;the current process environment.
- Parameters:
keys (List[str]) – Keys required to be present in the resolved configuration.
config_type (str) – Human-readable label used only in log messages.
path (Optional[str], optional) – Path to a configuration file or a directory containing one. If None or empty, this step is skipped.
env_files (Optional[List[str]], optional) –
.envfiles to load intoos.environbefore reading variables. Defaults to[".env"].
- Returns:
Mapping with one entry per requested key.
- Return type:
- Raises:
RuntimeError – If none of the sources provide all required keys.
Example
>>> config = get_config(["host", "port"], "database", path="config.yaml") >>> config {'host': 'localhost', 'port': 5432}