commit 02e573b95adc324651989d3d39e9b7e0e25b1151 Author: songwenping Date: Tue Oct 6 15:26:11 2020 +0800 Remove usage of six Remove six-library Replace the following items with Python 3 style code. - six.interger_types - six.text_type - six.string_types - six.PY3 - six.moves - six.add_metaclass - six.ensure_str - six.reraise Change-Id: I0c7691b3d6f6315137d8f0b9eeaa3b8e76749c04 diff --git a/cyborg/accelerator/common/exception.py b/cyborg/accelerator/common/exception.py index f5a3eb7..285c8bc 100644 --- a/cyborg/accelerator/common/exception.py +++ b/cyborg/accelerator/common/exception.py @@ -15,10 +15,9 @@ """Accelerator base exception handling. """ import collections +from http import client as http_client from oslo_log import log as logging from oslo_serialization import jsonutils -import six -from six.moves import http_client from cyborg.common.i18n import _ @@ -40,7 +39,7 @@ def _ensure_exception_kwargs_serializable(exc_class_name, kwargs): :returns: a dictionary of serializable keyword arguments. """ serializers = [(jsonutils.dumps, _('when converting to JSON')), - (six.text_type, _('when converting to string'))] + (str, _('when converting to string'))] exceptions = collections.defaultdict(list) serializable_kwargs = {} for k, v in kwargs.items(): @@ -80,8 +79,7 @@ class AcceleratorException(Exception): with the keyword arguments provided to the constructor. If you need to access the message from an exception you should use - six.text_type(exc) - + str(exc). """ _msg_fmt = _("An unknown exception occurred.") code = http_client.INTERNAL_SERVER_ERROR diff --git a/cyborg/accelerator/drivers/driver.py b/cyborg/accelerator/drivers/driver.py index 99931f2..1135ad9 100644 --- a/cyborg/accelerator/drivers/driver.py +++ b/cyborg/accelerator/drivers/driver.py @@ -12,11 +12,9 @@ # under the License. import abc -import six -@six.add_metaclass(abc.ABCMeta) -class GenericDriver(object): +class GenericDriver(object, metaclass=abc.ABCMeta): @abc.abstractmethod def discover(self): diff --git a/cyborg/api/controllers/v2/arqs.py b/cyborg/api/controllers/v2/arqs.py index c1c9871..138c384 100644 --- a/cyborg/api/controllers/v2/arqs.py +++ b/cyborg/api/controllers/v2/arqs.py @@ -13,8 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +from http import client as http_client import pecan -from six.moves import http_client import wsme from wsme import types as wtypes diff --git a/cyborg/api/controllers/v2/device_profiles.py b/cyborg/api/controllers/v2/device_profiles.py index cf0f9f5..8442386 100644 --- a/cyborg/api/controllers/v2/device_profiles.py +++ b/cyborg/api/controllers/v2/device_profiles.py @@ -14,9 +14,9 @@ # under the License. import copy +from http import client as http_client import pecan import re -from six.moves import http_client import wsme from wsme import types as wtypes diff --git a/cyborg/api/middleware/parsable_error.py b/cyborg/api/middleware/parsable_error.py index b3d0a0b..2a52b0f 100644 --- a/cyborg/api/middleware/parsable_error.py +++ b/cyborg/api/middleware/parsable_error.py @@ -20,8 +20,6 @@ response with one formatted so the client can parse it. Based on pecan.middleware.errordocument """ -import six - from oslo_serialization import jsonutils @@ -60,11 +58,9 @@ class ParsableErrorMiddleware(object): app_iter = self.app(environ, replacement_start_response) if (state['status_code'] // 100) not in (2, 3): - if six.PY3: - app_iter = [i.decode('utf-8') for i in app_iter] + app_iter = [i.decode('utf-8') for i in app_iter] body = [jsonutils.dumps({'error_message': '\n'.join(app_iter)})] - if six.PY3: - body = [i.encode('utf-8') for i in body] + body = [i.encode('utf-8') for i in body] state['headers'].append(('Content-Type', 'application/json')) state['headers'].append(('Content-Length', str(len(body[0])))) else: diff --git a/cyborg/common/exception.py b/cyborg/common/exception.py index e70a75e..9fd2e89 100644 --- a/cyborg/common/exception.py +++ b/cyborg/common/exception.py @@ -19,9 +19,8 @@ SHOULD include dedicated exception logging. """ +from http import client as http_client from oslo_log import log -import six -from six.moves import http_client from cyborg.common.i18n import _ from cyborg.conf import CONF @@ -38,8 +37,7 @@ class CyborgException(Exception): with the keyword arguments provided to the constructor. If you need to access the message from an exception you should use - six.text_type(exc) - + str(exc). """ _msg_fmt = _("An unknown exception occurred.") code = http_client.INTERNAL_SERVER_ERROR @@ -77,14 +75,11 @@ class CyborgException(Exception): def __str__(self): """Encode to utf-8 then wsme api can consume it as well.""" - if not six.PY3: - return six.text_type(self.args[0]).encode('utf-8') - return self.args[0] def __unicode__(self): """Return a unicode representation of the exception message.""" - return six.text_type(self.args[0]) + return str(self.args[0]) class Forbidden(CyborgException): diff --git a/cyborg/common/utils.py b/cyborg/common/utils.py index fd28707..cf46680 100644 --- a/cyborg/common/utils.py +++ b/cyborg/common/utils.py @@ -18,7 +18,6 @@ from concurrent.futures import ThreadPoolExecutor as CFThreadPoolExecutor from functools import wraps import queue -import six import time import traceback @@ -50,7 +49,7 @@ def safe_rstrip(value, chars=None): :return: Stripped value. """ - if not isinstance(value, six.string_types): + if not isinstance(value, str): LOG.warning("Failed to remove trailing character. Returning " "original object. Supplied object is not a string: " "%s,", value) @@ -155,7 +154,7 @@ def get_sdk_adapter(service_type, check_service=False): except sdk_exc.ServiceDiscoveryException as e: raise exception.ServiceUnavailable( _("The %(service_type)s service is unavailable: %(error)s") % - {'service_type': service_type, 'error': six.text_type(e)}) + {'service_type': service_type, 'error': str(e)}) return getattr(conn, service_type) @@ -356,7 +355,7 @@ class ThreadWorks(Singleton): LOG.error("Error during check the worker status. " "Exception info: %s, result: %s, state: %s. " "Reason %s", f.exception(), f._result, - f._state, six.text_type(e)) + f._state, str(e)) yield f._result, f.exception(), f._state, err finally: # Do best to cancel remain jobs. @@ -401,7 +400,7 @@ def wrap_job_tb(msg="Reason: %s"): try: output = method(self, *args, **kwargs) except Exception as e: - LOG.error(msg, six.text_type(e)) + LOG.error(msg, str(e)) LOG.error(traceback.format_exc()) raise return output diff --git a/cyborg/conductor/manager.py b/cyborg/conductor/manager.py index 315c205..1f37907 100644 --- a/cyborg/conductor/manager.py +++ b/cyborg/conductor/manager.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from oslo_log import log as logging import oslo_messaging as messaging import uuid @@ -365,7 +363,7 @@ class ConductorManager(object): def _get_sub_provider(self, context, parent, name): old_sub_pr_uuid = str(uuid.uuid3(uuid.NAMESPACE_DNS, - six.ensure_str(name))) + str(name))) new_sub_pr_uuid = self.placement_client.ensure_resource_provider( context, old_sub_pr_uuid, name=name, parent_provider_uuid=parent) @@ -396,7 +394,7 @@ class ConductorManager(object): attrs = obj.attribute_list resource_class = [i.value for i in attrs if i.key == 'rc'][0] traits = [i.value for i in attrs - if six.ensure_str(i.key).startswith("trait")] + if str(i.key).startswith("trait")] total = obj.num_accelerators rp_uuid = self.provider_report(context, pr_name, resource_class, traits, total, parent_uuid) @@ -405,7 +403,7 @@ class ConductorManager(object): dep_obj.save(context) def get_rp_uuid_from_obj(self, obj): - return str(uuid.uuid3(uuid.NAMESPACE_DNS, six.ensure_str(obj.name))) + return str(uuid.uuid3(uuid.NAMESPACE_DNS, str(obj.name))) def _delete_provider_and_sub_providers(self, context, rp_uuid): rp_in_tree = self.placement_client.get_providers_in_tree(context, diff --git a/cyborg/context.py b/cyborg/context.py index 118bfb7..9cdf5c1 100644 --- a/cyborg/context.py +++ b/cyborg/context.py @@ -20,7 +20,6 @@ from keystoneauth1 import plugin from oslo_context import context from oslo_db.sqlalchemy import enginefacade from oslo_utils import timeutils -import six from cyborg.common import exception from cyborg.common import utils @@ -87,7 +86,7 @@ class RequestContext(context.RequestContext): self.remote_address = remote_address if not timestamp: timestamp = timeutils.utcnow() - if isinstance(timestamp, six.string_types): + if isinstance(timestamp, str): timestamp = timeutils.parse_strtime(timestamp) self.timestamp = timestamp diff --git a/cyborg/db/api.py b/cyborg/db/api.py index ebe09b1..faa7897 100644 --- a/cyborg/db/api.py +++ b/cyborg/db/api.py @@ -19,7 +19,6 @@ import abc from oslo_config import cfg from oslo_db import api as db_api -import six _BACKEND_MAPPING = {'sqlalchemy': 'cyborg.db.sqlalchemy.api'} @@ -33,8 +32,7 @@ def get_instance(): return IMPL -@six.add_metaclass(abc.ABCMeta) -class Connection(object): +class Connection(object, metaclass=abc.ABCMeta): """Base class for storage system connections.""" @abc.abstractmethod diff --git a/cyborg/db/sqlalchemy/models.py b/cyborg/db/sqlalchemy/models.py index 744df9c..dfc4e45 100644 --- a/cyborg/db/sqlalchemy/models.py +++ b/cyborg/db/sqlalchemy/models.py @@ -18,7 +18,6 @@ from oslo_db import options as db_options from oslo_db.sqlalchemy import models from oslo_utils import timeutils -import six.moves.urllib.parse as urlparse from sqlalchemy import Boolean from sqlalchemy import Column from sqlalchemy import DateTime @@ -31,6 +30,7 @@ from sqlalchemy import orm from sqlalchemy import schema from sqlalchemy import String from sqlalchemy import Text +import urllib.parse as urlparse from cyborg.common import constants from cyborg.common import paths diff --git a/cyborg/image/glance.py b/cyborg/image/glance.py index c110f96..8b69755 100644 --- a/cyborg/image/glance.py +++ b/cyborg/image/glance.py @@ -34,8 +34,6 @@ from oslo_log import log as logging from oslo_serialization import jsonutils from oslo_utils import excutils from oslo_utils import timeutils -import six -from six.moves import range from cyborg.common import exception from cyborg.common import utils @@ -176,7 +174,7 @@ class GlanceClientWrapper(object): 'method': method, 'extra': extra}) if attempt == num_attempts: raise exception.GlanceConnectionFailed( - server=str(self.api_server), reason=six.text_type(e)) + server=str(self.api_server), reason=str(e)) time.sleep(1) @@ -418,7 +416,7 @@ def _convert_to_v2(image_meta): # v1 client accepts any values and converts them to string, # v2 doesn't - so we have to take care of it. elif prop_value is None or isinstance( - prop_value, six.string_types): + prop_value, str): output[prop_name] = prop_value else: output[prop_name] = str(prop_value) @@ -454,13 +452,13 @@ def _convert_timestamps_to_datetimes(image_meta): # NOTE(bcwaldon): used to store non-string data in glance metadata def _json_loads(properties, attr): prop = properties[attr] - if isinstance(prop, six.string_types): + if isinstance(prop, str): properties[attr] = jsonutils.loads(prop) def _json_dumps(properties, attr): prop = properties[attr] - if not isinstance(prop, six.string_types): + if not isinstance(prop, str): properties[attr] = jsonutils.dumps(prop) @@ -576,14 +574,14 @@ def _reraise_translated_image_exception(image_id): """Transform the exception for the image but keep its traceback intact.""" exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_image_exception(image_id, exc_value) - six.reraise(type(new_exc), new_exc, exc_trace) + raise new_exc.with_traceback(exc_trace) def _reraise_translated_exception(): """Transform the exception but keep its traceback intact.""" exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_plain_exception(exc_value) - six.reraise(type(new_exc), new_exc, exc_trace) + raise new_exc.with_traceback(exc_trace) def _translate_image_exception(image_id, exc_value): @@ -596,18 +594,18 @@ def _translate_image_exception(image_id, exc_value): msg='with uuid=%s' % image_id) if isinstance(exc_value, glanceclient.exc.BadRequest): return exception.ImageBadRequest(image_id=image_id, - response=six.text_type(exc_value)) + response=str(exc_value)) return exc_value def _translate_plain_exception(exc_value): if isinstance(exc_value, (glanceclient.exc.Forbidden, glanceclient.exc.Unauthorized)): - return exception.Forbidden(six.text_type(exc_value)) + return exception.Forbidden(str(exc_value)) if isinstance(exc_value, glanceclient.exc.NotFound): - return exception.NotFound(six.text_type(exc_value)) + return exception.NotFound(str(exc_value)) if isinstance(exc_value, glanceclient.exc.BadRequest): - return exception.Invalid(six.text_type(exc_value)) + return exception.Invalid(str(exc_value)) return exc_value diff --git a/cyborg/objects/ext_arq.py b/cyborg/objects/ext_arq.py index 681962d..cc3762c 100644 --- a/cyborg/objects/ext_arq.py +++ b/cyborg/objects/ext_arq.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from openstack import connection from oslo_log import log as logging from oslo_versionedobjects import base as object_base @@ -194,7 +192,7 @@ class ExtARQ(base.CyborgObject, object_base.VersionedObjectDictCompat, except Exception as e: LOG.error("Failed to allocate attach handle for ARQ %s" "from deployable %s. Reason: %s", - self.arq.uuid, deployable.uuid, six.text_type(e)) + self.arq.uuid, deployable.uuid, str(e)) # TODO(Shaohe) Rollback? We have _update_placement, # should cancel it. self.update_check_state( diff --git a/cyborg/quota.py b/cyborg/quota.py index be82d8e..68e15bd 100644 --- a/cyborg/quota.py +++ b/cyborg/quota.py @@ -16,7 +16,6 @@ import datetime from oslo_config import cfg from oslo_log import log as logging from oslo_utils import timeutils -import six from cyborg.common import exception from cyborg import db as db_api @@ -147,7 +146,7 @@ class DbQuotaDriver(object): # Set up the reservation expiration if expire is None: expire = CONF.reservation_expire - if isinstance(expire, six.integer_types): + if isinstance(expire, int): expire = datetime.timedelta(seconds=expire) if isinstance(expire, datetime.timedelta): expire = timeutils.utcnow() + expire diff --git a/cyborg/tests/unit/accelerator/drivers/test_driver.py b/cyborg/tests/unit/accelerator/drivers/test_driver.py index b68b876..7e24693 100644 --- a/cyborg/tests/unit/accelerator/drivers/test_driver.py +++ b/cyborg/tests/unit/accelerator/drivers/test_driver.py @@ -11,8 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from cyborg.accelerator.drivers.driver import GenericDriver from cyborg.tests import base @@ -40,4 +38,4 @@ class TestGenericDriver(base.TestCase): # abstract methods get_stats, update result = self.assertRaises(TypeError, NotCompleteDriver) self.assertIn("Can't instantiate abstract class", - six.text_type(result)) + str(result)) diff --git a/cyborg/tests/unit/api/controllers/v2/test_arqs.py b/cyborg/tests/unit/api/controllers/v2/test_arqs.py index ea555db..358389b 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_arqs.py +++ b/cyborg/tests/unit/api/controllers/v2/test_arqs.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -from six.moves import http_client +from http import client as http_client import unittest from unittest import mock diff --git a/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py b/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py index 192e65c..7ee2d11 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py +++ b/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -from six.moves import http_client +from http import client as http_client from unittest import mock import webtest diff --git a/cyborg/tests/unit/api/controllers/v2/test_fpga_program.py b/cyborg/tests/unit/api/controllers/v2/test_fpga_program.py index 77241ae..e1d9c9d 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_fpga_program.py +++ b/cyborg/tests/unit/api/controllers/v2/test_fpga_program.py @@ -10,8 +10,8 @@ # License for the specific language governing permissions and limitations # under the License. +from http import client as http_client from oslo_serialization import jsonutils -from six.moves import http_client from unittest import mock from cyborg.common import exception diff --git a/cyborg/tests/unit/objects/test_extarq.py b/cyborg/tests/unit/objects/test_extarq.py index dff0f4d..cdda927 100644 --- a/cyborg/tests/unit/objects/test_extarq.py +++ b/cyborg/tests/unit/objects/test_extarq.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six from unittest import mock from testtools.matchers import HasLength @@ -334,7 +333,7 @@ class TestExtARQObject(base.DbTestCase): exception.ResourceNotFound, obj_extarq._allocate_attach_handle, self.context, fake_dep) mock_log.assert_called_once_with( - msg, obj_extarq.arq.uuid, fake_dep.uuid, six.text_type(e)) + msg, obj_extarq.arq.uuid, fake_dep.uuid, str(e)) @mock.patch('cyborg.objects.ExtARQ.get') @mock.patch('cyborg.objects.ExtARQ._from_db_object') diff --git a/cyborg/tests/unit/policies/test_device_profiles.py b/cyborg/tests/unit/policies/test_device_profiles.py index e0fae7d..4c17d0e 100644 --- a/cyborg/tests/unit/policies/test_device_profiles.py +++ b/cyborg/tests/unit/policies/test_device_profiles.py @@ -13,10 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. - +from http import client as http_client from oslo_log import log as logging from oslo_serialization import jsonutils -from six.moves import http_client from unittest import mock from cyborg.api.controllers.v2 import device_profiles diff --git a/requirements.txt b/requirements.txt index d9d60cc..1e74cb2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,6 @@ pbr>=0.11,!=2.1.0 # Apache-2.0 pecan>=1.0.0,!=1.0.2,!=1.0.3,!=1.0.4,!=1.2 # BSD WSME>=0.8.0 # MIT -six>=1.8.0 # MIT eventlet>=0.26.0 # MIT oslo.i18n>=1.5.0 # Apache-2.0 oslo.config>=1.1.0,!=4.3.0,!=4.4.0 # Apache-2.0