A configuration class that supports plugins, multiple file formats, heirarchical configuration, and more.
Project description
BYOConfig
Bring your own configuration
Features
- Loading/Dumping configuration data from/to:
- YAML
- TOML
- JSON
- File format auto-detect and override options
- Ability to load configuration data from environment variables
- Allows hierarchical data to be loaded, then updated according to precedence rules.
- Extensible via plugins, allowing your own arbitrary data sources to be merged with config file and environment data.
- Configuration data available as class attributes (ex.
config.variable_1)
Installing
pip install byconfig
Usage
From file
from pathlib import Path
from byoconfig import Config
"""
# imagining the contents of config.yaml are:
important_path: path/that/must/exists
"""
# Auto-detects the file type based on file extension suffix
conf = Config('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.important_path)
From Environment Variables
from byoconfig import Config
conf = Config(env_prefix="MY_APP")
# Imagining that you have an env var `MY_APP_var_1` set
print(conf.var_1)
# If you want to load all of the environment variables, use the '*' wildcard as env_prefix
conf2 = Config(env_prefix="*")
print(conf2.PATH)
From Custom plugins
from byoconfig.sources import BaseVariableSource
from byoconfig import Config
# Subclass the BaseVariableSource class
class MyVarSource(BaseVariableSource):
def __init__(self, init_options):
# Using an imaginary function 'get_external_data' in place of something like an http request or DB query
self.external_data = get_external_data(init_options)
# Initializes the class attrs, making the data availalble via MyVarSource.var_name
self.set(self.external_data)
# Start with a config object, containing the data from any source (optional)
my_config = Config("some/file/data.json")
# Include your plugin, merging the data from MyVarSource and the config object above
# You can pass kwargs to include as if you're passing them to MyVarSource's __init__ method.
my_config.include(MyVarSource, init_options="some data to initialize your custom data source")
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
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_file_type="TOML")
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.1.0.tar.gz.
File metadata
- Download URL: byoconfig-1.1.0.tar.gz
- Upload date:
- Size: 30.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64d4c8c4827da2b5ac1d7ee1d873f5fbdb65cba48dab38c8b1317b4caecfd0e7
|
|
| MD5 |
3b60fe1edde5b1ea0677ee90552d0692
|
|
| BLAKE2b-256 |
0472ed75edf0732c8efc4ac883849c096c546d4123fa75bdb9a3a5b469553021
|
Provenance
The following attestation bundles were made for byoconfig-1.1.0.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.1.0.tar.gz -
Subject digest:
64d4c8c4827da2b5ac1d7ee1d873f5fbdb65cba48dab38c8b1317b4caecfd0e7 - Sigstore transparency entry: 813336914
- Sigstore integration time:
-
Permalink:
camratchford/byoconfig@e715b2621b49a3151efaf61885e94cc00b6079f2 -
Branch / Tag:
refs/pull/12/merge - Owner: https://github.com/camratchford
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e715b2621b49a3151efaf61885e94cc00b6079f2 -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file byoconfig-1.1.0-py3-none-any.whl.
File metadata
- Download URL: byoconfig-1.1.0-py3-none-any.whl
- Upload date:
- Size: 23.2 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 |
43f4d8e3cabe094b33481e13e439561e4a0f1b2ee3ebd4028b7fdbf0b9d91288
|
|
| MD5 |
563363a53a4341b5d75d23990c1de54e
|
|
| BLAKE2b-256 |
bb8da83b0eb85aa708a6a735df671689bb4d6960014c67302b13f5cffe89578c
|
Provenance
The following attestation bundles were made for byoconfig-1.1.0-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.1.0-py3-none-any.whl -
Subject digest:
43f4d8e3cabe094b33481e13e439561e4a0f1b2ee3ebd4028b7fdbf0b9d91288 - Sigstore transparency entry: 813336917
- Sigstore integration time:
-
Permalink:
camratchford/byoconfig@e715b2621b49a3151efaf61885e94cc00b6079f2 -
Branch / Tag:
refs/pull/12/merge - Owner: https://github.com/camratchford
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e715b2621b49a3151efaf61885e94cc00b6079f2 -
Trigger Event:
pull_request
-
Statement type: