ConfigParser meets marshmallow
Project description
Overview
Ever wanted to load plain .ini config files and then validate loaded config?
Ever wanted to load config from multiple locations (/etc/appconfig.con, ~/.appconfig.conf) into single object and then validate that?
Worry no more!
Python’s ConfigParser met marshmallow and now they get along just fine - without any JSON in sight to spoil the fun.
Installation
pip install marshmallow_configparser
Example
Having config file /tmp/example_config.conf looking like this:
[Section1]
option1 = mandatory string
option2 = optional string
option3 = 42
option4 = 24
[Section2]
option1 = mandatory string
option2 = optional string
option3 = 42
option4 = 24
And wanting to load it into our config object:
class ConfigObject(object):
MANDATORY_STRING1 = None
OPTIONAL_STRING1 = None
MANDATORY_INTEGER1 = None
OPTIONAL_INTEGER1 = None
MANDATORY_STRING2 = None
OPTIONAL_STRING2 = None
MANDATORY_INTEGER2 = None
OPTIONAL_INTEGER2 = None
We can define marshmallow schema:
from marshmallow.validate import Range
from marshmallow_configparser import (ConfigBoolean, ConfigInteger,
ConfigParserSchema, ConfigString,
IsNotBlank)
class ConfigSchema(ConfigParserSchema):
class Meta:
model = ConfigObject
MANDATORY_STRING1 = ConfigString(
section='Section1', load_from='option1', dump_to='option1',
validate=[IsNotBlank()]
)
OPTIONAL_STRING1 = ConfigString(
section='Section1', load_from='option2', dump_to='option2',
)
MANDATORY_INTEGER1 = ConfigInteger(
section='Section1', load_from='option3', dump_to='option3',
validate=[Range(min=24, max=42)]
)
OPTIONAL_INTEGER1 = ConfigInteger(
section='Section1', load_from='option4', dump_to='option4',
)
MANDATORY_STRING2 = ConfigString(
section='Section2', load_from='option1', dump_to='option1',
validate=[IsNotBlank()]
)
OPTIONAL_STRING2 = ConfigString(
section='Section2', load_from='option2', dump_to='option2',
)
MANDATORY_INTEGER2 = ConfigInteger(
section='Section2', load_from='option3', dump_to='option3',
validate=[Range(min=24, max=42)]
)
OPTIONAL_INTEGER2 = ConfigInteger(
section='Section2', load_from='option4', dump_to='option4',
)
Which can then load and validate our config:
schema = ConfigSchema()
obj, errors = schema.load(['/tmp/example_config.conf'])
In the end we have:
obj.__dict_
{'MANDATORY_INTEGER1': 42,
'MANDATORY_INTEGER2': 42,
'MANDATORY_STRING1': 'mandatory string',
'MANDATORY_STRING2': 'mandatory string',
'OPTIONAL_INTEGER1': 24,
'OPTIONAL_INTEGER2': 24,
'OPTIONAL_STRING1': 'optional string',
'OPTIONAL_STRING2': 'optional string'}
Instead of using convenience classes like ConfigString, there are also classes in marshmallow_configparser.fields module that expose full marshmallow API. Check the docs for details.
Documentation
http://marshmallow-configparser.readthedocs.io/en/latest/index.html
Changelog
0.1.0 (2017-04-30)
First release on PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for marshmallow_configparser-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a875d5dded9a360b11e4f21a06d95ce52a2444870c70cbce0ecd292fbf32d57b |
|
MD5 | 5a619d59b1aefdcf8ce81a5ebdcf2ac1 |
|
BLAKE2b-256 | 4d6dfb1c105f2dffbf7d7765702ec8c4d33768b1a501848d6f01c894af76a21d |
Hashes for marshmallow_configparser-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f999e53daa80422b0c98efb33dd9438a9e2e005df35b43a26dc7c74c5b69f0d8 |
|
MD5 | 4e248114284cca3d46648278562d52c8 |
|
BLAKE2b-256 | 06cfe2b7fd41bb3d4b18610388a3130500bc843d0422e058ecb8ea400259f884 |