使用强大、直观、命令行友好的语法读取和更改yaml/兼容数据
yamlpath的Python项目详细描述
yaml路径和命令行工具
内容:
- 简介
- 插图
- 安装
- 支持的yaml路径段
- 基于ruamel.yaml和python 3的
- 此项目的文件
- 基本用法
- 基本用法:命令行工具
- 基本用法:libraries
- 初始化ruamel.yaml和这些helpers
- 搜索yaml节点
- 更改值 < > >
< > >
< > > 哈希子属性键
(点表示法)hash.child_attr[.=key]
(搜索所有子键中的一个名为,key
,和 让出它的价值)/hash/child\u attr/key
(与1相同,但使用正斜杠表示法)/hash/child\u attr[.=key]
(与2相同,但使用正斜杠表示法) < > >别名[0]
(显式数组元素号)别名.0
(点表示法中的隐式数组元素号)别名[第一个锚点]
(按锚点名称搜索)别名[.^simple]
(搜索以"simple"开头的任何元素)别名[..%string]
(搜索任何包含"string"的元素)别名[.$value]
(搜索以"value"结尾的任何元素)别名[.=~/^(\b[s s][a-z]+\s){2}[a-z]+$/]
(搜索任何匹配的元素 一个复杂的正则表达式,恰好与示例匹配)/aliases[0]
(与1相同,但使用正斜杠表示法)/aliases/0
(与2相同,但使用正斜杠表示法)/aliases[第一个锚点]
(与3相同,但使用正斜杠表示法) < > >- 单个节点的选择:
/users/0/roles/0
=writers
- 任何给定父节点的所有子节点:
/users/1/roles
。=["超级用户","编辑器"]
- 按子属性搜索:
/users[name="user one"]/password=
一些解密值,前提是您有适当的eyaml密钥
- 根据哈希数组传递选择:
/users/roles
。=["writers"]\n["power user s","editors"]
(每个用户的角色列表是 分离结果) - 不同结果的集合:
(/users/name)
。=["user one","user two"]
(所有名称都显示在一个结果中,而不是 每行一个) - 顶级哈希键选择:
键
- 显式顶级数组元素选择:
[]
其中是基于零的 元素编号;
也可以为负数,从而选择元素 从数组末尾开始
- 隐式数组元素选择或编号哈希键选择:
其中,
是基于0的元素号或哈希键的确切名称 本身就是一个数字
- 顶级(哈希)锚查找:
&anchor_name
(需要&;
才能 表示您正在按名称查找锚点) - 散列子键:
hash.child.key
或/hash/child/key
- 虚线散列键的划分:
hash.'dotted.child.key'
或散列。"点。子。键"
(使用正斜杠表示法时不需要,/hash/dotted.child.key
) - 命名数组元素选择:
array[]
,array.
,/array[]
,或/array/
其中array
是包含数组数据的哈希键的名称是基于0的元素编号
- 在命名数组中定位查找:
array[&;anchor\u name]
其中array是 包含数组数据以及
[]
对和&;对的哈希键的名称 必须指示您正在数组中按名称查找锚点
- 数组切片:
数组[开始:停止]
其中开始
是第一个包含的, 基于零的元素和stop
是要选择的最后一个独占元素; 其中一个或两个都可以为负,导致从 数组的结尾;当start
和stop
相同时,它与数组[开始]
- 散列切片:
hash[min:max]
其中min
和max
是字母数字术语 比较散列键之间的值 - 转义符号识别:
hash.dotted\.child\.key
,/hash/whacked\/child\/key
和带有斜杠的键 - 哈希属性搜索(可以返回零个或多个匹配项):
- 完全匹配:
hash[name=admin]
- 以match开头:
hash[name^adm]
- 以匹配结尾:
hash[name$min]
- 包含匹配项:
hash[name%dmi]
- 小于匹配:
哈希[访问级别<;500]
- 大于匹配:
hash[access_level>;0]
- 小于或等于匹配:
哈希[访问级别<;=100]
- 大于或等于匹配:
hash[access_level>;=0]
- 正则表达式匹配:
hash[access\u level=~/^\d+$/]
(正则表达式 表达式分隔符可以替换所需的任何字符,但 空白;注意/
不干扰正斜杠表示法 它不需要转义,因为整个搜索表达式 包含在[]
对中) - 用
反转任何匹配项!
,例如:哈希[名称!=管理员]
甚至 <代码>哈希[!name=admin](当yaml路径是 严格化,但两种形式都是等价的) - 划分和/或转义表达式操作数,如:
hash[full\name="some user's name"]
(请注意,嵌入式的,单个的"
必须转义,以免它们被视为不匹配的标界对) - 多级匹配:
hash[name%admin].pass[encrypted!^ enc \[]
或/hash[名称%admin]/pass[加密!^附件\[]
- 完全匹配:
- 数组元素通过
(产生 任何匹配元素):
数组[.>;9000]
- 通过
(产生 它们的值,而不是键本身:
hash[.^app]
- 哈希传递选择数组:省略
哈希数组和该级别的所有匹配哈希属性将是
屈服的(或当有更多的路径时搜索的)。例如,
战士[1].power\u level
或/warrior[1]/power\u level
将返回 仅哈希数组中第二个哈希的幂级属性,而战士.力量等级
或/warriors/power\u level
将返回力量等级 同一哈希数组中每个哈希的属性。当然这些结果 可以通过多种方式进行过滤,例如战士[power珣level>;9000]
,/warriors[力量等级>;9000]
,战士。力量等级[.>;9000]
,以及/勇士/功率级[.gt;9000]
全部仅产生全部的功率级 在同一组战士散列中,战士的能量等级超过9000。 - 收集器:将yaml路径的任何部分放在括号中定义
虚拟列表收集器,如
(yaml path)
;连接和排除 支持运算符--+
和-
以及嵌套, 喜欢(…)-(…)+(…)
- 复杂组合:
一些::深层次层次结构!=""]。'any.valid'[.=~/(yaml json)/][数据%结构]。或.complexity[4].2
或者,/some::deep/hierarchy[with!=""]/any.valid[.=~/(yaml json)/][数据%结构]/或/复杂性[4]/2
- 通常有用的python库文件。它们包含了 此项目的yaml路径功能。
- 这些库的一些实现,展示了它们的功能和 简单易用的api作为命令行工具。
- 各种支持、文档和生成文件。 < > >
- yamlpath.py--核心yaml路径解析器逻辑。
- processor.py--处理要读取的yamlpath实例 或将数据写入yaml/兼容源。
- eyamlprocessor.py--扩展 支持EyaML数据加密和解密的处理器类。
简介
这个项目展示并使用yaml路径,这是一个强大的、直观的 在yaml中标识一个或多个节点的方法, eyaml,或兼容的数据结构 比如json。双点符号(灵感来自 hiera)和正斜杠表示法 (受xpath的影响 支持。库(模块)和多个命令行工具 提供了实现。有了这些,你可以建造 yaml路径支持直接进入您自己的应用程序或轻松使用其功能 立即从命令行检索或更新yaml/兼容数据。
yaml path的这个实现是一个查询语言,除了一个节点之外 描述符。使用它,您可以描述或选择一个精确的节点或 搜索符合某些条件的任意数量的节点。键、值和 所有元素都可以在任何nu中搜索数据结构中的级别数 使用相同的查询。收集器还可用于收集和进一步选择 源数据的不同部分。
插图
为了说明其中一些概念,请考虑以下示例:
---hash:child_attr:key:5280
此值5280
,可以通过yaml path标识为以下任一值:
---aliases:-&first_anchorSimple string value
使用yaml path,您可以通过以下任何等效选项选择此锚定值 表达式:
---users:-name:User Onepassword:ENC[PKCS7,MIIBiQY...Jk==]roles:-Writers-name:User Twopassword:ENC[PKCS7,MIIBiQY...vF==]roles:-Power Users-Editors
通过这样的示例,yaml path启用:
要深入了解yaml path的功能,请访问 项目wiki
支持的yaml路径段
yaml path段是标识父级的分隔符之间的文本
或数据结构中的叶节点。对于点表示法,路径如下
hash.key
标识两个段:hash
(父节点)和key
(叶
节点)。正斜杠表示法中的相同路径是:/hash/key
yaml path了解这些段类型:
这种yaml路径的实现鼓励了创造力。使用任何符号 以及在每个应用程序中对您最有意义的段类型。
安装
这个项目需要python3.6。大多数操作 系统和发行版可以访问Python3,即使只有Python2——或者 根本没有python——是预先安装的。一般来说,多吃一点是安全的 在您的系统上同时使用一个版本的python,特别是当 使用 虚拟python环境
此项目的每个已发布版本都可以从
pypi使用pip
。请注意,在具有多个
python的版本,您可能需要使用pip3
或等效的(例如:
cygwin用户可能需要使用pip3.6
)。
pip3 install yamlpath
EyaML支持是完全可选的。您不需要eyaml来使用yaml path。
yaml path支持eyaml是一种面向大量观众的服务:puppet
用户。在撰写本文时,eyaml(被归类为hiera
后端/插件)只能作为ruby gem提供。也就是说,它提供了
命令行工具,eyaml
,可用于此python
项目。要享受eyaml支持,请安装兼容版本的ruby和
rubygems,然后执行:
gem install hiera-eyaml
如果这将eyaml
命令放在系统path
上,则无需
除了生成或获取加密密钥之外。否则,你
可以告诉yaml路径库和工具在哪里可以找到eyaml
命令。
基于ruamel.yaml和python 3
为了支持最好的可用yaml编辑功能(所谓, 支持注释保存的往返编辑),此项目基于 关于 巨蟒3.6。而ruamel.yaml基于pyyaml-- python的"标准"yaml库ruamel.yaml在客观上比 pyyaml,它缺少关键 往返编辑功能以及最新的yaml/兼容数据 解析功能(在编写本文时)。
如果pyyaml与ruamel.yaml合并,或者至少赶上ruamel.yaml,这个 相反,项目可以(稍微)调整以依赖它。这些对话 可以提供一些有关何时或是否可能发生这种情况的信息:
此项目的文件
此存储库包含:
命令行工具
这个项目提供了一些使用yaml的命令行工具实现。 路径。对于这些工具的一些用例示例, 请参见下文
提供的命令行工具包括:
usage: eyaml-rotate-keys [-h] [-V] [-d | -v | -q] [-b] [-x EYAML] -i OLDPRIVATEKEY -c OLDPUBLICKEY -r NEWPRIVATEKEY -u NEWPUBLICKEY YAML_FILE [YAML_FILE ...] Rotates the encryption keys used for all EYAML values within a set of YAML files, decrypting with old keys and re-encrypting using replacement keys. positional arguments: YAML_FILE one or more YAML files containing EYAML values optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -d, --debug output debugging details -v, --verbose increase output verbosity -q, --quiet suppress all output except errors -b, --backup save a backup of each modified YAML_FILE with an extra .bak file-extension -x EYAML, --eyaml EYAML the eyaml binary to use when it isn't on the PATH EYAML_KEYS: All key arguments are required -r NEWPRIVATEKEY, --newprivatekey NEWPRIVATEKEY the new EYAML private key -u NEWPUBLICKEY, --newpublickey NEWPUBLICKEY the new EYAML public key -i OLDPRIVATEKEY, --oldprivatekey OLDPRIVATEKEY the old EYAML private key -c OLDPUBLICKEY, --oldpublickey OLDPUBLICKEY the old EYAML public key Any YAML_FILEs lacking EYAML values will not be modified (or backed up, even when -b/--backup is specified).
usage: yaml-get [-h] [-V] -p YAML_PATH [-t ['.', '/', 'auto', 'dot', 'fslash']] [-x EYAML] [-r PRIVATEKEY] [-u PUBLICKEY] [-d | -v | -q] YAML_FILE Retrieves one or more values from a YAML file at a specified YAML Path. Output is printed to STDOUT, one line per result. When a result is a complex data- type (Array or Hash), a JSON dump is produced to represent it. EYAML can be employed to decrypt the values. positional arguments: YAML_FILE the YAML file to query optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -t ['.', '/', 'auto', 'dot', 'fslash'], --pathsep ['.', '/', 'auto', 'dot', 'fslash'] indicate which YAML Path seperator to use when rendering results; default=dot -d, --debug output debugging details -v, --verbose increase output verbosity -q, --quiet suppress all output except errors required settings: -p YAML_PATH, --query YAML_PATH YAML Path to query EYAML options: Left unset, the EYAML keys will default to your system or user defaults. Both keys must be set either here or in your system or user EYAML configuration file when using EYAML. -x EYAML, --eyaml EYAML the eyaml binary to use when it isn't on the PATH -r PRIVATEKEY, --privatekey PRIVATEKEY EYAML private key -u PUBLICKEY, --publickey PUBLICKEY EYAML public key For more information about YAML Paths, please visit https://github.com/wwkimball/yamlpath.
usage: yaml-paths [-h] [-V] -s EXPRESSION [-c EXPRESSION] [-d | -v | -q] [-p] [-m] [-t ['.', '/', 'auto', 'dot', 'fslash']] [-i | -k | -K] [-a] [-A | -Y | -y | -l] [-e] [-x EYAML] [-r PRIVATEKEY] [-u PUBLICKEY] YAML_FILE [YAML_FILE ...] Returns zero or more YAML Paths indicating where in given YAML/Compatible data one or more search expressions match. Values, keys, and/or anchors can be searched. EYAML can be employed to search encrypted values. positional arguments: YAML_FILE one or more YAML files to search optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -c EXPRESSION, --except EXPRESSION except results matching this search expression; can be set more than once -d, --debug output debugging details -v, --verbose increase output verbosity -q, --quiet suppress all non-result output except errors -p, --pathonly print results without any search expression decorators -m, --expand expand matching parent nodes to list all permissible child leaf nodes (see "Reference handling options" for restrictions) -t ['.', '/', 'auto', 'dot', 'fslash'], --pathsep ['.', '/', 'auto', 'dot', 'fslash'] indicate which YAML Path seperator to use when rendering results; default=dot -a, --refnames also search the names of &anchor and *alias references required settings: -s EXPRESSION, --search EXPRESSION the search expression; can be set more than once Key name searching options: -i, --ignorekeynames (default) do not search key names -k, --keynames search key names in addition to values and array elements -K, --onlykeynames only search key names (ignore all values and array elements) Reference handling options: Indicate how to treat anchor and alias references. An anchor is an original, reusable key or value. All aliases become replaced by the anchors they reference when YAML data is read. These options specify how to handle this duplication of keys and values. Note that the default behavior includes all aliased keys but not aliased values. -A, --anchorsonly include only original matching key and value anchors in results, discarding all aliased keys and values (including child nodes) -Y, --allowkeyaliases (default) include matching key aliases, permitting search traversal into their child nodes -y, --allowvaluealiases include matching value aliases (does not permit search traversal into aliased keys) -l, --allowaliases include all matching key and value aliases EYAML options: Left unset, the EYAML keys will default to your system or user defaults. Both keys must be set either here or in your system or user EYAML configuration file when using EYAML. -e, --decrypt decrypt EYAML values in order to search them (otherwise, search the encrypted blob) -x EYAML, --eyaml EYAML the eyaml binary to use when it isn't on the PATH -r PRIVATEKEY, --privatekey PRIVATEKEY EYAML private key -u PUBLICKEY, --publickey PUBLICKEY EYAML public key A search or exception EXPRESSION takes the form of a YAML Path search operator -- %, $, =, ^, >, <, >=, <=, =~, or ! -- followed by the search term, omitting the left-hand operand. For more information about YAML Paths, please visit https://github.com/wwkimball/yamlpath.
usage: yaml-set [-h] [-V] -g YAML_PATH [-a VALUE | -f FILE | -i | -R LENGTH] [-F {bare,boolean,default,dquote,float,folded,int,literal,squote}] [-c CHECK] [-s YAML_PATH] [-m] [-b] [-t ['.', '/', 'auto', 'dot', 'fslash']] [-e] [-x EYAML] [-r PRIVATEKEY] [-u PUBLICKEY] [-d | -v | -q] YAML_FILE Changes one or more values in a YAML file at a specified YAML Path. Matched values can be checked before they are replaced to mitigate accidental change. When matching singular results, the value can be archived to another key before it is replaced. Further, EYAML can be employed to encrypt the new values and/or decrypt an old value before checking them. positional arguments: YAML_FILE the YAML file to update optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -F {bare,boolean,default,dquote,float,folded,int,literal,squote}, --format {bare,boolean,default,dquote,float,folded,int,literal,squote} override automatic formatting of the new value -c CHECK, --check CHECK check the value before replacing it -s YAML_PATH, --saveto YAML_PATH save the old value to YAML_PATH before replacing it; implies --mustexist -m, --mustexist require that the --change YAML_PATH already exist in YAML_FILE -b, --backup save a backup YAML_FILE with an extra .bak file- extension -t ['.', '/', 'auto', 'dot', 'fslash'], --pathsep ['.', '/', 'auto', 'dot', 'fslash'] indicate which YAML Path seperator to use when rendering results; default=dot -d, --debug output debugging details -v, --verbose increase output verbosity -q, --quiet suppress all output except errors required settings: -g YAML_PATH, --change YAML_PATH YAML Path where the target value is found input options: -a VALUE, --value VALUE set the new value from the command-line instead of STDIN -f FILE, --file FILE read the new value from file (discarding any trailing new-lines) -i, --stdin accept the new value from STDIN (best for sensitive data) -R LENGTH, --random LENGTH randomly generate a replacement value of a set length EYAML options: Left unset, the EYAML keys will default to your system or user defaults. You do not need to supply a private key unless you enable --check and the old value is encrypted. -e, --eyamlcrypt encrypt the new value using EYAML -x EYAML, --eyaml EYAML the eyaml binary to use when it isn't on the PATH -r PRIVATEKEY, --privatekey PRIVATEKEY EYAML private key -u PUBLICKEY, --publickey PUBLICKEY EYAML public key When no changes are made, no backup is created, even when -b/--backup is specified. For more information about YAML Paths, please visit https://github.com/wwkimball/yamlpath.
库
虽然有几个支持的库文件,如枚举、类型和 例外情况,最有趣的库文件包括:
基本用法
此项目的文件可以用作命令行工具,也可以用作 图书馆补充您自己的工作。
基本用法:命令行工具
命令行工具是自文档化的,它们的文档被捕获
上面的便于参考。只需将--help
传递给他们
订购以获得相同的详细文档。
请查看综合测试命令py unit tests 探索yaml文件的示例以及这些工具帮助获取和设置的多种方式 他们的数据。
下面是一些典型用例的简单示例。
旋转eyaml键
如果eyaml命令已经在您的路径上(如果不是,请确保还提供
可选的--eyaml
或-x
参数:
---hash:child_attr:key:52800
如果您的e/yaml文件是 分散在目录层次结构中,如hiera数据。
获取yaml值
最简单的是:
---hash:child_attr:key:52801
搜索yaml路径
最简单的用法:
---hash:child_attr:key:52802
搜索多个表达式并排除不需要的结果:
---hash:child_attr:key:52803
返回匹配父项下的所有叶节点(在与哈希键匹配时最有用,并且只希望原始叶节点位于它们下面):
---hash:child_attr:key:52804
更改yaml值
对于无修饰的更改,请更改为具有深度嵌套哈希结构的yaml文件:
---hash:child_attr:key:52805
要旋转密码,请保留旧密码,这样您的自动化就可以 将新密码应用于您的应用程序:
---hash:child_attr:key:52806
非常小心的是,你可以在旋转之前检查旧密码 然后保存原始文件的备份:
---hash:child_attr:key:52807
您还可以添加eyaml加密(假设
路径;如果不是,则可以通过--eyaml
指定其位置)。在这个例子中,
我添加了可选的--format=folded
以便将long eyaml值分解
变成一个多行值而不是一个很长的字符串。这是首选
用于人类易读性和Eyaml用户的格式
木偶。注意--format
有几个其他设置
仅适用于新值。
---hash:child_attr:key:52808
如果不是默认系统或用户,您甚至可以告诉eyaml要使用哪些密钥 按键:
---hash:child_attr:key:52809
请注意,对于更高的安全性方案,您可以不使用
通过交换--value,您的命令行、进程列表和命令历史记录
对于
--stdin
、--file
或甚至--随机长度
(使用python的
如果不需要指定替换项,则使用最强的随机值生成器
预先估价)。
基本用法:库
至于库,它们也有大量的文档和示例 实现也许可以作为良好的复制粘贴素材(如果您提供 归功于消息来源)。也就是说,这里有一个总的流程/概要。
初始化ruamel.yaml和这些帮助程序
您的偏好可能不同,但我使用此设置进行双向yaml解析
与ruamel.yaml一起编辑。当您需要处理eyaml加密数据时,
将yamlpath.processor
替换为yamlpath.eyaml.eyamlprocessor
并添加错误
处理yamlpath.eyaml.eyamlcandexception
注意import yamlpath.patches
是完全可选的。我用它来写
阻止ruamel.yaml的发射器向折叠的
值(它不正确地将每一个新行转换为两个用于左刷新
多行值,在编写本文时)。因为"block"输出eyaml
值是左对齐的多行折叠字符串,在
使用eyaml功能。至少,在ruamel.yaml有自己的解决方案之前
问题。
还要注意,这些示例使用consolepinter
来处理stdout和stderr
消息传递。你不必。但是,必须将某种记录器传递给
这些库可以在某处写消息。您的自定义消息
处理程序或记录器必须提供与consolepinter相同的api;请查看
consoleprint.py中的标题文档
详细情况。一般来说,写你自己的习惯是微不足道的
python标准日志记录工具的包装(如果需要其他目标)
而不是标准输出和标准输出。
---aliases:-&first_anchorSimple string value0
搜索yaml节点
---aliases:-&first_anchorSimple string value1
更改值
最简单的是,您只需要向一个或多个节点提供yaml路径。
更新,以及应用于它们的值。吸引人的
yamlpath.exceptions.yamlpatheexception
是可选的,但通常优先于
允许python在用户面前转储调用堆栈。使用时
eyaml,同样适用于yamlpath.eyaml.exceptions.eyamlcandexception
---aliases:-&first_anchorSimple string value2