Schema-checked YAML config files, with default values for missing fields.
-
Define your config file structure with a JSON schema.
-
Help your users get started by showing them a auto-commented default config which is:
- auto-generated from your defaults, and
- auto-commented with your schema
description
.
-
Load a user's config file with
load_config()
, which will:- validate it against your schema
- auto-inject default values for any settings the user omitted
Install from PyPI:
pip install confiddler
...or with conda:
conda install -c stuarteberg -c conda-forge confiddler
>>> from confiddler import dump_default_config, load_config
>>> schema = {
"description": "Settings for a robot vacuum cleaner",
"properties": {
"speed": {
"description": "Speed of the robot (1-10)",
"type": "number",
"minValue": 1,
"maxValue": 10,
"default": 1
},
"movement": {
"description": "Movement strategy: random, raster, or spiral",
"type": "string",
"enum": ["random", "raster", "spiral"],
"default": "random"
}
}
}
>>> dump_default_config(schema, sys.stdout, 'yaml')
speed: 1
movement: random
>>> dump_default_config(schema, sys.stdout, 'yaml-with-comments')
#
# Settings for a robot vacuum cleaner
#
# Speed of the robot (1-10)
speed: 1
# Movement strategy: random, raster, or spiral
movement: random
# my-robot-config.yaml
speed: 2
# (Other settings omitted -- will be set as default.)
>>> load_config('my-robot-config.yaml', schema, inject_defaults=True)
{'speed': 2, 'movement': 'random'}