A dict-style API for TOML-based config files.
Project description
Config API
Installation
pip install configapi
Basic Usage
Create a dictionary-like object that merges multiple TOML files.
>>> from configapi import Config
>>> import somepackage.config
>>> configs = Config(sources={
... 'default': (somepackage.config, 'defaults.toml'), # Read from package resource.
... 'user': Path.home() / 'user-config.toml', # Read from textfile.
... })
>>> configs.load()
There are 3 kind of sources supported:
- Files: Specified as a path to a TOML file (pathlib.Path or str).
- Package file: Specified as a tuple with a package and a path to a file in the package.
- In-memory: Specified as a dictionary.
The configs object provides a dictionary-like interface to the merged configuration, and to each source separately. If multiple sources provide the same key, the value from the source added last to the config container is used.
>>> configs['project.authors']
['me']
>>> configs.default['project.authors']
['Bob', 'Alice']
>>> configs.source('project.authors')
'user'
Iterate over all entries. Optionally, the source can be included.
>>> for (key, value, source) in configs.items(source=True):
... print(f'{key}: {value} (from {source})')
project.authors: ['me'] (from user)
project.name: Example Project (from default)
Modifications must be done on the separate source dictionaries (also called scopes). The changes are not saved automatically.
>>> configs.user['project.name'] = 'My Project'
>>> configs.user.save()
>>> del configs.default['project.name']
>>> value, source, scope = configs.get('project.name', source=True, scope=True)
>>> print(f'{value} (from {source})')
My Project (from user)
>>> assert scope is configs.user
Advanced Usage
The config files may be versioned, and upgrade patches applied automatically. Specify the current version, or let it be inferred from the patches.
>>> configs = Config(target_version='1.5.0')
Sources may be specified more explicitly for finer control. The autosave feature ensures that changes applied by patches. during loading are immediately saved back to the source.
>>> local = FileConfigSource(Path.cwd() / 'local-config.toml', read_only=False)
>>> configs.add_source('local', local, autosave_updates=True)
>>> @configs.patch('0.2.1')
>>> def patch_0_2_1(cfg):
... if 'project.author' in cfg:
... cfg['project.authors'] = [cfg['project.author']]
... del cfg['project.author']
... return cfg
>>> configs.local.load()
If needed, individual sources may be (re-)loaded separately.
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 configapi-0.0.7.tar.gz.
File metadata
- Download URL: configapi-0.0.7.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1fbb7811ca715e73b735027570fb44168e21d90386e253a8a578b00cd992319
|
|
| MD5 |
6d551001ed8bfd8fae19981323729587
|
|
| BLAKE2b-256 |
f38cf9844b828d2e17cdcd7a96fd33afd1b9f88901a91c7e9e940a793f399859
|
Provenance
The following attestation bundles were made for configapi-0.0.7.tar.gz:
Publisher:
python-app.yml on PatrickSchaffner/ConfigAPI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
configapi-0.0.7.tar.gz -
Subject digest:
c1fbb7811ca715e73b735027570fb44168e21d90386e253a8a578b00cd992319 - Sigstore transparency entry: 163720995
- Sigstore integration time:
-
Permalink:
PatrickSchaffner/ConfigAPI@1c3277520525400cc7397d73a524182f67edd285 -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/PatrickSchaffner
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-app.yml@1c3277520525400cc7397d73a524182f67edd285 -
Trigger Event:
push
-
Statement type:
File details
Details for the file configapi-0.0.7-py3-none-any.whl.
File metadata
- Download URL: configapi-0.0.7-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6754c290f2c505bc50a5782bc49194521c79b3c0c3e36927d2ce21da7a9ce4fe
|
|
| MD5 |
a74d3c152947e49a5da2ac5e56ceb011
|
|
| BLAKE2b-256 |
450f529939f672787ecc262909ce41570fa6e63791cb8c39b80e2ff89a0cbd01
|
Provenance
The following attestation bundles were made for configapi-0.0.7-py3-none-any.whl:
Publisher:
python-app.yml on PatrickSchaffner/ConfigAPI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
configapi-0.0.7-py3-none-any.whl -
Subject digest:
6754c290f2c505bc50a5782bc49194521c79b3c0c3e36927d2ce21da7a9ce4fe - Sigstore transparency entry: 163720997
- Sigstore integration time:
-
Permalink:
PatrickSchaffner/ConfigAPI@1c3277520525400cc7397d73a524182f67edd285 -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/PatrickSchaffner
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-app.yml@1c3277520525400cc7397d73a524182f67edd285 -
Trigger Event:
push
-
Statement type: