Skip to main content

A configuration class that supports plugins, multiple file formats, heirarchical configuration, and more.

Project description

BYOConfig

Bring your own configuration

Tests Passing

Build

PyPi Version

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_data(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_data({"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("running_config.yml")
# Overriding the auto-detect in case you have no extension
conf.dump("running_config", forced_file_type="TOML")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

byoconfig-0.0.5.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

byoconfig-0.0.5-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file byoconfig-0.0.5.tar.gz.

File metadata

  • Download URL: byoconfig-0.0.5.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for byoconfig-0.0.5.tar.gz
Algorithm Hash digest
SHA256 8512c548b2fda4d2a18c3d8689bde61bd7e21dcccabdd1699e3fd94da81c40d2
MD5 1a079d8adfef515de901ff38a9266c6d
BLAKE2b-256 478c1ae18ea52571221d7a4c8a4aa015ca5082d34e5fb5d2c5e4378bc08cd740

See more details on using hashes here.

Provenance

The following attestation bundles were made for byoconfig-0.0.5.tar.gz:

Publisher: publish.yml on camratchford/byoconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file byoconfig-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: byoconfig-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for byoconfig-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 91d29d1c4803e9318e3d47ddae1d529924e9262abccb8b3111953deecb0b3542
MD5 c150ba7086dc0eac7da8ba735426d929
BLAKE2b-256 3d524f5d44b479914df7891cf336dce392a367845fb51d785f57fde26a353df8

See more details on using hashes here.

Provenance

The following attestation bundles were made for byoconfig-0.0.5-py3-none-any.whl:

Publisher: publish.yml on camratchford/byoconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page