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
This commit is contained in:
songwenping 2020-10-06 15:26:11 +08:00 committed by root
parent 665abe1f00
commit 02e573b95a
21 changed files with 39 additions and 68 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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,10 +58,8 @@ 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]
body = [jsonutils.dumps({'error_message': '\n'.join(app_iter)})]
if six.PY3:
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]))))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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