admin管理员组

文章数量:1295914

I need Ruamel or PyYaml or whatever Python-based YAML parser to read following lines:

TEST: &test !include test.yaml
<<: *test

Unfortunately, I cannot modify any of YAML code.

Idea is to get following result:

TEST:
  content1: value1
  content2: value2

content1: value1
content2: value2

As recommended in docs, I added constructor for tag !include:

def _getConfigWithInclusions(path, inclusionHandler):
  yaml = YAML()
  yaml.Constructor.add_constructor('!include', inclusionHandler)
  try:
    with open(path) as stream:
      res = yaml.load(stream)
  except YAMLError as e:
    raise RuntimeError(f'Most likely config file {path} is corrupted.', e)
  return res

But I encountered issue:

ruamel.yaml.constructor.ConstructorError: while constructing a mapping
  in ".../config/yaml/default/sim_top.yaml", line 2, column 1
expected a mapping or list of mappings for merging, but found scalar
  in ".../config/yaml/default/sim_top.yaml", line 2, column 10

Is I investigated, it happens because node, containing !include test.yaml, which was created at composition is (obviously) ScalarNode. But merging is done before invoking my inclusionHandler, so it cannot be any different.

This seems to be legit both for ruamel.yaml and PyYaml.

Please, could you help finding some workarounds for this case?

本文标签: pythonRuamel and PyYaml fail to combine custom tag and merge operatorStack Overflow