Skip to content

Commit

Permalink
Remove six usage and basestring check
Browse files Browse the repository at this point in the history
Remove basestring check.
Remove six Replace the following items with Python 3 style code.

- six.string_types
- six.int2byte
- six.indexbytes
- six.add_metaclass
- six.StringIO
- six.text_type
- six.integer_types
- six.binary_type
- six.BytesIO
- six.reraise

Change-Id: I4fb9033d152963c504ceb4d5c4d08f934ee4accb
  • Loading branch information
wangzihao committed Oct 16, 2020
1 parent 9e2515a commit e954184
Show file tree
Hide file tree
Showing 42 changed files with 106 additions and 184 deletions.
2 changes: 0 additions & 2 deletions HACKING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ Trove Library Specific Commandments
-------------------------------------

- [T103] Exception messages should be translated
- [T104] Python 3 is not support basestring,replace basestring with
six.string_types
- [T105] Validate no LOG translations
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ python-swiftclient>=2.2.0
python-cinderclient>=1.1.0
python-keystoneclient>=2.0.0,!=2.1.0 # Apache-2.0
kombu>=2.5.0
six>=1.7.0
babel
python-heatclient>=0.2.9
passlib
Expand Down
1 change: 0 additions & 1 deletion integration/tests/integration/int_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import atexit
import gettext
import os
import six
import sys
import proboscis

Expand Down
1 change: 0 additions & 1 deletion lower-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ restructuredtext-lint==1.1.3
rfc3986==1.1.0
Routes==2.3.1
simplejson==3.13.2
six==1.10.0
smmap2==2.0.3
snowballstemmer==1.2.1
Sphinx==1.6.2
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ oslo.upgradecheck>=0.1.0 # Apache-2.0
oslo.utils>=3.33.0 # Apache-2.0
oslo.concurrency>=3.26.0 # Apache-2.0
PyMySQL>=0.7.6 # MIT License
six>=1.10.0 # MIT
stevedore>=1.20.0 # Apache-2.0
oslo.messaging>=5.29.0 # Apache-2.0
osprofiler>=1.4.0 # Apache-2.0
Expand Down
4 changes: 1 addition & 3 deletions tools/trove-pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io
import os
import re
import six
import sys

from pylint import lint
Expand Down Expand Up @@ -60,8 +59,7 @@ def sort_config(self):
sorted_config = OrderedDict()
for key in sorted(self.config.keys()):
value = self.get(key)
if isinstance(value, list) and not isinstance(value,
six.string_types):
if isinstance(value, list) and not isinstance(value,str):
sorted_config[key] = sorted(value)
else:
sorted_config[key] = value
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import_exceptions = trove.common.i18n
[flake8:local-plugins]
extension =
# T103= checks:check_raised_localized_exceptions
T104 = checks:check_no_basestring
T105 = checks:no_translate_logs
N335 = checks:assert_raises_regexp
paths = ./trove/hacking
Expand Down
4 changes: 1 addition & 3 deletions trove/cluster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

from neutronclient.common import exceptions as neutron_exceptions
Expand Down Expand Up @@ -324,7 +322,7 @@ def action(self, context, req, action, param):
node['availability_zone'])
if 'type' in node:
instance_type = node['type']
if isinstance(instance_type, six.string_types):
if isinstance(instance_type, str):
instance_type = instance_type.split(',')
instance['instance_type'] = instance_type
instances.append(instance)
Expand Down
5 changes: 2 additions & 3 deletions trove/common/crypto_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import os
from oslo_utils import encodeutils
import random
import six
import string

from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -67,12 +66,12 @@ def decode_data(data):
# Pad the data string to an multiple of pad_size
def pad_for_encryption(data, pad_size=IV_BYTE_COUNT):
pad_count = pad_size - (len(data) % pad_size)
return data + six.int2byte(pad_count) * pad_count
return data + bytes((pad_count,)) * pad_count


# Unpad the data string by stripping off excess characters
def unpad_after_decryption(data):
return data[:len(data) - six.indexbytes(data, -1)]
return data[:len(data) - data[-1]]


def encrypt_data(data, key, iv_byte_count=IV_BYTE_COUNT):
Expand Down
3 changes: 1 addition & 2 deletions trove/common/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# under the License.

import abc
import six

from trove.common import cfg
from trove.common.i18n import _
Expand Down Expand Up @@ -85,7 +84,7 @@ def check_string(value, desc):
:param desc: Description for exception message.
:raises: ValueError if not a string/unicode.
"""
if not isinstance(value, six.string_types):
if not isinstance(value, str):
raise ValueError(_("%(desc)s is not a string. Type = %(t)s.")
% {'desc': desc, 't': type(value)})

Expand Down
4 changes: 1 addition & 3 deletions trove/common/db/postgresql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

import re

from six import u

from trove.common.db import models


class PostgreSQLSchema(models.DatastoreSchema):
"""Represents a PostgreSQL schema and its associated properties."""

name_regex = re.compile(u(r'^[\u0001-\u007F\u0080-\uFFFF]+[^\s]$'))
name_regex = re.compile(str(r'^[\u0001-\u007F\u0080-\uFFFF]+[^\s]$'))

def __init__(self, name=None, collate=None, character_set=None,
deserializing=False):
Expand Down
4 changes: 1 addition & 3 deletions trove/common/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from oslo_log import log as logging
from oslo_utils import encodeutils
import routes
import six
import stevedore
import webob.dec
import webob.exc
Expand All @@ -34,8 +33,7 @@
XMLNS_ATOM = "http://www.w3.org/2005/Atom"


@six.add_metaclass(abc.ABCMeta)
class ExtensionDescriptor(object):
class ExtensionDescriptor(object, metaclass=abc.ABCMeta):
"""Base class that defines the contract for extensions.
Note that you don't have to derive from this class to have a valid
Expand Down
4 changes: 1 addition & 3 deletions trove/common/server_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# under the License.
#

import six

from oslo_log import log as logging

from trove.common.clients import create_nova_client
Expand Down Expand Up @@ -84,7 +82,7 @@ def build_scheduler_hint(cls, context, locality, name_suffix):
scheduler_hint = None
if locality:
# Build the scheduler hint, but only if locality's a string
if isinstance(locality, six.string_types):
if isinstance(locality, str):
server_group = cls.create(
context, locality, name_suffix)
scheduler_hint = cls.convert_to_hint(
Expand Down
4 changes: 1 addition & 3 deletions trove/common/strategies/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import abc

from oslo_log import log as logging
import six

from trove.common.i18n import _
from trove.common import utils
Expand All @@ -26,8 +25,7 @@
LOG = logging.getLogger(__name__)


@six.add_metaclass(abc.ABCMeta)
class Strategy(object):
class Strategy(object, metaclass=abc.ABCMeta):

__strategy_ns__ = None

Expand Down
16 changes: 7 additions & 9 deletions trove/common/stream_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import re
import sys

import six
import xmltodict
import yaml

Expand Down Expand Up @@ -74,7 +73,7 @@ def _to_object(self, value):
# Return known mappings and quoted strings right away.
if value in self._object_mappings:
return self._object_mappings[value]
elif (isinstance(value, six.string_types) and
elif (isinstance(value, str) and
re.match("^'(.*)'|\"(.*)\"$", value)):
return value

Expand All @@ -84,8 +83,7 @@ def _to_object(self, value):
return value


@six.add_metaclass(abc.ABCMeta)
class StreamCodec(object):
class StreamCodec(object, metaclass=abc.ABCMeta):

@abc.abstractmethod
def serialize(self, data):
Expand Down Expand Up @@ -202,7 +200,7 @@ def __init__(self, default_value=None, comment_markers=('#', ';')):

def serialize(self, dict_data):
parser = self._init_config_parser(dict_data)
output = six.StringIO()
output = io.StringIO()
parser.write(output)

return output.getvalue()
Expand All @@ -217,8 +215,8 @@ def deserialize(self, stream):
for s in parser.sections()}

def _pre_parse(self, stream):
buf = six.StringIO()
for line in six.StringIO(stream):
buf = io.StringIO()
for line in io.StringIO(stream):
# Ignore commented lines.
if not line.startswith(self._comment_markers):
# Strip leading and trailing whitespaces from each line.
Expand Down Expand Up @@ -297,7 +295,7 @@ def __init__(self, delimiter=' ', comment_markers=('#'),
self._unpack_singletons = unpack_singletons

def serialize(self, dict_data):
output = six.StringIO()
output = io.StringIO()
writer = csv.writer(output, delimiter=self._delimiter,
quoting=self.QUOTING_MODE,
strict=self.STRICT_MODE,
Expand All @@ -309,7 +307,7 @@ def serialize(self, dict_data):
return output.getvalue()

def deserialize(self, stream):
reader = csv.reader(six.StringIO(stream),
reader = csv.reader(io.StringIO(stream),
delimiter=self._delimiter,
quoting=self.QUOTING_MODE,
strict=self.STRICT_MODE,
Expand Down
3 changes: 1 addition & 2 deletions trove/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from oslo_utils import importutils
from oslo_utils import strutils
from passlib import pwd
import six

from trove.common import cfg
from trove.common import exception
Expand Down Expand Up @@ -333,7 +332,7 @@ def is_collection(item):
"""Return True is a given item is an iterable collection, but not a string.
"""
return (isinstance(item, collections.Iterable) and
not isinstance(item, (bytes, six.text_type)))
not isinstance(item, (bytes, str)))


def format_output(message, format_len=79, truncate_len=None, replace_index=0):
Expand Down
7 changes: 3 additions & 4 deletions trove/configuration/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# under the License.

from oslo_log import log as logging
import six

from trove.cluster import models as cluster_models
import trove.common.apischema as apischema
Expand Down Expand Up @@ -307,7 +306,7 @@ def _validate_configuration(values, datastore_version, config_rules):
raise exception.UnprocessableEntity(message=msg)

# integer min/max checking
if isinstance(v, six.integer_types) and not isinstance(v, bool):
if isinstance(v, int) and not isinstance(v, bool):
if rule.min_size is not None:
try:
min_value = int(rule.min_size)
Expand Down Expand Up @@ -345,9 +344,9 @@ def _find_type(value_type):
if value_type == "boolean":
return bool
elif value_type == "string":
return six.string_types
return str
elif value_type == "integer":
return six.integer_types
return int
elif value_type == "float":
return float
else:
Expand Down
4 changes: 1 addition & 3 deletions trove/extensions/common/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from oslo_config.cfg import NoSuchOptError
from oslo_log import log as logging
from oslo_utils import importutils
import six

from trove.cluster import models as cluster_models
from trove.cluster.models import DBCluster
Expand Down Expand Up @@ -62,8 +61,7 @@ def authorize_target_action(cls, context, target_rule_name,
{'tenant': target.tenant_id})


@six.add_metaclass(abc.ABCMeta)
class BaseDatastoreRootController(ExtensionController):
class BaseDatastoreRootController(ExtensionController, metaclass=abc.ABCMeta):
"""Base class that defines the contract for root controllers."""

@abc.abstractmethod
Expand Down
4 changes: 1 addition & 3 deletions trove/guestagent/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import re

from oslo_log import log as logging
import six

from trove.guestagent.common import guestagent_utils
from trove.guestagent.common import operating_system
Expand Down Expand Up @@ -224,8 +223,7 @@ def refresh_cache(self):
self._value_cache = self.parse_configuration()


@six.add_metaclass(abc.ABCMeta)
class ConfigurationOverrideStrategy(object):
class ConfigurationOverrideStrategy(object, metaclass=abc.ABCMeta):
"""ConfigurationOverrideStrategy handles configuration files.
The strategy provides functionality to enumerate, apply and remove
configuration overrides.
Expand Down
4 changes: 1 addition & 3 deletions trove/guestagent/common/guestagent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import os
import re

import six

from trove.common import cfg
from trove.common import pagination
from trove.common import utils
Expand Down Expand Up @@ -112,7 +110,7 @@ def build_file_path(base_dir, base_name, *extensions):
def to_bytes(value):
"""Convert numbers with a byte suffix to bytes.
"""
if isinstance(value, six.string_types):
if isinstance(value, str):
pattern = re.compile(r'^(\d+)([K,M,G]{1})$')
match = pattern.match(value)
if match:
Expand Down
4 changes: 1 addition & 3 deletions trove/guestagent/datastore/mysql_common/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from oslo_log import log as logging
from oslo_utils import encodeutils
import six
import sqlalchemy
from sqlalchemy import exc
from sqlalchemy.sql.expression import text
Expand Down Expand Up @@ -93,8 +92,7 @@ def get_actual_db_status(self):
return service_status.ServiceStatuses.UNKNOWN


@six.add_metaclass(abc.ABCMeta)
class BaseMySqlAdmin(object):
class BaseMySqlAdmin(object, metaclass=abc.ABCMeta):
"""Handles administrative tasks on the MySQL database."""

def __init__(self, mysql_root_access, mysql_app):
Expand Down
4 changes: 1 addition & 3 deletions trove/guestagent/module/drivers/module_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import abc
import functools
import re
import six

from oslo_log import log as logging

Expand All @@ -27,8 +26,7 @@
LOG = logging.getLogger(__name__)


@six.add_metaclass(abc.ABCMeta)
class ModuleDriver(object):
class ModuleDriver(object, metaclass=abc.ABCMeta):
"""Base class that defines the contract for module drivers.
Note that you don't have to derive from this class to have a valid
Expand Down
Loading

0 comments on commit e954184

Please sign in to comment.