Skip to content

Commit d9759b2

Browse files
committed
Add compatibility with python 3
1 parent 87019ea commit d9759b2

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

hierarchical_yaml/config_generator.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#Copyright 2019 Adobe. All rights reserved.
2-
#This file is licensed to you under the Apache License, Version 2.0 (the "License");
3-
#you may not use this file except in compliance with the License. You may obtain a copy
4-
#of the License at http://www.apache.org/licenses/LICENSE-2.0
1+
# Copyright 2019 Adobe. All rights reserved.
2+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License. You may obtain a copy
4+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
55

6-
#Unless required by applicable law or agreed to in writing, software distributed under
7-
#the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8-
#OF ANY KIND, either express or implied. See the License for the specific language
9-
#governing permissions and limitations under the License.
6+
# Unless required by applicable law or agreed to in writing, software distributed under
7+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
# OF ANY KIND, either express or implied. See the License for the specific language
9+
# governing permissions and limitations under the License.
1010

1111
import os
1212
from collections import OrderedDict
@@ -16,11 +16,12 @@
1616
import json
1717
from .interpolation import InterpolationResolver, InterpolationValidator
1818
from .remote_state import S3TerraformRemoteStateRetriever
19-
19+
from .python_compat import iteritems
2020

2121
class ConfigProcessor(object):
2222

23-
def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml, print_data=False,
23+
def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml,
24+
print_data=False,
2425
output_file=None, skip_interpolations=False, skip_interpolation_validation=False):
2526

2627
path = self.get_relative_path(path)
@@ -94,7 +95,7 @@ def yaml_dumper():
9495
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
9596

9697
def dict_representer(dumper, data):
97-
return dumper.represent_dict(data.iteritems())
98+
return dumper.represent_dict(iteritems(data))
9899

99100
def dict_constructor(loader, node):
100101
return OrderedDict(loader.construct_pairs(node))
@@ -134,10 +135,10 @@ def merge_value(reference, new_value):
134135

135136
@staticmethod
136137
def merge_yamls(values, yaml_content):
137-
for key, value in yaml_content.iteritems():
138+
for key, value in iteritems(yaml_content):
138139
if key in values and type(values[key]) != type(value):
139140
raise Exception("Failed to merge key '{}', because of mismatch in type: {} vs {}"
140-
.format(key, type(values[key]), type(value)))
141+
.format(key, type(values[key]), type(value)))
141142
if key in values and not isinstance(value, (basestring, int, bool)):
142143
values[key] = ConfigGenerator.merge_value(values[key], value)
143144
else:

hierarchical_yaml/python_compat.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2019 Adobe. All rights reserved.
2+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License. You may obtain a copy
4+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
6+
# Unless required by applicable law or agreed to in writing, software distributed under
7+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
# OF ANY KIND, either express or implied. See the License for the specific language
9+
# governing permissions and limitations under the License.
10+
11+
import sys
12+
13+
PY3 = sys.version_info >= (3, 0)
14+
15+
if PY3:
16+
iteritems = lambda d: iter(d.items())
17+
integer_types = (int,)
18+
string_types = (str,)
19+
else:
20+
iteritems = lambda d: d.iteritems()
21+
integer_types = (int, long)
22+
string_types = (str, unicode)

0 commit comments

Comments
 (0)