PyConfita: Confita-like library for Python
Project description
PyConfita: Confita-like for Python
Library that ease reading a value from multiple key-value stores/backends with ordered evaluation.
Disclaimer
Free implementation of the GO library Confita.
Features
- Backends/stores supported:
- Environment variables (
EnvBackend
); - File (YAML format) (
FileBackend
); - Python dictionary object (
DictBackend
); - Vault key-value store (
VaultBackend
);
- Environment variables (
- Backends evaluation order: directly set by the order of backends in
Confita.backends
list. The last notNone
evaluated value is returned; - Explicit type conversion supported for
str, bool, int, float
; - Case sensitivity option: option to read key with casing variations (uppercased, lowercased).
Quickstart
from pyconfita import (
LoggingInterface,
Confita,
EnvBackend,
DictBackend,
VaultBackend,
FileBackend
)
dumb_logger = LoggingInterface()
c = Confita(
logger=dumb_logger,
backends=[
FileBackend("/abs/path/vars.yaml"),
DictBackend({"FOO": "bar"}),
VaultBackend(dumb_logger, default_key_path=f"path1"),
EnvBackend(),
],
)
v = c.get("FOO")
# At least DictBackend should evaluate it as not None
assert v is not None
# Type should be `str`
assert isinstance(v, str)
Evaluation order
Evaluation is performed in order of the list of backends. Next backend has priority over previous backend to set the value.
import os
from pyconfita import (
LoggingInterface,
Confita,
EnvBackend,
DictBackend
)
dumb_logger = LoggingInterface()
os.environ.setdefault("KEY", "VALUE_FROM_ENV")
c = Confita(
logger=dumb_logger,
backends=[
DictBackend({
"KEY": "VALUE",
"BOOL_1": "false",
"BOOL_2": "true"
}),
EnvBackend(),
],
)
assert c.get("KEY") == "VALUE_FROM_ENV" # Environment backend overrides previous backends' values
assert c.get("BOOL_1") == "false" # No implicit type conversion
assert c.get("BOOL_2", v_type=bool) # Explicit type conversion requested
# Reverse evaluation order by reversing list of backends
c = Confita(
logger=dumb_logger,
backends=[
EnvBackend(),
DictBackend({"KEY": "VALUE"}),
],
)
assert c.get("KEY") == "VALUE" # Dict backend overrides previous backends' values
Explicit type conversion
Type conversion must be explicit. Only str, bool, int, float
types are supported.
Default type is str
.
from pyconfita import (
LoggingInterface,
Confita,
DictBackend
)
dumb_logger = LoggingInterface()
c = Confita(
logger=dumb_logger,
backends=[
DictBackend({
"BOOL_1": "false",
"BOOL_2": "true"
}),
],
)
assert c.get("BOOL_1") == "false" # No implicit type conversion
assert c.get("BOOL_2", v_type=bool) # Explicit type conversion requested
Case sensitivity
from pyconfita import (
LoggingInterface,
Confita,
DictBackend
)
dumb_logger = LoggingInterface()
# Case sensitivity is enabled by default
c = Confita(
logger=dumb_logger,
backends=[DictBackend({"KEY": "VALUE"})],
)
assert c.get("KEY") == "VALUE"
assert c.get("key") == None
# Disable case sensitivity
c = Confita(
logger=dumb_logger,
backends=[DictBackend({"KEY": "VALUE"})],
case_sensitive=False
)
assert c.get("KEY") == "VALUE"
assert c.get("key") == "VALUE"
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
pyconfita-1.0.6.tar.gz
(8.8 kB
view hashes)
Built Distribution
Close
Hashes for pyconfita-1.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b17f5190c5abb8e43aaa29f4f7919fcfa02a92c41731c5dcf16dbadc28eaa7f |
|
MD5 | 4f51621e57a09f356446f37cab3d5c11 |
|
BLAKE2b-256 | 2506cc8b2534912d9b40f202ff2d8eae68c5f0bb3b4627f76f8ff72804c33ace |