Use unicode formatting

The openstack clients return unicode strings as ressource names.
In python2.7, we need to use unicode encoding when dealing with string
formatting otherwise it is treated as python2 str and we try to cast
a unicode string into an str.
In python 3 prefixing a string with u has no effect and is supported
and considered as a python3 str which are natively unicode strings.

Change-Id: Id77ed2f28fd206b900a0e273fece244517e55505
Closes-Bug: #1645761
This commit is contained in:
zarrouk 2016-11-29 16:40:03 +01:00
parent d18f6617b4
commit 712bb2bce5
2 changed files with 30 additions and 30 deletions

View File

@ -48,7 +48,7 @@ def retry(service_name):
# If this happens, We raise a Warning. # If this happens, We raise a Warning.
logging.warning( logging.warning(
"Can not delete the resource because it does not" "Can not delete the resource because it does not"
" exist : %s" % e " exist : %s", e
) )
# No need to retry deleting an non existing resource # No need to retry deleting an non existing resource
break break
@ -57,8 +57,8 @@ def retry(service_name):
raise exceptions.DeletionFailed(service_name) raise exceptions.DeletionFailed(service_name)
n += 1 n += 1
logging.info("* Deletion failed - " logging.info("* Deletion failed - "
"Retrying in {} seconds - " "Retrying in %s seconds - "
"Retry count {}".format(constants.TIMEOUT, n)) "Retry count %s", constants.TIMEOUT, n)
time.sleep(constants.TIMEOUT) time.sleep(constants.TIMEOUT)
return wrapper return wrapper
return factory return factory
@ -146,14 +146,14 @@ class Resources(object):
def delete(self, resource): def delete(self, resource):
"""Displays informational message about a resource deletion.""" """Displays informational message about a resource deletion."""
logging.info("* Deleting {}.".format(self.resource_str(resource))) logging.info("* Deleting %s.", self.resource_str(resource))
def purge(self): def purge(self):
"""Delete all resources.""" """Delete all resources."""
# Purging is displayed and done only if self.list succeeds # Purging is displayed and done only if self.list succeeds
resources = self.list() resources = self.list()
c_name = self.__class__.__name__ c_name = self.__class__.__name__
logging.info("* Purging {}".format(c_name)) logging.info("* Purging %s", c_name)
for resource in resources: for resource in resources:
retry(c_name)(self.delete)(resource) retry(c_name)(self.delete)(resource)
@ -162,7 +162,7 @@ class Resources(object):
# Resources type and resources are displayed only if self.list succeeds # Resources type and resources are displayed only if self.list succeeds
resources = self.list() resources = self.list()
c_name = self.__class__.__name__ c_name = self.__class__.__name__
print("* Resources type: {}".format(c_name)) print("* Resources type: %s" % c_name)
for resource in resources: for resource in resources:
print(self.resource_str(resource)) print(self.resource_str(resource))
print("") print("")

View File

@ -90,7 +90,7 @@ class SwiftObjects(SwiftResources):
container=obj['container'], name=obj['name']) container=obj['container'], name=obj['name'])
def resource_str(self, obj): def resource_str(self, obj):
return "object {} in container {}".format(obj['name'], obj['container']) return u"object {} in container {}".format(obj['name'], obj['container'])
class SwiftContainers(SwiftResources): class SwiftContainers(SwiftResources):
@ -104,7 +104,7 @@ class SwiftContainers(SwiftResources):
swift_client.delete_container(self.endpoint, self.token, container, http_conn=self.http_conn) swift_client.delete_container(self.endpoint, self.token, container, http_conn=self.http_conn)
def resource_str(self, obj): def resource_str(self, obj):
return "container {}".format(obj) return u"container {}".format(obj)
class CinderResources(base.Resources): class CinderResources(base.Resources):
@ -125,7 +125,7 @@ class CinderSnapshots(CinderResources):
self.client.volume_snapshots.delete(snap) self.client.volume_snapshots.delete(snap)
def resource_str(self, snap): def resource_str(self, snap):
return "snapshot {} (id {})".format(snap.display_name, snap.id) return u"snapshot {} (id {})".format(snap.display_name, snap.id)
class CinderVolumes(CinderResources): class CinderVolumes(CinderResources):
@ -139,7 +139,7 @@ class CinderVolumes(CinderResources):
self.client.volumes.delete(vol) self.client.volumes.delete(vol)
def resource_str(self, vol): def resource_str(self, vol):
return "volume {} (id {})".format(vol.display_name, vol.id) return u"volume {} (id {})".format(vol.display_name, vol.id)
class CinderBackups(CinderResources): class CinderBackups(CinderResources):
@ -159,7 +159,7 @@ class CinderBackups(CinderResources):
self.client.backups.delete(backup) self.client.backups.delete(backup)
def resource_str(self, backup): def resource_str(self, backup):
return "backup {} (id {}) of volume {}".format(backup.name, backup.id, backup.volume_id) return u"backup {} (id {}) of volume {}".format(backup.name, backup.id, backup.volume_id)
class NeutronResources(base.Resources): class NeutronResources(base.Resources):
@ -198,7 +198,7 @@ class NeutronRouters(NeutronResources):
@staticmethod @staticmethod
def resource_str(router): def resource_str(router):
return "router {} (id {})".format(router['name'], router['id']) return u"router {} (id {})".format(router['name'], router['id'])
class NeutronInterfaces(NeutronResources): class NeutronInterfaces(NeutronResources):
@ -220,8 +220,8 @@ class NeutronInterfaces(NeutronResources):
@staticmethod @staticmethod
def resource_str(interface): def resource_str(interface):
return "interface {} (id {})".format(interface['name'], return u"interface {} (id {})".format(interface['name'],
interface['id']) interface['id'])
class NeutronPorts(NeutronResources): class NeutronPorts(NeutronResources):
@ -244,7 +244,7 @@ class NeutronPorts(NeutronResources):
@staticmethod @staticmethod
def resource_str(port): def resource_str(port):
return "port {} (id {})".format(port['name'], port['id']) return u"port {} (id {})".format(port['name'], port['id'])
class NeutronNetworks(NeutronResources): class NeutronNetworks(NeutronResources):
@ -265,7 +265,7 @@ class NeutronNetworks(NeutronResources):
@staticmethod @staticmethod
def resource_str(net): def resource_str(net):
return "network {} (id {})".format(net['name'], net['id']) return u"network {} (id {})".format(net['name'], net['id'])
class NeutronSecgroups(NeutronResources): class NeutronSecgroups(NeutronResources):
@ -293,7 +293,7 @@ class NeutronSecgroups(NeutronResources):
@staticmethod @staticmethod
def resource_str(secgroup): def resource_str(secgroup):
return "security group {} (id {})".format( return u"security group {} (id {})".format(
secgroup['name'], secgroup['id']) secgroup['name'], secgroup['id'])
@ -310,7 +310,7 @@ class NeutronFloatingIps(NeutronResources):
@staticmethod @staticmethod
def resource_str(floating_ip): def resource_str(floating_ip):
return "floating ip {} (id {})".format( return u"floating ip {} (id {})".format(
floating_ip['floating_ip_address'], floating_ip['id']) floating_ip['floating_ip_address'], floating_ip['id'])
@ -326,7 +326,7 @@ class NeutronLbMembers(NeutronResources):
@staticmethod @staticmethod
def resource_str(member): def resource_str(member):
return "lb-member {} (id {})".format(member['address'], member['id']) return u"lb-member {} (id {})".format(member['address'], member['id'])
class NeutronLbPool(NeutronResources): class NeutronLbPool(NeutronResources):
@ -341,7 +341,7 @@ class NeutronLbPool(NeutronResources):
@staticmethod @staticmethod
def resource_str(pool): def resource_str(pool):
return "lb-pool {} (id {})".format(pool['name'], pool['id']) return u"lb-pool {} (id {})".format(pool['name'], pool['id'])
class NeutronLbVip(NeutronResources): class NeutronLbVip(NeutronResources):
@ -356,7 +356,7 @@ class NeutronLbVip(NeutronResources):
@staticmethod @staticmethod
def resource_str(vip): def resource_str(vip):
return "lb-vip {} (id {})".format(vip['name'], vip['id']) return u"lb-vip {} (id {})".format(vip['name'], vip['id'])
class NeutronLbHealthMonitor(NeutronResources): class NeutronLbHealthMonitor(NeutronResources):
@ -371,7 +371,7 @@ class NeutronLbHealthMonitor(NeutronResources):
@staticmethod @staticmethod
def resource_str(health_monitor): def resource_str(health_monitor):
return "lb-health_monotor type {} (id {})".format( return u"lb-health_monotor type {} (id {})".format(
health_monitor['type'], health_monitor['id']) health_monitor['type'], health_monitor['id'])
@ -387,7 +387,7 @@ class NeutronMeteringLabel(NeutronResources):
@staticmethod @staticmethod
def resource_str(metering_label): def resource_str(metering_label):
return "meter-label {} (id {})".format( return u"meter-label {} (id {})".format(
metering_label['name'], metering_label['id']) metering_label['name'], metering_label['id'])
@ -403,7 +403,7 @@ class NeutronFireWallPolicy(NeutronResources):
@staticmethod @staticmethod
def resource_str(firewall_policy): def resource_str(firewall_policy):
return "Firewall policy {} (id {})".format( return u"Firewall policy {} (id {})".format(
firewall_policy['name'], firewall_policy['id']) firewall_policy['name'], firewall_policy['id'])
@ -419,7 +419,7 @@ class NeutronFireWallRule(NeutronResources):
@staticmethod @staticmethod
def resource_str(firewall_rule): def resource_str(firewall_rule):
return "Firewall rule {} (id {})".format( return u"Firewall rule {} (id {})".format(
firewall_rule['name'], firewall_rule['id']) firewall_rule['name'], firewall_rule['id'])
@ -435,7 +435,7 @@ class NeutronFireWall(NeutronResources):
@staticmethod @staticmethod
def resource_str(firewall): def resource_str(firewall):
return "Firewall {} (id {})".format(firewall['name'], firewall['id']) return u"Firewall {} (id {})".format(firewall['name'], firewall['id'])
class NovaServers(base.Resources): class NovaServers(base.Resources):
@ -456,7 +456,7 @@ class NovaServers(base.Resources):
self.client.servers.delete(server) self.client.servers.delete(server)
def resource_str(self, server): def resource_str(self, server):
return "server {} (id {})".format(server.name, server.id) return u"server {} (id {})".format(server.name, server.id)
class GlanceImages(base.Resources): class GlanceImages(base.Resources):
@ -476,7 +476,7 @@ class GlanceImages(base.Resources):
self.client.images.delete(image.id) self.client.images.delete(image.id)
def resource_str(self, image): def resource_str(self, image):
return "image {} (id {})".format(image.name, image.id) return u"image {} (id {})".format(image.name, image.id)
def _owned_resource(self, res): def _owned_resource(self, res):
# Only considering resources owned by project # Only considering resources owned by project
@ -503,7 +503,7 @@ class HeatStacks(base.Resources):
self.client.stacks.delete(stack.id) self.client.stacks.delete(stack.id)
def resource_str(self, stack): def resource_str(self, stack):
return "stack {})".format(stack.id) return u"stack {})".format(stack.id)
class CeilometerAlarms(base.Resources): class CeilometerAlarms(base.Resources):
@ -530,7 +530,7 @@ class CeilometerAlarms(base.Resources):
self.client.alarms.delete(alarm.alarm_id) self.client.alarms.delete(alarm.alarm_id)
def resource_str(self, alarm): def resource_str(self, alarm):
return "alarm {}".format(alarm.name) return u"alarm {}".format(alarm.name)
class KeystoneManager(object): class KeystoneManager(object):