From 11eb5109a144ce10471fa8bf8afc046d4de61c1f Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Thu, 12 Dec 2013 14:25:06 +0100 Subject: [PATCH] Python3: replace urllib by six.moves.urllib This makes the code compatible with both Python 2 and 3. Change-Id: I721a5567842f2df6ce2a8af501787204daba3082 --- keystoneclient/base.py | 14 +++++++++++--- keystoneclient/contrib/ec2/utils.py | 6 +++--- keystoneclient/middleware/auth_token.py | 4 ++-- keystoneclient/v2_0/tenants.py | 5 ++--- keystoneclient/v2_0/users.py | 4 ++-- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/keystoneclient/base.py b/keystoneclient/base.py index 529e0c3da..df88f0998 100644 --- a/keystoneclient/base.py +++ b/keystoneclient/base.py @@ -23,9 +23,9 @@ Base utilities to build API operation managers and objects on top of. import abc import functools -import urllib import six +from six.moves import urllib from keystoneclient import exceptions from keystoneclient.openstack.common import strutils @@ -331,10 +331,14 @@ class CrudManager(Manager): def list(self, **kwargs): url = self.build_url(dict_args_in_out=kwargs) + if kwargs: + query = '?%s' % urllib.parse.urlencode(kwargs) + else: + query = '' return self._list( '%(url)s%(query)s' % { 'url': url, - 'query': '?%s' % urllib.urlencode(kwargs) if kwargs else '', + 'query': query, }, self.collection_key) @@ -364,10 +368,14 @@ class CrudManager(Manager): """Find a single item with attributes matching ``**kwargs``.""" url = self.build_url(dict_args_in_out=kwargs) + if kwargs: + query = '?%s' % urllib.parse.urlencode(kwargs) + else: + query = '' rl = self._list( '%(url)s%(query)s' % { 'url': url, - 'query': '?%s' % urllib.urlencode(kwargs) if kwargs else '', + 'query': query, }, self.collection_key) num = len(rl) diff --git a/keystoneclient/contrib/ec2/utils.py b/keystoneclient/contrib/ec2/utils.py index 3eaa60733..6383267d7 100644 --- a/keystoneclient/contrib/ec2/utils.py +++ b/keystoneclient/contrib/ec2/utils.py @@ -22,9 +22,9 @@ import base64 import hashlib import hmac import re -import urllib import six +from six.moves import urllib class Ec2Signer(object): @@ -134,8 +134,8 @@ class Ec2Signer(object): pairs = [] for key in keys: val = Ec2Signer._get_utf8_value(params[key]) - val = urllib.quote(val, safe='-_~') - pairs.append(urllib.quote(key, safe='') + '=' + val) + val = urllib.parse.quote(val, safe='-_~') + pairs.append(urllib.parse.quote(key, safe='') + '=' + val) qs = '&'.join(pairs) return qs diff --git a/keystoneclient/middleware/auth_token.py b/keystoneclient/middleware/auth_token.py index ada4aa3ce..984a1190f 100644 --- a/keystoneclient/middleware/auth_token.py +++ b/keystoneclient/middleware/auth_token.py @@ -151,10 +151,10 @@ import requests import stat import tempfile import time -import urllib import netaddr import six +from six.moves import urllib from keystoneclient.common import cms from keystoneclient import exceptions @@ -343,7 +343,7 @@ def confirm_token_not_expired(data): def safe_quote(s): """URL-encode strings that are not already URL-encoded.""" - return urllib.quote(s) if s == urllib.unquote(s) else s + return urllib.parse.quote(s) if s == urllib.parse.unquote(s) else s class InvalidUserToken(Exception): diff --git a/keystoneclient/v2_0/tenants.py b/keystoneclient/v2_0/tenants.py index 488fe93f9..46bce565e 100644 --- a/keystoneclient/v2_0/tenants.py +++ b/keystoneclient/v2_0/tenants.py @@ -14,9 +14,8 @@ # License for the specific language governing permissions and limitations # under the License. -import urllib - import six +from six.moves import urllib from keystoneclient import base @@ -108,7 +107,7 @@ class TenantManager(base.ManagerWithFind): query = "" if params: - query = "?" + urllib.urlencode(params) + query = "?" + urllib.parse.urlencode(params) reset = 0 if self.api.management_url is None: diff --git a/keystoneclient/v2_0/users.py b/keystoneclient/v2_0/users.py index d42cf6082..4ce10b24f 100644 --- a/keystoneclient/v2_0/users.py +++ b/keystoneclient/v2_0/users.py @@ -14,7 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. -import urllib +from six.moves import urllib from keystoneclient import base @@ -113,7 +113,7 @@ class UserManager(base.ManagerWithFind): query = "" if params: - query = "?" + urllib.urlencode(params) + query = "?" + urllib.parse.urlencode(params) if not tenant_id: return self._list("/users%s" % query, "users")