Fix unicode issue for Python 2/3 compatible code.

replace unicode with six.text_type

Story: 2003433
Task: 27695

Change-Id: Ie7bc1b649b94fb1695eac51b65294d30e01f26f2
Signed-off-by: Sun Austin <austin.sun@intel.com>
This commit is contained in:
Sun Austin 2018-11-21 17:30:25 +08:00
parent 30b5b97208
commit f999509bca
15 changed files with 50 additions and 42 deletions

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import six
import sys import sys
@ -207,7 +208,7 @@ class CgtsclientException(Exception):
if self.__class__.__name__.endswith('_Remote'): if self.__class__.__name__.endswith('_Remote'):
return self.args[0] return self.args[0]
else: else:
return unicode(self) return six.text_type(self)
class AmbiguousEndpoints(CgtsclientException): class AmbiguousEndpoints(CgtsclientException):

View File

@ -140,7 +140,7 @@ class HostProvisionState(state.State):
class HostProvisionStateController(rest.RestController): class HostProvisionStateController(rest.RestController):
# GET ihosts/<uuid>/state/provision # GET ihosts/<uuid>/state/provision
@wsme_pecan.wsexpose(HostProvisionState, unicode) @wsme_pecan.wsexpose(HostProvisionState, six.text_type)
def get(self, ihost_id): def get(self, ihost_id):
ihost = objects.host.get_by_uuid(pecan.request.context, ihost = objects.host.get_by_uuid(pecan.request.context,
ihost_id) ihost_id)
@ -149,7 +149,7 @@ class HostProvisionStateController(rest.RestController):
return provision_state return provision_state
# PUT ihosts/<uuid>/state/provision # PUT ihosts/<uuid>/state/provision
@wsme_pecan.wsexpose(HostProvisionState, unicode, unicode, status=202) @wsme_pecan.wsexpose(HostProvisionState, six.text_type, six.text_type, status=202)
def put(self, ihost_id, target): def put(self, ihost_id, target):
"""Set the provision state of the machine.""" """Set the provision state of the machine."""
# TODO(lucasagomes): Test if target is a valid state and if it's able # TODO(lucasagomes): Test if target is a valid state and if it's able
@ -188,7 +188,7 @@ class HostStatesController(rest.RestController):
} }
# GET ihosts/<uuid>/state # GET ihosts/<uuid>/state
@wsme_pecan.wsexpose(HostStates, unicode) @wsme_pecan.wsexpose(HostStates, six.text_type)
def get(self, ihost_id): def get(self, ihost_id):
"""List or update the state of a ihost.""" """List or update the state of a ihost."""
ihost = objects.host.get_by_uuid(pecan.request.context, ihost = objects.host.get_by_uuid(pecan.request.context,
@ -207,7 +207,7 @@ class HostStatesController(rest.RestController):
# PUT ihosts/<uuid>/state/update_install_uuid # PUT ihosts/<uuid>/state/update_install_uuid
@cutils.synchronized(LOCK_NAME_STATE) @cutils.synchronized(LOCK_NAME_STATE)
@wsme_pecan.wsexpose(HostStates, types.uuid, body=unicode) @wsme_pecan.wsexpose(HostStates, types.uuid, body=six.text_type)
def update_install_uuid(self, host_uuid, install_uuid): def update_install_uuid(self, host_uuid, install_uuid):
""" Update install_uuid in /etc/platform/platform.conf """ Update install_uuid in /etc/platform/platform.conf
on the specified host. on the specified host.
@ -223,7 +223,7 @@ class HostStatesController(rest.RestController):
# PUT ihosts/<uuid>/state/host_cpus_modify # PUT ihosts/<uuid>/state/host_cpus_modify
@cutils.synchronized(cpu_api.LOCK_NAME) @cutils.synchronized(cpu_api.LOCK_NAME)
@wsme_pecan.wsexpose(cpu_api.CPUCollection, types.uuid, body=[unicode]) @wsme_pecan.wsexpose(cpu_api.CPUCollection, types.uuid, body=[six.text_type])
def host_cpus_modify(self, host_uuid, capabilities): def host_cpus_modify(self, host_uuid, capabilities):
""" Perform bulk host cpus modify. """ Perform bulk host cpus modify.
:param host_uuid: UUID of the host :param host_uuid: UUID of the host
@ -1170,8 +1170,8 @@ class HostController(rest.RestController):
activity = 'Controller-Standby' activity = 'Controller-Standby'
host['capabilities'].update({'Personality': activity}) host['capabilities'].update({'Personality': activity})
@wsme_pecan.wsexpose(HostCollection, unicode, unicode, int, unicode, @wsme_pecan.wsexpose(HostCollection, six.text_type, six.text_type, int, six.text_type,
unicode, unicode) six.text_type, six.text_type)
def get_all(self, isystem_id=None, marker=None, limit=None, def get_all(self, isystem_id=None, marker=None, limit=None,
personality=None, personality=None,
sort_key='id', sort_dir='asc'): sort_key='id', sort_dir='asc'):
@ -1182,7 +1182,7 @@ class HostController(rest.RestController):
sort_key=sort_key, sort_key=sort_key,
sort_dir=sort_dir) sort_dir=sort_dir)
@wsme_pecan.wsexpose(unicode, unicode, body=unicode) @wsme_pecan.wsexpose(six.text_type, six.text_type, body=six.text_type)
def install_progress(self, uuid, install_state, def install_progress(self, uuid, install_state,
install_state_info=None): install_state_info=None):
""" Update the install status for the given host.""" """ Update the install status for the given host."""
@ -1200,8 +1200,8 @@ class HostController(rest.RestController):
'install_state_info': 'install_state_info':
install_state_info}) install_state_info})
@wsme_pecan.wsexpose(HostCollection, unicode, unicode, int, unicode, @wsme_pecan.wsexpose(HostCollection, six.text_type, six.text_type, int, six.text_type,
unicode, unicode) six.text_type, six.text_type)
def detail(self, isystem_id=None, marker=None, limit=None, def detail(self, isystem_id=None, marker=None, limit=None,
personality=None, personality=None,
sort_key='id', sort_dir='asc'): sort_key='id', sort_dir='asc'):
@ -1220,7 +1220,7 @@ class HostController(rest.RestController):
sort_key=sort_key, sort_key=sort_key,
sort_dir=sort_dir) sort_dir=sort_dir)
@wsme_pecan.wsexpose(Host, unicode) @wsme_pecan.wsexpose(Host, six.text_type)
def get_one(self, uuid): def get_one(self, uuid):
"""Retrieve information about the given ihost.""" """Retrieve information about the given ihost."""
if self._from_isystem: if self._from_isystem:
@ -1708,7 +1708,7 @@ class HostController(rest.RestController):
return self._do_post(ihost_dict) return self._do_post(ihost_dict)
@wsme_pecan.wsexpose(Host, unicode, body=[unicode]) @wsme_pecan.wsexpose(Host, six.text_type, body=[six.text_type])
def patch(self, uuid, patch): def patch(self, uuid, patch):
""" Update an existing ihost. """ Update an existing ihost.
""" """
@ -2230,7 +2230,7 @@ class HostController(rest.RestController):
return True return True
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(None, unicode, status_code=204) @wsme_pecan.wsexpose(None, six.text_type, status_code=204)
def delete(self, ihost_id): def delete(self, ihost_id):
"""Delete an ihost. """Delete an ihost.
""" """
@ -2487,7 +2487,7 @@ class HostController(rest.RestController):
target_load) target_load)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(Host, unicode, body=unicode) @wsme_pecan.wsexpose(Host, six.text_type, body=six.text_type)
def upgrade(self, uuid, body): def upgrade(self, uuid, body):
"""Upgrade the host to the specified load""" """Upgrade the host to the specified load"""
@ -2571,7 +2571,7 @@ class HostController(rest.RestController):
return Host.convert_with_links(rpc_ihost) return Host.convert_with_links(rpc_ihost)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(Host, unicode, body=unicode) @wsme_pecan.wsexpose(Host, six.text_type, body=six.text_type)
def downgrade(self, uuid, body): def downgrade(self, uuid, body):
"""Downgrade the host to the specified load""" """Downgrade the host to the specified load"""

View File

@ -827,7 +827,7 @@ def _check_interface_vlan_id(op, interface, ihost, from_profile=False):
if interface['vlan_id'] < 1 or interface['vlan_id'] > 4094: if interface['vlan_id'] < 1 or interface['vlan_id'] > 4094:
raise wsme.exc.ClientSideError(_("VLAN id must be between 1 and 4094.")) raise wsme.exc.ClientSideError(_("VLAN id must be between 1 and 4094."))
else: else:
interface['vlan_id'] = unicode(interface['vlan_id']) interface['vlan_id'] = six.text_type(interface['vlan_id'])
return interface return interface

View File

@ -23,6 +23,7 @@ import jsonpatch
import socket import socket
import pecan import pecan
import six
from pecan import rest from pecan import rest
import wsme import wsme
@ -192,7 +193,7 @@ class LoadController(rest.RestController):
return self._get_loads_collection(marker, limit, sort_key, sort_dir, return self._get_loads_collection(marker, limit, sort_key, sort_dir,
expand, resource_url) expand, resource_url)
@wsme_pecan.wsexpose(Load, unicode) @wsme_pecan.wsexpose(Load, six.text_type)
def get_one(self, load_uuid): def get_one(self, load_uuid):
"""Retrieve information about the given Load.""" """Retrieve information about the given Load."""
@ -285,8 +286,8 @@ class LoadController(rest.RestController):
return Load.convert_with_links(new_load) return Load.convert_with_links(new_load)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme.validate(unicode, [LoadPatchType]) @wsme.validate(six.text_type, [LoadPatchType])
@wsme_pecan.wsexpose(Load, unicode, @wsme_pecan.wsexpose(Load, six.text_type,
body=[LoadPatchType]) body=[LoadPatchType])
def patch(self, load_id, patch): def patch(self, load_id, patch):
"""Update an existing load.""" """Update an existing load."""
@ -317,7 +318,7 @@ class LoadController(rest.RestController):
return Load.convert_with_links(rpc_load) return Load.convert_with_links(rpc_load)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(None, unicode, status_code=204) @wsme_pecan.wsexpose(None, six.text_type, status_code=204)
def delete(self, load_id): def delete(self, load_id):
"""Delete a load.""" """Delete a load."""

View File

@ -25,6 +25,7 @@ import jsonpatch
import pecan import pecan
from pecan import rest from pecan import rest
import six
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan import wsmeext.pecan as wsme_pecan
@ -259,7 +260,7 @@ class InfraNetworkController(rest.RestController):
raise wsme.exc.ClientSideError(_( raise wsme.exc.ClientSideError(_(
"VLAN id must be between 1 and 4094.")) "VLAN id must be between 1 and 4094."))
else: else:
infra['infra_vlan_id'] = unicode(infra['infra_vlan_id']) infra['infra_vlan_id'] = six.text_type(infra['infra_vlan_id'])
return infra return infra
@staticmethod @staticmethod

View File

@ -809,8 +809,8 @@ class ProfileController(rest.RestController):
return stor_profiles return stor_profiles
@wsme_pecan.wsexpose(ProfileCollection, unicode, unicode, int, @wsme_pecan.wsexpose(ProfileCollection, six.text_type, six.text_type, int,
unicode, unicode) six.text_type, six.text_type)
def get_all(self, chassis_id=None, marker=None, limit=None, def get_all(self, chassis_id=None, marker=None, limit=None,
sort_key='id', sort_dir='asc'): sort_key='id', sort_dir='asc'):
"""Retrieve a list of ihosts.""" """Retrieve a list of ihosts."""
@ -820,8 +820,8 @@ class ProfileController(rest.RestController):
sort_key=sort_key, sort_key=sort_key,
sort_dir=sort_dir) sort_dir=sort_dir)
@wsme_pecan.wsexpose(ProfileCollection, unicode, unicode, int, @wsme_pecan.wsexpose(ProfileCollection, six.text_type, six.text_type, int,
unicode, unicode) six.text_type, six.text_type)
def detail(self, chassis_id=None, marker=None, limit=None, def detail(self, chassis_id=None, marker=None, limit=None,
sort_key='id', sort_dir='asc'): sort_key='id', sort_dir='asc'):
"""Retrieve a list of ihosts with detail.""" """Retrieve a list of ihosts with detail."""
@ -839,7 +839,7 @@ class ProfileController(rest.RestController):
sort_key=sort_key, sort_key=sort_key,
sort_dir=sort_dir) sort_dir=sort_dir)
@wsme_pecan.wsexpose(Profile, unicode) @wsme_pecan.wsexpose(Profile, six.text_type)
def get_one(self, uuid): def get_one(self, uuid):
"""Retrieve information about the given ihost.""" """Retrieve information about the given ihost."""
if self._from_chassis: if self._from_chassis:
@ -944,7 +944,7 @@ class ProfileController(rest.RestController):
return iprofile.convert_with_links(new_ihost) return iprofile.convert_with_links(new_ihost)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(Profile, unicode, body=[unicode]) @wsme_pecan.wsexpose(Profile, six.text_type, body=[six.text_type])
def patch(self, uuid, patch): def patch(self, uuid, patch):
"""Update an existing iprofile. """Update an existing iprofile.
""" """
@ -999,7 +999,7 @@ class ProfileController(rest.RestController):
raise wsme.exc.ClientSideError(msg) raise wsme.exc.ClientSideError(msg)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(None, unicode, status_code=204) @wsme_pecan.wsexpose(None, six.text_type, status_code=204)
def delete(self, ihost_id): def delete(self, ihost_id):
"""Delete an ihost profile. """Delete an ihost profile.
""" """
@ -2694,9 +2694,9 @@ def ifprofile_apply_to_host(host, profile):
uses_list = [] uses_list = []
usedby_list = [] usedby_list = []
for u in iinterfaces: for u in iinterfaces:
if unicode(u.ifname) in i.uses or u.uuid in i.uses: if six.text_type(u.ifname) in i.uses or u.uuid in i.uses:
uses_list.append(u.uuid) uses_list.append(u.uuid)
if unicode(u.ifname) in i.used_by or u.uuid in i.used_by: if six.text_type(u.ifname) in i.used_by or u.uuid in i.used_by:
usedby_list.append(u.uuid) usedby_list.append(u.uuid)
idict['uses'] = uses_list idict['uses'] = uses_list

View File

@ -414,7 +414,7 @@ class SensorGroupController(rest.RestController):
raise wsme.exc.ClientSideError("_get_host_uuid lookup failed") raise wsme.exc.ClientSideError("_get_host_uuid lookup failed")
return host.uuid return host.uuid
@wsme_pecan.wsexpose('json', body=unicode) @wsme_pecan.wsexpose('json', body=six.text_type)
def relearn(self, body): def relearn(self, body):
""" Handle Sensor Model Relearn Request.""" """ Handle Sensor Model Relearn Request."""
host_uuid = self._get_host_uuid(body) host_uuid = self._get_host_uuid(body)

View File

@ -150,7 +150,7 @@ LOCK_NAME = 'SMServiceController'
class SMServiceController(rest.RestController): class SMServiceController(rest.RestController):
@wsme_pecan.wsexpose(SMService, unicode) @wsme_pecan.wsexpose(SMService, six.text_type)
def get_one(self, uuid): def get_one(self, uuid):
sm_service = sm_api.service_show(uuid) sm_service = sm_api.service_show(uuid)
if sm_service is None: if sm_service is None:
@ -172,7 +172,7 @@ class SMServiceController(rest.RestController):
"Bad response from SM API")) "Bad response from SM API"))
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(Service, wtypes.text, body=[unicode]) @wsme_pecan.wsexpose(Service, wtypes.text, body=[six.text_type])
def patch(self, service_name, patch): def patch(self, service_name, patch):
"""Update the service configuration.""" """Update the service configuration."""

View File

@ -11,6 +11,7 @@ import copy
import netaddr import netaddr
import pecan import pecan
from pecan import rest from pecan import rest
import six
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan import wsmeext.pecan as wsme_pecan
@ -996,7 +997,7 @@ class ServiceParameterController(rest.RestController):
return service return service
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose('json', body=unicode) @wsme_pecan.wsexpose('json', body=six.text_type)
def apply(self, body): def apply(self, body):
""" Apply the service parameters.""" """ Apply the service parameters."""
service = self._get_service(body) service = self._get_service(body)

View File

@ -7,6 +7,7 @@
# this file is used for service group requests. Keeping naming consistent with sm client # this file is used for service group requests. Keeping naming consistent with sm client
from pecan import rest from pecan import rest
import six
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan import wsmeext.pecan as wsme_pecan
@ -55,7 +56,7 @@ class SMServiceGroupCollection(base.APIBase):
class SMServiceGroupController(rest.RestController): class SMServiceGroupController(rest.RestController):
@wsme_pecan.wsexpose(SMServiceGroup, unicode) @wsme_pecan.wsexpose(SMServiceGroup, six.text_type)
def get_one(self, uuid): def get_one(self, uuid):
sm_servicegroup = sm_api.sm_servicegroup_show(uuid) sm_servicegroup = sm_api.sm_servicegroup_show(uuid)
if sm_servicegroup is None: if sm_servicegroup is None:

View File

@ -5,6 +5,7 @@
# #
from pecan import rest from pecan import rest
import six
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan import wsmeext.pecan as wsme_pecan
@ -51,7 +52,7 @@ class SMServiceNodeCollection(base.APIBase):
class SMServiceNodeController(rest.RestController): class SMServiceNodeController(rest.RestController):
@wsme_pecan.wsexpose(SMServiceNode, unicode) @wsme_pecan.wsexpose(SMServiceNode, six.text_type)
def get_one(self, uuid): def get_one(self, uuid):
sm_servicenode = sm_api.servicenode_show(uuid) sm_servicenode = sm_api.servicenode_show(uuid)
if sm_servicenode is None: if sm_servicenode is None:

View File

@ -358,7 +358,7 @@ class SystemController(rest.RestController):
raise exception.OperationNotPermitted raise exception.OperationNotPermitted
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(System, types.uuid, body=[unicode]) @wsme_pecan.wsexpose(System, types.uuid, body=[six.text_type])
def patch(self, isystem_uuid, patch): def patch(self, isystem_uuid, patch):
"""Update an existing isystem. """Update an existing isystem.

View File

@ -10,6 +10,7 @@
import pecan import pecan
from pecan import rest, expose from pecan import rest, expose
import os import os
import six
import socket import socket
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
@ -185,7 +186,7 @@ class UpgradeController(rest.RestController):
return Upgrade.convert_with_links(rpc_upgrade) return Upgrade.convert_with_links(rpc_upgrade)
@cutils.synchronized(LOCK_NAME) @cutils.synchronized(LOCK_NAME)
@wsme_pecan.wsexpose(Upgrade, body=unicode) @wsme_pecan.wsexpose(Upgrade, body=six.text_type)
def post(self, body): def post(self, body):
"""Create a new Software Upgrade instance and start upgrade.""" """Create a new Software Upgrade instance and start upgrade."""
@ -420,7 +421,7 @@ class UpgradeController(rest.RestController):
return Upgrade.convert_with_links(upgrade) return Upgrade.convert_with_links(upgrade)
@wsme_pecan.wsexpose(wtypes.text, unicode) @wsme_pecan.wsexpose(wtypes.text, six.text_type)
def in_upgrade(self, uuid): def in_upgrade(self, uuid):
# uuid is added here for potential future use # uuid is added here for potential future use
try: try:

View File

@ -26,6 +26,7 @@ SHOULD include dedicated exception logging.
""" """
import functools import functools
import six
from oslo_config import cfg from oslo_config import cfg
@ -158,7 +159,7 @@ class SysinvException(Exception):
if self.__class__.__name__.endswith('_Remote'): if self.__class__.__name__.endswith('_Remote'):
return self.args[0] return self.args[0]
else: else:
return unicode(self) return six.text_type(self)
class NotAuthorized(SysinvException): class NotAuthorized(SysinvException):

View File

@ -536,7 +536,7 @@ def convert_to_list_dict(lst, label):
def sanitize_hostname(hostname): def sanitize_hostname(hostname):
"""Return a hostname which conforms to RFC-952 and RFC-1123 specs.""" """Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
if isinstance(hostname, unicode): if isinstance(hostname, six.string_types):
hostname = hostname.encode('latin-1', 'ignore') hostname = hostname.encode('latin-1', 'ignore')
hostname = re.sub('[ _]', '-', hostname) hostname = re.sub('[ _]', '-', hostname)