A library that allows to easily load configuration settings.
Project description
Cfg-Loader
Cfg-Loader is a library that allows to easily load configuration settings. It uses marshmallow to deserialize input data into a target format configuration data.
Main features
input data validation and automatic processing using marshmallow
substitution of environment variables in input data (following docker compose variable substitution syntax)
configuration loading from .yaml file
Requirements
Python>=3.6
A simple example
>>> from cfg_loader import ConfigSchema, BaseConfigLoader
>>> from marshmallow import fields
# Declare your configuration schema
>>> class MyConfigSchema(ConfigSchema):
... setting1 = fields.Str()
... setting2 = fields.Int(required=True)
... setting3 = fields.Float(missing=13.2)
# Declare mapping to substitute environment variable
>>> substitution_mapping = {'FILE_PATH': 'file'}
# Initialize config loader
>>> my_config_loader = BaseConfigLoader(MyConfigSchema, substitution_mapping=substitution_mapping)
# Load configuration
>>> config = my_config_loader.load({'setting1': '/home/folder/${FILE_PATH?:file path required}', 'setting2': '4'})
>>> config == {'setting1': '/home/folder/file', 'setting2': 4, 'setting3': 13.2}
True
# Invalid input data
>>> my_config_loader.load({'setting1': '/home/folder/${FILE_PATH?:file path required}', 'setting3': 13.4})
Traceback (most recent call last):
...
cfg_loader.exceptions.ValidationError: {'setting2': ['Missing data for required field.']}
>>> my_config_loader.load({'setting2': 12, 'setting3': 'string'})
Traceback (most recent call last):
...
cfg_loader.exceptions.ValidationError: {'setting3': ['Not a valid number.']}
# Variable substitution invalid
>>> my_config_loader.load({'setting2': '${UNSET_VARIABLE?Variable "UNSET_VARIABLE" required}'})
Traceback (most recent call last):
...
cfg_loader.exceptions.UnsetRequiredSubstitution: Variable "UNSET_VARIABLE" required
Documentation
Full documentation is available at https://cfg-loader.readthedocs.io/en/stable/.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
theatro-cfg-loader-0.3.1.tar.gz
(17.6 kB
view hashes)
Built Distribution
Close
Hashes for theatro_cfg_loader-0.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea7970c91c1a770d0566c3d23fecb5723f5ac3420d8ebfc3d9f6e935e7ddea6f |
|
MD5 | ac19cacd7d73d1b3112de0f6c273a004 |
|
BLAKE2b-256 | f046b7a0280ab85fbb06f5e2981d55cf47197ce6d49760b88ff2ebc406d6fbfa |