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.0.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distributions

yamlreader-3.0.1-py2.7.egg (7.3 kB view details)

Uploaded Egg

yamlreader-3.0.1-py2-none-any.whl (6.7 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for yamlreader-3.0.1.tar.gz
Algorithm Hash digest
SHA256 7a12335b49601084b4778854366c66a60ecb2fabf659ed86ceb343e04610250f
MD5 3d6ed5d4667a38c9edc20e7c9c6b8441
BLAKE2b-256 b208e352fb9b4d34d3ec4852d9302ebf573c904466b60593dafef85735d8214d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for yamlreader-3.0.1-py2.7.egg
Algorithm Hash digest
SHA256 be0a267fb8995e34e45c087badcff689cb2a7876c31db7358d65d85b5ba4ee1b
MD5 924fc92ae11b49f93d61d19a13ea5e2d
BLAKE2b-256 098f4ddf114dda45191b7d2064c0e1e92b49456ad4f3bc5936dcd6a0c0a508e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for yamlreader-3.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 f54fc40fe358bf5c8407a15d21a2e64ff92aa664c27609a2452ae05ea54accf8
MD5 8bd80cb246cc936f58a04680a6736e46
BLAKE2b-256 33419bd4cb0fdfc0ef186487724fb4abb13e5866d8358ff5cdceca925d90207b

See more details on using hashes here.

Supported by

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