From 3cfa54fa56674362ffbbbd3199e6be3c0e4ffbb9 Mon Sep 17 00:00:00 2001 From: zhanghao Date: Thu, 14 May 2020 02:03:50 -0400 Subject: [PATCH] Remove usage of six With python3.x, classes can use 'metaclass=' instead of 'six.add_metaclass', 'six.iteritems' and 'six.iterkeys' can be replaced by 'items' and 'keys', 'six.moves.urllib.parse' can be replaced by 'urllib.parse', 'six.StringIO' and 'six.moves.cStringIO' can be replaced by 'io.StringIO', 'six.text_type' and 'six.string_type' are just 'str'. Change-Id: I357968c6a1932856b1600f6c191966bc90cbc258 --- lower-constraints.txt | 1 - neutronclient/common/exceptions.py | 2 -- neutronclient/common/serializer.py | 3 +-- neutronclient/common/utils.py | 5 ++--- neutronclient/neutron/v2_0/__init__.py | 16 +++++++--------- neutronclient/neutron/v2_0/lb/pool.py | 5 +---- .../neutron/v2_0/network_ip_availability.py | 3 +-- neutronclient/neutron/v2_0/quota.py | 7 +++---- neutronclient/tests/unit/test_casual_args.py | 5 ++--- neutronclient/tests/unit/test_cli20.py | 16 ++++++++-------- .../tests/unit/test_cli20_securitygroup.py | 4 ++-- neutronclient/tests/unit/test_exceptions.py | 3 +-- neutronclient/tests/unit/test_http.py | 4 +--- neutronclient/tests/unit/test_shell.py | 6 +++--- neutronclient/v2_0/client.py | 5 ++--- requirements.txt | 1 - 16 files changed, 34 insertions(+), 52 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 08d37a452..48b0056b0 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -97,7 +97,6 @@ requestsexceptions==1.2.0 rfc3986==0.3.1 Routes==2.3.1 simplejson==3.5.1 -six==1.10.0 snowballstemmer==1.2.1 statsd==3.2.1 stestr==2.0.0 diff --git a/neutronclient/common/exceptions.py b/neutronclient/common/exceptions.py index 1040ee120..443f78155 100644 --- a/neutronclient/common/exceptions.py +++ b/neutronclient/common/exceptions.py @@ -14,7 +14,6 @@ # under the License. from oslo_utils import encodeutils -import six from neutronclient._i18n import _ @@ -40,7 +39,6 @@ def _safe_decode_dict(kwargs): return kwargs -@six.python_2_unicode_compatible class NeutronException(Exception): """Base Neutron Exception. diff --git a/neutronclient/common/serializer.py b/neutronclient/common/serializer.py index 98865a6b3..d4c8bbbb6 100644 --- a/neutronclient/common/serializer.py +++ b/neutronclient/common/serializer.py @@ -14,7 +14,6 @@ # under the License. from oslo_serialization import jsonutils -import six from neutronclient._i18n import _ from neutronclient.common import exceptions as exception @@ -48,7 +47,7 @@ class JSONDictSerializer(DictSerializer): def default(self, data): def sanitizer(obj): - return six.text_type(obj) + return str(obj) return jsonutils.dumps(data, default=sanitizer) diff --git a/neutronclient/common/utils.py b/neutronclient/common/utils.py index b61ee3b7a..38274018c 100644 --- a/neutronclient/common/utils.py +++ b/neutronclient/common/utils.py @@ -25,7 +25,6 @@ import os from oslo_utils import encodeutils from oslo_utils import importutils -import six from neutronclient._i18n import _ from neutronclient.common import exceptions @@ -180,7 +179,7 @@ def http_log_req(_logger, args, kwargs): else: string_parts.append(' %s' % element) - for (key, value) in six.iteritems(kwargs['headers']): + for (key, value) in kwargs['headers'].items(): if key in SENSITIVE_HEADERS: v = value.encode('utf-8') h = hashlib.sha256(v) @@ -205,7 +204,7 @@ def http_log_resp(_logger, resp, body): def _safe_encode_without_obj(data): - if isinstance(data, six.string_types): + if isinstance(data, str): return encodeutils.safe_encode(data) return data diff --git a/neutronclient/neutron/v2_0/__init__.py b/neutronclient/neutron/v2_0/__init__.py index a5b262c93..3a6e086ee 100644 --- a/neutronclient/neutron/v2_0/__init__.py +++ b/neutronclient/neutron/v2_0/__init__.py @@ -25,7 +25,6 @@ from cliff import command from cliff import lister from cliff import show from oslo_serialization import jsonutils -import six from neutronclient._i18n import _ from neutronclient.common import exceptions @@ -261,12 +260,12 @@ def parse_args_to_dict(values_specs): # Populate the parser with arguments _parser = argparse.ArgumentParser(add_help=False) - for opt, optspec in six.iteritems(_options): + for opt, optspec in _options.items(): _parser.add_argument(opt, **optspec) _args = _parser.parse_args(_values_specs) result_dict = {} - for opt in six.iterkeys(_options): + for opt in _options.keys(): _opt = opt.split('--', 2)[1] _opt = _opt.replace('-', '_') _value = getattr(_args, _opt) @@ -285,7 +284,7 @@ def _merge_args(qCmd, parsed_args, _extra_values, value_specs): @param values_specs: the unparsed unknown parts """ temp_values = _extra_values.copy() - for key, value in six.iteritems(temp_values): + for key, value in temp_values.items(): if hasattr(parsed_args, key): arg_value = getattr(parsed_args, key) if arg_value is not None and value is not None: @@ -321,8 +320,7 @@ class NeutronCommandMeta(abc.ABCMeta): name, bases, cls_dict) -@six.add_metaclass(NeutronCommandMeta) -class NeutronCommand(command.Command): +class NeutronCommand(command.Command, metaclass=NeutronCommandMeta): values_specs = [] json_indent = None @@ -363,7 +361,7 @@ class NeutronCommand(command.Command): def format_output_data(self, data): # Modify data to make it more readable if self.resource in data: - for k, v in six.iteritems(data[self.resource]): + for k, v in data[self.resource].items(): if isinstance(v, list): value = '\n'.join(jsonutils.dumps( i, indent=self.json_indent) if isinstance(i, dict) @@ -425,7 +423,7 @@ class CreateCommand(NeutronCommand, show.ShowOne): file=self.app.stdout) else: info = {'': ''} - return zip(*sorted(six.iteritems(info))) + return zip(*sorted(info.items())) class UpdateCommand(NeutronCommand): @@ -825,6 +823,6 @@ class ShowCommand(NeutronCommand, show.ShowOne): self.format_output_data(data) resource = data[self.resource] if self.resource in data: - return zip(*sorted(six.iteritems(resource))) + return zip(*sorted(resource.items())) else: return None diff --git a/neutronclient/neutron/v2_0/lb/pool.py b/neutronclient/neutron/v2_0/lb/pool.py index afc0cc13e..5684e8e6a 100644 --- a/neutronclient/neutron/v2_0/lb/pool.py +++ b/neutronclient/neutron/v2_0/lb/pool.py @@ -14,9 +14,6 @@ # under the License. # - -import six - from neutronclient._i18n import _ from neutronclient.neutron import v2_0 as neutronV20 @@ -119,6 +116,6 @@ class RetrievePoolStats(neutronV20.ShowCommand): self.format_output_data(data) stats = data['stats'] if 'stats' in data: - return zip(*sorted(six.iteritems(stats))) + return zip(*sorted(stats.items())) else: return None diff --git a/neutronclient/neutron/v2_0/network_ip_availability.py b/neutronclient/neutron/v2_0/network_ip_availability.py index d1332a21c..ba7af8f80 100644 --- a/neutronclient/neutron/v2_0/network_ip_availability.py +++ b/neutronclient/neutron/v2_0/network_ip_availability.py @@ -12,7 +12,6 @@ # from cliff import show -import six from neutronclient._i18n import _ from neutronclient.neutron import v2_0 as neutronV20 @@ -68,6 +67,6 @@ class ShowIpAvailability(neutronV20.NeutronCommand, show.ShowOne): self.format_output_data(data) resource = data[self.resource] if self.resource in data: - return zip(*sorted(six.iteritems(resource))) + return zip(*sorted(resource.items())) else: return None diff --git a/neutronclient/neutron/v2_0/quota.py b/neutronclient/neutron/v2_0/quota.py index 6e7058d8c..1a59ac546 100644 --- a/neutronclient/neutron/v2_0/quota.py +++ b/neutronclient/neutron/v2_0/quota.py @@ -22,7 +22,6 @@ import argparse from cliff import lister from cliff import show from oslo_serialization import jsonutils -import six from neutronclient._i18n import _ from neutronclient.common import exceptions @@ -121,7 +120,7 @@ class ShowQuotaBase(neutronV20.NeutronCommand, show.ShowOne): tenant_id = get_tenant_id(parsed_args, neutron_client) data = self.retrieve_data(tenant_id, neutron_client) if self.resource in data: - return zip(*sorted(six.iteritems(data[self.resource]))) + return zip(*sorted(data[self.resource].items())) return @@ -241,7 +240,7 @@ class UpdateQuota(neutronV20.NeutronCommand, show.ShowOne): tenant_id = get_tenant_id(parsed_args, neutron_client) data = obj_updator(tenant_id, body) if self.resource in data: - for k, v in six.iteritems(data[self.resource]): + for k, v in data[self.resource].items(): if isinstance(v, list): value = "" for _item in v: @@ -254,6 +253,6 @@ class UpdateQuota(neutronV20.NeutronCommand, show.ShowOne): data[self.resource][k] = value elif v is None: data[self.resource][k] = '' - return zip(*sorted(six.iteritems(data[self.resource]))) + return zip(*sorted(data[self.resource].items())) else: return diff --git a/neutronclient/tests/unit/test_casual_args.py b/neutronclient/tests/unit/test_casual_args.py index f2ca03101..cbf061617 100644 --- a/neutronclient/tests/unit/test_casual_args.py +++ b/neutronclient/tests/unit/test_casual_args.py @@ -14,7 +14,6 @@ # under the License. # -import six import testtools from neutronclient.common import exceptions @@ -116,7 +115,7 @@ class CLITestArgs(testtools.TestCase): neutronV20.parse_args_to_dict, _specs) self.assertEqual('Invalid value_specs --badtypearg type=badtype val1: ' 'type badtype is not supported', - six.text_type(ex)) + str(ex)) def test_clear_action(self): _specs = ['--anyarg', 'action=clear'] @@ -128,7 +127,7 @@ class CLITestArgs(testtools.TestCase): ex = self.assertRaises(exceptions.CommandError, neutronV20.parse_args_to_dict, _specs) self.assertEqual('Invalid values_specs --strarg type=str', - six.text_type(ex)) + str(ex)) def test_bad_values_list(self): _specs = ['--listarg', 'list=true', 'type=str'] diff --git a/neutronclient/tests/unit/test_cli20.py b/neutronclient/tests/unit/test_cli20.py index 3a8bb4719..73104b54c 100644 --- a/neutronclient/tests/unit/test_cli20.py +++ b/neutronclient/tests/unit/test_cli20.py @@ -15,16 +15,16 @@ # import contextlib +from io import StringIO import itertools import sys +import urllib.parse as urlparse import mock from oslo_serialization import jsonutils from oslo_utils import encodeutils from oslotest import base import requests -import six -import six.moves.urllib.parse as urlparse import yaml from neutronclient.common import constants @@ -43,7 +43,7 @@ REQUEST_ID = 'test_request_id' @contextlib.contextmanager def capture_std_streams(): - fake_stdout, fake_stderr = six.StringIO(), six.StringIO() + fake_stdout, fake_stderr = StringIO(), StringIO() stdout, stderr = sys.stdout, sys.stderr try: sys.stdout, sys.stderr = fake_stdout, fake_stderr @@ -125,7 +125,7 @@ class MyComparator(object): def _com_dict(self, lhs, rhs): if len(lhs) != len(rhs): return False - for key, value in six.iteritems(lhs): + for key, value in lhs.items(): if key not in rhs: return False rhs_value = rhs[key] @@ -743,7 +743,7 @@ class ClientV2TestJson(CLITestV20Base): def test_do_request_error_without_response_body(self): params = {'test': 'value'} - expect_query = six.moves.urllib.parse.urlencode(params) + expect_query = urlparse.urlencode(params) self.client.httpclient.auth_token = 'token' resp_headers = {'x-openstack-request-id': REQUEST_ID} resp = (MyResp(400, headers=resp_headers, reason='An error'), '') @@ -772,7 +772,7 @@ class ClientV2TestJson(CLITestV20Base): def test_do_request_request_ids(self): params = {'test': 'value'} - expect_query = six.moves.urllib.parse.urlencode(params) + expect_query = urlparse.urlencode(params) self.client.httpclient.auth_token = 'token' body = params expect_body = self.client.serialize(body) @@ -866,7 +866,7 @@ class ClientV2TestJson(CLITestV20Base): def test_update_resource(self): params = {'test': 'value'} - expect_query = six.moves.urllib.parse.urlencode(params) + expect_query = urlparse.urlencode(params) self.client.httpclient.auth_token = 'token' body = params expect_body = self.client.serialize(body) @@ -889,7 +889,7 @@ class ClientV2TestJson(CLITestV20Base): def test_update_resource_with_revision_number(self): params = {'test': 'value'} - expect_query = six.moves.urllib.parse.urlencode(params) + expect_query = urlparse.urlencode(params) self.client.httpclient.auth_token = 'token' body = params expect_body = self.client.serialize(body) diff --git a/neutronclient/tests/unit/test_cli20_securitygroup.py b/neutronclient/tests/unit/test_cli20_securitygroup.py index 0f5a240cf..010f7dcae 100644 --- a/neutronclient/tests/unit/test_cli20_securitygroup.py +++ b/neutronclient/tests/unit/test_cli20_securitygroup.py @@ -15,10 +15,10 @@ # under the License. import sys +import urllib.parse as urlparse import mock from oslo_utils import uuidutils -import six from neutronclient.common import exceptions from neutronclient.common import utils @@ -250,7 +250,7 @@ class CLITestV20SecurityGroupsJSON(test_cli20.CLITestV20Base): resp_str = self.client.serialize({'security_groups': response}) result.append({ - 'filter': six.moves.urllib.parse.urlencode(params, doseq=1), + 'filter': urlparse.urlencode(params, doseq=1), 'response': (test_cli20.MyResp(200), resp_str), }) diff --git a/neutronclient/tests/unit/test_exceptions.py b/neutronclient/tests/unit/test_exceptions.py index aefc24c66..50b2bdbbe 100644 --- a/neutronclient/tests/unit/test_exceptions.py +++ b/neutronclient/tests/unit/test_exceptions.py @@ -16,7 +16,6 @@ import sys import mock from oslo_utils import encodeutils -import six import testtools from neutronclient._i18n import _ @@ -46,4 +45,4 @@ class TestExceptions(testtools.TestCase): multibyte_binary = encodeutils.safe_encode(multibyte_string) e = TestException(reason=multibyte_binary) self.assertEqual('Exception with %s' % multibyte_string, - six.text_type(e)) + str(e)) diff --git a/neutronclient/tests/unit/test_http.py b/neutronclient/tests/unit/test_http.py index d76e9bce9..5680d4375 100644 --- a/neutronclient/tests/unit/test_http.py +++ b/neutronclient/tests/unit/test_http.py @@ -19,7 +19,6 @@ from oslo_utils import uuidutils import osprofiler.profiler import osprofiler.web from requests_mock.contrib import fixture as mock_fixture -import six import testtools from neutronclient import client @@ -33,8 +32,7 @@ URL = 'http://test.test:1234/v2.0/test' BODY = 'IAMFAKE' -@six.add_metaclass(abc.ABCMeta) -class TestHTTPClientMixin(object): +class TestHTTPClientMixin(object, metaclass=abc.ABCMeta): def setUp(self): super(TestHTTPClientMixin, self).setUp() diff --git a/neutronclient/tests/unit/test_shell.py b/neutronclient/tests/unit/test_shell.py index 0395d6233..86250a139 100644 --- a/neutronclient/tests/unit/test_shell.py +++ b/neutronclient/tests/unit/test_shell.py @@ -14,6 +14,7 @@ # under the License. import argparse +from io import StringIO import logging import os import re @@ -22,7 +23,6 @@ import sys import fixtures from keystoneauth1 import session import mock -import six import testtools from testtools import matchers @@ -75,8 +75,8 @@ class ShellTest(testtools.TestCase): clean_env = {} _old_env, os.environ = os.environ, clean_env.copy() try: - sys.stdout = six.moves.cStringIO() - sys.stderr = six.moves.cStringIO() + sys.stdout = StringIO() + sys.stderr = StringIO() _shell = openstack_shell.NeutronShell('2.0') _shell.run(argstr.split()) except SystemExit: diff --git a/neutronclient/v2_0/client.py b/neutronclient/v2_0/client.py index 5fa869840..13f90e42d 100644 --- a/neutronclient/v2_0/client.py +++ b/neutronclient/v2_0/client.py @@ -21,12 +21,11 @@ import itertools import logging import re import time +import urllib.parse as urlparse import debtcollector.renames from keystoneauth1 import exceptions as ksa_exc import requests -import six.moves.urllib.parse as urlparse -from six import string_types from neutronclient._i18n import _ from neutronclient import client @@ -398,7 +397,7 @@ class ClientBase(object): if item: if isinstance(item, dict): return _DictWithMeta(item, resp) - elif isinstance(item, string_types): + elif isinstance(item, str): return _StrWithMeta(item, resp) else: return _TupleWithMeta((), resp) diff --git a/requirements.txt b/requirements.txt index 5204dc21e..c1c9c28e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,4 +18,3 @@ keystoneauth1>=3.4.0 # Apache-2.0 python-keystoneclient>=3.8.0 # Apache-2.0 requests>=2.14.2 # Apache-2.0 simplejson>=3.5.1 # MIT -six>=1.10.0 # MIT