Package: container-app-conf
container-app-confcontainer-app-confis 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 bypython-n26InfiniteWisdomDeineMuddapy-image-dedupand hopefully many others :)How to useInstall dependencypipinstallcontainer-app-confExtendConfigBasebaseCreate a custom configuration class and define your config entries:fromcontainer_app_confimportConfigBasefromcontainer_app_conf.entry.stringimportStringConfigEntryclassAppConfig(ConfigBase):MY_CONFIG=StringConfigEntry(description="This is just a demo text config entry",example="example",key_path=["my_app","example"],required=True)Use configuration valuesconfig=AppConfig()value=config.MY_CONFIG.valuePrint current configOftentimes it can be useful to print the current configuration of an
application. To do this you can useconfig=AppConfig()config.print()which will result in an output similar to this: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 customConfigFormatterlike this:fromcontainer_app_conf.formatter.tomlimportTomlFormatterconfig=AppConfig()config.print(TomlFormatter())Which would output the same config like this:[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 configYou can generate a reference configuration from a config object.
This reference containsallavailable configuration options.
If adefaultwas specified for an entry it will be used,
otherwise theexamplevalue.fromcontainer_app_conf.utilimportgenerate_reference_configconfig=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:fromcontainer_app_conf.utilimportwrite_referencefromcontainer_app_conf.formatter.yamlimportYamlFormatterconfig=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 ownexampleor better yetdefaultvalue using the respective config entry
constructor parameter.Config TypesNameDescriptionTypeBoolConfigEntryParsesbool,int(0and1) andstrvalues (yes,noetc.) to a boolean valueboolIntConfigEntryParses input to an integerintFloatConfigEntryParses input to a floating numberfloatRangeConfigEntryParses input to a range (seepy-range-parse)RangeStringConfigEntryTakes the raw string inputstrRegexConfigEntryParses and compiles regular expressionsre.patternDateConfigEntryParses various datetime formats (seepython-dateutil)datetimeTimeDeltaConfigEntryParses various timedelta formats (seepytimeparse)timedeltaFileConfigEntryParses a file pathPathDirectoryConfigEntryParses a directory pathPathDictConfigEntryParses a dictionarydictListConfigEntryParses a comma separated string to a list of items specified in anotherConfigEntry(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 theConfigEntrybase class.Default ValuesA default value can be specified for everyConfigEntryby using thedefaultconstructor parameter.Required valuesBy default config entries with a default different fromNoneare
required. ANonevalue is only allowed for an entry if it has no
default (or it is set toNoneexplicitly).For required entries it is not possible to set its valueNoneeven
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 therequiredconstructor parameter toTrue.If you want to allow setting aNonevalue even if the default value
isnotNone, you have to explicitly setrequired=False.Secret valuesIf your config contains secret values like passwords you can mark them
as such using thesecret=Trueconstructor parameter. That way their
value will be redacted whenprinting the current configuration.Data sourcescontainer-app-confsupports the simultaneous use of multiple data
sources to determine configuration values. The following
implementations are available:NameDescriptionEnvSourceReads environment variablesYamlSourceParsesYAMLfilesTomlSourceParsesTOMLfilesJsonSourceParsesJSONfilesEnvSourceENV KeySince 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:key_path=["my_app","my-example"]would yieldMY_APP_MY_EXAMPLE.Filesystem SourceMultiple data sources using the filesystem are available:YamlSourceTomlSourceJsonSourceFile pathsBy default config files are searched for in multiple
directories that are commonly used for configuration files which include:./~/.config/~/This can be customized using thepathconstructor parameter:fromcontainer_app_conf.source.yaml_sourceimportYamlSourceyaml_source=YamlSource(file_name="myapp",path=["/my/path","/my/other/path"])SingletonBy default everyConfigsubclass 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 thesingletonconstructor parameter:config1=AppConfig(singleton=False)config2=AppConfig(singleton=False)ContributingGitHub 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.Licensecontainer-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.