对yaml格式的增强,以支持include和Python表达式
项目描述
注意:这是https://bitbucket.org/atagunov/metayaml项目的分支。
Mata Yaml是对yaml格式的几个增强,允许以下操作
从一个yaml文件中包含另一个文件
使用Python表达式和其他字段作为值
包含文件语法
要包含一个或多个文件,使用“extend”键。例如
base.yaml:
extend: - file1.yaml - file2.yaml name: base.yaml b: overridden_by_base new: new
file1.yaml:
a: a b: b c: c name: file1.yaml
file2.yaml:
a: aa b: bb d: d name: file2.yaml
文件处理顺序和顺序如下表所示
步骤 |
操作 |
中间字典 |
|---|---|---|
1 |
读取base.yaml并提取extend键 |
{"extend":
["file1.yaml", "file2.yaml"] }
|
2 |
读取file1.yaml |
{
"a": "a",
"b": "b",
"c": "c",
"name": "file1.yaml"
}
|
3 |
读取file2.yaml并合并/覆盖值 |
{
"a": "aa", # overridden
"b": "b", # overridden
"c": "c",
"d": "d", # added
"name": "file2.yaml" # overridden
}
|
4 |
读取base.yaml中的其余值并合并/覆盖 |
{
"a": "aa",
"b": "overridden_by_base", # overridden
"c": "c",
"d": "d",
"name": "base.yaml" # overridden
"new": "new" # added
}
|
表达式语法
Metayaml支持任何有效的Python表达式。为此,表达式应括在${}或$()内。第一个括号用于贪婪替换,$()用于延迟。即$()中的表达式在读取整个文件及其包含的文件后应用,而${}在文件读取过程中应用。
可以通过使用字典语法或“短划线字典语法”来访问表达式中的其他值。
示例:
base.yaml
extend:
- f1.yaml
hour: ${60*60} # just simple python expression
${2+2}: four # expression can be in the key
delay: ${hour*2} # delay is two hour or 7200 seconds
loggers:
metayaml:
name: metayaml
level: debug
console: false
backend:
name: backend
level: ${loggers.metayaml.level}
console: ${loggers.metayaml.console}
ext: ${loggers.metayaml} # copy whole dict from loggers.metayaml this key
incorrect: ${delay} ${loggers.ext} # In this case string representation of objects will be concatenated
f1.yaml
run_interval: $(hour*5) # 5 hours. But 'hour' is not defined when this file is processed.
# Therefore only $() brackets can be used here.
安装
Meta Yaml在PyPI中,因此可以直接使用以下命令安装
$ pip install metayaml
或从BitBucket
$ git clone https://bitbucket.org/atagunov/metayaml $ cd metayaml $ python setup.py install
文档
文档(如有)可在以下网址找到:https://bitbucket.org/atagunov/metayaml
使用方法
from metayaml import read
read(["config.yaml",
"test.yaml"],
{'join': os.path.join, # allows get right os specific path in yaml file
'env': os.environ} # allows use system environments from yaml file
)
config.yaml
extend:
- ${join(env["HOME"], ".metayaml", "localconfig.yaml")} # added reading local config from $HOME
user_name: ${env["USER"]}
email: ${user_name + "@example.com"}
debug: false
test.yaml
debug: true
替换顺序
替换是按照文件中的值进行的。例如,以下示例将失败
B: ${A+1} <--- A is not defined here
AA: ${B}
A: 1
但以下结果正常
A: 1
B: ${A+1}
AA: ${B}
更改合并行为
默认情况下,可以在字典中添加新键并替换列表。在某些情况下,需要从基本文件中删除键或向列表中添加一些值。例如
base.yaml:
main:
iso_3166:
China: CN
Honduras: HN
Madagascar: MG
country_codes:
- CN
- HN
- MG
country_codes_3:
- CHN
- HND
- MDG
last.yaml:
extend:
- base.yaml
main:
iso_3166:
China: ${__del__} # key 'China' will be removed from the result
Liberia: LR # add new key
country_codes:
- LR # after merge country_codes contains only one element.
country_codes_3:
${__extend__}:
- LBR # the result list is ["CHN", "HND", "MDG", "LBR"]
代码的结果
d = read("last.yaml")
print d
{
"main": {
"iso_3166": {
"Honduras": "HN",
"Madagascar": "MG",
"Liberia": "LR"
},
"country_codes": [
"LR"
],
"country_codes_3": [
"CHN",
"HND",
"MDG",
"LBR"
]
}
}
复制方法
存在一个名为‘cp’的方法,可以扩展性地复制字典/列表
cron:
daily:
min: 0
hour: 0
monthly:
min: 0
hour: 0
day: 1
schedule:
nighttask: ${cp(cron.daily, min=5)} # min will be replaced to 5
# min: 5
# hour: 0
daytask: ${cp(cron.daily, min=7, hour=13)} # min and hour are replaced
# min: 7
# hour: 13
monthtask: ${cp(cron.monthly, day=2)}
# min: 0
# hour: 0
# day: 2
deploy:
subnets:
- 1.1.1.1
- 2.2.2.2
base_elb:
- 4.4.4.4
- 5.5.5.5
elb: ${cp(deploy.subnets, "3.3.3.3", *deploy.base_elb)}
# - 1.1.1.1
# - 2.2.2.2
# - 3.3.3.3
# - 4.4.4.4
# - 5.5.5.5
继承方法
还有另一种复制现有字典并更新一些字段的方法
foo:
bar:
baz: 1
buz: 2
foobar: 3
foobar: [4, 5]
bar:
${__inherit__}: foo.bar # bar will be replaces by content of for.bar
buz: 33
# the result value of 'bar' will be
# baz: 1
# buz: 33
# foobar: 3
许可证
MetaYaml是在MIT许可证下发布的。
项目详情
下载文件
下载适合您平台的自定义文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
构建分发
metayaml-ng-1.2.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | c16f00995e211939a6ae6a51d33cf9835f85f3a42cf3857da1f761ebb4637fa8 |
|
| MD5 | 82b12cb67c312698b81768ddcc9f4836 |
|
| BLAKE2b-256 | 01326f96752f266c87c3a86b2f3064cb62d03f5c7c463f0e64a66100f8fd933e |
metayaml_ng-1.2-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 54b5c99d4ca3f32b14f36ba937f90cf614cb77f1cdc427b7577246d05bed3ddc |
|
| MD5 | 5af4d33c20faf16117bac2b450123a97 |
|
| BLAKE2b-256 | bb1bcd52277b5b469dd3292a129eb8b96bc0dea3883326fe924486277aa6da1d |