Skip to content

Commit

Permalink
Rebase/blackify new commits into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuss committed Sep 4, 2019
1 parent 547874b commit 6c4ed24
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 46 deletions.
9 changes: 2 additions & 7 deletions samcli/yamlhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ def yaml_dump(dict_to_dump):
:return:
"""
FlattenAliasDumper.add_representer(OrderedDict, _dict_representer)
return yaml.dump(
dict_to_dump,
default_flow_style=False,
Dumper=FlattenAliasDumper,
)
return yaml.dump(dict_to_dump, default_flow_style=False, Dumper=FlattenAliasDumper)


def _dict_constructor(loader, node):
Expand All @@ -96,8 +92,7 @@ def yaml_parse(yamlstr):
return json.loads(yamlstr, object_pairs_hook=OrderedDict)
except ValueError:
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor)
yaml.SafeLoader.add_multi_constructor(
"!", intrinsics_multi_constructor)
yaml.SafeLoader.add_multi_constructor("!", intrinsics_multi_constructor)
return yaml.safe_load(yamlstr)


Expand Down
74 changes: 35 additions & 39 deletions tests/unit/test_yamlhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_yaml_getatt(self):
def test_parse_json_with_tabs(self):
template = '{\n\t"foo": "bar"\n}'
output = yaml_parse(template)
self.assertEqual(output, {'foo': 'bar'})
self.assertEqual(output, {"foo": "bar"})

def test_parse_json_preserve_elements_order(self):
input_template = """
Expand All @@ -88,31 +88,35 @@ def test_parse_json_preserve_elements_order(self):
}
}
"""
expected_dict = OrderedDict([
('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),
('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))
])
expected_dict = OrderedDict(
[
("B_Resource", OrderedDict([("Key2", {"Name": "name2"}), ("Key1", {"Name": "name1"})])),
("A_Resource", OrderedDict([("Key2", {"Name": "name2"}), ("Key1", {"Name": "name1"})])),
]
)
output_dict = yaml_parse(input_template)
self.assertEqual(expected_dict, output_dict)

def test_parse_yaml_preserve_elements_order(self):
input_template = (
'B_Resource:\n'
' Key2:\n'
' Name: name2\n'
' Key1:\n'
' Name: name1\n'
'A_Resource:\n'
' Key2:\n'
' Name: name2\n'
' Key1:\n'
' Name: name1\n'
"B_Resource:\n"
" Key2:\n"
" Name: name2\n"
" Key1:\n"
" Name: name1\n"
"A_Resource:\n"
" Key2:\n"
" Name: name2\n"
" Key1:\n"
" Name: name1\n"
)
output_dict = yaml_parse(input_template)
expected_dict = OrderedDict([
('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),
('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))
])
expected_dict = OrderedDict(
[
("B_Resource", OrderedDict([("Key2", {"Name": "name2"}), ("Key1", {"Name": "name1"})])),
("A_Resource", OrderedDict([("Key2", {"Name": "name2"}), ("Key1", {"Name": "name1"})])),
]
)
self.assertEqual(expected_dict, output_dict)

output_template = yaml_dump(output_dict)
Expand All @@ -127,30 +131,22 @@ def test_yaml_merge_tag(self):
"""
output = yaml_parse(test_yaml)
self.assertTrue(isinstance(output, OrderedDict))
self.assertEqual(output.get('test').get('property'), 'value')
self.assertEqual(output.get("test").get("property"), "value")

def test_unroll_yaml_anchors(self):
properties = {
"Foo": "bar",
"Spam": "eggs",
}
template = {
"Resources": {
"Resource1": {"Properties": properties},
"Resource2": {"Properties": properties}
}
}
properties = {"Foo": "bar", "Spam": "eggs"}
template = {"Resources": {"Resource1": {"Properties": properties}, "Resource2": {"Properties": properties}}}

expected = (
'Resources:\n'
' Resource1:\n'
' Properties:\n'
' Foo: bar\n'
' Spam: eggs\n'
' Resource2:\n'
' Properties:\n'
' Foo: bar\n'
' Spam: eggs\n'
"Resources:\n"
" Resource1:\n"
" Properties:\n"
" Foo: bar\n"
" Spam: eggs\n"
" Resource2:\n"
" Properties:\n"
" Foo: bar\n"
" Spam: eggs\n"
)
actual = yaml_dump(template)
self.assertEqual(actual, expected)

0 comments on commit 6c4ed24

Please sign in to comment.