Simple configuration loader for python.
Project description
zen-config
Simple configuration loader for python.
Compared to other solutions, the goal is to bring:
- simple usage for simple use cases
- multiple format support
- use objects rather than plain dict to interact with the config
- optionally use the power of pydantic for validation
Simple usage
If you don't want to configure much, pass the config path through the env variable CONFIG, and simply use:
from dataclasses import dataclass
from zenconfig import Config
@dataclass
class MyConfig(Config):
some_key: str
some_optional_key: bool = False
cfg = MyConfig(some_key="hello")
cfg.save()
...
cfg = MyConfig.load()
cfg.some_optional_key = True
cfg.save()
...
cfg.clear()
Config file loading
When creating your config, you can specify at least one of those two attributes:
ENV_PATHthe environment variable name containing the path to the config file, defaults toCONFIGPATHdirectly the config path
[!TIP] When supplying both, if the env var is not set, it will use
PATH.
User constructs will be expanded.
If the file does not exist it will be created.
You can specify the file mode via Config.FILE_MODE.
The config can be loaded from multiple files, see fnmatch for syntax. Note that you will not be able to save if not handling exactly one file.
Read only
If you do not want to be able to modify the config from your code, you can use ReadOnlyConfig.
Supported formats
Currently, those formats are supported:
- JSON
- YAML - requires the
yamlextra - TOML - requires the
tomlextra
The format is automatically inferred from the config file extension. When loading from multiple files, files can be of multiple formats.
Other formats can be added by subclassing Format: Config.register_format(MyFormat(...), ".ext1", ".ext2").
[!TIP] You can re-register a format to change dumping options.
Supported schemas
Currently, those schemas are supported:
- plain dict
- dataclasses
- pydantic models - requires the
pydanticextra - attrs - requires the
attrsextra
The schema is automatically inferred from the config class.
Other schemas can be added by subclassing Schema: Config.register_schema(MySchema(...)).
You can also force the schema by directly overriding the SCHEMA class attribute on your config.
This can be used to disable auto selection, or pass arguments to the schema instance.
[!WARNING] When using pydantic, you have to supply the
ClassVartype annotations to all class variable you override otherwise pydantic will treat those as its own fields and complain.
Conversions
For all schemas and formats, common built in types are handled when dumping.
[!IMPORTANT] Keep in mind that only
attrsandpydanticsupport casting when loading the config.
You can add custom encoders with Config.ENCODERS.
For pydantic, stick with the standard way of doing it.
Contributing
See contributing guide.
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 zenconfig-2.2.0.tar.gz.
File metadata
- Download URL: zenconfig-2.2.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5c8b7470c2288fe3c44af50b83552f659380479834247a670ea3b9fc95d888e
|
|
| MD5 |
7330c2f11403f1731aaa35d25badf523
|
|
| BLAKE2b-256 |
1dbb969caeead72816d4036597ef76dc0254010c170689ddd5a82dd24839d8ba
|
Provenance
The following attestation bundles were made for zenconfig-2.2.0.tar.gz:
Publisher:
publish.yml on gpajot/zen-config
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zenconfig-2.2.0.tar.gz -
Subject digest:
f5c8b7470c2288fe3c44af50b83552f659380479834247a670ea3b9fc95d888e - Sigstore transparency entry: 618552473
- Sigstore integration time:
-
Permalink:
gpajot/zen-config@1958848e239157e28e6112ba773730cfc62e6aa7 -
Branch / Tag:
refs/tags/2.2.0 - Owner: https://github.com/gpajot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1958848e239157e28e6112ba773730cfc62e6aa7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file zenconfig-2.2.0-py3-none-any.whl.
File metadata
- Download URL: zenconfig-2.2.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dffc90a4ad8a89654bed617383227ef59b7be0bc913e5254faa243df5eca0303
|
|
| MD5 |
f5af69f4a327902c4a201fe8e3abcc25
|
|
| BLAKE2b-256 |
56b91336db716ebb4b7964f3bc8344134f16b739e47c933c72dbe210b0469ed2
|
Provenance
The following attestation bundles were made for zenconfig-2.2.0-py3-none-any.whl:
Publisher:
publish.yml on gpajot/zen-config
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zenconfig-2.2.0-py3-none-any.whl -
Subject digest:
dffc90a4ad8a89654bed617383227ef59b7be0bc913e5254faa243df5eca0303 - Sigstore transparency entry: 618552478
- Sigstore integration time:
-
Permalink:
gpajot/zen-config@1958848e239157e28e6112ba773730cfc62e6aa7 -
Branch / Tag:
refs/tags/2.2.0 - Owner: https://github.com/gpajot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1958848e239157e28e6112ba773730cfc62e6aa7 -
Trigger Event:
release
-
Statement type: