Configuration providing library
Project description
Kaifario
Kaifario is a lightweight and flexible configuration builder library for Python, inspired by the builder-style configuration system from C#. It allows you to compose multiple configuration sources (memory, environment variables, files, etc.) into a single, unified configuration object.
🔧 Installation
pip install kaifario
🚀 Quick Start
from kaifario.builder import ConfigurationBuilder
from kaifario.providers.memory import MemoryProvider
config = ConfigurationBuilder().add_providers(
MemoryProvider({"a": 1, "b": 2})
).build()
print(config.get_value("a", int)) # Output: 1
📦 Available Providers
Kaifario supports various configuration sources:
-
MemoryProvider— Load configuration from a Python dictionary. -
EnviromentProvider— Load values from environment variables, with support for nested configuration structures using a prefix and separator. Environment variable keys are lowercased and split by the given separator (default is"__") to create nested dictionaries. For example:export APP_DATABASE__HOST=localhost export APP_DATABASE__PORT=5432
With
EnviromentProvider(prefix="APP_"), this would produce:{ "database": { "host": "localhost", "port": "5432" } }
-
IniProvider— Load from.inifiles. -
JsonProvider— Load from JSON files. -
TomlProvider— Load from TOML files. -
YamlProvider— Load from YAML files.
Provider Merge Order
When using multiple providers, their configurations are merged into a single dictionary. Later providers override the values from earlier ones if keys conflict. This allows flexible layering of configuration sources, such as defaults overridden by environment variables.
Example with multiple providers
from kaifario.builder import ConfigurationBuilder
from kaifario.providers.env import EnviromentProvider
from kaifario.providers.json import JsonProvider
from kaifario.providers.memory import MemoryProvider
config = ConfigurationBuilder() \
.add_providers(
EnviromentProvider(prefix="APP"),
JsonProvider("config.json"),
MemoryProvider({"fallback": "value"})
) \
.build()
print(config["database"].get_value("host", str))
# or config.get_section("database").get_value("host", str)
🧹 Custom Providers
You can define your own provider by implementing the ConfigurationProvider protocol:
from typing import Protocol, Any
class ConfigurationProvider(Protocol):
def load(self) -> dict[str, Any]:
...
Example
class CustomProvider:
def load(self) -> dict[str, Any]:
return {"custom_key": "custom_value"}
Then use it just like any other provider:
config = ConfigurationBuilder().add_providers(CustomProvider()).build()
print(config.get_value("custom_key", str))
📊 Dataclass Mapping
Kaifario supports direct mapping of configuration data to Python dataclasses. Under the hood, it uses Retort, but you can also provide your own loader by implementing a custom Loader.
Usage Example
from dataclasses import dataclass
@dataclass
class DatabaseConfig:
host: str
port: int
config = ConfigurationBuilder().add_providers(...).build() # inner config data should contain {"host": ..., "port": ...}
db_config = config.get(DatabaseConfig)
# with nested data like {"database": {"host": ..., "port": ...}} you can use:
db_config = config['database'].get(DatabaseConfig)
Custom Loader Support
You can provide your own loader by implementing the following protocol:
class Loader(Protocol):
def load(self, data: dict[str, Any], model: type[T]) -> T:
...
The Configuration class accepts an optional loader argument:
🤝 Contributing
Contributions, ideas, and bug reports are welcome! Feel free to open issues or submit pull requests.
📃 License
MIT License
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 kaifario-0.3.1.tar.gz.
File metadata
- Download URL: kaifario-0.3.1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba6ae90b5174b7d30f143d90ebf408be47d332353bf5e86ce545e63e2ad47ffa
|
|
| MD5 |
65ba1b27c1fab0205787056536ca85e2
|
|
| BLAKE2b-256 |
3740003fa1e080cbf60ade0a3eb3373bb37ff33d298dd7a70b88646be234ee96
|
Provenance
The following attestation bundles were made for kaifario-0.3.1.tar.gz:
Publisher:
python-publish.yml on KurosawaAngel/kaifario
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kaifario-0.3.1.tar.gz -
Subject digest:
ba6ae90b5174b7d30f143d90ebf408be47d332353bf5e86ce545e63e2ad47ffa - Sigstore transparency entry: 200590392
- Sigstore integration time:
-
Permalink:
KurosawaAngel/kaifario@2f2160a93a54a94bcea4c47d80d268f1d529a197 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KurosawaAngel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2f2160a93a54a94bcea4c47d80d268f1d529a197 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kaifario-0.3.1-py3-none-any.whl.
File metadata
- Download URL: kaifario-0.3.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b25931357d1ee71e49d9b2a4df3771fa15289562fe85875f1eae5e1af13daa56
|
|
| MD5 |
ed4d442c7a2648961a612f4e4932dcf4
|
|
| BLAKE2b-256 |
a5f8c5e8067462da4e68e861528e702973817854a19fed0b6cd959628d2f64ed
|
Provenance
The following attestation bundles were made for kaifario-0.3.1-py3-none-any.whl:
Publisher:
python-publish.yml on KurosawaAngel/kaifario
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kaifario-0.3.1-py3-none-any.whl -
Subject digest:
b25931357d1ee71e49d9b2a4df3771fa15289562fe85875f1eae5e1af13daa56 - Sigstore transparency entry: 200590394
- Sigstore integration time:
-
Permalink:
KurosawaAngel/kaifario@2f2160a93a54a94bcea4c47d80d268f1d529a197 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KurosawaAngel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2f2160a93a54a94bcea4c47d80d268f1d529a197 -
Trigger Event:
workflow_dispatch
-
Statement type: