Browse Source

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
changes/80/702080/5
Andreas Jaeger 2 years ago
parent
commit
7103caa069
  1. 6
      heat_dashboard/api/heat.py
  2. 5
      heat_dashboard/content/stacks/forms.py
  3. 5
      heat_dashboard/content/stacks/mappings.py
  4. 19
      heat_dashboard/test/helpers.py
  5. 5
      heat_dashboard/test/test_data/exceptions.py
  6. 7
      heat_dashboard/test/tests/api/test_heat.py
  7. 7
      heat_dashboard/test/tests/content/test_stacks.py

6
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:

5
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

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

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

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

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

7
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'<pre>%s</pre>' % html.escape(expected_text),
mappings.stack_output(outputs))

Loading…
Cancel
Save