3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 from six
.moves
.urllib
.parse
import urlparse
26 _localedir
= os
.environ
.get('yang-translator'.upper() + '_LOCALEDIR')
27 _t
= gettext
.translation('yang-translator', localedir
=_localedir
,
32 return _t
.gettext(msg
)
35 class CompareUtils(object):
37 MISMATCH_VALUE1_LABEL
= "<Expected>"
38 MISMATCH_VALUE2_LABEL
= "<Provided>"
39 ORDERLESS_LIST_KEYS
= ['allowed_values', 'depends_on']
42 def compare_dicts(dict1
, dict2
, log
):
43 """Return False if not equal, True if both are equal."""
45 if dict1
is None and dict2
is None:
47 if dict1
is None or dict2
is None:
51 for dict1_item
, dict2_item
in zip(dict1
.items(), dict2
.items()):
52 if dict1_item
!= dict2_item
:
53 msg
= (_("%(label1)s: %(item1)s \n is not equal to \n:"
54 "%(label2)s: %(item2)s")
55 % {'label1': CompareUtils
.MISMATCH_VALUE2_LABEL
,
57 'label2': CompareUtils
.MISMATCH_VALUE1_LABEL
,
66 '''Canonicalize list items in the dictionary for ease of comparison.
68 For properties whose value is a list in which the order does not
69 matter, some pre-processing is required to bring those lists into a
70 canonical format. We use sorting just to make sure such differences
71 in ordering would not cause to a mismatch.
74 if type(dic
) is not dict:
78 for key
in dic
.keys():
80 if type(value
) is dict:
81 reordered
[key
] = CompareUtils
.reorder(value
)
82 elif type(value
) is list \
83 and key
in CompareUtils
.ORDERLESS_LIST_KEYS
:
84 reordered
[key
] = sorted(value
)
86 reordered
[key
] = value
90 def diff_dicts(dict1
, dict2
, reorder
=True):
91 '''Compares two dictionaries and returns their differences.
93 Returns a dictionary of mismatches between the two dictionaries.
94 An empty dictionary is returned if two dictionaries are equivalent.
95 The reorder parameter indicates whether reordering is required
96 before comparison or not.
100 dict1
= CompareUtils
.reorder(dict1
)
101 dict2
= CompareUtils
.reorder(dict2
)
103 if dict1
is None and dict2
is None:
105 if dict1
is None or dict2
is None:
106 return {CompareUtils
.MISMATCH_VALUE1_LABEL
: dict1
,
107 CompareUtils
.MISMATCH_VALUE2_LABEL
: dict2
}
110 keys1
= set(dict1
.keys())
111 keys2
= set(dict2
.keys())
112 for key
in keys1
.union(keys2
):
113 if key
in keys1
and key
not in keys2
:
114 diff
[key
] = {CompareUtils
.MISMATCH_VALUE1_LABEL
: dict1
[key
],
115 CompareUtils
.MISMATCH_VALUE2_LABEL
: None}
116 elif key
not in keys1
and key
in keys2
:
117 diff
[key
] = {CompareUtils
.MISMATCH_VALUE1_LABEL
: None,
118 CompareUtils
.MISMATCH_VALUE2_LABEL
: dict2
[key
]}
123 if type(val1
) is dict and type(val2
) is dict:
124 diff
[key
] = CompareUtils
.diff_dicts(val1
, val2
, False)
126 diff
[key
] = {CompareUtils
.MISMATCH_VALUE1_LABEL
: val1
,
127 CompareUtils
.MISMATCH_VALUE2_LABEL
: val2
}
131 class YamlUtils(object):
134 def get_dict(yaml_file
):
135 '''Returns the dictionary representation of the given YAML spec.'''
137 return yaml
.load(open(yaml_file
))
142 def compare_yamls(yaml1_file
, yaml2_file
):
143 '''Returns true if two dictionaries are equivalent, false otherwise.'''
144 dict1
= YamlUtils
.get_dict(yaml1_file
)
145 dict2
= YamlUtils
.get_dict(yaml2_file
)
146 return CompareUtils
.compare_dicts(dict1
, dict2
)
149 def compare_yaml_dict(yaml_file
, dic
):
150 '''Returns true if yaml matches the dictionary, false otherwise.'''
151 return CompareUtils
.compare_dicts(YamlUtils
.get_dict(yaml_file
), dic
)
154 class UrlUtils(object):
157 def validate_url(path
):
158 """Validates whether the given path is a URL or not.
160 If the given path includes a scheme (http, https, ftp, ...) and a net
161 location (a domain name such as www.github.com) it is validated as a
164 parsed
= urlparse(path
)
165 return bool(parsed
.scheme
) and bool(parsed
.netloc
)
168 def str_to_num(value
):
169 """Convert a string representation of a number into a numeric type."""
170 if isinstance(value
, numbers
.Number
):
178 def map_name_to_python(name
):
181 return name
.replace('-', '_')
184 def convert_keys_to_python(d
):
185 '''Change all keys from - to _'''
186 if isinstance(d
, dict):
188 d
[map_name_to_python(key
)] = convert_keys_to_python(d
.pop(key
))
190 elif isinstance(d
, list):
193 arr
.append(convert_keys_to_python(memb
))
198 def map_name_to_yang(name
):
199 return name
.replace('_', '-')
202 def convert_keys_to_yang(d
):
203 '''Change all keys from _ to -'''
204 if isinstance(d
, dict):
206 d
[map_name_to_python(key
)] = convert_keys_to_yang(d
.pop(key
))
208 elif isinstance(d
, list):
211 arr
.append(convert_keys_to_yang(memb
))
217 def stringify_dict(d
):
218 '''Convert all integer, float, etc to str'''
219 if isinstance(d
, dict):
221 d
[key
] = stringify_dict(d
[key
])
223 elif isinstance(d
, list):
226 arr
.append(stringify_dict(memb
))
229 if not isinstance(d
, str):