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, *, allow_ambient_env=True)[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 (skipped when
allow_ambient_envisFalse).
- 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"].allow_ambient_env (bool, optional) – When
True(the default, and the only behavior before this parameter existed), step 3 reads the live process environment — which includes both the requested.envfiles and whatever the process happened to inherit at start-up. WhenFalse, step 3 is restricted to exactly the requestedenv_files’ own contents (read directly, never merged into or read back fromos.environ): a key that resolves only because it happens to be set in the ambient environment is treated as unresolved. Used by the HTTP API (seeos_helper.api), where “the caller can name any environment-variable key and get its live value back” is a credential-exposure shape, not a feature — a local CLI/library caller already has that access by definition, so this only ever needs to beFalseacross a network boundary.
- 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}