cfn-kickflip 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cfn_clean/__init__.py +176 -0
- cfn_clean/yaml_dumper.py +36 -0
- cfn_flip/__init__.py +129 -0
- cfn_flip/__main__.py +26 -0
- cfn_flip/main.py +88 -0
- cfn_flip/yaml_dumper.py +136 -0
- cfn_kickflip-2.0.0.dist-info/METADATA +193 -0
- cfn_kickflip-2.0.0.dist-info/RECORD +19 -0
- cfn_kickflip-2.0.0.dist-info/WHEEL +4 -0
- cfn_kickflip-2.0.0.dist-info/entry_points.txt +4 -0
- cfn_kickflip-2.0.0.dist-info/licenses/LICENSE +201 -0
- cfn_kickflip-2.0.0.dist-info/licenses/NOTICE.txt +9 -0
- cfn_tools/__init__.py +61 -0
- cfn_tools/_config.py +159 -0
- cfn_tools/json_encoder.py +33 -0
- cfn_tools/literal.py +29 -0
- cfn_tools/odict.py +65 -0
- cfn_tools/yaml_dumper.py +98 -0
- cfn_tools/yaml_loader.py +94 -0
cfn_clean/__init__.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
#ruff: noqa: E501
|
|
2
|
+
"""
|
|
3
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
cfn-kickflip
|
|
7
|
+
Copyright 2026 Ben Lampert
|
|
8
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
9
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
10
|
+
Python runtimes and packaging
|
|
11
|
+
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
13
|
+
You may not use this file except in compliance with the License.
|
|
14
|
+
A copy of the License is located at
|
|
15
|
+
|
|
16
|
+
https://aws.amazon.com/apache2.0/
|
|
17
|
+
|
|
18
|
+
or in the "license" file accompanying this file.
|
|
19
|
+
This file is distributed on an "AS IS" BASIS,
|
|
20
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
See the License for the specific language governing permissions
|
|
22
|
+
and limitations under the License.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
import json
|
|
27
|
+
from collections.abc import KeysView
|
|
28
|
+
|
|
29
|
+
from cfn_tools.literal import LiteralString
|
|
30
|
+
from cfn_tools.odict import ODict
|
|
31
|
+
|
|
32
|
+
UNCONVERTED_KEYS = [
|
|
33
|
+
# Resource Type, String Attribute to keep Json
|
|
34
|
+
("AWS::StepFunctions::StateMachine", "DefinitionString")
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def has_intrinsic_functions(parameter):
|
|
39
|
+
intrinsic_functions = ["Fn::Sub", "!Sub", "!GetAtt"]
|
|
40
|
+
result = False
|
|
41
|
+
if isinstance(parameter, (list, tuple, dict, KeysView)):
|
|
42
|
+
for item in parameter:
|
|
43
|
+
if item in intrinsic_functions:
|
|
44
|
+
result = True
|
|
45
|
+
break
|
|
46
|
+
return result
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def convert_join(value):
|
|
50
|
+
"""
|
|
51
|
+
Fix a Join ;)
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
if not isinstance(value, list) or len(value) != 2:
|
|
55
|
+
# Cowardly refuse
|
|
56
|
+
return value
|
|
57
|
+
|
|
58
|
+
sep, parts = value[0], value[1]
|
|
59
|
+
|
|
60
|
+
if isinstance(parts, str):
|
|
61
|
+
return parts
|
|
62
|
+
|
|
63
|
+
if not isinstance(parts, list):
|
|
64
|
+
# This looks tricky, just return the join as it was
|
|
65
|
+
return {
|
|
66
|
+
"Fn::Join": value,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
plain_string = True
|
|
70
|
+
|
|
71
|
+
args = ODict()
|
|
72
|
+
new_parts = []
|
|
73
|
+
|
|
74
|
+
for part in parts:
|
|
75
|
+
part = clean(part)
|
|
76
|
+
|
|
77
|
+
if isinstance(part, dict):
|
|
78
|
+
plain_string = False
|
|
79
|
+
|
|
80
|
+
if "Ref" in part:
|
|
81
|
+
new_parts.append("${{{}}}".format(part["Ref"]))
|
|
82
|
+
elif "Fn::GetAtt" in part:
|
|
83
|
+
params = part["Fn::GetAtt"]
|
|
84
|
+
new_parts.append("${{{}}}".format(".".join(params)))
|
|
85
|
+
else:
|
|
86
|
+
for key, val in args.items():
|
|
87
|
+
# we want to bail if a conditional can evaluate to AWS::NoValue
|
|
88
|
+
if isinstance(val, dict):
|
|
89
|
+
if "Fn::If" in val and "AWS::NoValue" in str(val["Fn::If"]):
|
|
90
|
+
return {
|
|
91
|
+
"Fn::Join": value,
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if val == part:
|
|
95
|
+
param_name = key
|
|
96
|
+
break
|
|
97
|
+
else:
|
|
98
|
+
param_name = f"Param{len(args) + 1}"
|
|
99
|
+
args[param_name] = part
|
|
100
|
+
|
|
101
|
+
new_parts.append(f"${{{param_name}}}")
|
|
102
|
+
|
|
103
|
+
elif isinstance(part, str):
|
|
104
|
+
new_parts.append(part.replace("${", "${!"))
|
|
105
|
+
|
|
106
|
+
else:
|
|
107
|
+
# Doing something weird; refuse
|
|
108
|
+
return {
|
|
109
|
+
"Fn::Join": value
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
source = sep.join(new_parts)
|
|
113
|
+
|
|
114
|
+
if plain_string:
|
|
115
|
+
return source
|
|
116
|
+
|
|
117
|
+
if args:
|
|
118
|
+
return ODict((
|
|
119
|
+
("Fn::Sub", [source, args]),
|
|
120
|
+
))
|
|
121
|
+
|
|
122
|
+
return ODict((
|
|
123
|
+
("Fn::Sub", source),
|
|
124
|
+
))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def clean(source):
|
|
128
|
+
"""
|
|
129
|
+
Clean up the source:
|
|
130
|
+
* Replace use of Fn::Join with Fn::Sub
|
|
131
|
+
* Keep json body for specific resource properties
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
if isinstance(source, dict):
|
|
135
|
+
for key, value in source.items():
|
|
136
|
+
if key == "Fn::Join":
|
|
137
|
+
return convert_join(value)
|
|
138
|
+
|
|
139
|
+
else:
|
|
140
|
+
source[key] = clean(value)
|
|
141
|
+
|
|
142
|
+
elif isinstance(source, list):
|
|
143
|
+
return [clean(item) for item in source]
|
|
144
|
+
|
|
145
|
+
return source
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def cfn_literal_parser(source):
|
|
149
|
+
"""
|
|
150
|
+
Sanitize the source:
|
|
151
|
+
* Keep json body for specific resource properties
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
if isinstance(source, dict):
|
|
155
|
+
for key, value in source.items():
|
|
156
|
+
if key == "Type":
|
|
157
|
+
for item in UNCONVERTED_KEYS:
|
|
158
|
+
if value == item[0]:
|
|
159
|
+
# Checking if this resource has "Properties" and the property literal to maintain
|
|
160
|
+
# Better check than just try/except KeyError :-)
|
|
161
|
+
if source.get("Properties") and source.get("Properties", {}).get(item[1]):
|
|
162
|
+
if isinstance(source["Properties"][item[1]], dict) and \
|
|
163
|
+
not has_intrinsic_functions(source["Properties"][item[1]].keys()):
|
|
164
|
+
source["Properties"][item[1]] = LiteralString("{}".format(json.dumps(
|
|
165
|
+
source["Properties"][item[1]],
|
|
166
|
+
indent=2,
|
|
167
|
+
separators=(',', ': '))
|
|
168
|
+
))
|
|
169
|
+
|
|
170
|
+
else:
|
|
171
|
+
source[key] = cfn_literal_parser(value)
|
|
172
|
+
|
|
173
|
+
elif isinstance(source, list):
|
|
174
|
+
return [cfn_literal_parser(item) for item in source]
|
|
175
|
+
|
|
176
|
+
return source
|
cfn_clean/yaml_dumper.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
cfn-kickflip
|
|
6
|
+
Copyright 2026 Ben Lampert
|
|
7
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
8
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
9
|
+
Python runtimes and packaging
|
|
10
|
+
|
|
11
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
12
|
+
You may not use this file except in compliance with the License.
|
|
13
|
+
A copy of the License is located at
|
|
14
|
+
|
|
15
|
+
https://aws.amazon.com/apache2.0/
|
|
16
|
+
|
|
17
|
+
or in the "license" file accompanying this file.
|
|
18
|
+
This file is distributed on an "AS IS" BASIS,
|
|
19
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
See the License for the specific language governing permissions
|
|
21
|
+
and limitations under the License.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from cfn_tools.yaml_dumper import CfnYamlDumper
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CleanCfnYamlDumper(CfnYamlDumper):
|
|
28
|
+
"""
|
|
29
|
+
Format multi-line strings with |
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def represent_scalar(self, tag, value, style=None):
|
|
33
|
+
if "\n" in value:
|
|
34
|
+
style = "|"
|
|
35
|
+
|
|
36
|
+
return super().represent_scalar(tag, value, style)
|
cfn_flip/__init__.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#ruff: noqa: E501, B904
|
|
2
|
+
"""
|
|
3
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
cfn-kickflip
|
|
7
|
+
Copyright 2026 Ben Lampert
|
|
8
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
9
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
10
|
+
Python runtimes and packaging
|
|
11
|
+
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
13
|
+
You may not use this file except in compliance with the License.
|
|
14
|
+
A copy of the License is located at
|
|
15
|
+
|
|
16
|
+
https://aws.amazon.com/apache2.0/
|
|
17
|
+
|
|
18
|
+
or in the "license" file accompanying this file.
|
|
19
|
+
This file is distributed on an "AS IS" BASIS,
|
|
20
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
See the License for the specific language governing permissions
|
|
22
|
+
and limitations under the License.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
import yaml
|
|
26
|
+
|
|
27
|
+
from cfn_clean import cfn_literal_parser, clean
|
|
28
|
+
from cfn_tools import dump_json, load_json, load_yaml
|
|
29
|
+
from cfn_tools._config import config
|
|
30
|
+
|
|
31
|
+
from .yaml_dumper import get_dumper
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def load(template):
|
|
35
|
+
"""
|
|
36
|
+
Try to guess the input format
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
data = load_json(template)
|
|
41
|
+
return data, "json"
|
|
42
|
+
except ValueError as e:
|
|
43
|
+
try:
|
|
44
|
+
data = load_yaml(template)
|
|
45
|
+
return data, "yaml"
|
|
46
|
+
except Exception:
|
|
47
|
+
raise e
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def dump_yaml(data, clean_up=False, long_form=False):
|
|
51
|
+
"""
|
|
52
|
+
Output some YAML
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
return yaml.dump(
|
|
56
|
+
data,
|
|
57
|
+
Dumper=get_dumper(clean_up, long_form),
|
|
58
|
+
default_flow_style=False,
|
|
59
|
+
allow_unicode=True,
|
|
60
|
+
width=config.max_col_width
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def to_json(template, clean_up=False):
|
|
65
|
+
"""
|
|
66
|
+
Assume the input is YAML and convert to JSON
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
data, _ = load(template)
|
|
70
|
+
|
|
71
|
+
if clean_up:
|
|
72
|
+
data = clean(data)
|
|
73
|
+
|
|
74
|
+
return dump_json(data)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def to_yaml(template, clean_up=False, long_form=False, literal=True):
|
|
78
|
+
"""
|
|
79
|
+
Assume the input is JSON and convert to YAML
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
data, _ = load(template)
|
|
83
|
+
|
|
84
|
+
if clean_up:
|
|
85
|
+
data = clean(data)
|
|
86
|
+
|
|
87
|
+
if literal:
|
|
88
|
+
data = cfn_literal_parser(data)
|
|
89
|
+
|
|
90
|
+
return dump_yaml(data, clean_up, long_form)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=False, long_form=False):
|
|
94
|
+
"""
|
|
95
|
+
Figure out the input format and convert the data to the opposing output format
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
# Do we need to figure out the input format?
|
|
99
|
+
if not in_format:
|
|
100
|
+
# Load the template as JSON?
|
|
101
|
+
if (out_format == "json" and no_flip) or (out_format == "yaml" and not no_flip):
|
|
102
|
+
in_format = "json"
|
|
103
|
+
elif (out_format == "yaml" and no_flip) or (out_format == "json" and not no_flip):
|
|
104
|
+
in_format = "yaml"
|
|
105
|
+
|
|
106
|
+
# Load the data
|
|
107
|
+
if in_format == "json":
|
|
108
|
+
data = load_json(template)
|
|
109
|
+
elif in_format == "yaml":
|
|
110
|
+
data = load_yaml(template)
|
|
111
|
+
else:
|
|
112
|
+
data, in_format = load(template)
|
|
113
|
+
|
|
114
|
+
# Clean up?
|
|
115
|
+
if clean_up:
|
|
116
|
+
data = clean(data)
|
|
117
|
+
|
|
118
|
+
# Figure out the output format
|
|
119
|
+
if not out_format:
|
|
120
|
+
if (in_format == "json" and no_flip) or (in_format == "yaml" and not no_flip):
|
|
121
|
+
out_format = "json"
|
|
122
|
+
else:
|
|
123
|
+
out_format = "yaml"
|
|
124
|
+
|
|
125
|
+
# Finished!
|
|
126
|
+
if out_format == "json":
|
|
127
|
+
return dump_json(data)
|
|
128
|
+
|
|
129
|
+
return dump_yaml(data, clean_up, long_form)
|
cfn_flip/__main__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
cfn-kickflip
|
|
6
|
+
Copyright 2026 Ben Lampert
|
|
7
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
8
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
9
|
+
Python runtimes and packaging
|
|
10
|
+
|
|
11
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
12
|
+
You may not use this file except in compliance with the License.
|
|
13
|
+
A copy of the License is located at
|
|
14
|
+
|
|
15
|
+
https://aws.amazon.com/apache2.0/
|
|
16
|
+
|
|
17
|
+
or in the "license" file accompanying this file.
|
|
18
|
+
This file is distributed on an "AS IS" BASIS,
|
|
19
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
See the License for the specific language governing permissions
|
|
21
|
+
and limitations under the License.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from .main import main
|
|
25
|
+
|
|
26
|
+
main()
|
cfn_flip/main.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#ruff: noqa: E501
|
|
2
|
+
"""
|
|
3
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
cfn-kickflip
|
|
7
|
+
Copyright 2026 Ben Lampert
|
|
8
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
9
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
10
|
+
Python runtimes and packaging
|
|
11
|
+
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
13
|
+
You may not use this file except in compliance with the License.
|
|
14
|
+
A copy of the License is located at
|
|
15
|
+
|
|
16
|
+
https://aws.amazon.com/apache2.0/
|
|
17
|
+
|
|
18
|
+
or in the "license" file accompanying this file.
|
|
19
|
+
This file is distributed on an "AS IS" BASIS,
|
|
20
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
See the License for the specific language governing permissions
|
|
22
|
+
and limitations under the License.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
import sys
|
|
26
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
27
|
+
|
|
28
|
+
import click
|
|
29
|
+
|
|
30
|
+
from . import flip
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
__version__ = version("cfn_kickflip")
|
|
34
|
+
except PackageNotFoundError:
|
|
35
|
+
__version__ = "0.0.0+unknown"
|
|
36
|
+
|
|
37
|
+
@click.command()
|
|
38
|
+
@click.option("--input", "-i", "in_format", type=click.Choice(["json", "yaml"]), help="Specify the input format. Overrides -j and -y flags.")
|
|
39
|
+
@click.option("--output", "-o", "out_format", type=click.Choice(["json", "yaml"]), help="Specify the output format. Overrides -j, -y, and -n flags.")
|
|
40
|
+
@click.option("--json", "-j", "out_flag", flag_value="json", help="Convert to JSON. Assume the input is YAML.")
|
|
41
|
+
@click.option("--yaml", "-y", "out_flag", flag_value="yaml", help="Convert to YAML. Assume the input is JSON.")
|
|
42
|
+
@click.option("--clean", "-c", is_flag=True, help="Performs some opinionated cleanup on your template.")
|
|
43
|
+
@click.option("--long", "-l", is_flag=True, help="Use long-form syntax for functions when converting to YAML.")
|
|
44
|
+
@click.option("--no-flip", "-n", is_flag=True, help="Perform other operations but do not flip the output format.")
|
|
45
|
+
@click.argument("input", type=click.File("r"), default=sys.stdin)
|
|
46
|
+
@click.argument("output", type=click.File("w"), default=sys.stdout)
|
|
47
|
+
@click.version_option(
|
|
48
|
+
version=__version__,
|
|
49
|
+
message='AWS Cloudformation Template Flip, Version %(version)s'
|
|
50
|
+
)
|
|
51
|
+
@click.pass_context
|
|
52
|
+
def main(ctx, **kwargs):
|
|
53
|
+
"""
|
|
54
|
+
AWS CloudFormation Template Flip is a tool that converts
|
|
55
|
+
AWS CloudFormation templates between JSON and YAML formats,
|
|
56
|
+
making use of the YAML format's short function syntax where possible.
|
|
57
|
+
"""
|
|
58
|
+
in_format = kwargs.pop('in_format')
|
|
59
|
+
out_format = kwargs.pop('out_format') or kwargs.pop('out_flag')
|
|
60
|
+
no_flip = kwargs.pop('no_flip')
|
|
61
|
+
clean = kwargs.pop('clean')
|
|
62
|
+
long_form = kwargs.pop('long')
|
|
63
|
+
input_file = kwargs.pop('input')
|
|
64
|
+
output_file = kwargs.pop('output')
|
|
65
|
+
|
|
66
|
+
if not in_format:
|
|
67
|
+
if input_file.name.endswith(".json"):
|
|
68
|
+
in_format = "json"
|
|
69
|
+
elif input_file.name.endswith(".yaml") or input_file.name.endswith(".yml"):
|
|
70
|
+
in_format = "yaml"
|
|
71
|
+
|
|
72
|
+
if input_file.name == "<stdin>" and sys.stdin.isatty():
|
|
73
|
+
click.echo(ctx.get_help())
|
|
74
|
+
ctx.exit()
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
flipped = flip(
|
|
78
|
+
input_file.read(),
|
|
79
|
+
in_format=in_format,
|
|
80
|
+
out_format=out_format,
|
|
81
|
+
clean_up=clean,
|
|
82
|
+
no_flip=no_flip,
|
|
83
|
+
long_form=long_form
|
|
84
|
+
)
|
|
85
|
+
output_file.write(flipped)
|
|
86
|
+
except Exception as e:
|
|
87
|
+
raise click.ClickException(f"{e}") from e
|
|
88
|
+
|
cfn_flip/yaml_dumper.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
cfn-kickflip
|
|
6
|
+
Copyright 2026 Ben Lampert
|
|
7
|
+
This product is a modified fork of AWS CloudFormation Template Flip
|
|
8
|
+
(https://github.com/awslabs/aws-cfn-template-flip), updates for modern
|
|
9
|
+
Python runtimes and packaging
|
|
10
|
+
|
|
11
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
|
12
|
+
You may not use this file except in compliance with the License.
|
|
13
|
+
A copy of the License is located at
|
|
14
|
+
|
|
15
|
+
https://aws.amazon.com/apache2.0/
|
|
16
|
+
|
|
17
|
+
or in the "license" file accompanying this file.
|
|
18
|
+
This file is distributed on an "AS IS" BASIS,
|
|
19
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
See the License for the specific language governing permissions
|
|
21
|
+
and limitations under the License.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from cfn_clean.yaml_dumper import CleanCfnYamlDumper
|
|
25
|
+
from cfn_tools._config import config
|
|
26
|
+
from cfn_tools.literal import LiteralString
|
|
27
|
+
from cfn_tools.odict import ODict
|
|
28
|
+
from cfn_tools.yaml_dumper import CfnYamlDumper
|
|
29
|
+
|
|
30
|
+
TAG_STR = "tag:yaml.org,2002:str"
|
|
31
|
+
TAG_MAP = "tag:yaml.org,2002:map"
|
|
32
|
+
CONVERTED_SUFFIXES = ["Ref", "Condition"]
|
|
33
|
+
|
|
34
|
+
FN_PREFIX = "Fn::"
|
|
35
|
+
|
|
36
|
+
# Maximum length of a string before switching to angle-bracket-style representation
|
|
37
|
+
STR_MAX_LENGTH_QUOTED = config.max_col_width
|
|
38
|
+
# Maximum number of newlines a string can have
|
|
39
|
+
# before switching to pipe-style representation
|
|
40
|
+
STR_MAX_LINES_QUOTED = 10
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Dumper(CfnYamlDumper):
|
|
44
|
+
"""
|
|
45
|
+
The standard dumper
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class CleanDumper(CleanCfnYamlDumper):
|
|
50
|
+
"""
|
|
51
|
+
Cleans up strings
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class LongDumper(CfnYamlDumper):
|
|
56
|
+
"""
|
|
57
|
+
Preserves long-form function syntax
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class LongCleanDumper(CleanCfnYamlDumper):
|
|
62
|
+
"""
|
|
63
|
+
Preserves long-form function syntax
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def literal_unicode_representer(dumper, value):
|
|
68
|
+
return dumper.represent_scalar(TAG_STR, value, style='|')
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def string_representer(dumper, value):
|
|
72
|
+
if sum(1 for nl in value if nl in ('\n', '\r')) >= STR_MAX_LINES_QUOTED:
|
|
73
|
+
return dumper.represent_scalar(TAG_STR, value, style="|")
|
|
74
|
+
|
|
75
|
+
if len(value) >= STR_MAX_LENGTH_QUOTED and '\n' not in value:
|
|
76
|
+
return dumper.represent_scalar(TAG_STR, value, style=">")
|
|
77
|
+
|
|
78
|
+
if value.startswith("0"):
|
|
79
|
+
return dumper.represent_scalar(TAG_STR, value, style="'")
|
|
80
|
+
|
|
81
|
+
return dumper.represent_scalar(TAG_STR, value)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def fn_representer(dumper, fn_name, value):
|
|
85
|
+
tag = f"!{fn_name}"
|
|
86
|
+
|
|
87
|
+
if tag == "!GetAtt" and isinstance(value, list):
|
|
88
|
+
value = ".".join(value)
|
|
89
|
+
|
|
90
|
+
if isinstance(value, list):
|
|
91
|
+
return dumper.represent_sequence(tag, value)
|
|
92
|
+
|
|
93
|
+
if isinstance(value, dict):
|
|
94
|
+
return dumper.represent_mapping(tag, value)
|
|
95
|
+
|
|
96
|
+
return dumper.represent_scalar(tag, value)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def map_representer(dumper, value):
|
|
100
|
+
"""
|
|
101
|
+
Deal with !Ref style function format and OrderedDict
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
value = ODict(value.items())
|
|
105
|
+
|
|
106
|
+
if len(value.keys()) == 1:
|
|
107
|
+
key = list(value.keys())[0]
|
|
108
|
+
|
|
109
|
+
if key in CONVERTED_SUFFIXES:
|
|
110
|
+
return fn_representer(dumper, key, value[key])
|
|
111
|
+
|
|
112
|
+
if key.startswith(FN_PREFIX):
|
|
113
|
+
return fn_representer(dumper, key[4:], value[key])
|
|
114
|
+
|
|
115
|
+
return dumper.represent_mapping(TAG_MAP, value, flow_style=False)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# Customise our dumpers
|
|
119
|
+
Dumper.add_representer(ODict, map_representer)
|
|
120
|
+
Dumper.add_representer(str, string_representer)
|
|
121
|
+
Dumper.add_representer(LiteralString, literal_unicode_representer)
|
|
122
|
+
CleanDumper.add_representer(LiteralString, literal_unicode_representer)
|
|
123
|
+
CleanDumper.add_representer(ODict, map_representer)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def get_dumper(clean_up=False, long_form=False):
|
|
127
|
+
if clean_up:
|
|
128
|
+
if long_form:
|
|
129
|
+
return LongCleanDumper
|
|
130
|
+
|
|
131
|
+
return CleanDumper
|
|
132
|
+
|
|
133
|
+
if long_form:
|
|
134
|
+
return LongDumper
|
|
135
|
+
|
|
136
|
+
return Dumper
|