Coder Social home page Coder Social logo

jeliebig / configcrunch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thecapypara/configcrunch

0.0 0.0 0.0 212 KB

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

Home Page: https://configcrunch.readthedocs.io

License: MIT License

Shell 0.32% Python 29.81% Rust 69.88%

configcrunch's Introduction

Configcrunch

Build Status Documentation Status Version Downloads License (MIT) Supported Python versions

Configcrunch is a Python library written in Rust for reading YAML-based configuration files. It aims to be simple and fast 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 nested documents of any type.
  • Documents can contain Minijinja templates 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 ordinary 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, variable_helper


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

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

    @classmethod
    def subdocuments(cls):
        return [
            ("sub", Two)
        ]

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


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

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

    @classmethod
    def subdocuments(cls):
        return []

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 tests.

To run the tests, see run_tests.sh.

Documentation

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

configcrunch's People

Contributors

thecapypara avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.