Skip to main content

Hierarchical Yaml Python Config

Project description

HiYaPyCo - A Hierarchical Yaml Python Config

Description

A simple python lib allowing hierarchical overlay of config files in YAML syntax, offering different merge methods and variable interpolation based on jinja2.

Key Features

  • hierarchical overlay of multiple YAML files

  • multiple merge methods for hierarchical YAML files

  • variable interpolation using jinja2

Requirements

  • PyYAML aka. python-yaml

  • Jinja2 aka. python-jinja2

  • ordereddict for python2.6 (if you like to use the OrderedDict PyYAML loader / dumper ODYLDo)

Python Version

HiYaPyCo was designed to run on both current major python versions without changes. Tested versions:

  • 2.6

  • 2.7

  • 3.2

Usage

A simple example:

import hiyapyco
conf = hiyapyco.load('yamlfile1' [,'yamlfile2' [,'yamlfile3' [...]]] [,kwargs])
print(hiyapyco.dump(conf))

real life example:

yaml1.yaml:

---
first: first element
second: xxx
deep:
    k1:
        - 1
        - 2

yaml2.yaml:

---
second: again {{ first }}
deep:
    k1:
        - 4
        - 6
    k2:
        - 3
        - 6

load …

>>> import pprint
>>> import hiyapyco
>>> conf = hiyapyco.load('yaml1.yaml', 'yaml2.yaml', method=hiyapyco.METHOD_MERGE, interpolate=True, failonmissingfiles=True)
>>> pprint.PrettyPrinter(indent=4).pprint(conf)
{   'deep': {   'k1': [1, 2, 4, 6], 'k2': [3, 6]},
    'first': u'first element',
    'ma': {   'ones': u'12', 'sum': u'22'},
    'second': u'again first element'}

args

All args are handled as file names. They may be strings or list of strings.

kwargs

  • method: bit (one of the listed below):

    • hiyapyco.METHOD_SIMPLE: replace values (except for lists a simple merge is performed) (default method)

    • hiyapyco.METHOD_MERGE: perform a deep merge

  • interpolate: boolean : perform interpolation after the merge (default: False)

  • usedefaultyamlloader: boolean : force the usage of the default PyYAML loader/dumper instead of HiYaPyCos implementation of a OrderedDict loader/dumper (see: ODYLDo (default: False)

  • failonmissingfiles: boolean : fail if a supplied YAML file can not be found (default: True)

  • loglevel: int : loglevel for the hiyapyco logger; should be one of the valid levels from logging: ‘WARN’, ‘ERROR’, ‘DEBUG’, ‘I NFO’, ‘WARNING’, ‘CRITICAL’, ‘NOTSET’ (default: default of logging)

  • loglevelmissingfiles: int : one of the valid levels from logging: ‘WARN’, ‘ERROR’, ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘CRITICAL’, ‘NOTSET’ (default: logging.ERROR if failonmissingfiles = True, else logging.WARN)

interpolation

For using interpolation, I strongly recomend not to use the default PyYAML loader, as it sorts the dict entrys alphabetically, a fact that may break interpolation in some cases (see test/odict.yaml and test/test_odict.py for an example). See ODYLDo

default

The default jinja2.Environment for the interpolation is

hiyapyco.jinja2env = Environment(undefined=Undefined)

This means that undefined vars will be ignored and replaced with a empty string.

change the jinja2 Environment

If you like to change the jinja2 Environment used for the interpolation, set hiyapyco.jinja2env before calling hiyapyco.load!

use jinja2 DebugUndefined

If you like to keep the undefined var as string but raise no error, use

from jinja2 import Environment, Undefined, DebugUndefined, StrictUndefined
hiyapyco.jinja2env = Environment(undefined=DebugUndefined)

use jinja2 StrictUndefined

If you like to raise a error on undefined vars, use

from jinja2 import Environment, Undefined, DebugUndefined, StrictUndefined
hiyapyco.jinja2env = Environment(undefined=StrictUndefined)

This will raise a hiyapyco.HiYaPyCoImplementationException wrapped arround the jinja2.UndefinedError pointing at the strig causing the error.

more informations

See: jinja2.Environment

Ordered Dict Yaml Loader / Dumper aka. ODYLDo

This is a simple implementation of a PyYAML loader / dumper using OrderedDict from collections. Because chaos is fun but order matters on loading dicts from a yaml file. In order to use this on python 2.6, please install ordereddict:

sudo pip-2.6 install ordereddict

Install

From Source

GitHub

git clone https://github.com/zerwes/hiyapyco
cd hiyapyco
sudo python setup.py install

PyPi

Download the latest or desired version of the source package from https://pypi.python.org/pypi/HiYaPyCo. Unpack the archive and install by executing:

sudo python setup.py install

pip

Install the latest wheel package using:

pip install HiYaPyCo

debian packages

echo "deb http://repo.zero-sys.net/hiyapyco/deb ./" > /etc/apt/sources.list.d/hiyapyco.list
gpg --recv-key ED7D414C
gpg --armor --export ED7D414C | apt-key add -
apt-get update
apt-get install python3-hiyapyco python-hiyapyco

rpm packages

use http://repo.zero-sys.net/hiyapyco/rpm as URL for the yum repo and http://jwhoisserver.net/key.asc as the URL for the key.

License

  1. 2014 Klaus Zerwes zero-sys.net This package is free software. This software is licensed under the terms of the GNU LESSER GENERAL PUBLIC LICENSE version 3 or later, as published by the Free Software Foundation. See https://www.gnu.org/licenses/lgpl.html

Changelog

0.1.0 / 0.1.1

Initial release

0.2.0

Fixed unicode handling

0.3.0

Implemented a Ordered Dict Yaml Loader

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

HiYaPyCo-0.3.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

HiYaPyCo-0.3.0-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file HiYaPyCo-0.3.0.tar.gz.

File metadata

  • Download URL: HiYaPyCo-0.3.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for HiYaPyCo-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e902c28fe0a05f2363ec8142f39b4d6ba236cadb0346301695496ca643f1f7b5
MD5 cfcb58855314e097d44f410f55bff3d4
BLAKE2b-256 5c1a26dbbcb71e79918ae3f1e127072a619882f237e9092106fbf0cdbd4843ec

See more details on using hashes here.

File details

Details for the file HiYaPyCo-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for HiYaPyCo-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56c74d03c19c6fd512fd01a0cce47183b28ee162cf0eafc1fa9e0d00e6f34268
MD5 cb59d31fca3a118a0c7ece9205c4b00f
BLAKE2b-256 2f0f938d56030be3f9d14be5ea034a0a6bbf34285e4b1dc3520f5fd7ae88b519

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page