container-app-conf


Namecontainer-app-conf JSON
Version 5.3.0 PyPI version JSON
download
home_pagehttps://github.com/markusressel/container-app-conf
SummaryConvenient configuration of containerized applications
upload_time2024-02-29 17:53:07
maintainer
docs_urlNone
authorMarkus Ressel
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # container-app-conf [![Contributors](https://img.shields.io/github/contributors/markusressel/container-app-conf.svg)](https://github.com/markusressel/container-app-conf/graphs/contributors) [![MIT License](https://img.shields.io/github/license/markusressel/container-app-conf.svg)](/LICENSE) [![Code Climate](https://codeclimate.com/github/markusressel/container-app-conf.svg)](https://codeclimate.com/github/markusressel/container-app-conf) ![Code Size](https://img.shields.io/github/languages/code-size/markusressel/container-app-conf.svg) ![https://badge.fury.io/py/container-app-conf](https://badge.fury.io/py/container-app-conf.svg) [![Build Status](https://travis-ci.org/markusressel/container-app-conf.svg?branch=master)](https://travis-ci.org/markusressel/container-app-conf)

**container-app-conf** is a library to easily read application configuration values
from multiple sources (YAML, env) while providing type validation.

The initial purpose of this library was to have an easy way to configure
an application running inside of a container using environment variables 
(Docker in this case) and still provide the possibility to use a more simple 
form of configuration like a YAML file.

**container-app-conf is used by**
* [python-n26](https://github.com/femueller/python-n26)
* [InfiniteWisdom](https://github.com/ekeih/InfiniteWisdom)
* [DeineMudda](https://github.com/markusressel/DeineMudda)
* [py-image-dedup](https://github.com/markusressel/py-image-dedup)

and hopefully many others :)

# How to use


## Install dependency

```shell
pip install container-app-conf
```

## Extend `ConfigBase` base

Create a custom configuration class and define your config entries:

```python
from container_app_conf import ConfigBase
from container_app_conf.entry.string import StringConfigEntry

class AppConfig(ConfigBase):

    MY_CONFIG = StringConfigEntry(
        description="This is just a demo text config entry",
        example="example",
        key_path=[
            "my_app",
            "example"
        ],
        required=True)
```

## Use configuration values

```python
config = AppConfig()

value = config.MY_CONFIG.value
```

## Print current config

Oftentimes it can be useful to print the current configuration of an
application. To do this you can use

```python
config = AppConfig()
config.print()
```

which will result in an output similar to this:

```text
test->bool: _REDACTED_
test->this->date->is->nested->deep: 2019-10-22T04:21:02.316907
test->this->is->a->range: [0..100]
test->this->is->a->list: None
test->this->timediff->is->in->this->branch: 0:00:10
test->directory: None
test->file: None
test->float: 1.23
test->int: 100
test->regex: ^[a-zA-Z0-9]$
test->string: default value
secret->list: _REDACTED_
secret->regex: _REDACTED_
```

If you don't like the style you can specify a custom `ConfigFormatter`
like this:

```python
from container_app_conf.formatter.toml import TomlFormatter
config = AppConfig()
config.print(TomlFormatter())
```

Which would output the same config like this:

```text
[test]
bool = "_REDACTED_"
float = 1.23
int = 100
regex = "^[a-zA-Z0-9]$"
string = "default value"

[secret]
list = "_REDACTED_"
regex = "_REDACTED_"

[test.this.is.a]
range = "[0..100]"

[test.this.date.is.nested]
deep = "2019-10-22T04:26:10.654541"

[test.this.timediff.is.in.this]
branch = "0:00:10"
```

## Generate reference config

You can generate a reference configuration from a config object.
This reference contains **all** available configuration options. 
If a **default** was specified for an entry it will be used, 
otherwise the **example** value.

```python
from container_app_conf.util import generate_reference_config
config = AppConfig()
reference_config = generate_reference_config(config._config_entries.values())
```

This will return a dictionary representing the config entry tree.
You can also specify a formatter and write a reference config to a 
file using:

```python
from container_app_conf.util import write_reference
from container_app_conf.formatter.yaml import YamlFormatter
config = AppConfig()
write_reference(config, "/home/markus/.config/example.yaml", YamlFormatter())
```

If the generated reference contains values that do not make sense 
because of application constraints, specify your own **example** 
or better yet **default** value using the respective config entry 
constructor parameter.

## Config Types

| Name                     | Description                              | Type     |
|--------------------------|------------------------------------------|----------|
| `BoolConfigEntry`        | Parses `bool`, `int` (`0` and `1`) and `str` values (`yes`, `no` etc.) to a boolean value | `bool` |
| `IntConfigEntry`         | Parses input to an integer | `int` |
| `FloatConfigEntry`       | Parses input to a floating number | `float` |
| `RangeConfigEntry`       | Parses input to a range (see [py-range-parse](https://github.com/markusressel/py-range-parse)) | `Range` |
| `StringConfigEntry`      | Takes the raw string input | `str` |
| `RegexConfigEntry`       | Parses and compiles regular expressions | `re.pattern` |
| `DateConfigEntry`        | Parses various datetime formats (see [python-dateutil](https://github.com/dateutil/dateutil/)) | `datetime` |
| `TimeDeltaConfigEntry`   | Parses various timedelta formats (see [pytimeparse](https://github.com/wroberts/pytimeparse)) | `timedelta` |
| `FileConfigEntry`        | Parses a file path | `Path` |
| `DirectoryConfigEntry`   | Parses a directory path | `Path` |
| `DictConfigEntry`        | Parses a dictionary | `dict` |
| `ListConfigEntry`        | Parses a comma separated string to a list of items specified in another `ConfigEntry` (in yaml it can also be specified as a yaml list) | `[]` |

If none of the existing types suit your needs you can easily create your 
own by extending the `ConfigEntry` base class.

## Default Values

A default value can be specified for every `ConfigEntry` by using the
`default` constructor parameter.

## Required values

By default config entries with a default different from `None` are 
required. A `None` value is only allowed for an entry if it has no 
default (or it is set to `None` explicitly).

For required entries it is not possible to set its value `None` even 
after initial parsing. Omitting a value for this entry in all data 
sources will result in an exception.

If an entry requires a value and has no default, set the `required`
constructor parameter to `True`.

If you want to allow setting a `None` value even if the default value 
is **not** `None`, you have to explicitly set `required=False`.

## Secret values

If your config contains secret values like passwords you can mark them
as such using the `secret=True` constructor parameter. That way their 
value will be redacted when [printing the current configuration](#print-current-config).

## Data sources

**container-app-conf** supports the simultaneous use of multiple data 
sources to determine configuration values. The following 
implementations are available:

| Name                     | Description                              |
|--------------------------|------------------------------------------|
| `EnvSource`              | Reads environment variables |
| `YamlSource`             | Parses `YAML` files |
| `TomlSource`             | Parses `TOML` files |
| `JsonSource`             | Parses `JSON` files |

### EnvSource

#### ENV Key

Since you only specify the key path of a config entry the ENV
key is generated automatically by concatenating all key path items 
using an underscore, converting to uppercase and replacing any remaining
hyphens also with an underscore:

```python
key_path = ["my_app", "my-example"]
```

would yield `MY_APP_MY_EXAMPLE`.

### Filesystem Source

Multiple data sources using the filesystem are available:

* YamlSource
* TomlSource
* JsonSource

#### File paths

By default config files are searched for in multiple 
directories that are commonly used for configuration files which include:

- `./`
- `~/.config/`
- `~/`

This can be customized using the `path` constructor parameter: 

```python
from container_app_conf.source.yaml_source import YamlSource
yaml_source = YamlSource(file_name="myapp", path=["/my/path", "/my/other/path"])
```

## Singleton

By default every `Config` subclass instance will behave like a 
singleton. This means if you change the config value in one instance it 
will also affect all other instances of the same `__class__`.

To be able to create multiple instances of a config that are independent 
of one another this behaviour can be disabled using the `singleton` 
constructor parameter:

```python
config1 = AppConfig(singleton=False)
config2 = AppConfig(singleton=False)
```

# Contributing

GitHub is for social coding: if you want to write code, I encourage contributions through pull requests from forks
of this repository. Create GitHub tickets for bugs and new features and comment on the ones that you are interested in.

# License
```text
container-app-conf
Copyright (c) 2019 Markus Ressel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/markusressel/container-app-conf",
    "name": "container-app-conf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Markus Ressel",
    "author_email": "mail@markusressel.de",
    "download_url": "https://files.pythonhosted.org/packages/c8/d7/e470804dc34a184ca58e8eda266c03abc7e640202da8c4ff2f0acb585f01/container_app_conf-5.3.0.tar.gz",
    "platform": null,
    "description": "# container-app-conf [![Contributors](https://img.shields.io/github/contributors/markusressel/container-app-conf.svg)](https://github.com/markusressel/container-app-conf/graphs/contributors) [![MIT License](https://img.shields.io/github/license/markusressel/container-app-conf.svg)](/LICENSE) [![Code Climate](https://codeclimate.com/github/markusressel/container-app-conf.svg)](https://codeclimate.com/github/markusressel/container-app-conf) ![Code Size](https://img.shields.io/github/languages/code-size/markusressel/container-app-conf.svg) ![https://badge.fury.io/py/container-app-conf](https://badge.fury.io/py/container-app-conf.svg) [![Build Status](https://travis-ci.org/markusressel/container-app-conf.svg?branch=master)](https://travis-ci.org/markusressel/container-app-conf)\n\n**container-app-conf** is a library to easily read application configuration values\nfrom multiple sources (YAML, env) while providing type validation.\n\nThe initial purpose of this library was to have an easy way to configure\nan application running inside of a container using environment variables \n(Docker in this case) and still provide the possibility to use a more simple \nform of configuration like a YAML file.\n\n**container-app-conf is used by**\n* [python-n26](https://github.com/femueller/python-n26)\n* [InfiniteWisdom](https://github.com/ekeih/InfiniteWisdom)\n* [DeineMudda](https://github.com/markusressel/DeineMudda)\n* [py-image-dedup](https://github.com/markusressel/py-image-dedup)\n\nand hopefully many others :)\n\n# How to use\n\n\n## Install dependency\n\n```shell\npip install container-app-conf\n```\n\n## Extend `ConfigBase` base\n\nCreate a custom configuration class and define your config entries:\n\n```python\nfrom container_app_conf import ConfigBase\nfrom container_app_conf.entry.string import StringConfigEntry\n\nclass AppConfig(ConfigBase):\n\n    MY_CONFIG = StringConfigEntry(\n        description=\"This is just a demo text config entry\",\n        example=\"example\",\n        key_path=[\n            \"my_app\",\n            \"example\"\n        ],\n        required=True)\n```\n\n## Use configuration values\n\n```python\nconfig = AppConfig()\n\nvalue = config.MY_CONFIG.value\n```\n\n## Print current config\n\nOftentimes it can be useful to print the current configuration of an\napplication. To do this you can use\n\n```python\nconfig = AppConfig()\nconfig.print()\n```\n\nwhich will result in an output similar to this:\n\n```text\ntest->bool: _REDACTED_\ntest->this->date->is->nested->deep: 2019-10-22T04:21:02.316907\ntest->this->is->a->range: [0..100]\ntest->this->is->a->list: None\ntest->this->timediff->is->in->this->branch: 0:00:10\ntest->directory: None\ntest->file: None\ntest->float: 1.23\ntest->int: 100\ntest->regex: ^[a-zA-Z0-9]$\ntest->string: default value\nsecret->list: _REDACTED_\nsecret->regex: _REDACTED_\n```\n\nIf you don't like the style you can specify a custom `ConfigFormatter`\nlike this:\n\n```python\nfrom container_app_conf.formatter.toml import TomlFormatter\nconfig = AppConfig()\nconfig.print(TomlFormatter())\n```\n\nWhich would output the same config like this:\n\n```text\n[test]\nbool = \"_REDACTED_\"\nfloat = 1.23\nint = 100\nregex = \"^[a-zA-Z0-9]$\"\nstring = \"default value\"\n\n[secret]\nlist = \"_REDACTED_\"\nregex = \"_REDACTED_\"\n\n[test.this.is.a]\nrange = \"[0..100]\"\n\n[test.this.date.is.nested]\ndeep = \"2019-10-22T04:26:10.654541\"\n\n[test.this.timediff.is.in.this]\nbranch = \"0:00:10\"\n```\n\n## Generate reference config\n\nYou can generate a reference configuration from a config object.\nThis reference contains **all** available configuration options. \nIf a **default** was specified for an entry it will be used, \notherwise the **example** value.\n\n```python\nfrom container_app_conf.util import generate_reference_config\nconfig = AppConfig()\nreference_config = generate_reference_config(config._config_entries.values())\n```\n\nThis will return a dictionary representing the config entry tree.\nYou can also specify a formatter and write a reference config to a \nfile using:\n\n```python\nfrom container_app_conf.util import write_reference\nfrom container_app_conf.formatter.yaml import YamlFormatter\nconfig = AppConfig()\nwrite_reference(config, \"/home/markus/.config/example.yaml\", YamlFormatter())\n```\n\nIf the generated reference contains values that do not make sense \nbecause of application constraints, specify your own **example** \nor better yet **default** value using the respective config entry \nconstructor parameter.\n\n## Config Types\n\n| Name                     | Description                              | Type     |\n|--------------------------|------------------------------------------|----------|\n| `BoolConfigEntry`        | Parses `bool`, `int` (`0` and `1`) and `str` values (`yes`, `no` etc.) to a boolean value | `bool` |\n| `IntConfigEntry`         | Parses input to an integer | `int` |\n| `FloatConfigEntry`       | Parses input to a floating number | `float` |\n| `RangeConfigEntry`       | Parses input to a range (see [py-range-parse](https://github.com/markusressel/py-range-parse)) | `Range` |\n| `StringConfigEntry`      | Takes the raw string input | `str` |\n| `RegexConfigEntry`       | Parses and compiles regular expressions | `re.pattern` |\n| `DateConfigEntry`        | Parses various datetime formats (see [python-dateutil](https://github.com/dateutil/dateutil/)) | `datetime` |\n| `TimeDeltaConfigEntry`   | Parses various timedelta formats (see [pytimeparse](https://github.com/wroberts/pytimeparse)) | `timedelta` |\n| `FileConfigEntry`        | Parses a file path | `Path` |\n| `DirectoryConfigEntry`   | Parses a directory path | `Path` |\n| `DictConfigEntry`        | Parses a dictionary | `dict` |\n| `ListConfigEntry`        | Parses a comma separated string to a list of items specified in another `ConfigEntry` (in yaml it can also be specified as a yaml list) | `[]` |\n\nIf none of the existing types suit your needs you can easily create your \nown by extending the `ConfigEntry` base class.\n\n## Default Values\n\nA default value can be specified for every `ConfigEntry` by using the\n`default` constructor parameter.\n\n## Required values\n\nBy default config entries with a default different from `None` are \nrequired. A `None` value is only allowed for an entry if it has no \ndefault (or it is set to `None` explicitly).\n\nFor required entries it is not possible to set its value `None` even \nafter initial parsing. Omitting a value for this entry in all data \nsources will result in an exception.\n\nIf an entry requires a value and has no default, set the `required`\nconstructor parameter to `True`.\n\nIf you want to allow setting a `None` value even if the default value \nis **not** `None`, you have to explicitly set `required=False`.\n\n## Secret values\n\nIf your config contains secret values like passwords you can mark them\nas such using the `secret=True` constructor parameter. That way their \nvalue will be redacted when [printing the current configuration](#print-current-config).\n\n## Data sources\n\n**container-app-conf** supports the simultaneous use of multiple data \nsources to determine configuration values. The following \nimplementations are available:\n\n| Name                     | Description                              |\n|--------------------------|------------------------------------------|\n| `EnvSource`              | Reads environment variables |\n| `YamlSource`             | Parses `YAML` files |\n| `TomlSource`             | Parses `TOML` files |\n| `JsonSource`             | Parses `JSON` files |\n\n### EnvSource\n\n#### ENV Key\n\nSince you only specify the key path of a config entry the ENV\nkey is generated automatically by concatenating all key path items \nusing an underscore, converting to uppercase and replacing any remaining\nhyphens also with an underscore:\n\n```python\nkey_path = [\"my_app\", \"my-example\"]\n```\n\nwould yield `MY_APP_MY_EXAMPLE`.\n\n### Filesystem Source\n\nMultiple data sources using the filesystem are available:\n\n* YamlSource\n* TomlSource\n* JsonSource\n\n#### File paths\n\nBy default config files are searched for in multiple \ndirectories that are commonly used for configuration files which include:\n\n- `./`\n- `~/.config/`\n- `~/`\n\nThis can be customized using the `path` constructor parameter: \n\n```python\nfrom container_app_conf.source.yaml_source import YamlSource\nyaml_source = YamlSource(file_name=\"myapp\", path=[\"/my/path\", \"/my/other/path\"])\n```\n\n## Singleton\n\nBy default every `Config` subclass instance will behave like a \nsingleton. This means if you change the config value in one instance it \nwill also affect all other instances of the same `__class__`.\n\nTo be able to create multiple instances of a config that are independent \nof one another this behaviour can be disabled using the `singleton` \nconstructor parameter:\n\n```python\nconfig1 = AppConfig(singleton=False)\nconfig2 = AppConfig(singleton=False)\n```\n\n# Contributing\n\nGitHub is for social coding: if you want to write code, I encourage contributions through pull requests from forks\nof this repository. Create GitHub tickets for bugs and new features and comment on the ones that you are interested in.\n\n# License\n```text\ncontainer-app-conf\nCopyright (c) 2019 Markus Ressel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Convenient configuration of containerized applications",
    "version": "5.3.0",
    "project_urls": {
        "Homepage": "https://github.com/markusressel/container-app-conf"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7f1fc9b924ea60cc52a3c84d68b56581f753e72a723ff63d32eaa00f03816f06",
                "md5": "003c395b3356bd84c4c81d5ba61eb4d6",
                "sha256": "fa7d9530da2f62d4fed720a0b29ea854aadf0626104a2c17aace467608711160"
            },
            "downloads": -1,
            "filename": "container_app_conf-5.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "003c395b3356bd84c4c81d5ba61eb4d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 52765,
            "upload_time": "2024-02-29T17:53:06",
            "upload_time_iso_8601": "2024-02-29T17:53:06.154213Z",
            "url": "https://files.pythonhosted.org/packages/7f/1f/c9b924ea60cc52a3c84d68b56581f753e72a723ff63d32eaa00f03816f06/container_app_conf-5.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8d7e470804dc34a184ca58e8eda266c03abc7e640202da8c4ff2f0acb585f01",
                "md5": "fb8eb50e97e7d4e7aa138841c4df1138",
                "sha256": "81fa5e7da03834ab6a7b9ad09a93dd767ecb57085751dfb7b4cf6570d51768db"
            },
            "downloads": -1,
            "filename": "container_app_conf-5.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "fb8eb50e97e7d4e7aa138841c4df1138",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 18070,
            "upload_time": "2024-02-29T17:53:07",
            "upload_time_iso_8601": "2024-02-29T17:53:07.495166Z",
            "url": "https://files.pythonhosted.org/packages/c8/d7/e470804dc34a184ca58e8eda266c03abc7e640202da8c4ff2f0acb585f01/container_app_conf-5.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-29 17:53:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "markusressel",
    "github_project": "container-app-conf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "container-app-conf"
}
        
Elapsed time: 0.22638s