【问题标题】:Basic usage of ccorp-ruamel-yaml-include in Python to include YAMLPython 中 ccorp-ruamel-yaml-include 的基本用法来包含 YAML
【发布时间】:2021-05-10 08:12:27
【问题描述】:

我正在尝试使用 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 行的阅读器初始化中删除一个或两个参数(typ=['safe']pure=True)会产生相同的错误。

我做错了什么?

【问题讨论】:

  • 您的问题解决了吗?我看到了同样的问题。即使使用项目中提供的示例。

标签: python python-3.x yaml ruamel.yaml


【解决方案1】:

看起来 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']):

做了这两个改动后,效果很好。

【讨论】:

    【解决方案2】:

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

    您在查看 init 参数方面走在正确的轨道上,安全模式是默认设置,因此将它们取出无济于事,但您需要放入允许扩展的参数,我不记得确切的细节了我的头顶

    【讨论】:

    • Github 存储库的用户报告代码已损坏。该项目的一个分支纠正了它。他们的示例代码有效,我的(上面)仍然没有,但那是因为我在我的 MWE 中滥用了别名。我在回家的路上 90%。现在将其应用于我的原始代码。
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多