Python 中 ccorp-ruamel-yaml-include 的基本用法以包含 YAML

Basic usage of ccorp-ruamel-yaml-include in Python to include YAML

我正在尝试使用 ccorp-ruamel-yaml-include 来处理 YAML 文件中的 !include 指令。我试图按照源文件中的工作示例进行操作,但收到一个神秘的错误。有没有关于如何使用这个包的教程,或者有人可以在下面的例子中指出我的错误吗?

有了这两个 YAML 文件,founder.yaml

founder: &alice
   name: Alice P. Smith
   birthdate: 29 February 1970
   pref-pronouns: she/her

org.yaml

!exclude includes:
- !include founder.yaml

organization:
 name: The Eudaimonia Fund
 tagline: Promoting a better life for all
 founder: <<: *alice
 board:
   - name: Jane Person
     birthdate: September 1, 1970
     role: secretary
   - <<: *alice
     role: chair

和这个脚本,

#!/usr/bin/python

import ruamel.yaml

from ccorp.ruamel.yaml.include import YAML

reader = YAML(typ='safe', pure=True)
reader.allow_duplicate_keys = True

with open('org.yaml', 'r') as g:
    data = reader.load(g)

print("Ccorp Ruamel Yaml Include") 
print(data)

响应是:

Traceback (most recent call last):
  File "ccorp-ruamel-yaml-test.py", line 11, in <module>
    data = reader.load(g)
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/main.py", line 343, in load
    return constructor.get_single_data()
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/constructor.py", line 111, in get_single_data
    node = self.composer.get_single_node()
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 78, in get_single_node
    document = self.compose_document()
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 101, in compose_document
    node = self.compose_node(None, None)
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 138, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 38, in compose_mapping_node
    return self.__compose_dispatch(anchor, MappingNode, super().compose_mapping_node)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 27, in __compose_dispatch
    return compositor(anchor)
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 218, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 136, in compose_node
    node = self.compose_sequence_node(anchor)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 35, in compose_sequence_node
    return self.__compose_dispatch(anchor, SequenceNode, super().compose_sequence_node)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 27, in __compose_dispatch
    return compositor(anchor)
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 180, in compose_sequence_node
    node.value.append(self.compose_node(node, index))
  File "/Users/<uname>/Library/Python/3.8/lib/python/site-packages/ruamel/yaml/composer.py", line 134, in compose_node
    node = self.compose_scalar_node(anchor)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 32, in compose_scalar_node
    return self.__compose_dispatch(anchor, ScalarNode, super().compose_scalar_node)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 29, in __compose_dispatch
    return compositor(self, anchor)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 106, in include_compositor
    yaml = self.loader.fork()
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 99, in fork
    yaml = type(self)(typ=self.typ, pure=self.pure)
  File "/usr/local/lib/python3.8/site-packages/ccorp/ruamel/yaml/include/__init__.py", line 64, in __init__
    raise Exception("Can't do typ={} parsing w/ composition time directives!".format(kwargs['typ']))
Exception: Can't do typ=['safe'] parsing w/ composition time directives!

从第 7 行 reader 的初始化中删除一个或两个参数(typ=['safe']pure=True)会产生相同的错误。

我做错了什么?

错误看起来有点神秘,但它是准确的。 include 是一种组合时间扩展,换句话说,它发生在解释 yaml 的组合阶段,你不能在安全模式下使用它们。

你在查看 init 参数方面走在了正确的轨道上,安全模式是默认模式,所以将它们移除没有帮助,但你需要放入允许扩展的参数,我记不清具体是我脑海中浮现的细节

看起来 ruamel.yaml 和 ccorp.ruamel.yaml.include.

之间存在类型不匹配

在ruamel.yaml中,'typ'类型是列表(line 65)。

example.py中,应该是

yaml = YAML(typ=['safe'], pure=True)

以及 init.py

if 'typ' not in kwargs:
    kwargs['typ'] = ['safe']
elif kwargs['typ'] not in (['safe'], ['unsafe']):

完成这两个更改后,它工作正常。