confz


Nameconfz JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/Zuehlke/ConfZ
SummaryConfZ is a configuration management library for Python based on pydantic.
upload_time2023-08-13 12:08:08
maintainer
docs_urlNone
authorZühlke
requires_python>=3.7.2,<4.0.0
licenseMIT
keywords configuration management config management
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ConfZ – Pydantic Config Management

[![test](https://github.com/Zuehlke/ConfZ/actions/workflows/test.yml/badge.svg)](https://github.com/Zuehlke/ConfZ/actions/workflows/test.yml)
[![documentation](https://readthedocs.org/projects/confz/badge/?version=latest)](https://confz.readthedocs.io/en/latest/)
[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/Zuehlke/ConfZ/actions/workflows/coverage.yml)  <!-- hard-code because can not merge if below 100 -->
[![python](https://img.shields.io/pypi/pyversions/confz)](https://pypi.org/project/confz/)
[![pypi](https://img.shields.io/pypi/v/confz)](https://pypi.org/project/confz/)

`ConfZ` is a configuration management library for Python based on [pydantic](https://pydantic-docs.helpmanual.io/).
It easily allows you to

* load your configuration from config files, environment variables, command line arguments and more
* transform the loaded data into a desired format and validate it
* access the results as Python dataclass-like objects with full IDE support

It furthermore supports you in common use cases like:

* Multiple environments
* Singleton with lazy loading
* Config changes for unit tests
* Custom config sources

**UPDATE**: ConfZ 2 is here, with support for pydantic 2 and improved naming conventions.
Check out the [migration guide](https://confz.readthedocs.io/en/latest/migration_guide.html). 

## :package: Installation

`ConfZ` is on [PyPI](https://pypi.org/project/confz/) and can be installed with pip:

```shell
pip install confz
```


## :rocket: Quick Start

The first step of using `ConfZ` is to declare your config classes and sources, for example in `config.py`:

```python
from confz import BaseConfig, FileSource
from pydantic import SecretStr, AnyUrl

class DBConfig(BaseConfig):
    user: str
    password: SecretStr

class APIConfig(BaseConfig):
    host: AnyUrl
    port: int
    db: DBConfig

    CONFIG_SOURCES = FileSource(file='/path/to/config.yml')
```

Thanks to [pydantic](https://pydantic-docs.helpmanual.io/), you can use a wide variety of
[field types](https://pydantic-docs.helpmanual.io/usage/types/) and
[validators](https://pydantic-docs.helpmanual.io/usage/validators/).

From now on, in any other file, you can access your config directly:

```python
from config import APIConfig

print(f"Serving API at {APIConfig().host}, port {APIConfig().port}.")
```

As can be seen, the config does neither have to be loaded explicitly, nor instantiated globally. `ConfZ` automatically
loads your config as defined in `CONFIG_SOURCES` the first time you access it. Thanks to its singleton mechanism, this
happens the first time only, afterwards you get back a cached,
[immutable](https://pydantic-docs.helpmanual.io/usage/models/#faux-immutability) instance, behaving like any other
_pydantic_ instance.

```python
assert APIConfig() is APIConfig()   # true because of singleton mechanism
APIConfig().port = 1234             # raises an error because of immutability
APIConfig().model_dump()            # call pydantic's method to get a dict representation
```

**Note:** While the implicit and hidden loading of your config might be surprising and feel a bit like Python magic at
first, it allows you to reduce a lot of boilerplate. Instead of having to load your config explicitly and then passing
it down to all code layers that need it, you can directly access it from anywhere by just importing your config class
and accessing for example `APIConfig().db.user` directly.

### More Config Sources

`ConfZ` is highly flexible in defining the source of your config. Do you have multiple environments? No Problem:

```python
from confz import BaseConfig, FileSource

class MyConfig(BaseConfig):
    ...
    CONFIG_SOURCES = FileSource(
        folder='/path/to/config/folder',
        file_from_env='ENVIRONMENT'
    )
```

Your config file can now be defined in the environment variable `ENVIRONMENT` and is relative to `folder`.

You can also provide a list as config source and read for example from environment variables including a .env file and
from command line arguments:

```python
from confz import BaseConfig, EnvSource, CLArgSource

class MyConfig(BaseConfig):
    ...
    CONFIG_SOURCES = [
        EnvSource(allow_all=True, file=".env.local"),
        CLArgSource(prefix='conf_')
    ]
```

`ConfZ` now tries to populate your config either from environment variables having the same name as your attributes or
by reading command line arguments that start with `conf_`. Recursive models are supported too, for example if you want
to control the user-name in the API above, you can either set the environment variable `DB.USER` or pass the command
line argument `--conf_db.user`.

### Explicit Loading

In some scenarios, the config should not be a global singleton, but loaded explicitly and passed around locally.
Instead of defining `CONFIG_SOURCES` as class variable, the sources can also be defined in the constructor directly:

```python
from confz import BaseConfig, FileSource, EnvSource

class MyConfig(BaseConfig):
    number: int
    text: str

config1 = MyConfig(config_sources=FileSource(file='/path/to/config.yml'))
config2 = MyConfig(config_sources=EnvSource(prefix='CONF_', allow=['text']), number=1)
config3 = MyConfig(number=1, text='hello world')
```

As can be seen, additional keyword-arguments can be provided as well.

**Note:** If neither class variable `CONFIG_SOURCES` nor constructor argument `config_sources` is provided, `BaseConfig`
behaves like a regular _pydantic_ class.

### Change Config Values

In some scenarios, you might want to change your config values, for example within a unit test. However, if you set the
`CONFIG_SOURCES` class variable, this is not directly possible. To overcome this, every config class provides a context
manager to temporarily change your config:

```python
from confz import BaseConfig, FileSource, DataSource

class MyConfig(BaseConfig):
    number: int
    CONFIG_SOURCES = FileSource(file="/path/to/config.yml")

print(MyConfig().number)                            # will print the value from the config-file

new_source = DataSource(data={'number': 42})
with MyConfig.change_config_sources(new_source):
    print(MyConfig().number)                        # will print '42'

print(MyConfig().number)                            # will print the value from the config-file again
```

### Early Validation

By default, your config gets loaded the first time you instantiate the class, e.g. with `MyConfig().attribute`. This
prevents side effects like loading a file while you import your config classes. If the config class cannot populate all
mandatory fields in the correct format, _pydantic_ will raise an error at this point. To make sure this does not happen
in an inconvenient moment, you can also instruct `ConfZ` to load all configs beforehand:

```python
from confz import validate_all_configs

if __name__ == '__main__':
    validate_all_configs()
    # your application code
```

The function `validate_all_configs` will instantiate all config classes defined in your code at any (reachable)
location that have `CONFIG_SOURCES` set.


## :book: Documentation

Now you've seen the two ways how `ConfZ` can be used: With class variable config sources, unlocking a singleton with
lazy loading, or with keyword argument config sources, allowing to directly load your config values. In both cases,
defining your config sources from files, command line arguments and environment variables is highly flexible
(and also extendable, by the way), while _pydantic_ still makes sure that everything matches your expectations in the
end. You've also seen how to temporarily change your config for example in unit tests and how to validate
your singleton config classes early in the code already.

The full documentation of `ConfZ`'s features can be found at [readthedocs](https://confz.readthedocs.io/).


## :information_source: About

`ConfZ` was programmed and will be maintained by [Zühlke](https://www.zuehlke.com).
The first version was realized by [Silvan](https://github.com/silvanmelchior).
Special thanks to Iwan with his [ConfMe](https://github.com/iwanbolzern/ConfMe), which inspired this project.

Want to contribute to `ConfZ`? Check out the contribution [instruction & guidelines](CONTRIBUTING.md).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Zuehlke/ConfZ",
    "name": "confz",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7.2,<4.0.0",
    "maintainer_email": "",
    "keywords": "Configuration Management,Config Management",
    "author": "Z\u00fchlke",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/20/90/5d7e6b2555f369d67a999bc844d3fb6da3c46173542b57eebd980d4fdf55/confz-2.0.1.tar.gz",
    "platform": null,
    "description": "# ConfZ \u2013 Pydantic Config Management\n\n[![test](https://github.com/Zuehlke/ConfZ/actions/workflows/test.yml/badge.svg)](https://github.com/Zuehlke/ConfZ/actions/workflows/test.yml)\n[![documentation](https://readthedocs.org/projects/confz/badge/?version=latest)](https://confz.readthedocs.io/en/latest/)\n[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/Zuehlke/ConfZ/actions/workflows/coverage.yml)  <!-- hard-code because can not merge if below 100 -->\n[![python](https://img.shields.io/pypi/pyversions/confz)](https://pypi.org/project/confz/)\n[![pypi](https://img.shields.io/pypi/v/confz)](https://pypi.org/project/confz/)\n\n`ConfZ` is a configuration management library for Python based on [pydantic](https://pydantic-docs.helpmanual.io/).\nIt easily allows you to\n\n* load your configuration from config files, environment variables, command line arguments and more\n* transform the loaded data into a desired format and validate it\n* access the results as Python dataclass-like objects with full IDE support\n\nIt furthermore supports you in common use cases like:\n\n* Multiple environments\n* Singleton with lazy loading\n* Config changes for unit tests\n* Custom config sources\n\n**UPDATE**: ConfZ 2 is here, with support for pydantic 2 and improved naming conventions.\nCheck out the [migration guide](https://confz.readthedocs.io/en/latest/migration_guide.html). \n\n## :package: Installation\n\n`ConfZ` is on [PyPI](https://pypi.org/project/confz/) and can be installed with pip:\n\n```shell\npip install confz\n```\n\n\n## :rocket: Quick Start\n\nThe first step of using `ConfZ` is to declare your config classes and sources, for example in `config.py`:\n\n```python\nfrom confz import BaseConfig, FileSource\nfrom pydantic import SecretStr, AnyUrl\n\nclass DBConfig(BaseConfig):\n    user: str\n    password: SecretStr\n\nclass APIConfig(BaseConfig):\n    host: AnyUrl\n    port: int\n    db: DBConfig\n\n    CONFIG_SOURCES = FileSource(file='/path/to/config.yml')\n```\n\nThanks to [pydantic](https://pydantic-docs.helpmanual.io/), you can use a wide variety of\n[field types](https://pydantic-docs.helpmanual.io/usage/types/) and\n[validators](https://pydantic-docs.helpmanual.io/usage/validators/).\n\nFrom now on, in any other file, you can access your config directly:\n\n```python\nfrom config import APIConfig\n\nprint(f\"Serving API at {APIConfig().host}, port {APIConfig().port}.\")\n```\n\nAs can be seen, the config does neither have to be loaded explicitly, nor instantiated globally. `ConfZ` automatically\nloads your config as defined in `CONFIG_SOURCES` the first time you access it. Thanks to its singleton mechanism, this\nhappens the first time only, afterwards you get back a cached,\n[immutable](https://pydantic-docs.helpmanual.io/usage/models/#faux-immutability) instance, behaving like any other\n_pydantic_ instance.\n\n```python\nassert APIConfig() is APIConfig()   # true because of singleton mechanism\nAPIConfig().port = 1234             # raises an error because of immutability\nAPIConfig().model_dump()            # call pydantic's method to get a dict representation\n```\n\n**Note:** While the implicit and hidden loading of your config might be surprising and feel a bit like Python magic at\nfirst, it allows you to reduce a lot of boilerplate. Instead of having to load your config explicitly and then passing\nit down to all code layers that need it, you can directly access it from anywhere by just importing your config class\nand accessing for example `APIConfig().db.user` directly.\n\n### More Config Sources\n\n`ConfZ` is highly flexible in defining the source of your config. Do you have multiple environments? No Problem:\n\n```python\nfrom confz import BaseConfig, FileSource\n\nclass MyConfig(BaseConfig):\n    ...\n    CONFIG_SOURCES = FileSource(\n        folder='/path/to/config/folder',\n        file_from_env='ENVIRONMENT'\n    )\n```\n\nYour config file can now be defined in the environment variable `ENVIRONMENT` and is relative to `folder`.\n\nYou can also provide a list as config source and read for example from environment variables including a .env file and\nfrom command line arguments:\n\n```python\nfrom confz import BaseConfig, EnvSource, CLArgSource\n\nclass MyConfig(BaseConfig):\n    ...\n    CONFIG_SOURCES = [\n        EnvSource(allow_all=True, file=\".env.local\"),\n        CLArgSource(prefix='conf_')\n    ]\n```\n\n`ConfZ` now tries to populate your config either from environment variables having the same name as your attributes or\nby reading command line arguments that start with `conf_`. Recursive models are supported too, for example if you want\nto control the user-name in the API above, you can either set the environment variable `DB.USER` or pass the command\nline argument `--conf_db.user`.\n\n### Explicit Loading\n\nIn some scenarios, the config should not be a global singleton, but loaded explicitly and passed around locally.\nInstead of defining `CONFIG_SOURCES` as class variable, the sources can also be defined in the constructor directly:\n\n```python\nfrom confz import BaseConfig, FileSource, EnvSource\n\nclass MyConfig(BaseConfig):\n    number: int\n    text: str\n\nconfig1 = MyConfig(config_sources=FileSource(file='/path/to/config.yml'))\nconfig2 = MyConfig(config_sources=EnvSource(prefix='CONF_', allow=['text']), number=1)\nconfig3 = MyConfig(number=1, text='hello world')\n```\n\nAs can be seen, additional keyword-arguments can be provided as well.\n\n**Note:** If neither class variable `CONFIG_SOURCES` nor constructor argument `config_sources` is provided, `BaseConfig`\nbehaves like a regular _pydantic_ class.\n\n### Change Config Values\n\nIn some scenarios, you might want to change your config values, for example within a unit test. However, if you set the\n`CONFIG_SOURCES` class variable, this is not directly possible. To overcome this, every config class provides a context\nmanager to temporarily change your config:\n\n```python\nfrom confz import BaseConfig, FileSource, DataSource\n\nclass MyConfig(BaseConfig):\n    number: int\n    CONFIG_SOURCES = FileSource(file=\"/path/to/config.yml\")\n\nprint(MyConfig().number)                            # will print the value from the config-file\n\nnew_source = DataSource(data={'number': 42})\nwith MyConfig.change_config_sources(new_source):\n    print(MyConfig().number)                        # will print '42'\n\nprint(MyConfig().number)                            # will print the value from the config-file again\n```\n\n### Early Validation\n\nBy default, your config gets loaded the first time you instantiate the class, e.g. with `MyConfig().attribute`. This\nprevents side effects like loading a file while you import your config classes. If the config class cannot populate all\nmandatory fields in the correct format, _pydantic_ will raise an error at this point. To make sure this does not happen\nin an inconvenient moment, you can also instruct `ConfZ` to load all configs beforehand:\n\n```python\nfrom confz import validate_all_configs\n\nif __name__ == '__main__':\n    validate_all_configs()\n    # your application code\n```\n\nThe function `validate_all_configs` will instantiate all config classes defined in your code at any (reachable)\nlocation that have `CONFIG_SOURCES` set.\n\n\n## :book: Documentation\n\nNow you've seen the two ways how `ConfZ` can be used: With class variable config sources, unlocking a singleton with\nlazy loading, or with keyword argument config sources, allowing to directly load your config values. In both cases,\ndefining your config sources from files, command line arguments and environment variables is highly flexible\n(and also extendable, by the way), while _pydantic_ still makes sure that everything matches your expectations in the\nend. You've also seen how to temporarily change your config for example in unit tests and how to validate\nyour singleton config classes early in the code already.\n\nThe full documentation of `ConfZ`'s features can be found at [readthedocs](https://confz.readthedocs.io/).\n\n\n## :information_source: About\n\n`ConfZ` was programmed and will be maintained by [Z\u00fchlke](https://www.zuehlke.com).\nThe first version was realized by [Silvan](https://github.com/silvanmelchior).\nSpecial thanks to Iwan with his [ConfMe](https://github.com/iwanbolzern/ConfMe), which inspired this project.\n\nWant to contribute to `ConfZ`? Check out the contribution [instruction & guidelines](CONTRIBUTING.md).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "ConfZ is a configuration management library for Python based on pydantic.",
    "version": "2.0.1",
    "project_urls": {
        "Documentation": "https://confz.readthedocs.io",
        "Homepage": "https://github.com/Zuehlke/ConfZ",
        "Repository": "https://github.com/Zuehlke/ConfZ"
    },
    "split_keywords": [
        "configuration management",
        "config management"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "782c000d7673e3cfc93b42002d16287240ed1378d13bc0afb11f5a17cd028bc4",
                "md5": "554811c137f65033743ae04da1591bec",
                "sha256": "271dfbc7343dafda414f731c4931ba91bc62a448e4a3b2fdf2fb7334ffac1d36"
            },
            "downloads": -1,
            "filename": "confz-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "554811c137f65033743ae04da1591bec",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.2,<4.0.0",
            "size": 16455,
            "upload_time": "2023-08-13T12:08:06",
            "upload_time_iso_8601": "2023-08-13T12:08:06.842202Z",
            "url": "https://files.pythonhosted.org/packages/78/2c/000d7673e3cfc93b42002d16287240ed1378d13bc0afb11f5a17cd028bc4/confz-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "20905d7e6b2555f369d67a999bc844d3fb6da3c46173542b57eebd980d4fdf55",
                "md5": "8623e08cd20fda489bf057f2cc9f6ec1",
                "sha256": "c2d7af28224d8e70c72f66c4ea7ca0069abdc73dda11732d74f2be7dbe8641a0"
            },
            "downloads": -1,
            "filename": "confz-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "8623e08cd20fda489bf057f2cc9f6ec1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.2,<4.0.0",
            "size": 15631,
            "upload_time": "2023-08-13T12:08:08",
            "upload_time_iso_8601": "2023-08-13T12:08:08.334484Z",
            "url": "https://files.pythonhosted.org/packages/20/90/5d7e6b2555f369d67a999bc844d3fb6da3c46173542b57eebd980d4fdf55/confz-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-13 12:08:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Zuehlke",
    "github_project": "ConfZ",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "confz"
}
        
Elapsed time: 0.94387s