byoconfig


Namebyoconfig JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryA configuration class that supports plugins, multiple file formats, heirarchical configuration, and more.
upload_time2025-07-16 19:23:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords config yaml toml json plugins
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # BYOConfig

> Bring your own configuration

[![Tests Passing](https://github.com/camratchford/byoconfig/actions/workflows/lint_and_test.yml/badge.svg)](https://github.com/camratchford/byoconfig/actions/workflows/lint_and_test.yml)
[![Build](https://github.com/camratchford/byoconfig/actions/workflows/publish.yml/badge.svg)](https://github.com/camratchford/byoconfig/actions/workflows/publish.yml)
[![PyPi Version](https://img.shields.io/pypi/v/byoconfig)](https://pypi.org/project/byoconfig/)

## 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

```bash
pip install byconfig
```

## Usage


### From file

```python
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

```python

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

```python

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

```python
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

```python
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")

```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "byoconfig",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "config, yaml, toml, json, plugins",
    "author": null,
    "author_email": "Cam Ratchford <camratchford@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f6/a0/532553c074793cbbebbbc3acabc1201337b363d05a07ac4c4810bc022643/byoconfig-0.1.1.tar.gz",
    "platform": null,
    "description": "# BYOConfig\n\n> Bring your own configuration\n\n[![Tests Passing](https://github.com/camratchford/byoconfig/actions/workflows/lint_and_test.yml/badge.svg)](https://github.com/camratchford/byoconfig/actions/workflows/lint_and_test.yml)\n[![Build](https://github.com/camratchford/byoconfig/actions/workflows/publish.yml/badge.svg)](https://github.com/camratchford/byoconfig/actions/workflows/publish.yml)\n[![PyPi Version](https://img.shields.io/pypi/v/byoconfig)](https://pypi.org/project/byoconfig/)\n\n## Features\n\n- Loading/Dumping configuration data from/to:\n  - YAML\n  - TOML\n  - JSON\n- File format auto-detect and override options\n- Ability to load configuration data from environment variables\n- Allows hierarchical data to be loaded, then updated according to precedence rules.\n- Extensible via plugins, allowing your own arbitrary data sources to be merged with config file and environment data.\n- Configuration data available as class attributes (ex. `config.variable_1`)\n\n## Installing\n\n```bash\npip install byconfig\n```\n\n## Usage\n\n\n### From file\n\n```python\nfrom pathlib import Path\n\nfrom byoconfig import Config\n\n\"\"\"\n# imagining the contents of config.yaml are:\nimportant_path: path/that/must/exists\n\"\"\"\n\n# Auto-detects the file type based on file extension suffix\nconf = Config('path/to/config.yaml')\n\n# Alternatively, specify a forced_file_type argument (One of 'YAML', 'TOML', or 'JSON'\n# conf = Config(\"path/to/config\", forced_file_extension=\"YAML\")\n\ndef ensure_file(path: str):\n    Path(path).mkdir(parents=True)\n\nif __name__ == \"__main__\":\n    # The configuration variable is accessible by the instance attribute conf.important_path\n    ensure_file(conf.important_path)\n\n```\n\n### From Environment Variables\n\n```python\n\nfrom byoconfig import Config\n\nconf = Config(env_prefix=\"MY_APP\")\n\n# Imagining that you have an env var `MY_APP_var_1` set\nprint(conf.var_1)\n\n# If you want to load all of the environment variables, use the '*' wildcard as env_prefix\n\nconf2 = Config(env_prefix=\"*\")\n\nprint(conf2.PATH)\n\n```\n\n\n### From Custom plugins\n\n```python\n\nfrom byoconfig.sources import BaseVariableSource\nfrom byoconfig import Config\n\n\n# Subclass the BaseVariableSource class\nclass MyVarSource(BaseVariableSource):\n  def __init__(self, init_options):\n    # Using an imaginary function 'get_external_data' in place of something like an http request or DB query\n    self.external_data = get_external_data(init_options)\n\n    # Initializes the class attrs, making the data availalble via MyVarSource.var_name  \n    self.set_data(self.external_data)\n\n\n# Start with a config object, containing the data from any source (optional)\nmy_config = Config(\"some/file/data.json\")\n\n# Include your plugin, merging the data from MyVarSource and the config object above\n# You can pass kwargs to include as if you're passing them to MyVarSource's __init__ method.\nmy_config.include(MyVarSource, init_options=\"some data to initialize your custom data source\")\n\n```\n\n\n### Loading Arbitrary Values\n\n```python\nfrom byoconfig import Config\n\n# Via kwargs\nconf = Config(my_var=\"abc\", my_var_2=123)\n\n# Via the set_data method / data property\nconf.set_data({\"my_var\": \"abc\"})\n# Equivalent to\nconf.data = {\"my_var\": \"abc\"}\n\n```\n\n\n### Dumping Data\n\n```python\nfrom byoconfig import Config\n\nconf = Config()\n\n# We can pretend that you've loaded your configuration data in conf, and you'd like it output to a file\n# to be used later\n...\n\n# Auto-detects the file-type based on file extension suffix\nconf.dump(\"running_config.yml\")\n# Overriding the auto-detect in case you have no extension\nconf.dump(\"running_config\", forced_file_type=\"TOML\")\n\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A configuration class that supports plugins, multiple file formats, heirarchical configuration, and more.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/camratchford/byoconfig",
        "Source": "https://github.com/camratchford/byoconfig"
    },
    "split_keywords": [
        "config",
        " yaml",
        " toml",
        " json",
        " plugins"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7da6dc9d403694de1a31a827e7cc39f292f2650ba737317b30cb4a24140b2d4",
                "md5": "93c5004e397ccb835a45f489d9e558e8",
                "sha256": "342dcdf50fd59ac4cff4cedfd17fcf7dc88b629fde4ba203d4550072e8b56ce0"
            },
            "downloads": -1,
            "filename": "byoconfig-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "93c5004e397ccb835a45f489d9e558e8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 17701,
            "upload_time": "2025-07-16T19:23:46",
            "upload_time_iso_8601": "2025-07-16T19:23:46.613266Z",
            "url": "https://files.pythonhosted.org/packages/a7/da/6dc9d403694de1a31a827e7cc39f292f2650ba737317b30cb4a24140b2d4/byoconfig-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6a0532553c074793cbbebbbc3acabc1201337b363d05a07ac4c4810bc022643",
                "md5": "c1205570f10075b80f2a4d1a653449cc",
                "sha256": "430d5f08639ef4de8a1c129784c878bbb6f06327aab8a8fcd9cae19a3efe9d2f"
            },
            "downloads": -1,
            "filename": "byoconfig-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c1205570f10075b80f2a4d1a653449cc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 24150,
            "upload_time": "2025-07-16T19:23:48",
            "upload_time_iso_8601": "2025-07-16T19:23:48.143854Z",
            "url": "https://files.pythonhosted.org/packages/f6/a0/532553c074793cbbebbbc3acabc1201337b363d05a07ac4c4810bc022643/byoconfig-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-16 19:23:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "camratchford",
    "github_project": "byoconfig",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "byoconfig"
}
        
Elapsed time: 1.24051s