From 4c65bf3f750a7b42a4da23cb2b9c6be5514b6522 Mon Sep 17 00:00:00 2001 From: Brad Topol Date: Tue, 15 Dec 2015 11:02:36 -0800 Subject: [PATCH] Use six compatibility library to load yaml files. Previously, python 2.x and python 3 compatibility for loading yaml files was performed by checking for exceptions. Standard python practice is to use the six compatibility library for this purpose. Code has been updated to use the six library for loading yaml files. Rearranged imports to comply with OpenStack style guidelines. Change-Id: I08637ee83daa3fbe28ce1e3795e3b4b9ccd76d54 --- toscaparser/utils/yamlparser.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/toscaparser/utils/yamlparser.py b/toscaparser/utils/yamlparser.py index a4794e7a..713650d8 100644 --- a/toscaparser/utils/yamlparser.py +++ b/toscaparser/utils/yamlparser.py @@ -12,17 +12,14 @@ import codecs from collections import OrderedDict + +from six.moves import urllib +import yaml + from toscaparser.common.exception import ExceptionCollector from toscaparser.common.exception import URLException from toscaparser.utils.gettextutils import _ -import yaml -try: - # Python 3.x - import urllib.request as urllib2 -except ImportError: - # Python 2.x - import urllib2 if hasattr(yaml, 'CSafeLoader'): yaml_loader = yaml.CSafeLoader @@ -34,8 +31,8 @@ def load_yaml(path, a_file=True): f = None try: f = codecs.open(path, encoding='utf-8', errors='strict') if a_file \ - else urllib2.urlopen(path) - except urllib2.URLError as e: + else urllib.request.urlopen(path) + except urllib.error.URLError as e: if hasattr(e, 'reason'): msg = (_('Failed to reach server "%(path)s". Reason is: ' '%(reason)s.')