Skip to main content

Configuration parser based on YAML-Files with support for variables, overlaying and hierarchies

Project description

Build Status Documentation Status

Configcrunch is a Python library for reading YAML-based configuration files that aims to be simple while also providing some very powerful features.

Configcrunch is compatible with Python 3.6 and up.

Install it via pip: pip install configcrunch

Features:

  • Read configuration files from YAML files.

  • Define various types of configuration files, that can be validated via a schema.

  • Types of configuration files are defined as separate Python classes.

  • Documents can be configured to contain sub-documents of any type.

  • Documents can contain Jinja2 based variables that can reference any other field inside the same or parent document.

  • The classes that represent your document types can contain methods that can be used inside the configuration files.

  • Documents can reference documents from other files. Configcrunch will merge them together. You decide where referenced documents are looked up.

  • Configuration objects can also be created without YAML files, by using default Python dicts.

  • All features are optional.

Used by:

  • Riptide

  • (Your project here! Open an issue.)

By default Configcrunch uses schema to validate schemas. But you can also use your own validation logic!

Example

This is an example that uses most of the features described above, using two document types.

# doc1.yml - Type: one
one:
    name: Document
    number: 1
    sub:
        # Sub-document of type "two"
        $ref: /doc2
        two_field: "{{ parent().method() }}"
# <lookup path>/doc2.yml - Type: two
two:
    name: Doc 2
    number: 2
    two_field: This is overridden
# classes.py
from schema import Schema, Optional

from configcrunch import YamlConfigDocument, DocReference, load_subdocument, variable_helper


class One(YamlConfigDocument):
    @classmethod
    def header(cls) -> str:
        return "one"

    def schema(self) -> Schema:
        return Schema(
            {
                Optional('$ref'): str,  # reference to other One documents
                'name': str,
                'number': int,
                Optional('sub'): DocReference(Two)
            }
        )

    def resolve_and_merge_references(self, lookup_paths):
        super().resolve_and_merge_references(lookup_paths)
        if "sub" in self:
            self["sub"] = load_subdocument(self["sub"], self, Two, lookup_paths)
        return self

    @variable_helper
    def method(self):
        return "I will return something"


class Two(YamlConfigDocument):
    @classmethod
    def header(cls) -> str:
        return "two"

    def schema(self) -> Schema:
        return Schema(
            {
                Optional('$ref'): str,  # reference to other Two documents
                'name': str,
                'number': int,
                'two_field': str
            }
        )

The document “one.yml” can then be read via Python:

>>> import yaml
>>> from classes import One
>>> doc = One.from_yaml('./doc1.yml')
>>> doc.resolve_and_merge_references(['<lookup path>'])
>>> doc.process_vars()
>>> print(yaml.dump(doc.to_dict(), default_flow_style=False))
one:
  name: Document
  number: 1
  sub:
    name: Doc 2
    number: 2
    two_field: I will return something

Tests

Inside the configcrunch.tests package are acceptance tests. Unit tests are WIP.

To run the tests, see run_tests.sh.

Documentation

The complete documentation can be found at Read the Docs (or in the docs directory).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

configcrunch-0.1.2-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file configcrunch-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: configcrunch-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for configcrunch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 917b32f3e199d258b2bf6853d654cb722bcc0dcb1349e9128f0247132dcfa0b9
MD5 571ebd7f2877596b9c972a200056c27e
BLAKE2b-256 e524fb1fc98b924ac4311fae0cbe2de5aea8ff6e44e74d3b7437515d216fbc89

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