From 1706b3cff57cdaadb7943d94f348afccda5ae0db Mon Sep 17 00:00:00 2001 From: Ananth Narayan S Date: Wed, 11 Jan 2017 17:36:32 +0530 Subject: [PATCH] Remove object imports. This patch modifies import statements to import only modules and remove object imports as per the OpenStack import guidelines. Change-Id: Id4841375a15f41dd166bf16d4c8736954ca61440 Closes-Bug: #1650063 --- valence/api/app.py | 9 ++-- valence/api/link.py | 4 +- valence/api/root.py | 30 ++++++------- valence/api/route.py | 49 ++++++++++------------ valence/cmd/db_manager.py | 4 +- valence/common/utils.py | 4 +- valence/redfish/redfish.py | 4 +- valence/tests/unit/api/test_link.py | 4 +- valence/tests/unit/api/test_route.py | 4 +- valence/tests/unit/common/test_types.py | 4 +- valence/tests/unit/flavors/test_flavors.py | 19 +++++---- 11 files changed, 69 insertions(+), 66 deletions(-) diff --git a/valence/api/app.py b/valence/api/app.py index 5b33746..36d71b7 100644 --- a/valence/api/app.py +++ b/valence/api/app.py @@ -11,13 +11,13 @@ # limitations under the License. import logging +import logging.handlers import os.path -from logging.handlers import RotatingFileHandler import flask -from valence import config as cfg +import valence.config as cfg _app = None @@ -29,8 +29,9 @@ def setup_app(): TEN_KB = 10 * 1024 # Configure logging - if os.path.isfile(cfg.log_file) and os.access(cfg.log_file, os.W_OK): - handler = RotatingFileHandler( + if os.path.isfile(cfg.log_file) and os.access(cfg.log_file, + os.W_OK): + handler = logging.handlers.RotatingFileHandler( cfg.log_file, maxBytes=TEN_KB, backupCount=1) handler.setLevel(cfg.log_level) formatter = logging.Formatter(cfg.log_format) diff --git a/valence/api/link.py b/valence/api/link.py index b303453..d72bd13 100644 --- a/valence/api/link.py +++ b/valence/api/link.py @@ -13,7 +13,7 @@ # under the License. -from flask import request +import flask from valence.common import base from valence.common import types @@ -21,7 +21,7 @@ from valence.common import types def build_url(resource, resource_args, bookmark=False, base_url=None): if base_url is None: - base_url = request.root_url + base_url = flask.request.root_url base_url = base_url.rstrip("//") template = '%(url)s/%(res)s' if bookmark else '%(url)s/v1/%(res)s' template += '%(args)s' if resource_args.startswith('?') else '/%(args)s' diff --git a/valence/api/root.py b/valence/api/root.py index 389f9c0..98b6bc3 100644 --- a/valence/api/root.py +++ b/valence/api/root.py @@ -12,10 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. -from flask import abort -from flask import request -from flask import Response -from flask_restful import Resource +import flask +import flask_restful from six.moves import http_client from valence.api import link @@ -52,7 +50,7 @@ class Version(base.ObjectBase): version.id = id version.status = "CURRENT" if current else "DEPRECTED" version.min_version = min_version - version.links = [link.Link.make_link('self', request.url_root, + version.links = [link.Link.make_link('self', flask.request.url_root, id, '', bookmark=True)] return version @@ -83,14 +81,14 @@ class RootBase(base.ObjectBase): return root -class Root(Resource): +class Root(flask_restful.Resource): def get(self): obj = RootBase.convert() return utils.make_response(http_client.OK, obj.as_dict()) -class PODMProxy(Resource): +class PODMProxy(flask_restful.Resource): """Passthrough Proxy for PODM. This function bypasses valence processing @@ -106,30 +104,34 @@ class PODMProxy(Resource): filterext = ["Chassis", "Services", "Managers", "Systems", "EventService", "Nodes", "EthernetSwitches"] if resource not in filterext: - abort(http_client.NOT_FOUND) + flask.abort(http_client.NOT_FOUND) def get(self, url): self.check_url(url) resp = rfs.send_request(url) - return Response(resp.text, resp.status_code, resp.headers.items()) + return flask.Response(resp.text, resp.status_code, + resp.headers.items()) def post(self, url): self.check_url(url) resp = rfs.send_request(url, "POST", headers={'Content-type': 'application/json'}, - data=request.data) - return Response(resp.text, resp.status_code, resp.headers.items()) + data=flask.request.data) + return flask.Response(resp.text, resp.status_code, + resp.headers.items()) def delete(self, url): self.check_url(url) resp = rfs.send_request(url, "DELETE") - return Response(resp.text, resp.status_code, resp.headers.items()) + return flask.Response(resp.text, resp.status_code, + resp.headers.items()) def patch(self, url): self.check_url(url) resp = rfs.send_request(url, "PATCH", headers={'Content-type': 'application/json'}, - data=request.data) - return Response(resp.text, resp.status_code, resp.headers.items()) + data=flask.request.data) + return flask.Response(resp.text, resp.status_code, + resp.headers.items()) diff --git a/valence/api/route.py b/valence/api/route.py index 2723eb7..dac8557 100644 --- a/valence/api/route.py +++ b/valence/api/route.py @@ -15,32 +15,28 @@ import logging import traceback -from flask_cors import CORS -from flask_restful import Api +import flask_cors +import flask_restful from six.moves import http_client from valence.api import app as flaskapp -from valence.api.root import PODMProxy -from valence.api.root import Root -from valence.api.v1.flavors import Flavors as v1Flavors -from valence.api.v1.nodes import Nodes as v1Nodes -from valence.api.v1.nodes import NodesList as v1NodesList -from valence.api.v1.nodes import NodesStorage as v1NodesStorage -from valence.api.v1.storages import Storages as v1Storages -from valence.api.v1.storages import StoragesList as v1StoragesList -from valence.api.v1.systems import Systems as v1Systems -from valence.api.v1.systems import SystemsList as v1SystemsList -from valence.api.v1.version import V1 +import valence.api.root as api_root +import valence.api.v1.flavors as v1_flavors +import valence.api.v1.nodes as v1_nodes +import valence.api.v1.storages as v1_storages +import valence.api.v1.systems as v1_systems +import valence.api.v1.version as v1_version + from valence.common import exception from valence.common import utils LOG = logging.getLogger(__name__) app = flaskapp.get_app() -cors = CORS(app) +cors = flask_cors.CORS(app) -class ValenceService(Api): +class ValenceService(flask_restful.Api): """Overriding Flask Restful Error handler""" def handle_error(self, error): @@ -65,30 +61,31 @@ api = ValenceService(app) # API Root operation -api.add_resource(Root, '/', endpoint='root') +api.add_resource(api_root.Root, '/', endpoint='root') # V1 Root operations -api.add_resource(V1, '/v1', endpoint='v1') +api.add_resource(v1_version.V1, '/v1', endpoint='v1') # Node(s) operations -api.add_resource(v1NodesList, '/v1/nodes', endpoint='nodes') -api.add_resource(v1Nodes, '/v1/nodes/', endpoint='node') -api.add_resource(v1NodesStorage, +api.add_resource(v1_nodes.NodesList, '/v1/nodes', endpoint='nodes') +api.add_resource(v1_nodes.Nodes, '/v1/nodes/', endpoint='node') +api.add_resource(v1_nodes.NodesStorage, '/v1/nodes//storages', endpoint='nodes_storages') # System(s) operations -api.add_resource(v1SystemsList, '/v1/systems', endpoint='systems') -api.add_resource(v1Systems, '/v1/systems/', endpoint='system') +api.add_resource(v1_systems.SystemsList, '/v1/systems', endpoint='systems') +api.add_resource(v1_systems.Systems, '/v1/systems/', + endpoint='system') # Flavor(s) operations -api.add_resource(v1Flavors, '/v1/flavors', endpoint='flavors') +api.add_resource(v1_flavors.Flavors, '/v1/flavors', endpoint='flavors') # Storage(s) operations -api.add_resource(v1StoragesList, '/v1/storages', endpoint='storages') -api.add_resource(v1Storages, +api.add_resource(v1_storages.StoragesList, '/v1/storages', endpoint='storages') +api.add_resource(v1_storages.Storages, '/v1/storages/', endpoint='storage') # Proxy to PODM -api.add_resource(PODMProxy, '/', endpoint='podmproxy') +api.add_resource(api_root.PODMProxy, '/', endpoint='podmproxy') diff --git a/valence/cmd/db_manager.py b/valence/cmd/db_manager.py index b394d28..bf4d0b0 100755 --- a/valence/cmd/db_manager.py +++ b/valence/cmd/db_manager.py @@ -16,13 +16,13 @@ import logging import sys -from valence.db import etcd_db +import valence.db.etcd_db as valence_etcdb LOG = logging.getLogger(__name__) def init(): - etcd_db.init_etcd_db() + valence_etcdb.init_etcd_db() def migrate(): diff --git a/valence/common/utils.py b/valence/common/utils.py index b1f0767..612df5f 100644 --- a/valence/common/utils.py +++ b/valence/common/utils.py @@ -20,7 +20,7 @@ import logging -from flask import jsonify +import flask import six LOG = logging.getLogger(__name__) @@ -90,7 +90,7 @@ def make_response(status_code, content="", headers=None): :returns: return_type -- flask Response object """ - response = jsonify(content) + response = flask.jsonify(content) if isinstance(status_code, int): response.status_code = status_code diff --git a/valence/redfish/redfish.py b/valence/redfish/redfish.py index 08d4956..501204d 100644 --- a/valence/redfish/redfish.py +++ b/valence/redfish/redfish.py @@ -18,7 +18,6 @@ import logging import os import requests -from requests.auth import HTTPBasicAuth from six.moves import http_client from valence.common import exception @@ -69,7 +68,8 @@ def send_request(resource, method="GET", **kwargs): LOG.debug(url) try: resp = requests.request(method, url, verify=False, - auth=HTTPBasicAuth(httpuser, httppwd), + auth=requests.auth.HTTPBasicAuth( + httpuser, httppwd), **kwargs) except requests.exceptions.RequestException as e: LOG.error(e) diff --git a/valence/tests/unit/api/test_link.py b/valence/tests/unit/api/test_link.py index 5eb2cbc..ae635e8 100644 --- a/valence/tests/unit/api/test_link.py +++ b/valence/tests/unit/api/test_link.py @@ -10,12 +10,12 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import TestCase +import unittest from valence.api import link as link_module -class TestLink(TestCase): +class TestLink(unittest.TestCase): def test_build_url_with_bookmark(self): link = link_module.build_url( 'v1', 'flavors', bookmark=True, base_url='http://localhost:8181') diff --git a/valence/tests/unit/api/test_route.py b/valence/tests/unit/api/test_route.py index 24e0d53..ce9899c 100644 --- a/valence/tests/unit/api/test_route.py +++ b/valence/tests/unit/api/test_route.py @@ -10,12 +10,12 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import TestCase +import unittest from valence.api import route -class TestRoute(TestCase): +class TestRoute(unittest.TestCase): def setUp(self): self.app = route.app diff --git a/valence/tests/unit/common/test_types.py b/valence/tests/unit/common/test_types.py index 3c3b2f6..0a15986 100644 --- a/valence/tests/unit/common/test_types.py +++ b/valence/tests/unit/common/test_types.py @@ -10,13 +10,13 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import TestCase +import unittest from valence.common import base from valence.common import types -class TestTypes(TestCase): +class TestTypes(unittest.TestCase): def test_text(self): self.assertIsNone(types.Text.validate(None)) diff --git a/valence/tests/unit/flavors/test_flavors.py b/valence/tests/unit/flavors/test_flavors.py index 6444f7a..cf4650b 100644 --- a/valence/tests/unit/flavors/test_flavors.py +++ b/valence/tests/unit/flavors/test_flavors.py @@ -11,14 +11,13 @@ # under the License. import mock -from unittest import TestCase +import unittest + from valence.flavors import flavors -from valence.flavors.plugins.assettag import assettagGenerator -from valence.flavors.plugins.default import defaultGenerator from valence.tests.unit.fakes import flavors_fakes as fakes -class TestFlavors(TestCase): +class TestFlavors(unittest.TestCase): def test_get_available_criteria(self): expected = {'criteria': [{'name': 'default', @@ -36,7 +35,8 @@ class TestFlavors(TestCase): result = sorted(result['criteria'], key=lambda x: x['name']) self.assertEqual(expected, result) - @mock.patch.object(assettagGenerator, 'generate') + @mock.patch( + 'valence.flavors.plugins.assettag.assettagGenerator.generate') @mock.patch('uuid.uuid4') @mock.patch('valence.redfish.redfish.systems_list') def test_create_flavors_asserttag(self, mock_systems, @@ -50,7 +50,8 @@ class TestFlavors(TestCase): expected = [fakes.fake_assettag_flavors()] self.assertEqual(expected, result) - @mock.patch.object(defaultGenerator, 'generate') + @mock.patch( + 'valence.flavors.plugins.default.defaultGenerator.generate') @mock.patch('uuid.uuid4') @mock.patch('valence.redfish.redfish.systems_list') def test_create_flavors_default(self, mock_systems, @@ -64,8 +65,10 @@ class TestFlavors(TestCase): expected = [fakes.fake_default_flavors()] self.assertEqual(expected, result) - @mock.patch.object(defaultGenerator, 'generate') - @mock.patch.object(assettagGenerator, 'generate') + @mock.patch( + 'valence.flavors.plugins.default.defaultGenerator.generate') + @mock.patch( + 'valence.flavors.plugins.assettag.assettagGenerator.generate') @mock.patch('uuid.uuid4') @mock.patch('valence.redfish.redfish.systems_list') def test_create_flavors_asserttag_and_default(self, mock_systems,