From 5c6e00406bbffb813555e2b32d63fe3f1852d402 Mon Sep 17 00:00:00 2001 From: llg8212 Date: Sat, 15 Feb 2014 10:25:40 +0800 Subject: [PATCH] Remove dependent module py3kcompat Module py3kcompat was removed from oslo-incubator, we can use six directly. * Sync commit of removing py3kcompat from oslo * use six replace usage of py3kcompat Change-Id: I15b9ffb10e2d5765a1ed07dd5fd4e2bf7b21ec49 Closes-Bug: #1280033 --- openstack-common.conf | 1 - troveclient/base.py | 6 +- troveclient/common.py | 7 +- troveclient/compat/common.py | 7 +- .../openstack/common/apiclient/__init__.py | 14 ---- .../openstack/common/apiclient/auth.py | 6 +- .../openstack/common/apiclient/base.py | 22 ++++--- .../openstack/common/apiclient/exceptions.py | 4 +- .../openstack/common/apiclient/fake_client.py | 7 +- .../openstack/common/py3kcompat/__init__.py | 16 ----- .../openstack/common/py3kcompat/urlutils.py | 65 ------------------- troveclient/tests/test_base.py | 16 ++--- 12 files changed, 31 insertions(+), 140 deletions(-) delete mode 100644 troveclient/openstack/common/py3kcompat/__init__.py delete mode 100644 troveclient/openstack/common/py3kcompat/urlutils.py diff --git a/openstack-common.conf b/openstack-common.conf index 02fa03e..7022afb 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -4,7 +4,6 @@ module=apiclient module=strutils module=install_venv_common -module=py3kcompat # The base module to hold the copy of openstack.common base=troveclient diff --git a/troveclient/base.py b/troveclient/base.py index 1189f79..318ab98 100644 --- a/troveclient/base.py +++ b/troveclient/base.py @@ -26,12 +26,12 @@ import hashlib import os import six +from six.moves.urllib import parse from troveclient.openstack.common.apiclient import base from troveclient.openstack.common.apiclient import exceptions from troveclient import utils from troveclient import common -from troveclient.openstack.common.py3kcompat import urlutils # Python 2.4 compat try: @@ -71,8 +71,8 @@ class Manager(utils.HookableMixin): next_marker = None for link in next_links: # Extract the marker from the url. - parsed_url = urlutils.urlparse(link) - query_dict = dict(urlutils.parse_qsl(parsed_url.query)) + parsed_url = parse.urlparse(link) + query_dict = dict(parse.parse_qsl(parsed_url.query)) next_marker = query_dict.get('marker') data = [self.resource_class(self, res) for res in body[response_key]] return common.Paginated(data, next_marker=next_marker, links=links) diff --git a/troveclient/common.py b/troveclient/common.py index 8869dbf..6015620 100644 --- a/troveclient/common.py +++ b/troveclient/common.py @@ -15,11 +15,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +from six.moves.urllib import parse from troveclient.openstack.common.apiclient import exceptions -from troveclient.openstack.common.py3kcompat import urlutils - def check_for_exceptions(resp, body, url): if resp.status_code in (400, 422, 500): @@ -41,9 +40,9 @@ def limit_url(url, limit=None, marker=None): def quote_user_host(user, host): quoted = '' if host: - quoted = urlutils.quote("%s@%s" % (user, host)) + quoted = parse.quote("%s@%s" % (user, host)) else: - quoted = urlutils.quote("%s" % user) + quoted = parse.quote("%s" % user) return quoted.replace('.', '%2e') diff --git a/troveclient/compat/common.py b/troveclient/compat/common.py index 11853c3..ca4b1e6 100644 --- a/troveclient/compat/common.py +++ b/troveclient/compat/common.py @@ -18,14 +18,13 @@ import optparse import os import pickle import six +from six.moves.urllib import parse import sys from troveclient.compat import client from troveclient.compat import xml from troveclient.compat import exceptions -from troveclient.openstack.common.py3kcompat import urlutils - def methods_of(obj): """Get all callable methods of an object that don't start with underscore @@ -75,9 +74,9 @@ def limit_url(url, limit=None, marker=None): def quote_user_host(user, host): quoted = '' if host: - quoted = urlutils.quote("%s@%s" % (user, host)) + quoted = parse.quote("%s@%s" % (user, host)) else: - quoted = urlutils.quote("%s" % user) + quoted = parse.quote("%s" % user) return quoted.replace('.', '%2e') diff --git a/troveclient/openstack/common/apiclient/__init__.py b/troveclient/openstack/common/apiclient/__init__.py index f3d0cde..e69de29 100644 --- a/troveclient/openstack/common/apiclient/__init__.py +++ b/troveclient/openstack/common/apiclient/__init__.py @@ -1,14 +0,0 @@ -# Copyright 2013 OpenStack Foundation -# 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. diff --git a/troveclient/openstack/common/apiclient/auth.py b/troveclient/openstack/common/apiclient/auth.py index eb925d4..ded1435 100644 --- a/troveclient/openstack/common/apiclient/auth.py +++ b/troveclient/openstack/common/apiclient/auth.py @@ -19,7 +19,6 @@ import abc import argparse -import logging import os import six @@ -28,9 +27,6 @@ from stevedore import extension from troveclient.openstack.common.apiclient import exceptions -logger = logging.getLogger(__name__) - - _discovered_plugins = {} @@ -80,7 +76,7 @@ def load_plugin_from_args(args): alphabetical order. :type args: argparse.Namespace - :raises: AuthorizationFailure + :raises: AuthPluginOptionsMissing """ auth_system = args.os_auth_system if auth_system: diff --git a/troveclient/openstack/common/apiclient/base.py b/troveclient/openstack/common/apiclient/base.py index 8c47ed2..10ff0db 100644 --- a/troveclient/openstack/common/apiclient/base.py +++ b/troveclient/openstack/common/apiclient/base.py @@ -24,11 +24,12 @@ Base utilities to build API operation managers and objects on top of. # pylint: disable=E1102 import abc +import copy import six +from six.moves.urllib import parse from troveclient.openstack.common.apiclient import exceptions -from troveclient.openstack.common.py3kcompat import urlutils from troveclient.openstack.common import strutils @@ -327,7 +328,7 @@ class CrudManager(BaseManager): return self._list( '%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), - 'query': '?%s' % urlutils.urlencode(kwargs) if kwargs else '', + 'query': '?%s' % parse.urlencode(kwargs) if kwargs else '', }, self.collection_key) @@ -366,7 +367,7 @@ class CrudManager(BaseManager): rl = self._list( '%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), - 'query': '?%s' % urlutils.urlencode(kwargs) if kwargs else '', + 'query': '?%s' % parse.urlencode(kwargs) if kwargs else '', }, self.collection_key) num = len(rl) @@ -456,17 +457,17 @@ class Resource(object): def __getattr__(self, k): if k not in self.__dict__: #NOTE(bcwaldon): disallow lazy-loading if already loaded once - if not self.is_loaded(): - self.get() + if not self.is_loaded: + self._get() return self.__getattr__(k) raise AttributeError(k) else: return self.__dict__[k] - def get(self): - # set_loaded() first ... so if we have to bail, we know we tried. - self.set_loaded(True) + def _get(self): + # set _loaded first ... so if we have to bail, we know we tried. + self._loaded = True if not hasattr(self.manager, 'get'): return @@ -484,8 +485,9 @@ class Resource(object): return self.id == other.id return self._info == other._info + @property def is_loaded(self): return self._loaded - def set_loaded(self, val): - self._loaded = val + def to_dict(self): + return copy.deepcopy(self._info) diff --git a/troveclient/openstack/common/apiclient/exceptions.py b/troveclient/openstack/common/apiclient/exceptions.py index 4776d58..faff990 100644 --- a/troveclient/openstack/common/apiclient/exceptions.py +++ b/troveclient/openstack/common/apiclient/exceptions.py @@ -425,8 +425,8 @@ def from_response(response, method, url): except ValueError: pass else: - if hasattr(body, "keys"): - error = body[body.keys()[0]] + if isinstance(body, dict): + error = list(body.values())[0] kwargs["message"] = error.get("message", None) kwargs["details"] = error.get("details", None) elif content_type.startswith("text/"): diff --git a/troveclient/openstack/common/apiclient/fake_client.py b/troveclient/openstack/common/apiclient/fake_client.py index 32e0524..8c733a8 100644 --- a/troveclient/openstack/common/apiclient/fake_client.py +++ b/troveclient/openstack/common/apiclient/fake_client.py @@ -28,10 +28,9 @@ import json import requests import six +from six.moves.urllib import parse from troveclient.openstack.common.apiclient import client -from troveclient.openstack.common.py3kcompat import urlutils -from troveclient.openstack.common import strutils def assert_has_keys(dct, required=[], optional=[]): @@ -64,7 +63,7 @@ class TestResponse(requests.Response): self._content = text default_headers = {} if six.PY3 and isinstance(self._content, six.string_types): - self._content = strutils.safe_encode(self._content) + self._content = self._content.encode('utf-8', 'strict') self.headers = data.get('headers') or default_headers else: self.status_code = data @@ -148,7 +147,7 @@ class FakeHTTPClient(client.HTTPClient): "text": fixture[1]}) # Call the method - args = urlutils.parse_qsl(urlutils.urlparse(url)[4]) + args = parse.parse_qsl(parse.urlparse(url)[4]) kwargs.update(args) munged_url = url.rsplit('?', 1)[0] munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_') diff --git a/troveclient/openstack/common/py3kcompat/__init__.py b/troveclient/openstack/common/py3kcompat/__init__.py deleted file mode 100644 index 97ae4e3..0000000 --- a/troveclient/openstack/common/py3kcompat/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# -# 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. -# diff --git a/troveclient/openstack/common/py3kcompat/urlutils.py b/troveclient/openstack/common/py3kcompat/urlutils.py deleted file mode 100644 index 6200271..0000000 --- a/troveclient/openstack/common/py3kcompat/urlutils.py +++ /dev/null @@ -1,65 +0,0 @@ -# -# 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 diff --git a/troveclient/tests/test_base.py b/troveclient/tests/test_base.py index 4e088a5..37f52ce 100644 --- a/troveclient/tests/test_base.py +++ b/troveclient/tests/test_base.py @@ -474,7 +474,7 @@ class ResourceTest(testtools.TestCase): manager.get = None robj.manager = object() - robj.get() + robj._get() manager = mock.Mock() robj.manager = mock.Mock() @@ -483,7 +483,7 @@ class ResourceTest(testtools.TestCase): new = mock.Mock() new._info = {"name": "test-human-id", "test_attr": 5} robj.manager.get = mock.Mock(return_value=new) - robj.get() + robj._get() self.assertEqual("test-human-id", robj.name) self.assertEqual(5, robj.test_attr) @@ -513,15 +513,7 @@ class ResourceTest(testtools.TestCase): def test_is_loaded(self): robj = self.get_mock_resource_obj() robj._loaded = True - self.assertTrue(robj.is_loaded()) + self.assertTrue(robj.is_loaded) robj._loaded = False - self.assertFalse(robj.is_loaded()) - - def test_set_loaded(self): - robj = self.get_mock_resource_obj() - robj.set_loaded(True) - self.assertTrue(robj._loaded) - - robj.set_loaded(False) - self.assertFalse(robj._loaded) + self.assertFalse(robj.is_loaded)