yinter

Value interpolator for YAML files.


Keywords
utilities, util, interpolation, yaml
License
MIT
Install
pip install yinter==0.3.0

Documentation

YAML Interpolation

Value interpolator for YAML files.

This project was inspired by the built-in configparser.ExtendedInterpolation feature for ini and cfg files.

Why?

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.

Installation

Use pip to install the package from PyPI:

$ python3 -m pip install --user yinter

Usage

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']}}
>>>

Further Development

The following are some might-be-nice-to-haves depending on the level of fanciness that you prefer with your YAML configurations.

  • Optimize procedure and algorithm
  • Support custom regex formats for interpolation variables
    • e.g. "*(key1:key2:key3)", or whatever your fancy
  • Support casting to different/original types

Python < 3.7

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!