Skip to main content

基于 YAML 文件的配置解析器,支持变量、覆盖和层次结构

项目描述

构建状态 文件状态 版本 下载 许可证(麻省理工学院) 支持的 Python 版本

Configcrunch 是一个用 Rust 编写的 Python 库,用于读取基于 YAML 的配置文件。它旨在简单快速,同时还提供一些非常强大的功能。

Configcrunch 与 Python 3.6 及更高版本兼容。

通过 pip 安装它:pip install configcrunch

特征:

  • 从 YAML 文件中读取配置文件。

  • 定义可以通过模式验证的各种类型的配置文件。

  • 配置文件的类型被定义为单独的 Python 类。

  • 可以将文档配置为包含任何类型的嵌套文档。

  • 文档可以包含Minijinja模板,这些模板可以引用同一文档或父文档中的任何其他字段。

  • 代表文档类型的类可以包含可以在配置文件中使用的方法。

  • 文档可以参考其他文件中的文档。Configcrunch 会将它们合并在一起。您决定在何处查找引用的文档。

  • 配置对象也可以在没有 YAML 文件的情况下通过使用普通的 Python dicts 来创建。

  • 所有功能都是可选的。

使用者:

  • 激流

  • (你的项目在这里!打开一个问题。)

默认情况下,Configcrunch 使用模式来验证模式。但您也可以使用自己的验证逻辑!

例子

这是一个使用上述大部分功能的示例,使用两种文档类型。

# doc1.yml - Type: one
one:
    name: Document
    number: 1
    sub:
        # Sub-document of type "two"
        $ref: /doc2
        two_field: <s>"{{</s> <s>parent().method()</s> <s>}}"</s>
# <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 []

然后可以通过 Python 读取文档“one.yml”:

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

测试

configcrunch.tests包内是测试

要运行测试,请参阅run_tests.sh

文档

完整的文档可以在Read the Docs(或 docs 目录)中找到。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

内置发行版

configcrunch-1.0.3-cp310-cp310-win_amd64.whl (621.7 kB 查看哈希

已上传 cp310

configcrunch-1.0.3-cp310-cp310-win32.whl (589.1 kB 查看哈希

已上传 cp310

configcrunch-1.0.3-cp310-cp310-macosx_11_0_x86_64.whl (759.1 kB 查看哈希

已上传 cp310

configcrunch-1.0.3-cp39-cp39-win_amd64.whl (621.8 kB 查看哈希

已上传 cp39

configcrunch-1.0.3-cp39-cp39-win32.whl (589.3 kB 查看哈希

已上传 cp39

configcrunch-1.0.3-cp39-cp39-macosx_11_0_x86_64.whl (760.4 kB 查看哈希

已上传 cp39

configcrunch-1.0.3-cp38-cp38-win_amd64.whl (621.2 kB 查看哈希

已上传 cp38

configcrunch-1.0.3-cp38-cp38-win32.whl (588.5 kB 查看哈希

已上传 cp38

configcrunch-1.0.3-cp38-cp38-macosx_11_0_x86_64.whl (758.4 kB 查看哈希

已上传 cp38

configcrunch-1.0.3-cp37-cp37m-win_amd64.whl (621.0 kB 查看哈希

已上传 cp37

configcrunch-1.0.3-cp37-cp37m-win32.whl (588.6 kB 查看哈希

已上传 cp37

configcrunch-1.0.3-cp37-cp37m-macosx_11_0_x86_64.whl (758.0 kB 查看哈希

已上传 cp37