Unify different names between Python2 and Python3

Some modules have different names in Python2 and Python3. This patch
make them compatible with Python 3.

 * Use six.moves.filter instead of itertools.ifilter() in Python 2.
 * Use common.py3kcompat.urlutils instead of urllib and urlparse.

Change-Id: Ia27ebf6057d91d0e129fbe90f995cfdaa89efa8a
This commit is contained in:
Chang Bo Guo 2013-12-07 20:34:28 -08:00
parent 1e8ef86626
commit db9e8d3c61
5 changed files with 88 additions and 10 deletions

View File

@ -7,6 +7,7 @@ module=gettextutils
module=importutils module=importutils
module=jsonutils module=jsonutils
module=network_utils module=network_utils
module=py3kcompat
module=sslutils module=sslutils
module=timeutils module=timeutils

View File

@ -0,0 +1,16 @@
#
# Copyright 2013 Canonical Ltd.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

View File

@ -0,0 +1,65 @@
#
# Copyright 2013 Canonical Ltd.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""
Python2/Python3 compatibility layer for OpenStack
"""
import six
if six.PY3:
# python3
import urllib.error
import urllib.parse
import urllib.request
urlencode = urllib.parse.urlencode
urljoin = urllib.parse.urljoin
quote = urllib.parse.quote
parse_qsl = urllib.parse.parse_qsl
unquote = urllib.parse.unquote
unquote_plus = urllib.parse.unquote_plus
urlparse = urllib.parse.urlparse
urlsplit = urllib.parse.urlsplit
urlunsplit = urllib.parse.urlunsplit
SplitResult = urllib.parse.SplitResult
urlopen = urllib.request.urlopen
URLError = urllib.error.URLError
pathname2url = urllib.request.pathname2url
else:
# python2
import urllib
import urllib2
import urlparse
urlencode = urllib.urlencode
quote = urllib.quote
unquote = urllib.unquote
unquote_plus = urllib.unquote_plus
parse = urlparse
parse_qsl = parse.parse_qsl
urljoin = parse.urljoin
urlparse = parse.urlparse
urlsplit = parse.urlsplit
urlunsplit = parse.urlunsplit
SplitResult = parse.SplitResult
urlopen = urllib2.urlopen
URLError = urllib2.URLError
pathname2url = urllib.pathname2url

View File

@ -27,13 +27,11 @@ __all__ = [
'set_transport_defaults', 'set_transport_defaults',
] ]
import urllib
import urlparse
from oslo.config import cfg from oslo.config import cfg
from stevedore import driver from stevedore import driver
from oslo.messaging import exceptions from oslo.messaging import exceptions
from oslo.messaging.openstack.common.py3kcompat import urlutils
_transport_opts = [ _transport_opts = [
@ -285,9 +283,9 @@ class TransportURL(object):
# Build the username and password portion of the transport URL # Build the username and password portion of the transport URL
if username is not None or password is not None: if username is not None or password is not None:
if username is not None: if username is not None:
netloc += urllib.quote(username, '') netloc += urlutils.quote(username, '')
if password is not None: if password is not None:
netloc += ':%s' % urllib.quote(password, '') netloc += ':%s' % urlutils.quote(password, '')
netloc += '@' netloc += '@'
# Build the network location portion of the transport URL # Build the network location portion of the transport URL
@ -305,7 +303,7 @@ class TransportURL(object):
url = '%s://%s/' % (self.transport, ','.join(netlocs)) url = '%s://%s/' % (self.transport, ','.join(netlocs))
if self.virtual_host: if self.virtual_host:
url += urllib.quote(self.virtual_host) url += urlutils.quote(self.virtual_host)
return url return url
@ -358,7 +356,7 @@ class TransportURL(object):
if not isinstance(url, basestring): if not isinstance(url, basestring):
raise InvalidTransportURL(url, 'Wrong URL type') raise InvalidTransportURL(url, 'Wrong URL type')
url = urlparse.urlparse(url) url = urlutils.urlparse(url)
# Make sure there's not a query string; that could identify # Make sure there's not a query string; that could identify
# requirements we can't comply with (e.g., ssl), so reject it if # requirements we can't comply with (e.g., ssl), so reject it if

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import itertools
import fixtures import fixtures
import mox import mox
from oslo.config import cfg from oslo.config import cfg
@ -231,7 +229,7 @@ class _SetDefaultsFixture(fixtures.Fixture):
def first(seq, default=None, key=None): def first(seq, default=None, key=None):
if key is None: if key is None:
key = bool key = bool
return next(itertools.ifilter(key, seq), default) return next(six.moves.filter(key, seq), default)
def default(opts, name): def default(opts, name):
return first(opts, key=lambda o: o.name == name).default return first(opts, key=lambda o: o.name == name).default