Override parameter defaults via environment variables & config files
Project description
envwrap
Override parameter defaults via environment variables & config files.
import envwrap
@envwrap.envwrap("name", "app")
def func(a=1):
...
Precedence (descending):
- call (
func(a=3)) - environment (
NAME_APP_FUNC_A=2,NAME_FUNC_A=2,NAME_APP_A=2,NAME_A=2)UPPER_CASEenv vars ->lower_caseparam names- other cases aren't supported because Windows ignores case
- config file:
- ./
{name}.{toml,yaml,yml,json,ini,cfg}::{app.func.a,func.a,app.a,a} - platformdirs.{user,site}_config_path(name, False)/
{app}.{toml,yaml,yml,json,ini,cfg}::{func.a,a}{name}.{toml,yaml,yml,json,ini,cfg}::{app.func.a,func.a,app.a,a}
- ./
pyproject.toml::tool.name.{app.func.a,func.a,app.a,a}
- ./
- signature (
def foo(a=1))
Installation
Any one of:
pip install envwrapconda install -c conda-forge envwrappip install "git+https://github.com/tqdm/envwrap@main"
[!TIP] Note that
tqdmships with a basictqdm.utils.envwrap, which falls back to the original env-var-only (no config file support) implementation ifimport envwrapfails.
Advanced Usage
CLI integration
"""CLI example program using envwrap for configuration management.
Usage:
myapp.py [options] <arg1> [<arg2>]
Options:
-h, --help Show this help message and exit.
-o=<value>, --option=<value> An option [default: foo].
Arguments:
<arg1> An argument.
<arg2> An integer argument [default: 2:int].
Defaults above may be overridden by environment variables or config files:
- `MYAPP[_CLI]_*`
- `{.,~/.config/myapp,/etc/xdg/myapp}/myapp[/cli].{toml,yml,json,ini}::[cli.]*`
- `pyproject.toml::tool.myapp[.cli]*`
"""
import argopt, shtab, envwrap
ONLY_PASS_VALID = True # trim envwrap defaults based on parser's valid actions
if __name__ == "__main__":
parser = argopt.argopt(__doc__)
shtab.add_argument_to(parser)
defaults = envwrap.get_defaults("myapp", "", "cli")
if ONLY_PASS_VALID:
valid = {i.dest for i in parser._actions}
defaults = {k: defaults[k] for k in defaults.keys() & valid}
parser.set_defaults(**defaults)
args = parser.parse_args()
print(args)
Live-reload
To force re-reading config files & environment variables without restarting the process:
envwrap.get_defaults.cache_clear()
Debugging
A CLI tool can print defaults. For example, with this config:
# config file: foo.toml
[test]
a = 1337
b = 2
python -m envwrap --help
FOO_A=42 python -m envwrap foo test
will print:
>>> @envwrap.envwrap('foo', '')
>>> def test(...):
... ...
will use defaults:
{'a': '42', 'b': 2, ...}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file envwrap-0.3.0.tar.gz.
File metadata
- Download URL: envwrap-0.3.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e6afadbc38c4e7d866b9714d2375fb640c263dd3f636e3e86194a695675e80b
|
|
| MD5 |
df13c76f72dc04acdc0ac830b90cf9c7
|
|
| BLAKE2b-256 |
2c02afa25af77a4b6615339f9d4838bd9be1e1d7e76cfd62301ba4342fa235ca
|
Provenance
The following attestation bundles were made for envwrap-0.3.0.tar.gz:
Publisher:
test.yml on tqdm/envwrap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envwrap-0.3.0.tar.gz -
Subject digest:
2e6afadbc38c4e7d866b9714d2375fb640c263dd3f636e3e86194a695675e80b - Sigstore transparency entry: 1750628845
- Sigstore integration time:
-
Permalink:
tqdm/envwrap@9bbebcc7e74f275659cd2cef09fe0e3937dbc13e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/tqdm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test.yml@9bbebcc7e74f275659cd2cef09fe0e3937dbc13e -
Trigger Event:
push
-
Statement type:
File details
Details for the file envwrap-0.3.0-py3-none-any.whl.
File metadata
- Download URL: envwrap-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a80d4456fe177897fc3451f2841d2f79e5b56d71b6168a720b06d492a11e97b
|
|
| MD5 |
14166da7c07d3d494e024c825c0115c0
|
|
| BLAKE2b-256 |
9546c44c255630caa84c78e5c83c5585d661b38da9f8a54fdbddd2b0de7a5115
|
Provenance
The following attestation bundles were made for envwrap-0.3.0-py3-none-any.whl:
Publisher:
test.yml on tqdm/envwrap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envwrap-0.3.0-py3-none-any.whl -
Subject digest:
1a80d4456fe177897fc3451f2841d2f79e5b56d71b6168a720b06d492a11e97b - Sigstore transparency entry: 1750628798
- Sigstore integration time:
-
Permalink:
tqdm/envwrap@9bbebcc7e74f275659cd2cef09fe0e3937dbc13e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/tqdm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
test.yml@9bbebcc7e74f275659cd2cef09fe0e3937dbc13e -
Trigger Event:
push
-
Statement type: