|
| 1 | +# Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +import json |
| 14 | +import yaml |
| 15 | +from yaml.resolver import ScalarNode, SequenceNode |
| 16 | + |
| 17 | +from botocore.compat import six |
| 18 | + |
| 19 | + |
| 20 | +def intrinsics_multi_constructor(loader, tag_prefix, node): |
| 21 | + """ |
| 22 | + YAML constructor to parse CloudFormation intrinsics. |
| 23 | + This will return a dictionary with key being the instrinsic name |
| 24 | + """ |
| 25 | + |
| 26 | + # Get the actual tag name excluding the first exclamation |
| 27 | + tag = node.tag[1:] |
| 28 | + |
| 29 | + # Some intrinsic functions doesn't support prefix "Fn::" |
| 30 | + prefix = "Fn::" |
| 31 | + if tag in ["Ref", "Condition"]: |
| 32 | + prefix = "" |
| 33 | + |
| 34 | + cfntag = prefix + tag |
| 35 | + |
| 36 | + if tag == "GetAtt" and isinstance(node.value, six.string_types): |
| 37 | + # ShortHand notation for !GetAtt accepts Resource.Attribute format |
| 38 | + # while the standard notation is to use an array |
| 39 | + # [Resource, Attribute]. Convert shorthand to standard format |
| 40 | + value = node.value.split(".", 1) |
| 41 | + |
| 42 | + elif isinstance(node, ScalarNode): |
| 43 | + # Value of this node is scalar |
| 44 | + value = loader.construct_scalar(node) |
| 45 | + |
| 46 | + elif isinstance(node, SequenceNode): |
| 47 | + # Value of this node is an array (Ex: [1,2]) |
| 48 | + value = loader.construct_sequence(node) |
| 49 | + |
| 50 | + else: |
| 51 | + # Value of this node is an mapping (ex: {foo: bar}) |
| 52 | + value = loader.construct_mapping(node) |
| 53 | + |
| 54 | + return {cfntag: value} |
| 55 | + |
| 56 | + |
| 57 | +def yaml_dump(dict_to_dump): |
| 58 | + """ |
| 59 | + Dumps the dictionary as a YAML document |
| 60 | + :param dict_to_dump: |
| 61 | + :return: |
| 62 | + """ |
| 63 | + return yaml.safe_dump(dict_to_dump, default_flow_style=False) |
| 64 | + |
| 65 | + |
| 66 | +def yaml_parse(yamlstr): |
| 67 | + """Parse a yaml string""" |
| 68 | + try: |
| 69 | + # PyYAML doesn't support json as well as it should, so if the input |
| 70 | + # is actually just json it is better to parse it with the standard |
| 71 | + # json parser. |
| 72 | + return json.loads(yamlstr) |
| 73 | + except ValueError: |
| 74 | + yaml.SafeLoader.add_multi_constructor( |
| 75 | + "!", intrinsics_multi_constructor) |
| 76 | + return yaml.safe_load(yamlstr) |
0 commit comments