Skip to main content

Merge YAML data from given files, dir or file glob

Project description

Merge YAML data from a directory, a list of files or a file glob. With a directory, the YAML files (*.yaml) are sorted alphabetically. The YAML files are expected to contain a complex key-value structure and merged with the following rules:

  • lists get appended

  • hashes get merged by key

  • scalars (numbers, strings) are overwritten

  • everything else will fail

The purpose is to allow several YAML files instead of a single YAML file. We use it to help our software read configuration data from an arbitrary amount of YAML files instead of a single YAML file.

Read the unit test to see some examples.

Building

  1. Check out the source

  2. Build a package

  1. RPM: Run python setup.py clean bdist_rpm, results will be in dist/

  2. DEB: Install python-stdeb and run python setup.py –command-packages=stdeb.command clean bdist_deb, results will be in deb_dist

Running

The package installs a command line script yamlreader that can be used to read one or many YAML files and dump the merge result as a YAML document.

Contributing

Create a new virtual environment:

virtualenv venv

Activate the virtual environment:

source venv/bin/activate

Install requirements using pip:

pip install -r requirements.txt

Now you are able to run the tests:

python setup.py test

Use it in your software

Wherever you had been using the safe_load function of PyYAML to read a single YAML file you can use the yamlreader.yaml_load function as a replacement to read all *.yaml files in a directory:

from yamlreader import *

defaultconfig = {
        "loglevel" : "error",
        "some" : "value"
}

config = yaml_load("/etc/myapp", defaultconfig)

load_yaml

def yaml_load(source,defaultdata=None):
    """merge YAML data from files found in source

    Always returns a dict. The YAML files are expected to contain some kind of
    key:value structures, possibly deeply nested. When merging, lists are
    appended and dict keys are replaced. The YAML files are read with the
    yaml.safe_load function.

    source can be a file, a dir, a list/tuple of files or a string containing
    a glob expression (with ?*[]).

    For a dir all *.yaml files will be read in alphabetical order.

    defaultdata can be used to initialize the data.
    """

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

yamlreader-3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distributions

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

yamlreader-3-py2.7.egg (5.9 kB view details)

Uploaded Egg

yamlreader-3-py2-none-any.whl (4.4 kB view details)

Uploaded Python 2

File details

Details for the file yamlreader-3.tar.gz.

File metadata

  • Download URL: yamlreader-3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yamlreader-3.tar.gz
Algorithm Hash digest
SHA256 c9615778ac6846cc25376474531fc2f9136100fe5523317a34a0b495fb4efbcc
MD5 6e797aeaa6f61725e81dcdc10ad35794
BLAKE2b-256 fcd0bb0ca3add150a2b54ac49f7fb894475b3d9896ea2af8812af0529299d33a

See more details on using hashes here.

File details

Details for the file yamlreader-3-py2.7.egg.

File metadata

  • Download URL: yamlreader-3-py2.7.egg
  • Upload date:
  • Size: 5.9 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yamlreader-3-py2.7.egg
Algorithm Hash digest
SHA256 2374a90055b100dcb2d56839e79d0cb68563c12cc306eeb30e448ba17ee5442a
MD5 12d42bd9b2bcd1562a5a4e401622a6fb
BLAKE2b-256 0a8cd56273b0b2053a976fd745b9f942f86aa16181a9025134f9b9be8436f8f9

See more details on using hashes here.

File details

Details for the file yamlreader-3-py2-none-any.whl.

File metadata

File hashes

Hashes for yamlreader-3-py2-none-any.whl
Algorithm Hash digest
SHA256 cf283bb6079a281d3c8d8e0b6a2b963f985ca28c3581fff902e9e0178e58784c
MD5 12b1e39d52da5b0f202a245000291c4c
BLAKE2b-256 58a17cd2466b1307a30a14ba572f9c0b61fc3fc23f697e98ce1b3552699d7388

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