From 7103caa0699e05ec9321d12485aabe1ce00bd36e Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Sat, 11 Jan 2020 16:39:59 +0100 Subject: [PATCH] Remove six usage (1/2) This repo does not support Python 2 anymore, so we don't need six for compatibility between Python2 and 3, convert six usage to Python 3 code. This changes everything besides urllib. Change-Id: I43f76bc07d846341c1f5da69614e2be51ee05e30 Needed-By: https://review.opendev.org/701743 --- heat_dashboard/api/heat.py | 6 ++---- heat_dashboard/content/stacks/forms.py | 5 ++--- heat_dashboard/content/stacks/mappings.py | 5 ++--- heat_dashboard/test/helpers.py | 19 ++++++++----------- heat_dashboard/test/test_data/exceptions.py | 5 ----- heat_dashboard/test/tests/api/test_heat.py | 7 +++++-- .../test/tests/content/test_stacks.py | 7 +------ 7 files changed, 20 insertions(+), 34 deletions(-) diff --git a/heat_dashboard/api/heat.py b/heat_dashboard/api/heat.py index 86c44fab..09286e9b 100644 --- a/heat_dashboard/api/heat.py +++ b/heat_dashboard/api/heat.py @@ -12,8 +12,6 @@ import contextlib -import six - from six.moves.urllib import request from django.conf import settings @@ -103,7 +101,7 @@ def stacks_list(request, marker=None, sort_dir='desc', sort_key='created_at', def _ignore_if(key, value): if key != 'get_file' and key != 'type': return True - if not isinstance(value, six.string_types): + if not isinstance(value, str): return True if (key == 'type' and not value.endswith(('.yaml', '.template'))): return True @@ -121,7 +119,7 @@ def get_template_files(template_data=None, template_url=None, files=None): return {}, None if not tpl: return {}, None - if isinstance(tpl, six.binary_type): + if isinstance(tpl, bytes): tpl = tpl.decode('utf-8') template = template_format.parse(tpl) if files is None: diff --git a/heat_dashboard/content/stacks/forms.py b/heat_dashboard/content/stacks/forms.py index d9def18b..b23561e6 100644 --- a/heat_dashboard/content/stacks/forms.py +++ b/heat_dashboard/content/stacks/forms.py @@ -20,7 +20,6 @@ from django.utils.translation import ugettext_lazy as _ from django.views.decorators.debug import sensitive_variables from oslo_utils import strutils -import six from horizon import exceptions from horizon import forms @@ -172,7 +171,7 @@ class TemplateForm(forms.SelfHandlingForm): cleaned['template_validate']['files'] = files cleaned['template_validate']['template'] = tpl except Exception as e: - raise forms.ValidationError(six.text_type(e)) + raise forms.ValidationError(str(e)) return cleaned @@ -211,7 +210,7 @@ class TemplateForm(forms.SelfHandlingForm): except Exception as e: msg = _('There was a problem parsing the' ' %(prefix)s: %(error)s') - msg = msg % {'prefix': prefix, 'error': six.text_type(e)} + msg = msg % {'prefix': prefix, 'error': str(e)} raise forms.ValidationError(msg) # URL handler diff --git a/heat_dashboard/content/stacks/mappings.py b/heat_dashboard/content/stacks/mappings.py index 838c1830..6156ac31 100644 --- a/heat_dashboard/content/stacks/mappings.py +++ b/heat_dashboard/content/stacks/mappings.py @@ -12,6 +12,7 @@ import json import logging +import urllib.parse as urlparse from django.conf import settings from django.template.defaultfilters import register @@ -19,8 +20,6 @@ from django.urls import reverse from django.utils import html from django.utils import safestring -import six -import six.moves.urllib.parse as urlparse from openstack_dashboard.api import swift @@ -133,7 +132,7 @@ def resource_to_url(resource): def stack_output(output): if not output: return u'' - if isinstance(output, six.string_types): + if isinstance(output, str): parts = urlparse.urlsplit(output) if parts.netloc and parts.scheme in ('http', 'https'): url = html.escape(output) diff --git a/heat_dashboard/test/helpers.py b/heat_dashboard/test/helpers.py index 5fb81553..d80feb26 100644 --- a/heat_dashboard/test/helpers.py +++ b/heat_dashboard/test/helpers.py @@ -36,9 +36,6 @@ from openstack_auth import user from openstack_auth import utils from requests.packages.urllib3.connection import HTTPConnection -import six -from six import moves - from horizon.test import helpers as horizon_helpers from openstack_dashboard import api as project_api @@ -203,7 +200,7 @@ class TestCase(horizon_helpers.TestCase): processing the view which is redirected to. """ if django.VERSION >= (1, 9): - loc = six.text_type(response._headers.get('location', None)[1]) + loc = str(response._headers.get('location', None)[1]) loc = http.urlunquote(loc) expected_url = http.urlunquote(expected_url) self.assertEqual(loc, expected_url) @@ -241,7 +238,7 @@ class TestCase(horizon_helpers.TestCase): assert len(errors) == count, \ "%d errors were found on the form, %d expected" % \ (len(errors), count) - if message and message not in six.text_type(errors): + if message and message not in str(errors): self.fail("Expected message not found, instead found: %s" % ["%s: %s" % (key, [e for e in field_errors]) for (key, field_errors) in errors.items()]) @@ -265,13 +262,13 @@ class TestCase(horizon_helpers.TestCase): def getAndAssertTableRowAction(self, response, table_name, action_name, row_id): table = response.context[table_name + '_table'] - rows = list(moves.filter(lambda x: x.id == row_id, - table.data)) + rows = list(filter(lambda x: x.id == row_id, + table.data)) self.assertEqual(1, len(rows), "Did not find a row matching id '%s'" % row_id) row_actions = table.get_row_actions(rows[0]) - actions = list(moves.filter(lambda x: x.name == action_name, - row_actions)) + actions = list(filter(lambda x: x.name == action_name, + row_actions)) msg_args = (action_name, table_name, row_id) self.assertGreater( @@ -289,8 +286,8 @@ class TestCase(horizon_helpers.TestCase): table = response.context[table_name + '_table'] table_actions = table.get_table_actions() - actions = list(moves.filter(lambda x: x.name == action_name, - table_actions)) + actions = list(filter(lambda x: x.name == action_name, + table_actions)) msg_args = (action_name, table_name) self.assertGreater( len(actions), 0, diff --git a/heat_dashboard/test/test_data/exceptions.py b/heat_dashboard/test/test_data/exceptions.py index 8b4c31f3..42bec768 100644 --- a/heat_dashboard/test/test_data/exceptions.py +++ b/heat_dashboard/test/test_data/exceptions.py @@ -13,7 +13,6 @@ # under the License. import heatclient.exc as heat_exceptions -import six from heat_dashboard.test.test_data import utils @@ -39,12 +38,8 @@ def create_stubbed_exception(cls, status_code=500): def fake_str(self): return str(self.message) - def fake_unicode(self): - return six.text_type(self.message) - cls.__init__ = fake_init_exception cls.__str__ = fake_str - cls.__unicode__ = fake_unicode cls.silence_logging = True return cls(status_code, msg) diff --git a/heat_dashboard/test/tests/api/test_heat.py b/heat_dashboard/test/tests/api/test_heat.py index f13abd37..d00118cd 100644 --- a/heat_dashboard/test/tests/api/test_heat.py +++ b/heat_dashboard/test/tests/api/test_heat.py @@ -9,6 +9,9 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. + +import io + import mock import six @@ -273,7 +276,7 @@ class HeatApiTests(test.APITestCase): expected_files = {u'http://test.example/example': b'echo "test"'} url = 'http://test.example/example' data = b'echo "test"' - mock_request.return_value = six.BytesIO(data) + mock_request.return_value = io.BytesIO(data) files = api.heat.get_template_files(template_data=tmpl)[0] self.assertEqual(files, expected_files) @@ -299,7 +302,7 @@ class HeatApiTests(test.APITestCase): data2 = b'echo "test"' expected_files = {'http://test.example/example': b'echo "test"'} mock_request.side_effect = \ - [six.BytesIO(data), six.BytesIO(data2)] + [io.BytesIO(data), io.BytesIO(data2)] files = api.heat.get_template_files(template_url=url)[0] self.assertEqual(files, expected_files) diff --git a/heat_dashboard/test/tests/content/test_stacks.py b/heat_dashboard/test/tests/content/test_stacks.py index e2410c13..ae071611 100644 --- a/heat_dashboard/test/tests/content/test_stacks.py +++ b/heat_dashboard/test/tests/content/test_stacks.py @@ -19,7 +19,6 @@ from django.test.utils import override_settings from django.urls import reverse from django.utils import html import mock -import six from heatclient.common import template_format as hc_format from openstack_dashboard import api as dashboard_api @@ -92,11 +91,7 @@ class MappingsTests(test.TestCase): self.assertEqual(u'', mappings.stack_output(None)) outputs = ['one', 'two', 'three'] - # On Python 3, the pretty JSON output doesn't add space before newline - if six.PY3: - expected_text = """[\n "one",\n "two",\n "three"\n]""" - else: - expected_text = """[\n "one", \n "two", \n "three"\n]""" + expected_text = """[\n "one",\n "two",\n "three"\n]""" self.assertEqual(u'
%s
' % html.escape(expected_text), mappings.stack_output(outputs))