A dependency injection configuration class supporting multiple file formats, environment variables, AWS Secrets Manager, singletons, and more.
Project description
BYOConfig
Bring your own configuration
A dependency injection configuration class supporting multiple file formats, environment variables, AWS Secrets Manager, singletons, and more.
Features
- Loading/Dumping configuration data using:
- YAML
- TOML
- JSON
- Environment Variables
- Loading secrets from AWS Secrets Manager
- File type detection
- Imported value type conversion to
pathlib.Path,datetime.datetime,datetime.date - Filtering configuration data by key name prefix
- Filtering configuration data by a
typing.Annotatedtype.
Installing
pip install byconfig
Usage
From file
from pathlib import Path
from byoconfig import Config
"""
# path/to/config.yaml
important_path: path/that/must/exists
"""
# Auto-detects the file type based on file extension suffix, assigns all top-level configuration data as object attributes.
conf = Config(file_path='path/to/config.yaml')
# Alternatively, specify a forced_file_type argument (One of 'YAML', 'TOML', or 'JSON'
# conf = Config("path/to/config", forced_file_extension="YAML")
def ensure_file(path: str):
Path(path).mkdir(parents=True)
if __name__ == "__main__":
# The configuration variable is accessible by the instance attribute conf.important_path
ensure_file(conf.get("important_path"))
From Environment Variables
from os import environ
from byoconfig import Config
# Environment variables are always stored as strings
environ.update({"MY_APP_VAR_1": "1", "TEST_SOMETHING_SOMETHING": "2"})
conf = Config(env_prefix="MY_APP")
print(conf.get("var_1"))
# > "1"
# To run the environment loader again
conf.load_from_environment(prefix="TEST")
print(conf.get("something_something"))
# > "2"
# If you want to load all the environment variables, use the '*' wildcard as env_prefix
conf2 = Config(env_prefix="*")
print(conf2.PATH)
From AWS Secrets Manager
from byoconfig import Config
# ~/.aws/credentials (Where the values provided are not foo, bar, baz but actual authentication parameters)
# See https://boto3.amazonaws.com/v1/documentation/api/1.9.46/guide/configuration.html#configuring-credentials
"""
[default]
aws_access_key_id=foo
aws_secret_access_key=bar
aws_session_token=baz
"""
# AWS Secrets Manager secrets, using the JSON option.
# Try not to let your top-level JSON keys overlap, as the two resulting dicts will be merged.
"""
# Where the name of the secret is api_keys/important
{
"important_api_key": "B3U1+L/ZLKfFfdLf+cdx/7f9HhMjiL6meZlS11RlojQ",
}
# Where the name of the secret is api_keys/different
{
"different_api_key": "Jc6Qq37sV+3SidDmkQ42RXtq1x7qEAQUZKCVr7JzADM",
}
"""
class ImportantAPIClient:
def __init__(self, config: Config):
self.api_key = config.get("important_api_key")
class DifferentAPIClient:
def __init__(self, config: Config):
self.api_key = config.get("different_api_key")
def main():
config = Config(aws_secret_name="api_keys/important")
important_api_client = ImportantAPIClient(config)
# Should you need to load more than 1 secret, there's a method available.
config.load_from_secrets_manager("api_keys/different")
different_api_client = DifferentAPIClient(config)
Loading Arbitrary Values
from byoconfig import Config
# Via kwargs
conf = Config(my_var="abc", my_var_2=123)
# Via the set_data method / data property
conf.set({"my_var": "abc"})
# Equivalent to
conf.data = {"my_var": "abc"}
Dumping Data
An example of dumping the contents of your config to a file.
from byoconfig import Config
conf = Config()
# We can pretend that you've loaded your configuration data in conf, and you'd like it output to a file
# to be used later
...
# Auto-detects the file-type based on file extension suffix
conf.dump_to_file("running_config.yml")
# Overriding the auto-detect in case you have no extension
conf.dump_to_file("running_config", forced_type="TOML")
An example of excluding configuration data from the dump_to_file method output.
from typing import Annotated
from byoconfig import Config
# Create a subclass of Config
class MyConfig(Config):
# The type annotation must appear in the class body and must be assigned a value. For example: None or ""
not_critically_secret_data: str = "This can be exported to file"
super_secret_data: Annotated[str, "excluded"] = ""
# Initialize your instance, defining the value of your super secret data. Like an API key or something you don't want to share.
config = MyConfig(env_selected_keys=['super_secret_data'])
# The contents of the resulting file will not contain 'super_secret_data' or any data annotated with "excluded"
config.dump_to_file("my_config.json")
Filtering Data
By Key Name Prefix
We can group our configuration data by the kwargs for a function/method/class, prefixing each parameter with a name.
Using uvicorn as the prefix to supply kwargs to uvicorn.run:
import uvicorn
from byoconfig import Config
# like fastapi or starlette
from my_app.asgi import asgi_app
class AppConfig(Config):
uvicorn_port = 8889
uvicorn_host = "127.0.0.1"
uvicorn_log_level = "info"
def run_server():
config = AppConfig()
# Results in the dict: {'port': 8889, 'host': '127.0.0.1', 'log_level': 'info'}
uvicorn_kwargs = config.get_by_prefix("uvicorn", trim_prefix=True)
uvicorn.run(asgi_app, **uvicorn_kwargs)
By Annotated Type
We can group our configuration data in arbitrary categories by supplying a type annotation via typing.Annotated
Same example as before, but with annotations.
from typing import Annotated
import uvicorn
from byoconfig import Config
from my_app.asgi import asgi_app
class AppConfig(Config):
port: Annotated[int, "uvicorn"] = 8889
host: Annotated[str, "uvicorn"] = "127.0.0.1"
log_level: Annotated[str, "uvicorn"] = "info"
def run_server():
config = AppConfig()
# Results in the dict: {'port': 8889, 'host': '127.0.0.1', 'log_level': 'info'}
uvicorn_kwargs = config.get_by_annotated_type("uvicorn")
uvicorn.run(asgi_app, **uvicorn_kwargs)
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 byoconfig-1.2.3.tar.gz.
File metadata
- Download URL: byoconfig-1.2.3.tar.gz
- Upload date:
- Size: 31.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91f3212ebddfddb65319cd2fccd785deb775bbc9530b9e9262dba6c0e0ddfc3e
|
|
| MD5 |
d2a5ffca868e83b56036591a2c1d3ea4
|
|
| BLAKE2b-256 |
c625f560d1d18bbd9f1f3fb1d4fd5e9483e727958945ee65c05ff80ad56e9f77
|
Provenance
The following attestation bundles were made for byoconfig-1.2.3.tar.gz:
Publisher:
publish.yml on camratchford/byoconfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
byoconfig-1.2.3.tar.gz -
Subject digest:
91f3212ebddfddb65319cd2fccd785deb775bbc9530b9e9262dba6c0e0ddfc3e - Sigstore transparency entry: 868875480
- Sigstore integration time:
-
Permalink:
camratchford/byoconfig@80f6c3187eda8a04a34c13ae4fc4e716430e7a77 -
Branch / Tag:
refs/pull/20/merge - Owner: https://github.com/camratchford
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@80f6c3187eda8a04a34c13ae4fc4e716430e7a77 -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file byoconfig-1.2.3-py3-none-any.whl.
File metadata
- Download URL: byoconfig-1.2.3-py3-none-any.whl
- Upload date:
- Size: 24.8 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 |
3ef3741a02704fde05cca6aaa283953ee9d203fc477331c29350dd5ab805f662
|
|
| MD5 |
48b98dbe690f6dbaf9fa8d8520a8bc10
|
|
| BLAKE2b-256 |
33b39d099451b2e85893b3194a242f44147897369e693a059014f2546f66a950
|
Provenance
The following attestation bundles were made for byoconfig-1.2.3-py3-none-any.whl:
Publisher:
publish.yml on camratchford/byoconfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
byoconfig-1.2.3-py3-none-any.whl -
Subject digest:
3ef3741a02704fde05cca6aaa283953ee9d203fc477331c29350dd5ab805f662 - Sigstore transparency entry: 868875489
- Sigstore integration time:
-
Permalink:
camratchford/byoconfig@80f6c3187eda8a04a34c13ae4fc4e716430e7a77 -
Branch / Tag:
refs/pull/20/merge - Owner: https://github.com/camratchford
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@80f6c3187eda8a04a34c13ae4fc4e716430e7a77 -
Trigger Event:
pull_request
-
Statement type: