Ever wanted to load plain `.ini` config files and then validate loaded config?
Ever wanted to load config from multiple locations (`/etc/appconfig.conf`, `~/.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
~~~sh
pip install marshmallow_configparser
~~~
## Example
Having config file `/tmp/example_config.conf` looking like this:
~~~ini
[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:
~~~python
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:
~~~python
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:
~~~python
schema = ConfigSchema()
obj, errors = schema.load(['/tmp/example_config.conf'])
~~~
In the end we have:
~~~python
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
[marshmallow]: https://github.com/marshmallow-code/marshmallow
[ConfigParser]: https://docs.python.org/3/library/configparser.html#configparser.ConfigParser
Metadata
Release files for marshmallow-configparser 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| marshmallow_configparser-0.4.0.tar.gz | 24.1 kB | Without provenance | Details for marshmallow_configparser-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| marshmallow_configparser-0.4.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for marshmallow_configparser-0.4.0-py2.py3-none-any.whl |
Total release size: 31.4 kB
Release files / marshmallow_configparser-0.4.0.tar.gz
| Download URL | marshmallow_configparser-0.4.0.tar.gz |
|---|---|
| Size | 24.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5eec3f6c581ef3cd3fecdbdc86cb834c4c8d71d79475409644d15f69094d050
|
|
BLAKE2b-256 checksum How to use checksums |
c691e392b112d4b93289b6a4e89190d3f57f93f0259be2343b8b05dea87896c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Release files / marshmallow_configparser-0.4.0-py2.py3-none-any.whl
| Download URL | marshmallow_configparser-0.4.0-py2.py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
09ed3a99a7a38d3ee8be8cdafeab493cc89a0380f8426554a04fcfa9c9ef464d
|
|
BLAKE2b-256 checksum How to use checksums |
ea3aebe3f46bdde8f35c3ff593d78e93b7f73362b948111c55b73c42e95ae7ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Security for marshmallow-configparser 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.