4

I've got a set of YAML AWS Cloud Formation Templates that I've recently converted from JSON.

When using JSON I was able to load these templates and transform them using jinja to generate some markdown documentation from them. I'm attempting to do the same with YAML in python.

I'm using the shorthand function syntax in the cloudformation templates which uses the YAML Tags. eg

Properties:
  MinSize: !Ref ClusterSize
  MaxSize: !Ref ClusterSize

When attempting to load these with the ruamel.yaml package the Constructor fails because it is unable to handle the Tags, because it has no knowledge about them.

Is there a way I can work around this so that I'm able to load the YAML document so that I can retrieve/query the Outputs and Resources?

1
  • What are the "Outputs" and "Resources"?
    – Anthon
    Dec 11, 2016 at 19:13

1 Answer 1

3

You're mistaken that ruamel.yaml cannot handle tags. But of course you have to provide the information on how to handle any unknown tags, it cannot guess what kind of data you want to load with !Ref:

import ruamel.yaml

yaml_str = """\
Properties:
  MinSize: !Ref ClusterSize
  MaxSize: !Ref ClusterSize
"""


class Blob(object):
    def update(self, value):
        self.value = value

    def __str__(self):
        return str(self.value)


def my_constructor(self, node):
    data = Blob()
    yield data
    value = self.construct_scalar(node)
    data.update(value)

ruamel.yaml.SafeLoader.add_constructor(u'!Ref', my_constructor)

data = ruamel.yaml.safe_load(yaml_str)
print('data', data['Properties']['MinSize'])

prints:

ClusterSize

If you want to get rid of many different tags, and don't care about "everything being a string" you can also do:

import ruamel.yaml

yaml_str = """\
Properties:
  MinSize: !Ref ClusterSize
  MaxSize: !Ref ClusterSize
  SizeList:
     - !abc 1
     - !xyz 3
"""


def general_constructor(loader, tag_suffix, node):
    return node.value


ruamel.yaml.SafeLoader.add_multi_constructor(u'!', general_constructor)


data = ruamel.yaml.safe_load(yaml_str)
print(data)

which gives:

{'Properties': {'SizeList': ['1', '3'], 'MinSize': 'ClusterSize', 'MaxSize': 'ClusterSize'}}

(Please note that the scalars 1 and 3 are loaded as string instead of the normal integer)

2
  • 2
    Thanks Anthon, I didn't mean to imply that ruamel.yaml could not handle tags - I just don't know the behaviour and implementation of those tags and because I don't know how to specify them I can't load the YAML. Their are more tags then just !Ref, and some are accept lists and other other Types - Is there a way I can ignore tags, or treat them as strings, or does this not work on a more fundamental level? Dec 13, 2016 at 13:15
  • If everything can be a string then that is relatively simple, it is a bit unfortunate that you had reduced your sample YAML so far that it looked like you had only one such tag and only nested dictionaries. I'll update the answer.
    – Anthon
    Dec 13, 2016 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.