From a6a76d68219e82137823b92904fcf7d4ba5cafcc Mon Sep 17 00:00:00 2001 From: chen-li Date: Fri, 26 Dec 2014 20:36:27 +0800 Subject: [PATCH] py3: use six.moves.urllib.parse instead of urlparse six is the canonical compatibility library for supporting Python 2 and 3 in a single codebase. The urlparse module was removed in Python 3 and we should use 'six.moves.urllib.parse' instead of 'urlparse' to make code compatible with py 2 and 3 as well. Partially-implements blueprint py3-compatibility Change-Id: Ib27244d0583e81e307d5e4236cbf85d29566dde9 --- manila/api/common.py | 12 ++++++------ manila/api/contrib/quotas.py | 8 ++++---- manila/tests/integrated/api/client.py | 4 ++-- tools/lintstack.py | 5 ++++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/manila/api/common.py b/manila/api/common.py index d51b632dd7..aeda94bc88 100644 --- a/manila/api/common.py +++ b/manila/api/common.py @@ -15,10 +15,10 @@ import os import re -import urlparse from oslo.config import cfg import six +from six.moves.urllib import parse import webob from manila.api.openstack import wsgi @@ -159,7 +159,7 @@ def remove_version_from_href(href): Returns: 'http://www.manila.com' """ - parsed_url = urlparse.urlsplit(href) + parsed_url = parse.urlsplit(href) url_parts = parsed_url.path.split('/', 2) # NOTE: this should match vX.X or vX @@ -176,7 +176,7 @@ def remove_version_from_href(href): parsed_url = list(parsed_url) parsed_url[2] = new_path - return urlparse.urlunsplit(parsed_url) + return parse.urlunsplit(parsed_url) def dict_to_query_str(params): @@ -249,10 +249,10 @@ class ViewBuilder(object): def _update_link_prefix(self, orig_url, prefix): if not prefix: return orig_url - url_parts = list(urlparse.urlsplit(orig_url)) - prefix_parts = list(urlparse.urlsplit(prefix)) + url_parts = list(parse.urlsplit(orig_url)) + prefix_parts = list(parse.urlsplit(prefix)) url_parts[0:2] = prefix_parts[0:2] - return urlparse.urlunsplit(url_parts) + return parse.urlunsplit(url_parts) class MetadataDeserializer(wsgi.MetadataXMLDeserializer): diff --git a/manila/api/contrib/quotas.py b/manila/api/contrib/quotas.py index 817801880e..419b5d0a7a 100644 --- a/manila/api/contrib/quotas.py +++ b/manila/api/contrib/quotas.py @@ -13,9 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. -import urlparse from oslo.utils import strutils +from six.moves.urllib import parse import webob from manila.api import extensions @@ -95,7 +95,7 @@ class QuotaSetsController(object): def show(self, req, id): context = req.environ['manila.context'] authorize_show(context) - params = urlparse.parse_qs(req.environ.get('QUERY_STRING', '')) + params = parse.parse_qs(req.environ.get('QUERY_STRING', '')) user_id = None if self.ext_mgr.is_loaded('os-user-quotas'): user_id = params.get('user_id', [None])[0] @@ -127,7 +127,7 @@ class QuotaSetsController(object): user_id = None if self.ext_mgr.is_loaded('os-user-quotas'): # Update user quotas only if the extended is loaded - params = urlparse.parse_qs(req.environ.get('QUERY_STRING', '')) + params = parse.parse_qs(req.environ.get('QUERY_STRING', '')) user_id = params.get('user_id', [None])[0] try: @@ -220,7 +220,7 @@ class QuotaSetsController(object): if self.ext_mgr.is_loaded('os-extended-quotas'): context = req.environ['manila.context'] authorize_delete(context) - params = urlparse.parse_qs(req.environ.get('QUERY_STRING', '')) + params = parse.parse_qs(req.environ.get('QUERY_STRING', '')) user_id = params.get('user_id', [None])[0] if user_id and not self.ext_mgr.is_loaded('os-user-quotas'): raise webob.exc.HTTPNotFound() diff --git a/manila/tests/integrated/api/client.py b/manila/tests/integrated/api/client.py index 658528a616..860d285129 100644 --- a/manila/tests/integrated/api/client.py +++ b/manila/tests/integrated/api/client.py @@ -13,9 +13,9 @@ # under the License. import httplib -import urlparse from oslo.serialization import jsonutils +from six.moves.urllib import parse from manila.openstack.common import log as logging @@ -87,7 +87,7 @@ class TestOpenStackClient(object): _headers = {'Content-Type': 'application/json'} _headers.update(headers or {}) - parsed_url = urlparse.urlparse(url) + parsed_url = parse.urlparse(url) port = parsed_url.port hostname = parsed_url.hostname scheme = parsed_url.scheme diff --git a/tools/lintstack.py b/tools/lintstack.py index c2c6a279e7..91d7b83513 100755 --- a/tools/lintstack.py +++ b/tools/lintstack.py @@ -31,7 +31,10 @@ from pylint.reporters import text ignore_codes = ["E1103"] # Note(maoy): the error message is the pattern of E0202. It should be ignored # for manila.tests modules -ignore_messages = ["An attribute affected in manila.tests"] +# Note(chen): the second error message is the pattern of [E0611] +# It should be ignored because use six module to keep py3.X compatibility. +ignore_messages = ["An attribute affected in manila.tests", + "No name 'urllib' in module '_MovedItems'"] # Note(maoy): we ignore all errors in openstack.common because it should be # checked elsewhere. We also ignore manila.tests for now due to high false # positive rate.