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.1.0.tar.gz (20.3 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.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for byoconfig-0.1.0.tar.gz
Algorithm Hash digest
SHA256 90027fc88a0851f39e47ed37fda8def8c13940de8b1e5fba4b69a6275d2b46ad
MD5 661877fdbe68707672db72dfeec8fa15
BLAKE2b-256 63d6d2e11830a7d865ed0cdfabe34e276a42ed2e9ff6d3ec02ae8651fa258dac

See more details on using hashes here.

Provenance

The following attestation bundles were made for byoconfig-0.1.0.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: byoconfig-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bfd94554a7782d52459bedf8208250c719714a3ee26d83167feb9f6bca7bec9
MD5 3809e44ab2394ef82b0437ce9710ee9a
BLAKE2b-256 d601e2425a3b10c838a279c8f173ba7206d4b1dcf77d3ebe2dcfd83a61a2b5da

See more details on using hashes here.

Provenance

The following attestation bundles were made for byoconfig-0.1.0-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