Value interpolator for YAML files.
pip install yinter==0.3.0
Value interpolator for YAML files.
This project was inspired by the built-in configparser.ExtendedInterpolation
feature for
ini
and cfg
files.
While configparser and ini
files are great for some project needs, YAML is a convenient and
all around awesome language for configuration files, especially when paired with pyyaml
for easily returning a dictionary representation of the configuration.
Having something that can access the value of another variable defined somewhere else in the file
can save tedious copy-paste, and will propagate modifications throughout the file from a single
edit point.
It's too bad that interpolation isn't supported out of the box, and I wanted to change that for other YAML enthusiasts.
Use pip
to install the package from PyPI:
$ python3 -m pip install --user yinter
Pass in a path to a YAML file and ask the package to give you an interpolated dict
.
An example YAML configuration (I'm aware the syntax is horrid for YAML files, but it outlines the weird cases better):
#
# test_config.yml
#
project:
name: 'yinter'
version: 0
default_lang: 'Python'
default_lang_version: 3.7
core_lang: C
languages: [ '${default_lang}', '${default_lang}${default_lang_version}' ]
directories:
virtualenv: venv
virtualenv_bin: ${virtualenv}/bin
source: '${project:name}'
words:
list:
- awesome
# Not the best formatting for YAML but demonstrates the package
recursiveness:
check_it:
- one_for_the_money
- [ 2, [ 'for', { 'the': 'sh${project:version}w' } ] ]
- { 'three' :
[ 'because', {
'yaml': [ 'and', '${project:default_lang}', {
'are' : '${words:list}'
}]
}
]
}
The package in action:
>>> from yinter import YamlInterpolator
>>> from pprint import pprint
>>> yinter = YamlInterpolator('test_config.yml')
>>> pprint(yinter.get_interpolated_dict())
{'directories': {'source': 'yinter',
'virtualenv': 'venv',
'virtualenv_bin': 'venv/bin'},
'project': {'core_lang': 'C',
'default_lang': 'Python',
'default_lang_version': 3.7,
'languages': ['Python', 'Python3.7'],
'name': 'yinter',
'version': 0},
'recursiveness': {'check_it': ['one_for_the_money',
[2, ['for', {'the': 'sh0w'}]],
{'three': ['because',
{'yaml': ['and',
'Python',
{'are': "['awesome']"}]}]}]},
'words': {'list': ['awesome']}}
>>>
The following are some might-be-nice-to-haves depending on the level of fanciness that you prefer with your YAML configurations.
This package was developed on Python 3.7. I didn't feel like testing it on other Python3 distributions, and I try to stay away from Python2 these days, so I didn't bother making it happy and checking backwards compatibility. If you would like this, open an merge request!