1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
.. |pylint| image:: https://github.com/zerwes/hiyapyco/actions/workflows/pylint.yml/badge.svg?branch=main
:target: https://github.com/zerwes/hiyapyco/actions/workflows/pylint.yml
.. |test| image:: https://github.com/zerwes/hiyapyco/actions/workflows/test.yml/badge.svg
:target: https://github.com/zerwes/hiyapyco/actions/workflows/test.yml
.. |gpl| image:: https://img.shields.io/badge/License-GPL%20v3-blue.svg
:target: http://www.gnu.org/licenses/gpl-3.0
|pylint| |test| |gpl|
hiyapyco
========
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.
The goal was to have something similar to puppets hiera
``merge_behavior: deeper`` for python.
Key Features
------------
- hierarchical overlay of multiple YAML files
- multiple merge methods for hierarchical YAML files
- variable interpolation using jinja2
Requirements
------------
- PyYAML aka. python3-yaml
- Jinja2 aka. python3-jinja2
Python Version
~~~~~~~~~~~~~~
HiYaPyCo was designed to run on current major python versions
without changes. Tested versions:
- 3.9
- 3.11
Usage
-----
A simple example:
::
import hiyapyco
conf = hiyapyco.load('yamlfile1' [,'yamlfile2' [,'yamlfile3' [...]]] [,kwargs])
print(hiyapyco.dump(conf, default_flow_style=False))
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'}
real life example using yaml documents as strings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
>>> import hiyapyco
>>> y1="""
... yaml: 1
... y:
... y1: abc
... y2: xyz
... """
>>> y2="""
... yaml: 2
... y:
... y2: def
... y3: XYZ
... """
>>> conf = hiyapyco.load([y1, y2], method=hiyapyco.METHOD_MERGE)
>>> print (conf)
OrderedDict([('yaml', 2), ('y', OrderedDict([('y1', 'abc'), ('y2', 'def'), ('y3', 'XYZ')]))])
>>> hiyapyco.dump(conf, default_flow_style=True)
'{yaml: 2, y: {y1: abc, y2: def, y3: XYZ}}\n'
args
~~~~
All ``args`` are handled as *file names* or *yaml documents*. 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
- ``hiyapyco.METHOD_SUBSTITUTE``: perform a merge w/ lists substituted (unsupported)
- ``mergelists``: boolean try to merge lists of dict (default: ``True``)
- ``none_behavior``: bit (one of the listed below):
- ``hiyapyco.NONE_BEHAVIOR_DEFAULT``: attempt to merge the value with ``None`` and fail if this is not possible (default method)
- ``hiyapyco.NONE_BEHAVIOR_OVERRIDE``: ``None`` always overrides any other value.
- ``interpolate``: boolean : perform interpolation after the merge
(default: ``False``)
- ``castinterpolated``: boolean : try to perform a *best possible
match* cast for interpolated strings (default: ``False``)
- ``usedefaultyamlloader``: boolean : force the usage of the default
*PyYAML* loader/dumper instead of *HiYaPyCo*\ s implementation of a
OrderedDict loader/dumper (see: Ordered Dict Yaml Loader / Dumper
aka. ODYLDo) (default: ``False``)
- ``dereferenceyamlanchors``: boolean : dereference yaml anchors and use a copy (default: ``True``)
- ``encoding``: string : encoding used to read yaml files (default: ``utf-8``)
- ``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``)
- ``mergeoverride``: optional function to customize merge for primitive values
(see `PR #76 <https://github.com/zerwes/hiyapyco/pull/76>`_.)
- ``loader_callback``: optional custom callback function to load yaml files.
The callback function shall behave like ``yaml.load_all`` from PyYAML,
taking a IO stream as input and returning a list of objects.
Using this method, for example `ruamel <https://pypi.org/project/ruamel.yaml/>`_
can be used instead of PyYAML etc.
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 Ordered Dict Yaml Loader /
Dumper aka. 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 string causing the
error.
more informations
^^^^^^^^^^^^^^^^^
See:
`jinja2.Environment <http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment>`_
cast interpolated strings
~~~~~~~~~~~~~~~~~~~~~~~~~
As you must use interpolation as strings (PyYAML will weep if you try to
start a value with ``{{``), you can set ``castinterpolated`` to *True*
in order to try to get a ``best match`` cast for the interpolated
values. **The ``best match`` cast is currently only a q&d implementation
and may not give you the expected results!**
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.**
Install
-------
From Source
~~~~~~~~~~~
GitHub
^^^^^^
`https://github.com/zerwes/hiyapyco <https://github.com/zerwes/hiyapyco>`_
::
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 <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
~~~~~~~~~~~~~~~
install the latest debian packages from http://repo.zero-sys.net/hiyapyco::
# create the sources list file:
sudo echo "deb http://repo.zero-sys.net/hiyapyco/deb ./" > /etc/apt/sources.list.d/hiyapyco.list
# import the key:
gpg --keyserver keys.gnupg.net --recv-key 77DE7FB4
# or use:
wget https://repo.zero-sys.net/77DE7FB4.asc -O - | gpg --import -
# apt tasks:
gpg --armor --export 77DE7FB4 | sudo tee /etc/apt/trusted.gpg.d/hiyapyco.asc
sudo apt-get update
sudo apt-get install python3-hiyapyco
a ansible playbook exists: https://github.com/zerwes/ansible-role-hiyapyco
rpm packages
~~~~~~~~~~~~
use
`http://repo.zero-sys.net/hiyapyco/rpm <http://repo.zero-sys.net/hiyapyco/rpm>`_
as URL for the yum repo and
`https://repo.zero-sys.net/77DE7FB4.asc <https://repo.zero-sys.net/77DE7FB4.asc>`_
as the URL for the key.
Arch Linux
~~~~~~~~~~
An `AUR package <https://aur.archlinux.org/packages/python-hiyapyco/>`_
is available (provided by `Pete Crighton <https://github.com/PeteCrighton>`_ and not always up to date).
License
-------
Copyright |copy| 2014 - 2024 Klaus Zerwes `zero-sys.net <https://zero-sys.net>`_
.. |copy| unicode:: 0xA9 .. copyright sign
This package is free software.
This software is licensed under the terms of the GNU GENERAL PUBLIC
LICENSE version 3 or later, as published by the Free Software
Foundation.
See
`https://www.gnu.org/licenses/gpl.html <https://www.gnu.org/licenses/gpl.html>`_
Changelog
---------
0.7.0
~~~~~~
MERGED: allow custom yaml loaders as callback functions by @grst (PR #77)
MERGED: implement none-behavior strategies by @grst (PR #78)
MERGED: update markupsafe requirement from <3 to <4 (#80)
IMPROVED: added some example how to use ruamel
0.6.1
~~~~~~
MERGED: #76 Override mechanism for primitive value merge by malachib
IMPROVED: added link to ansible playbook
0.6.0
~~~~~~
FIXED: #69 (weird merge behavior with anchors)
MERGED: #71 (dereference anchors)
0.5.6
~~~~~~
MERGED: #70 by itachi-cracker
FIXED: #61 (removed deprecated distutils)
0.5.5
~~~~~~
FIXED: #67 cosmetic changes
0.5.4
~~~~~~
FIXED: #60 recursive calls to _substmerge
IMPROVED: testing and python support (3.11)
0.5.1
~~~~~~
MERGED: #52 by ryanfaircloth
0.5.0
~~~~~~
MERGED: #41 Jinja2 dependency increased to include Jinja2 3.x.x
REMOVED: Support for Python 2
0.4.16
~~~~~~
MERGED: #37 alex-ber
0.4.15
~~~~~~
MERGED: #30 lesiak:issue-30-utf
MERGED: #28 lesiak:issue-28
0.4.14
~~~~~~
FIXED: issue #33
MERGED: issue #32
0.4.13
~~~~~~
IMPLEMENTED: [issue #27] support multiple yaml documents in one file
0.4.12
~~~~~~
FIXED: logging by Regev Golan
0.4.11
~~~~~~
IMPLEMENTED: mergelists (see issue #25)
0.4.10
~~~~~~
FIXED: issue #24 repo signing
0.4.9
~~~~~
FIXED: issue #23 loglevelonmissingfiles
0.4.8
~~~~~
Fixed pypi doc
0.4.7
~~~~~
Reverted: logger settings to initial state
Improved: dump
Merged:
- flatten mapping from Chris Petersen geek@ex-nerd.com
- arch linux package info from Peter Crighton git@petercrighton.de
0.4.6
~~~~~
MERGED: fixes from mmariani
0.4.5
~~~~~
FIXED: issues #9 and #11
0.4.4
~~~~~
deb packages:
- removed support for python 2.6
- include examples as doc
0.4.3
~~~~~
FIXED: issue #6 *import of hiyapyco **version** in setup.py causes pip
install failures*
0.4.2
~~~~~
Changed: moved to GPL
Improvements: missing files handling, doc
0.4.1
~~~~~
Implemented: ``castinterpolated``
0.4.0
~~~~~
Implemented: loading yaml docs from string
0.3.2
~~~~~
Improved tests and bool args checks
0.3.0 / 0.3.1
~~~~~~~~~~~~~
Implemented a Ordered Dict Yaml Loader
0.2.0
~~~~~
Fixed unicode handling
0.1.0 / 0.1.1
~~~~~~~~~~~~~
Initial release
|