Skip to content

Commit

Permalink
Remove six usage (1/2)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ajaeger committed Jan 11, 2020
1 parent 994eeb5 commit 7103caa
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
6 changes: 2 additions & 4 deletions heat_dashboard/api/heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

import contextlib

import six

from six.moves.urllib import request

from django.conf import settings
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions heat_dashboard/content/stacks/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from django.views.decorators.debug import sensitive_variables

from oslo_utils import strutils
import six

from horizon import exceptions
from horizon import forms
Expand Down Expand Up @@ -172,7 +171,7 @@ def clean(self):
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

Expand Down Expand Up @@ -211,7 +210,7 @@ def clean_uploaded_files(self, prefix, field_label, cleaned, files):
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
Expand Down
5 changes: 2 additions & 3 deletions heat_dashboard/content/stacks/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

import json
import logging
import urllib.parse as urlparse

from django.conf import settings
from django.template.defaultfilters import register
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

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 8 additions & 11 deletions heat_dashboard/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
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
Expand Down Expand Up @@ -203,7 +200,7 @@ def assertRedirectsNoFollow(self, response, expected_url):
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)
Expand Down Expand Up @@ -241,7 +238,7 @@ def assertFormErrors(self, response, count=0, message=None,
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()])
Expand All @@ -265,13 +262,13 @@ def assertItemsCollectionEqual(self, response, items_list):
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(
Expand All @@ -289,8 +286,8 @@ def getAndAssertTableAction(self, response, table_name, action_name):

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,
Expand Down
5 changes: 0 additions & 5 deletions heat_dashboard/test/test_data/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# under the License.

import heatclient.exc as heat_exceptions
import six

from heat_dashboard.test.test_data import utils

Expand All @@ -39,12 +38,8 @@ def fake_init_exception(self, code=None, message=None, **kwargs):
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)

Expand Down
7 changes: 5 additions & 2 deletions heat_dashboard/test/tests/api/test_heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -273,7 +276,7 @@ def test_get_template_files(self, mock_request):
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)
Expand All @@ -299,7 +302,7 @@ def test_get_template_files_with_template_url(self, mock_request):
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)
Expand Down
7 changes: 1 addition & 6 deletions heat_dashboard/test/tests/content/test_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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
Expand Down Expand Up @@ -92,11 +91,7 @@ def test_stack_output(self):
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))
Expand Down

0 comments on commit 7103caa

Please sign in to comment.