From 11c45de9e0474e14f787f2e5e2bf26283fcb02f0 Mon Sep 17 00:00:00 2001 From: ekudryashova Date: Mon, 30 Dec 2013 17:13:47 +0200 Subject: [PATCH] Update to the latest code from Oslo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update apiclient code for python3.3 support Сhanges from oslo-incubator related to apiclient: Change 41dc2b4: Encode response from FakeHTTPClient Change e6494c2: Use six.iteritems to make dict work on Python2/3 Change 0d8f18b: Use urlutils functions instead of urllib/urllib2 Change 16fb43b: Replace data structures' attribute with six module Change fdb0f0f: Common ConnectionRefused class Change 9d0ec6a: Use six.iteritems for python 3.3 support in apiclient module Change 12bcdb7: Remove vim header Change 4c22556: Use py3kcompat urlutils functions instead of urlparse Change 3970d46: Fix typos in oslo Change 1771a77: Adjust import order according to PEP8 imports rule Change da611e6: Transform the for loop to expression Change-Id: If2bbb247f0202bae6dd5a8bd66d9708738e52d71 --- .../openstack/common/apiclient/__init__.py | 2 -- troveclient/openstack/common/apiclient/auth.py | 10 ++++------ troveclient/openstack/common/apiclient/base.py | 17 ++++++++--------- .../openstack/common/apiclient/client.py | 4 +--- .../openstack/common/apiclient/exceptions.py | 7 +++++-- .../openstack/common/apiclient/fake_client.py | 10 ++++++---- .../openstack/common/py3kcompat/__init__.py | 1 - .../openstack/common/py3kcompat/urlutils.py | 16 +++++++++++++++- 8 files changed, 39 insertions(+), 28 deletions(-) diff --git a/troveclient/openstack/common/apiclient/__init__.py b/troveclient/openstack/common/apiclient/__init__.py index d5d00222..f3d0cdef 100644 --- a/troveclient/openstack/common/apiclient/__init__.py +++ b/troveclient/openstack/common/apiclient/__init__.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2013 OpenStack Foundation # All Rights Reserved. # diff --git a/troveclient/openstack/common/apiclient/auth.py b/troveclient/openstack/common/apiclient/auth.py index 15677227..eb925d4a 100644 --- a/troveclient/openstack/common/apiclient/auth.py +++ b/troveclient/openstack/common/apiclient/auth.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2013 OpenStack Foundation # Copyright 2013 Spanish National Research Council. # All Rights Reserved. @@ -24,6 +22,7 @@ import argparse import logging import os +import six from stevedore import extension from troveclient.openstack.common.apiclient import exceptions @@ -59,7 +58,7 @@ def load_auth_system_opts(parser): """ group = parser.add_argument_group("Common auth options") BaseAuthPlugin.add_common_opts(group) - for name, auth_plugin in _discovered_plugins.iteritems(): + for name, auth_plugin in six.iteritems(_discovered_plugins): group = parser.add_argument_group( "Auth-system '%s' options" % name, conflict_handler="resolve") @@ -90,7 +89,7 @@ def load_plugin_from_args(args): plugin.sufficient_options() return plugin - for plugin_auth_system in sorted(_discovered_plugins.iterkeys()): + for plugin_auth_system in sorted(six.iterkeys(_discovered_plugins)): plugin_class = _discovered_plugins[plugin_auth_system] plugin = plugin_class() plugin.parse_opts(args) @@ -102,6 +101,7 @@ def load_plugin_from_args(args): raise exceptions.AuthPluginOptionsMissing(["auth_system"]) +@six.add_metaclass(abc.ABCMeta) class BaseAuthPlugin(object): """Base class for authentication plugins. @@ -109,8 +109,6 @@ class BaseAuthPlugin(object): method to be a valid plugin. """ - __metaclass__ = abc.ABCMeta - auth_system = None opt_names = [] common_opt_names = [ diff --git a/troveclient/openstack/common/apiclient/base.py b/troveclient/openstack/common/apiclient/base.py index 56ce3b44..8c47ed24 100644 --- a/troveclient/openstack/common/apiclient/base.py +++ b/troveclient/openstack/common/apiclient/base.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2010 Jacob Kaplan-Moss # Copyright 2011 OpenStack Foundation # Copyright 2012 Grid Dynamics @@ -26,9 +24,11 @@ Base utilities to build API operation managers and objects on top of. # pylint: disable=E1102 import abc -import urllib + +import six from troveclient.openstack.common.apiclient import exceptions +from troveclient.openstack.common.py3kcompat import urlutils from troveclient.openstack.common import strutils @@ -201,11 +201,10 @@ class BaseManager(HookableMixin): return self.client.delete(url) +@six.add_metaclass(abc.ABCMeta) class ManagerWithFind(BaseManager): """Manager with additional `find()`/`findall()` methods.""" - __metaclass__ = abc.ABCMeta - @abc.abstractmethod def list(self): pass @@ -292,7 +291,7 @@ class CrudManager(BaseManager): def _filter_kwargs(self, kwargs): """Drop null values and handle ids.""" - for key, ref in kwargs.copy().iteritems(): + for key, ref in six.iteritems(kwargs.copy()): if ref is None: kwargs.pop(key) else: @@ -328,7 +327,7 @@ class CrudManager(BaseManager): return self._list( '%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), - 'query': '?%s' % urllib.urlencode(kwargs) if kwargs else '', + 'query': '?%s' % urlutils.urlencode(kwargs) if kwargs else '', }, self.collection_key) @@ -367,7 +366,7 @@ class CrudManager(BaseManager): rl = self._list( '%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), - 'query': '?%s' % urllib.urlencode(kwargs) if kwargs else '', + 'query': '?%s' % urlutils.urlencode(kwargs) if kwargs else '', }, self.collection_key) num = len(rl) @@ -446,7 +445,7 @@ class Resource(object): return None def _add_details(self, info): - for (k, v) in info.iteritems(): + for (k, v) in six.iteritems(info): try: setattr(self, k, v) self._info[k] = v diff --git a/troveclient/openstack/common/apiclient/client.py b/troveclient/openstack/common/apiclient/client.py index 6afe5751..128b5305 100644 --- a/troveclient/openstack/common/apiclient/client.py +++ b/troveclient/openstack/common/apiclient/client.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2010 Jacob Kaplan-Moss # Copyright 2011 OpenStack Foundation # Copyright 2011 Piston Cloud Computing, Inc. @@ -52,7 +50,7 @@ class HTTPClient(object): services (e.g., for compute and image clients); - reissue authentication request for expired tokens; - encode/decode JSON bodies; - - raise exeptions on HTTP errors; + - raise exceptions on HTTP errors; - pluggable authentication; - store authentication information in a keyring; - store time spent for requests; diff --git a/troveclient/openstack/common/apiclient/exceptions.py b/troveclient/openstack/common/apiclient/exceptions.py index fec8ade1..4776d587 100644 --- a/troveclient/openstack/common/apiclient/exceptions.py +++ b/troveclient/openstack/common/apiclient/exceptions.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2010 Jacob Kaplan-Moss # Copyright 2011 Nebula, Inc. # Copyright 2013 Alessio Ababilov @@ -62,6 +60,11 @@ class AuthorizationFailure(ClientException): pass +class ConnectionRefused(ClientException): + """Cannot connect to API service.""" + pass + + class AuthPluginOptionsMissing(AuthorizationFailure): """Auth plugin misses some options.""" def __init__(self, opt_names): diff --git a/troveclient/openstack/common/apiclient/fake_client.py b/troveclient/openstack/common/apiclient/fake_client.py index b423fedf..32e05247 100644 --- a/troveclient/openstack/common/apiclient/fake_client.py +++ b/troveclient/openstack/common/apiclient/fake_client.py @@ -1,5 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - # Copyright 2013 OpenStack Foundation # All Rights Reserved. # @@ -27,11 +25,13 @@ places where actual behavior differs from the spec. # pylint: disable=W0102 import json -import urlparse import requests +import six 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=[]): @@ -63,6 +63,8 @@ class TestResponse(requests.Response): else: self._content = text default_headers = {} + if six.PY3 and isinstance(self._content, six.string_types): + self._content = strutils.safe_encode(self._content) self.headers = data.get('headers') or default_headers else: self.status_code = data @@ -146,7 +148,7 @@ class FakeHTTPClient(client.HTTPClient): "text": fixture[1]}) # Call the method - args = urlparse.parse_qsl(urlparse.urlparse(url)[4]) + args = urlutils.parse_qsl(urlutils.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 index be894cf5..97ae4e34 100644 --- a/troveclient/openstack/common/py3kcompat/__init__.py +++ b/troveclient/openstack/common/py3kcompat/__init__.py @@ -1,4 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 # # Copyright 2013 Canonical Ltd. # All Rights Reserved. diff --git a/troveclient/openstack/common/py3kcompat/urlutils.py b/troveclient/openstack/common/py3kcompat/urlutils.py index 8459c3b4..6200271f 100644 --- a/troveclient/openstack/common/py3kcompat/urlutils.py +++ b/troveclient/openstack/common/py3kcompat/urlutils.py @@ -1,4 +1,3 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 # # Copyright 2013 Canonical Ltd. # All Rights Reserved. @@ -24,24 +23,34 @@ 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 @@ -49,3 +58,8 @@ else: urlparse = parse.urlparse urlsplit = parse.urlsplit urlunsplit = parse.urlunsplit + SplitResult = parse.SplitResult + + urlopen = urllib2.urlopen + URLError = urllib2.URLError + pathname2url = urllib.pathname2url