Remove six and python 2.7 full support

Six is in use to help us to keep support for python 2.7.
Since the ussuri cycle we decide to remove the python 2.7
support so we can go ahead and also remove six usage from
the python code.

Review process and help
-----------------------
Removing six introduce a lot of changes and an huge amount of modified files
To simplify reviews we decided to split changes into several patches to avoid
painful reviews and avoid mistakes.

To review this patch you can use the six documentation [1] to obtain help and
understand choices.

Additional informations
-----------------------
Changes related to 'six.b(data)' [2]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

six.b [2] encode the given datas in latin-1 in python3 so I did the same
things in this patch.

Latin-1 is equal to iso-8859-1 [3].

This encoding is the default encoding [4] of certain descriptive HTTP
headers.

I suggest to keep latin-1 for the moment and to move to another encoding
in a follow-up patch if needed to move to most powerful encoding (utf8).

HTML4 support utf8 charset and utf8 is the default charset for HTML5 [5].

Note that this commit message is autogenerated and not necesserly contains
changes related to 'six.b'

[1] https://six.readthedocs.io/
[2] https://six.readthedocs.io/#six.b
[3] https://docs.python.org/3/library/codecs.html#standard-encodings
[4] https://www.w3schools.com/charsets/ref_html_8859.asp
[5] https://www.w3schools.com/html/html_charset.asp

Patch 2 of a serie of 28 patches

Change-Id: I2795dee87f0e27b64820686acfc614ac2ba19a4f
This commit is contained in:
Hervé Beraud 2019-11-20 19:37:26 +01:00
parent 991e967846
commit bb02b2b5f1
10 changed files with 32 additions and 39 deletions

View File

@ -15,8 +15,7 @@
import contextlib
from oslo_log import log as logging
import six
from six.moves.urllib import parse
from urllib import parse
from webob import exc
from heat.api.openstack.v1 import util
@ -79,7 +78,7 @@ class InstantiationData(object):
try:
yield
except ValueError as parse_ex:
mdict = {'type': data_type, 'error': six.text_type(parse_ex)}
mdict = {'type': data_type, 'error': str(parse_ex)}
msg = _("%(type)s not in valid format: %(error)s") % mdict
raise exc.HTTPBadRequest(msg)
@ -101,7 +100,7 @@ class InstantiationData(object):
try:
adopt_data = template_format.simple_parse(adopt_data)
template_format.validate_template_limit(
six.text_type(adopt_data['template']))
str(adopt_data['template']))
return adopt_data['template']
except (ValueError, KeyError) as ex:
err_reason = _('Invalid adopt data: %s') % ex
@ -109,7 +108,7 @@ class InstantiationData(object):
elif self.PARAM_TEMPLATE in self.data:
template_data = self.data[self.PARAM_TEMPLATE]
if isinstance(template_data, dict):
template_format.validate_template_limit(six.text_type(
template_format.validate_template_limit(str(
template_data))
return template_data
@ -188,7 +187,7 @@ class StackController(object):
try:
return param_utils.extract_bool(name, value)
except ValueError as e:
raise exc.HTTPBadRequest(six.text_type(e))
raise exc.HTTPBadRequest(str(e))
def _extract_int_param(self, name, value,
allow_zero=True, allow_negative=False):
@ -196,13 +195,13 @@ class StackController(object):
return param_utils.extract_int(name, value,
allow_zero, allow_negative)
except ValueError as e:
raise exc.HTTPBadRequest(six.text_type(e))
raise exc.HTTPBadRequest(str(e))
def _extract_tags_param(self, tags):
try:
return param_utils.extract_tags(tags)
except ValueError as e:
raise exc.HTTPBadRequest(six.text_type(e))
raise exc.HTTPBadRequest(str(e))
def _index(self, req, use_admin_cnxt=False):
filter_whitelist = {
@ -392,7 +391,7 @@ class StackController(object):
if not is_update and key in args:
msg = _("%s flag only supported in stack update (or update "
"preview) request.") % key
raise exc.HTTPBadRequest(six.text_type(msg))
raise exc.HTTPBadRequest(str(msg))
return args
@util.registered_policy_enforce
@ -700,7 +699,7 @@ class StackController(object):
req.params.get(rpc_api.TEMPLATE_TYPE))
except ValueError as ex:
msg = _("Template type is not supported: %s") % ex
raise exc.HTTPBadRequest(six.text_type(msg))
raise exc.HTTPBadRequest(str(msg))
return self.rpc_client.generate_template(req.context,
type_name,
@ -753,10 +752,7 @@ class StackSerializer(serializers.JSONResponseSerializer):
def _populate_response_header(self, response, location, status):
response.status = status
if six.PY2:
response.headers['Location'] = location.encode('utf-8')
else:
response.headers['Location'] = location
response.headers['Location'] = location
response.headers['Content-Type'] = 'application/json'
return response
@ -764,7 +760,7 @@ class StackSerializer(serializers.JSONResponseSerializer):
self._populate_response_header(response,
result['stack']['links'][0]['href'],
201)
response.body = six.b(self.to_json(result))
response.body = self.to_json(result).encode('latin-1')
return response

View File

@ -11,7 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import functools
from webob import exc
from heat.common.i18n import _
@ -41,7 +42,7 @@ def registered_policy_enforce(handler):
def _policy_enforce(handler, is_registered_policy=False):
@six.wraps(handler)
@functools.wraps(handler)
def handle_stack_method(controller, req, tenant_id, **kwargs):
if req.context.tenant_id != tenant_id and not req.context.is_admin:
raise exc.HTTPForbidden()
@ -77,7 +78,7 @@ def registered_identified_stack(handler):
def _identified_stack(handler, is_registered_policy=False):
@six.wraps(handler)
@functools.wraps(handler)
def handle_stack_method(controller, req, stack_name, stack_id, **kwargs):
stack_identity = identifier.HeatIdentifier(req.context.tenant_id,
stack_name,
@ -126,7 +127,7 @@ def get_allowed_params(params, whitelist):
"""
allowed_params = {}
for key, get_type in six.iteritems(whitelist):
for key, get_type in whitelist.items():
assert get_type in PARAM_TYPES
value = None

View File

@ -11,7 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from six.moves.urllib import parse as urlparse
from urllib import parse as urlparse
def get_collection_links(request, items):

View File

@ -13,9 +13,9 @@
"""Controller that returns information on the heat API versions."""
import http.client
from oslo_serialization import jsonutils
import six
from six.moves import http_client
import webob.dec
@ -43,11 +43,11 @@ class Controller(object):
body = jsonutils.dumps(dict(versions=version_objs))
response = webob.Response(request=req,
status=http_client.MULTIPLE_CHOICES,
status=http.client.MULTIPLE_CHOICES,
content_type='application/json')
# NOTE(pas-ha) in WebOb, Response.body accepts only bytes,
# and Response.text accepts only unicode.
response.text = six.text_type(body)
response.text = str(body)
return response

View File

@ -27,7 +27,6 @@ from oslo_config import cfg
import oslo_i18n as i18n
from oslo_log import log as logging
from oslo_service import systemd
import six
from heat.cmd import api
from heat.cmd import api_cfn
@ -86,5 +85,5 @@ def main():
systemd.notify_once()
[service.wait() for service in services]
except RuntimeError as e:
msg = six.text_type(e)
msg = str(e)
sys.exit("ERROR: %s" % msg)

View File

@ -28,7 +28,6 @@ import oslo_i18n as i18n
from oslo_log import log as logging
from oslo_reports import guru_meditation_report as gmr
from oslo_service import systemd
import six
from heat.common import config
from heat.common import messaging
@ -72,5 +71,5 @@ def main():
systemd.notify_once()
server.wait()
except RuntimeError as e:
msg = six.text_type(e)
msg = str(e)
sys.exit("ERROR: %s" % msg)

View File

@ -30,7 +30,6 @@ import oslo_i18n as i18n
from oslo_log import log as logging
from oslo_reports import guru_meditation_report as gmr
from oslo_service import systemd
import six
from heat.common import config
from heat.common import messaging
@ -76,5 +75,5 @@ def main():
systemd.notify_once()
server.wait()
except RuntimeError as e:
msg = six.text_type(e)
msg = str(e)
sys.exit("ERROR: %s" % msg)

View File

@ -19,7 +19,6 @@ import sys
from oslo_config import cfg
from oslo_log import log
from six import moves
from heat.common import context
from heat.common import exception
@ -106,7 +105,7 @@ def do_reset_stack_status():
"intended to recover from specific crashes."))
print(_("It is advised to shutdown all Heat engines beforehand."))
print(_("Continue ? [y/N]"))
data = moves.input()
data = input()
if not data.lower().startswith('y'):
return
ctxt = context.get_admin_context()

View File

@ -11,6 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import functools
from keystoneauth1 import access
from keystoneauth1.identity import access as access_plugin
from keystoneauth1.identity import generic
@ -23,7 +25,6 @@ from oslo_log import log as logging
import oslo_messaging
from oslo_middleware import request_id as oslo_request_id
from oslo_utils import importutils
import six
from heat.common import config
from heat.common import endpoint_utils
@ -407,7 +408,7 @@ def ContextMiddleware_filter_factory(global_conf, **local_conf):
def request_context(func):
@six.wraps(func)
@functools.wraps(func)
def wrapped(self, ctx, *args, **kwargs):
try:
return func(self, ctx, *args, **kwargs)

View File

@ -13,7 +13,6 @@
import collections
from oslo_serialization import jsonutils
import six
from heat.common import environment_format as env_fmt
from heat.common import exception
@ -62,10 +61,10 @@ def merge_map(old, new, deep_merge=False):
old_v = old.get(k)
old[k] = merge_map(old_v, v, deep_merge) if old_v else v
elif (isinstance(v, collections.Sequence) and
not isinstance(v, six.string_types)):
not isinstance(v, str)):
old_v = old.get(k)
old[k] = merge_list(old_v, v) if old_v else v
elif isinstance(v, six.string_types):
elif isinstance(v, str):
old[k] = ''.join([old.get(k, ''), v])
else:
old[k] = v
@ -76,14 +75,14 @@ def merge_map(old, new, deep_merge=False):
def parse_param(p_val, p_schema):
try:
if p_schema.type == p_schema.MAP:
if not isinstance(p_val, six.string_types):
if not isinstance(p_val, str):
p_val = jsonutils.dumps(p_val)
if p_val:
return jsonutils.loads(p_val)
elif not isinstance(p_val, collections.Sequence):
raise ValueError()
except (ValueError, TypeError) as err:
msg = _("Invalid parameter in environment %s.") % six.text_type(err)
msg = _("Invalid parameter in environment %s.") % str(err)
raise ValueError(msg)
return p_val