From 997c12d3abfd9a571a4e8f0b022dfb1d27c62edb Mon Sep 17 00:00:00 2001 From: Cindy Pallares Date: Tue, 19 May 2015 19:59:06 -0500 Subject: [PATCH] Import only modules and update tox.ini As stated in the OpenStack Hacking Guidelines, it is prefered that only modules should be imported. Also updated tox.ini to ignore opestack/common among others. Change-Id: I2f0a603c31052eadee581c11880c0ec6bd392829 --- glanceclient/common/http.py | 8 +++---- glanceclient/common/https.py | 9 ++++---- glanceclient/shell.py | 3 +-- glanceclient/tests/unit/test_client.py | 12 +++++----- glanceclient/tests/unit/test_ssl.py | 27 +++++++++++----------- glanceclient/tests/unit/v2/test_schemas.py | 5 ++-- glanceclient/tests/utils.py | 4 ++-- glanceclient/v1/client.py | 8 +++---- glanceclient/v2/shell.py | 12 ++++++---- tox.ini | 8 ++++--- 10 files changed, 50 insertions(+), 46 deletions(-) diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index b5f37cb1..8538a3c3 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -23,7 +23,7 @@ from oslo_utils import importutils from oslo_utils import netutils import requests try: - from requests.packages.urllib3.exceptions import ProtocolError + ProtocolError = requests.packages.urllib3.exceptions.ProtocolError except ImportError: ProtocolError = requests.exceptions.ConnectionError import six @@ -42,7 +42,7 @@ if not hasattr(parse, 'parse_qsl'): from oslo_utils import encodeutils from glanceclient.common import https -from glanceclient.common.utils import safe_header +from glanceclient.common import utils from glanceclient import exc osprofiler_web = importutils.try_import("osprofiler.web") @@ -167,7 +167,7 @@ class HTTPClient(_BaseHTTPClient): headers.update(self.session.headers) for (key, value) in six.iteritems(headers): - header = '-H \'%s: %s\'' % safe_header(key, value) + header = '-H \'%s: %s\'' % utils.safe_header(key, value) curl.append(header) if not self.session.verify: @@ -193,7 +193,7 @@ class HTTPClient(_BaseHTTPClient): status = (resp.raw.version / 10.0, resp.status_code, resp.reason) dump = ['\nHTTP/%.1f %s %s' % status] headers = resp.headers.items() - dump.extend(['%s: %s' % safe_header(k, v) for k, v in headers]) + dump.extend(['%s: %s' % utils.safe_header(k, v) for k, v in headers]) dump.append('') content_type = resp.headers.get('Content-Type') diff --git a/glanceclient/common/https.py b/glanceclient/common/https.py index 649d14b7..30896e0b 100644 --- a/glanceclient/common/https.py +++ b/glanceclient/common/https.py @@ -47,11 +47,10 @@ try: else: raise ImportError except ImportError: - try: - from httplib import HTTPSConnection - except ImportError: - from http.client import HTTPSConnection - from OpenSSL.SSL import Connection as Connection + from OpenSSL import SSL + from six.moves import http_client + HTTPSConnection = http_client.HTTPSConnection + Connection = SSL.Connection from glanceclient import exc diff --git a/glanceclient/shell.py b/glanceclient/shell.py index 8cd2c2df..74496314 100644 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -25,7 +25,6 @@ import getpass import json import logging import os -from os.path import expanduser import sys import traceback @@ -551,7 +550,7 @@ class OpenStackImagesShell(object): return client def _cache_schemas(self, options, home_dir='~/.glanceclient'): - homedir = expanduser(home_dir) + homedir = os.path.expanduser(home_dir) if not os.path.exists(homedir): try: os.makedirs(homedir) diff --git a/glanceclient/tests/unit/test_client.py b/glanceclient/tests/unit/test_client.py index 62daaf56..fea7949e 100644 --- a/glanceclient/tests/unit/test_client.py +++ b/glanceclient/tests/unit/test_client.py @@ -16,8 +16,8 @@ import testtools from glanceclient import client -from glanceclient.v1 import client as v1 -from glanceclient.v2 import client as v2 +from glanceclient import v1 +from glanceclient import v2 class ClientTest(testtools.TestCase): @@ -28,19 +28,19 @@ class ClientTest(testtools.TestCase): def test_endpoint(self): gc = client.Client(1, "http://example.com") self.assertEqual("http://example.com", gc.http_client.endpoint) - self.assertIsInstance(gc, v1.Client) + self.assertIsInstance(gc, v1.client.Client) def test_versioned_endpoint(self): gc = client.Client(1, "http://example.com/v2") self.assertEqual("http://example.com", gc.http_client.endpoint) - self.assertIsInstance(gc, v1.Client) + self.assertIsInstance(gc, v1.client.Client) def test_versioned_endpoint_no_version(self): gc = client.Client(endpoint="http://example.com/v2") self.assertEqual("http://example.com", gc.http_client.endpoint) - self.assertIsInstance(gc, v2.Client) + self.assertIsInstance(gc, v2.client.Client) def test_versioned_endpoint_with_minor_revision(self): gc = client.Client(2.2, "http://example.com/v2.1") self.assertEqual("http://example.com", gc.http_client.endpoint) - self.assertIsInstance(gc, v2.Client) + self.assertIsInstance(gc, v2.client.Client) diff --git a/glanceclient/tests/unit/test_ssl.py b/glanceclient/tests/unit/test_ssl.py index 907b7bfe..4056850c 100644 --- a/glanceclient/tests/unit/test_ssl.py +++ b/glanceclient/tests/unit/test_ssl.py @@ -29,8 +29,9 @@ import threading from glanceclient.common import http from glanceclient.common import https -from glanceclient import Client from glanceclient import exc +from glanceclient import v1 +from glanceclient import v2 if six.PY3 is True: import socketserver @@ -90,9 +91,9 @@ class TestHTTPSVerifyCert(testtools.TestCase): url = 'https://0.0.0.0:%d' % port try: - client = Client('1', url, - insecure=False, - ssl_compression=True) + client = v1.client.Client(url, + insecure=False, + ssl_compression=True) client.images.get('image123') self.fail('No SSL exception raised') except exc.CommunicationError as e: @@ -107,9 +108,9 @@ class TestHTTPSVerifyCert(testtools.TestCase): url = 'https://0.0.0.0:%d' % port try: - client = Client('1', url, - insecure=False, - ssl_compression=False) + client = v1.client.Client(url, + insecure=False, + ssl_compression=False) client.images.get('image123') self.fail('No SSL exception raised') except SSL.Error as e: @@ -124,9 +125,9 @@ class TestHTTPSVerifyCert(testtools.TestCase): url = 'https://0.0.0.0:%d' % port try: - gc = Client('2', url, - insecure=False, - ssl_compression=True) + gc = v2.client.Client(url, + insecure=False, + ssl_compression=True) gc.images.get('image123') self.fail('No SSL exception raised') except exc.CommunicationError as e: @@ -141,9 +142,9 @@ class TestHTTPSVerifyCert(testtools.TestCase): url = 'https://0.0.0.0:%d' % port try: - gc = Client('2', url, - insecure=False, - ssl_compression=False) + gc = v2.client.Client(url, + insecure=False, + ssl_compression=False) gc.images.get('image123') self.fail('No SSL exception raised') except SSL.Error as e: diff --git a/glanceclient/tests/unit/v2/test_schemas.py b/glanceclient/tests/unit/v2/test_schemas.py index b084d14f..60442a8e 100644 --- a/glanceclient/tests/unit/v2/test_schemas.py +++ b/glanceclient/tests/unit/v2/test_schemas.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -from jsonpatch import JsonPatch +import jsonpatch import testtools import warlock @@ -61,7 +61,8 @@ _SCHEMA = schemas.Schema({ def compare_json_patches(a, b): """Return 0 if a and b describe the same JSON patch.""" - return JsonPatch.from_string(a) == JsonPatch.from_string(b) + return(jsonpatch.JsonPatch.from_string(a) == + jsonpatch.JsonPatch.from_string(b)) class TestSchemaProperty(testtools.TestCase): diff --git a/glanceclient/tests/utils.py b/glanceclient/tests/utils.py index a47dcb36..f7f3452d 100644 --- a/glanceclient/tests/utils.py +++ b/glanceclient/tests/utils.py @@ -19,7 +19,7 @@ import six import six.moves.urllib.parse as urlparse import testtools -from glanceclient.v2.schemas import Schema +from glanceclient.v2 import schemas class FakeAPI(object): @@ -68,7 +68,7 @@ class FakeAPI(object): class FakeSchemaAPI(FakeAPI): def get(self, *args, **kwargs): _, raw_schema = self._request('GET', *args, **kwargs) - return Schema(raw_schema) + return schemas.Schema(raw_schema) class RawRequest(object): diff --git a/glanceclient/v1/client.py b/glanceclient/v1/client.py index b36f3060..066867dd 100644 --- a/glanceclient/v1/client.py +++ b/glanceclient/v1/client.py @@ -15,8 +15,8 @@ from glanceclient.common import http from glanceclient.common import utils -from glanceclient.v1.image_members import ImageMemberManager -from glanceclient.v1.images import ImageManager +from glanceclient.v1 import image_members +from glanceclient.v1 import images class Client(object): @@ -33,5 +33,5 @@ class Client(object): """Initialize a new client for the Images v1 API.""" endpoint, self.version = utils.endpoint_version_from_url(endpoint, 1.0) self.http_client = http.get_http_client(endpoint=endpoint, **kwargs) - self.images = ImageManager(self.http_client) - self.image_members = ImageMemberManager(self.http_client) + self.images = images.ImageManager(self.http_client) + self.image_members = image_members.ImageMemberManager(self.http_client) diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 1ee16f3b..a56e5529 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -18,20 +18,20 @@ import sys from glanceclient.common import progressbar from glanceclient.common import utils from glanceclient import exc -from glanceclient.v2.image_members import MEMBER_STATUS_VALUES +from glanceclient.v2 import image_members from glanceclient.v2 import images from glanceclient.v2 import tasks import json import os -from os.path import expanduser +MEMBER_STATUS_VALUES = image_members.MEMBER_STATUS_VALUES IMAGE_SCHEMA = None def get_image_schema(): global IMAGE_SCHEMA if IMAGE_SCHEMA is None: - schema_path = expanduser("~/.glanceclient/image_schema.json") + schema_path = os.path.expanduser("~/.glanceclient/image_schema.json") if os.path.isfile(schema_path): with open(schema_path, "r") as f: schema_raw = f.read() @@ -394,7 +394,8 @@ NAMESPACE_SCHEMA = None def get_namespace_schema(): global NAMESPACE_SCHEMA if NAMESPACE_SCHEMA is None: - schema_path = expanduser("~/.glanceclient/namespace_schema.json") + schema_path = os.path.expanduser("~/.glanceclient/" + "namespace_schema.json") if os.path.isfile(schema_path): with open(schema_path, "r") as f: schema_raw = f.read() @@ -533,7 +534,8 @@ RESOURCE_TYPE_SCHEMA = None def get_resource_type_schema(): global RESOURCE_TYPE_SCHEMA if RESOURCE_TYPE_SCHEMA is None: - schema_path = expanduser("~/.glanceclient/resource_type_schema.json") + schema_path = os.path.expanduser("~/.glanceclient/" + "resource_type_schema.json") if os.path.isfile(schema_path): with open(schema_path, "r") as f: schema_raw = f.read() diff --git a/tox.ini b/tox.ini index 5a8b3fd9..74cf84ca 100644 --- a/tox.ini +++ b/tox.ini @@ -37,7 +37,6 @@ downloadcache = ~/cache/pip [flake8] # H233 Python 3.x incompatible use of print operator -# H302 import only modules # H303 no wildcard import # H404 multi line docstring should start with a summary @@ -49,6 +48,9 @@ downloadcache = ~/cache/pip # H238 old style class declaration, use new style (inherit from `object`) # E128 continuation line under-indented for visual indent -ignore = F403,F812,F821,H233,H302,H303,H404,E265,H405,E123,H238,E128 +ignore = F403,F812,F821,H233,H303,H404,E265,H405,E123,H238,E128 show-source = True -exclude = .venv,.tox,dist,*egg,build +exclude = .venv*,.tox,dist,*egg,build,.git,doc,*openstack/common*,*lib/python*,.update-venv + +[hacking] +import_exceptions = six.moves