python3: Fix python3 compatibility issues

- Replace 'range' with six.moves.range.
- Replace 'zip' with six.moves.zip.
- Replace 'map' with six.moves.map.
- Replace dict.keys() with list(dict.keys()) to get a list on Python
  3. On Python 3, dict.keys() now returns a view.
- Fix 'TypeError: unorderable types: NoneType() < int()' for python3
- Python 3 changed the iso8601 module iso8601.iso8601.Utc()
  function to iso8601.UTC.  This is compatible with Python2.7.

Testing:

1. Built new fm-rest-api rpm package.
2. Built new ISO with newer fm-rest-api and installed fine.
3. Checked for an exception in the logs while running "fm event-list".
4. Ran "fm alarm-list" without a problem.

Story: 2006729
Task: 42256

Signed-off-by: Charles Short <charles.short@windriver.com>
Change-Id: I2d0f4c2c85ea8057258d56632a102b2eac7db388
This commit is contained in:
Charles Short 2021-04-09 08:51:29 -04:00
parent b12f3745b1
commit b91aaaab90
9 changed files with 15 additions and 12 deletions

View File

@ -99,7 +99,7 @@ class Alarm(base.APIBase):
"A list containing a self link and associated alarm links"
def __init__(self, **kwargs):
self.fields = objects.alarm.fields.keys()
self.fields = list(objects.alarm.fields.keys())
for k in self.fields:
setattr(self, k, kwargs.get(k))
@ -284,7 +284,7 @@ class AlarmController(rest.RestController):
:param alarm_dict: Dictionary obtained from an alarm object.
"""
event_log_dict = {}
for key in alarm_dict.keys():
for key in list(alarm_dict.keys()):
if key == 'alarm_id':
event_log_dict['event_log_id'] = alarm_dict[key]
elif key == 'alarm_state':

View File

@ -62,7 +62,7 @@ class APIBase(wtypes.Base):
# Unset non-required fields so they do not appear
# in the message body
obj_dict.update(dict((k, wsme.Unset)
for k in obj_dict.keys()
for k in list(obj_dict.keys())
if fields and k not in fields))
return cls(**obj_dict)

View File

@ -93,7 +93,7 @@ class EventLog(base.APIBase):
def __init__(self, **kwargs):
self.fields = objects.event_log.fields.keys()
self.fields = list(objects.event_log.fields.keys())
for k in self.fields:
setattr(self, k, kwargs.get(k))

View File

@ -60,7 +60,7 @@ class EventSuppression(base.APIBase):
"A list containing a self link and associated links"
def __init__(self, **kwargs):
self.fields = objects.event_suppression.fields.keys()
self.fields = list(objects.event_suppression.fields.keys())
for k in self.fields:
if not hasattr(self, k):
continue

View File

@ -29,6 +29,7 @@ import wsme
from wsme import types as wtypes
from oslo_utils import strutils
from oslo_utils import uuidutils
from six.moves import map
from fm.common.i18n import _
from fm.common import exceptions

View File

@ -49,14 +49,17 @@ def save_and_reraise_exception():
LOG.error(_('Original exception being dropped: %s'),
traceback.format_exception(type_, value, tb))
raise
raise (type_, value, tb)
raise type_
def validate_limit(limit):
if limit and limit < 0:
raise wsme.exc.ClientSideError(_("Limit must be positive"))
return min(CONF.api.limit_max, limit) or CONF.api.limit_max
if limit:
return min(CONF.api.limit_max, limit) or CONF.api.limit_max
else:
return CONF.api_limit_max
def validate_sort_dir(sort_dir):

View File

@ -57,8 +57,7 @@ class AuthTokenMiddleware(auth_token.AuthProtocol):
# The information whether the API call is being performed against the
# public API is required for some other components. Saving it to the
# WSGI environment is reasonable thereby.
env['is_public_api'] = any(map(lambda pattern: re.match(pattern, path),
self.public_api_routes))
env['is_public_api'] = any([re.match(pattern, path) for pattern in self.public_api_routes])
if env['is_public_api']:
return self._app(env, start_response)

View File

@ -38,7 +38,7 @@ def datetime_or_none(dt):
# NOTE(danms): Legacy objects from sqlalchemy are stored in UTC,
# but are returned without a timezone attached.
# As a transitional aid, assume a tz-naive object is in UTC.
return dt.replace(tzinfo=iso8601.iso8601.Utc())
return dt.replace(tzinfo=iso8601.UTC)
else:
return dt
raise ValueError('A datetime.datetime is required here')

View File

@ -25,7 +25,7 @@ class TestRoot(base.FunctionalTest):
data = self.get_json('/', path_prefix='')
self.assertEqual(data['default_version']['id'], 'v1')
# Check fields are not empty
[self.assertNotIn(f, ['', []]) for f in data.keys()]
[self.assertNotIn(f, ['', []]) for f in list(data.keys())]
class TestV1Root(base.FunctionalTest):
@ -34,7 +34,7 @@ class TestV1Root(base.FunctionalTest):
data = self.get_json('/')
self.assertEqual(data['id'], 'v1')
# Check fields are not empty
[self.assertNotIn(f, ['', []]) for f in data.keys()]
[self.assertNotIn(f, ['', []]) for f in list(data.keys())]
# Check if the resources are present
self.assertIn({'type': 'application/vnd.openstack.fm.v1+json',
'base': 'application/json'}, data['media_types'])