从Jinja模板渲染配置文件
项目描述
ionit
ionit是一个简单小巧的配置模板工具。它收集上下文并在指定目录中渲染Jinja模板。上下文可以是静态的JSON或YAML文件或动态的Python文件。Python文件还可以定义传递给渲染器的函数。
上下文文件名需要以.json
结尾(用于JSON),以.py
结尾(用于Python),以.yaml
结尾(用于YAML)。上下文文件将按字母顺序读取。如果多个上下文文件定义了相同的键,则读取较晚的文件具有优先权。如果顺序相关,建议在文件名前加数字。
ionit附带一个早期启动的单次服务,该服务在网络服务之前执行,允许在启动之前生成网络和其他服务的配置文件。在这方面,ionit可以充当cloud-init的小型兄弟。
Python模块
Python模块可以定义一个collect_context
函数。该函数由ionit调用,并将当前上下文作为参数传递。可以使用当前上下文推导出更多上下文信息,但不应修改此变量。collect_context
必须返回一个字典(可以为空)或抛出异常,该异常将由ionit捕获。
Python模块还可以定义可以在渲染时从Jinja模板调用的函数。使用ionit_plugin.function
装饰器标记要导出的函数。
请注意,函数名称不应与其他上下文中的键冲突。如果一个Python模块定义了一个与上下文中同名的函数和值,上下文中的值将具有优先级。
一个示例Python模块可能看起来像这样
import ionit_plugin
@ionit_plugin.function
def double(value):
return 2 * value
@ionit_plugin.function
def example_function():
return "Lorem ipsum"
def collect_context(current_context):
return {"key": "value"}
先决条件
- Python >= 3.6
- Python模块
- jinja2
- PyYAML或ruamel.yaml
- pandoc(用于从
ionit.1.md
生成ionit.1
手册页)
测试用例有额外的需求
- black
- flake8
- isort
- pylint
示例
静态上下文
此示例来自一个测试用例,演示了ionit如何从一个JSON文件和一个YAML文件中收集上下文,并渲染一个模板
user@host:~/ionit$ cat tests/config/static/first.json
{"first": 1}
user@host:~/ionit$ cat tests/config/static/second.yaml
---
second: 2
user@host:~/ionit$ cat tests/template/static/counting.jinja
Counting:
* {{ first }}
* {{ second }}
* 3
user@host:~/ionit$ ./ionit -c tests/config/static -t tests/template/static
2018-08-08 17:39:06,956 ionit INFO: Reading configuration file 'tests/config/static/first.json'...
2018-08-08 17:39:06,956 ionit INFO: Reading configuration file 'tests/config/static/second.yaml'...
2018-08-08 17:39:06,960 ionit INFO: Rendered 'tests/template/static/counting.jinja' to 'tests/template/static/counting'.
user@host:~/ionit$ cat tests/template/static/counting
Counting:
* 1
* 2
* 3
Python函数
此示例来自一个测试用例,演示了如何在渲染时定义Python函数
user@host:~/ionit$ cat tests/config/function/function.py
import ionit_plugin
@ionit_plugin.function
def answer_to_all_questions():
return 42
user@host:~/ionit$ cat tests/template/function/Document.jinja
The answer to the Ultimate Question of Life, The Universe, and Everything is {{ answer_to_all_questions() }}.
user@host:~/ionit$ ./ionit -c tests/config/function -t tests/template/function
2018-08-13 11:58:16,905 ionit INFO: Loading Python module 'function' from 'tests/config/function/function.py'...
2018-08-13 11:58:16,909 ionit INFO: Rendered 'tests/template/function/Document.jinja' to 'tests/template/function/Document'.
user@host:~/ionit$ cat tests/template/function/Document
The answer to the Ultimate Question of Life, The Universe, and Everything is 42.
贡献
欢迎贡献。源代码具有100%的测试覆盖率,应予以保留。因此,请为每个错误修复提供一个测试用例,并为每个新功能提供一个或多个测试用例。请遵循如何编写Git提交信息以编写良好的提交信息。
创建发行版
此项目使用语义版本控制。要创建发行版,请增加setup.py
中的版本号,并在NEWS.md
中记录值得注意的变化。然后提交更改并标记发行版
git commit -sm "Release ionit $(./setup.py --version)" NEWS.md setup.py
git tag v$(./setup.py --version)
可以通过运行以下命令生成xz压缩的发布tar包
name="ionit-$(./setup.py --version)"
git archive --prefix="$name/" HEAD | xz -c9 > "../$name.tar.xz"
gpg --output "../$name.tar.xz.asc" --armor --detach-sign "../$name.tar.xz"
可以通过运行以下命令构建和上传用于PyPI的包
pyproject-build --no-isolation
twine upload --repository pypi dist/*
ionit-0.5.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 540daa4583504395fcffd9d5ad6174c30816f67474e464636bb9f5b25d22e587 |
|
MD5 | 87b8fb67dab1de4edfeea93334193111 |
|
BLAKE2b-256 | fbe9abfcc61257ccc66fda6f6f1ec22100a0feabe3fc75dce6e943ab85b00921 |
ionit-0.5.0-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | a5cca930e77d85e025c5f4b7ca9b8e8f5379119ef1f14ec72beee72c76bbc734 |
|
MD5 | fa5a7764702b898a89c51aea671e37ab |
|
BLAKE2b-256 | 01a1e6ca2c85754b6ca09317676d55d8516d4b878e070de7b93589b5aefd3acc |