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 ConfigParserSchema, Integer, String
from marshmallow_configparser import IsNotBlank
class ConfigSchema(ConfigParserSchema):
class Meta:
model = ConfigObject
MANDATORY_STRING1 = String(
section='Section1',
load_from='option1', dump_to='option1',
allow_null=False, required=True, default='', missing='',
validate=[IsNotBlank()]
)
OPTIONAL_STRING1 = String(
section='Section1',
load_from='option2', dump_to='option2',
allow_null=False, required=False, default='', missing=''
)
MANDATORY_INTEGER1 = Integer(
section='Section1',
load_from='option3', dump_to='option3',
allow_null=False, required=True, default=0, missing=0,
validate=[Range(min=24, max=42)]
)
OPTIONAL_INTEGER1 = Integer(
section='Section1',
load_from='option4', dump_to='option4',
allow_null=False, required=False, default=0, missing=0
)
MANDATORY_STRING2 = String(
section='Section2',
load_from='option1', dump_to='option1',
allow_null=False, required=True, default='', missing='',
validate=[IsNotBlank()]
)
OPTIONAL_STRING2 = String(
section='Section2',
load_from='option2', dump_to='option2',
allow_null=False, required=False, default='', missing=''
)
MANDATORY_INTEGER2 = Integer(
section='Section2',
load_from='option3', dump_to='option3',
allow_null=False, required=True, default=0, missing=0,
validate=[Range(min=24, max=42)]
)
OPTIONAL_INTEGER2 = Integer(
section='Section2',
load_from='option4', dump_to='option4',
allow_null=False, required=False, default=0, missing=0
)
Which can then load and validate our config:
schema = ConfigSchema()
obj, errors = schema.load(['/tmp/example_config.conf'])
Documentation
TODO
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
Close
Hashes for marshmallow_configparser-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0d8ab2bee235d63d530b2e8953e3176b13dd1c570b28bbbff412f8f71b408ad |
|
MD5 | cbe86acf0e8a44493089cbe3955cd0f7 |
|
BLAKE2b-256 | 25a97585f537db891448942aeca41c4e74f1c034c8b10e0b610adf7224ee2661 |
Close
Hashes for marshmallow_configparser-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb7875cfad6133cf2512d1a2c9f4502e5e41c6fbcb603af53d403ec66d2d254a |
|
MD5 | 6f42a2dc9e664d4829ca584fd3ba2cef |
|
BLAKE2b-256 | 3d4ad65dc492d1ea2676cdecd505c31b89fb8af60708607e6dd4aca400a42c8b |