Merge "Remove dependent module py3kcompat"

This commit is contained in:
Jenkins
2014-04-25 10:24:17 +00:00
committed by Gerrit Code Review
12 changed files with 31 additions and 140 deletions

View File

@@ -4,7 +4,6 @@
module=apiclient module=apiclient
module=strutils module=strutils
module=install_venv_common module=install_venv_common
module=py3kcompat
# The base module to hold the copy of openstack.common # The base module to hold the copy of openstack.common
base=troveclient base=troveclient

View File

@@ -24,12 +24,12 @@ import hashlib
import os import os
import six import six
from six.moves.urllib import parse
from troveclient.openstack.common.apiclient import base from troveclient.openstack.common.apiclient import base
from troveclient.openstack.common.apiclient import exceptions from troveclient.openstack.common.apiclient import exceptions
from troveclient import utils from troveclient import utils
from troveclient import common from troveclient import common
from troveclient.openstack.common.py3kcompat import urlutils
# Python 2.4 compat # Python 2.4 compat
try: try:
@@ -69,8 +69,8 @@ class Manager(utils.HookableMixin):
next_marker = None next_marker = None
for link in next_links: for link in next_links:
# Extract the marker from the url. # Extract the marker from the url.
parsed_url = urlutils.urlparse(link) parsed_url = parse.urlparse(link)
query_dict = dict(urlutils.parse_qsl(parsed_url.query)) query_dict = dict(parse.parse_qsl(parsed_url.query))
next_marker = query_dict.get('marker') next_marker = query_dict.get('marker')
data = [self.resource_class(self, res) for res in body[response_key]] data = [self.resource_class(self, res) for res in body[response_key]]
return common.Paginated(data, next_marker=next_marker, links=links) return common.Paginated(data, next_marker=next_marker, links=links)

View File

@@ -13,11 +13,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from six.moves.urllib import parse
from troveclient.openstack.common.apiclient import exceptions from troveclient.openstack.common.apiclient import exceptions
from troveclient.openstack.common.py3kcompat import urlutils
def check_for_exceptions(resp, body, url): def check_for_exceptions(resp, body, url):
if resp.status_code in (400, 422, 500): if resp.status_code in (400, 422, 500):
@@ -39,9 +38,9 @@ def limit_url(url, limit=None, marker=None):
def quote_user_host(user, host): def quote_user_host(user, host):
quoted = '' quoted = ''
if host: if host:
quoted = urlutils.quote("%s@%s" % (user, host)) quoted = parse.quote("%s@%s" % (user, host))
else: else:
quoted = urlutils.quote("%s" % user) quoted = parse.quote("%s" % user)
return quoted.replace('.', '%2e') return quoted.replace('.', '%2e')

View File

@@ -18,13 +18,12 @@ import optparse
import os import os
import pickle import pickle
import six import six
from six.moves.urllib import parse
import sys import sys
from troveclient.compat import client from troveclient.compat import client
from troveclient.compat import exceptions from troveclient.compat import exceptions
from troveclient.openstack.common.py3kcompat import urlutils
def methods_of(obj): def methods_of(obj):
"""Get all callable methods of an object that don't start with underscore """Get all callable methods of an object that don't start with underscore
@@ -74,9 +73,9 @@ def limit_url(url, limit=None, marker=None):
def quote_user_host(user, host): def quote_user_host(user, host):
quoted = '' quoted = ''
if host: if host:
quoted = urlutils.quote("%s@%s" % (user, host)) quoted = parse.quote("%s@%s" % (user, host))
else: else:
quoted = urlutils.quote("%s" % user) quoted = parse.quote("%s" % user)
return quoted.replace('.', '%2e') return quoted.replace('.', '%2e')

View File

@@ -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.

View File

@@ -19,7 +19,6 @@
import abc import abc
import argparse import argparse
import logging
import os import os
import six import six
@@ -28,9 +27,6 @@ from stevedore import extension
from troveclient.openstack.common.apiclient import exceptions from troveclient.openstack.common.apiclient import exceptions
logger = logging.getLogger(__name__)
_discovered_plugins = {} _discovered_plugins = {}
@@ -80,7 +76,7 @@ def load_plugin_from_args(args):
alphabetical order. alphabetical order.
:type args: argparse.Namespace :type args: argparse.Namespace
:raises: AuthorizationFailure :raises: AuthPluginOptionsMissing
""" """
auth_system = args.os_auth_system auth_system = args.os_auth_system
if auth_system: if auth_system:

View File

@@ -24,11 +24,12 @@ Base utilities to build API operation managers and objects on top of.
# pylint: disable=E1102 # pylint: disable=E1102
import abc import abc
import copy
import six import six
from six.moves.urllib import parse
from troveclient.openstack.common.apiclient import exceptions from troveclient.openstack.common.apiclient import exceptions
from troveclient.openstack.common.py3kcompat import urlutils
from troveclient.openstack.common import strutils from troveclient.openstack.common import strutils
@@ -327,7 +328,7 @@ class CrudManager(BaseManager):
return self._list( return self._list(
'%(base_url)s%(query)s' % { '%(base_url)s%(query)s' % {
'base_url': self.build_url(base_url=base_url, **kwargs), '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) self.collection_key)
@@ -366,7 +367,7 @@ class CrudManager(BaseManager):
rl = self._list( rl = self._list(
'%(base_url)s%(query)s' % { '%(base_url)s%(query)s' % {
'base_url': self.build_url(base_url=base_url, **kwargs), '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) self.collection_key)
num = len(rl) num = len(rl)
@@ -456,17 +457,17 @@ class Resource(object):
def __getattr__(self, k): def __getattr__(self, k):
if k not in self.__dict__: if k not in self.__dict__:
#NOTE(bcwaldon): disallow lazy-loading if already loaded once #NOTE(bcwaldon): disallow lazy-loading if already loaded once
if not self.is_loaded(): if not self.is_loaded:
self.get() self._get()
return self.__getattr__(k) return self.__getattr__(k)
raise AttributeError(k) raise AttributeError(k)
else: else:
return self.__dict__[k] return self.__dict__[k]
def get(self): def _get(self):
# set_loaded() first ... so if we have to bail, we know we tried. # set _loaded first ... so if we have to bail, we know we tried.
self.set_loaded(True) self._loaded = True
if not hasattr(self.manager, 'get'): if not hasattr(self.manager, 'get'):
return return
@@ -484,8 +485,9 @@ class Resource(object):
return self.id == other.id return self.id == other.id
return self._info == other._info return self._info == other._info
@property
def is_loaded(self): def is_loaded(self):
return self._loaded return self._loaded
def set_loaded(self, val): def to_dict(self):
self._loaded = val return copy.deepcopy(self._info)

View File

@@ -425,8 +425,8 @@ def from_response(response, method, url):
except ValueError: except ValueError:
pass pass
else: else:
if hasattr(body, "keys"): if isinstance(body, dict):
error = body[body.keys()[0]] error = list(body.values())[0]
kwargs["message"] = error.get("message", None) kwargs["message"] = error.get("message", None)
kwargs["details"] = error.get("details", None) kwargs["details"] = error.get("details", None)
elif content_type.startswith("text/"): elif content_type.startswith("text/"):

View File

@@ -28,10 +28,9 @@ import json
import requests import requests
import six import six
from six.moves.urllib import parse
from troveclient.openstack.common.apiclient import client 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=[]): def assert_has_keys(dct, required=[], optional=[]):
@@ -64,7 +63,7 @@ class TestResponse(requests.Response):
self._content = text self._content = text
default_headers = {} default_headers = {}
if six.PY3 and isinstance(self._content, six.string_types): 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 self.headers = data.get('headers') or default_headers
else: else:
self.status_code = data self.status_code = data
@@ -148,7 +147,7 @@ class FakeHTTPClient(client.HTTPClient):
"text": fixture[1]}) "text": fixture[1]})
# Call the method # Call the method
args = urlutils.parse_qsl(urlutils.urlparse(url)[4]) args = parse.parse_qsl(parse.urlparse(url)[4])
kwargs.update(args) kwargs.update(args)
munged_url = url.rsplit('?', 1)[0] munged_url = url.rsplit('?', 1)[0]
munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_') munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_')

View File

@@ -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.
#

View File

@@ -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

View File

@@ -472,7 +472,7 @@ class ResourceTest(testtools.TestCase):
manager.get = None manager.get = None
robj.manager = object() robj.manager = object()
robj.get() robj._get()
manager = mock.Mock() manager = mock.Mock()
robj.manager = mock.Mock() robj.manager = mock.Mock()
@@ -481,7 +481,7 @@ class ResourceTest(testtools.TestCase):
new = mock.Mock() new = mock.Mock()
new._info = {"name": "test-human-id", "test_attr": 5} new._info = {"name": "test-human-id", "test_attr": 5}
robj.manager.get = mock.Mock(return_value=new) robj.manager.get = mock.Mock(return_value=new)
robj.get() robj._get()
self.assertEqual("test-human-id", robj.name) self.assertEqual("test-human-id", robj.name)
self.assertEqual(5, robj.test_attr) self.assertEqual(5, robj.test_attr)
@@ -511,15 +511,7 @@ class ResourceTest(testtools.TestCase):
def test_is_loaded(self): def test_is_loaded(self):
robj = self.get_mock_resource_obj() robj = self.get_mock_resource_obj()
robj._loaded = True robj._loaded = True
self.assertTrue(robj.is_loaded()) self.assertTrue(robj.is_loaded)
robj._loaded = False robj._loaded = False
self.assertFalse(robj.is_loaded()) 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)