X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ice_validator%2Ftests%2Futils%2Fyaml_custom_utils.py;h=597352ada7a6a0e3b4a253cf740c1ee948bf09ff;hb=842888dc28ebccab45e627669f7ee23f04920dc7;hp=0d292fe6b4d0e3be1a82c7bc299719c38b46032d;hpb=cc21b8b08b6dbcec577bfb26ff397ac899da8002;p=vvp%2Fvalidation-scripts.git diff --git a/ice_validator/tests/utils/yaml_custom_utils.py b/ice_validator/tests/utils/yaml_custom_utils.py index 0d292fe..597352a 100644 --- a/ice_validator/tests/utils/yaml_custom_utils.py +++ b/ice_validator/tests/utils/yaml_custom_utils.py @@ -6,7 +6,7 @@ # =================================================================== # # Unless otherwise specified, all software contained herein is licensed -# under the Apache License, Version 2.0 (the “License”); +# under the Apache License, Version 2.0 (the "License"); # you may not use this software except in compliance with the License. # You may obtain a copy of the License at # @@ -21,7 +21,7 @@ # # # Unless otherwise specified, all documentation contained herein is licensed -# under the Creative Commons License, Attribution 4.0 Intl. (the “License”); +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); # you may not use this documentation except in compliance with the License. # You may obtain a copy of the License at # @@ -35,23 +35,46 @@ # # ============LICENSE_END============================================ # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. # from yaml.constructor import ConstructorError +from yaml.nodes import MappingNode +try: + from yaml import CLoader as SafeLoader +except ImportError: + from yaml import SafeLoader -def raise_duplicates_keys(loader, node, deep=False): - """Raise error when duplicate keys found in yaml file.""" - mapping = {} - for key_node, value_node in node.value: - key = loader.construct_object(key_node, deep=deep) - value = loader.construct_object(value_node, deep=deep) - if key in mapping: +class UniqueKeyLoader(SafeLoader): + def construct_mapping(self, node, deep=False): + if not isinstance(node, MappingNode): raise ConstructorError( - "while constructing a mapping", node.start_mark, - "found duplicate key (%s)" % key, key_node.start_mark) - mapping[key] = value - - return loader.construct_mapping(node, deep) + None, + None, + "expected a mapping node, but found %s" % node.id, + node.start_mark, + ) + mapping = {} + for key_node, value_node in node.value: + key = self.construct_object(key_node, deep=deep) + try: + hash(key) + except TypeError as exc: + raise ConstructorError( + "while constructing a mapping", + node.start_mark, + "found unacceptable key (%s)" % exc, + key_node.start_mark, + ) + # check for duplicate keys + if key in mapping: + raise ConstructorError( + "while constructing a mapping", + node.start_mark, + "found duplicate key", + key_node.start_mark, + ) + value = self.construct_object(value_node, deep=deep) + mapping[key] = value + return mapping