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
This commit is contained in:
Brad Topol 2015-12-15 11:02:36 -08:00
parent fca4573fa9
commit 4c65bf3f75

View File

@ -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.')