From 0718d3b868cf628e74966501ca1a00d3874f14f2 Mon Sep 17 00:00:00 2001 From: catborise Date: Thu, 21 May 2020 16:17:25 +0300 Subject: [PATCH] convert strings to python3 f-Strings --- vrtManager/instance.py | 88 ++++++++++++++++++++-------------------- vrtManager/interface.py | 26 ++++++------ vrtManager/network.py | 32 +++++++-------- vrtManager/secrets.py | 10 ++--- vrtManager/storage.py | 89 ++++++++++++++++++++--------------------- 5 files changed, 121 insertions(+), 124 deletions(-) diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 14f265d..25229d9 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -651,27 +651,27 @@ class wvmInstance(wvmConnect): additionals = '' if cache_mode is not None and cache_mode != 'default' and disk_device != 'cdrom': - additionals += "cache='%s' " % cache_mode + additionals += f"cache='{cache_mode}' " if io_mode is not None and io_mode != 'default': - additionals += "io='%s' " % io_mode + additionals += f"io='{io_mode}' " if discard_mode is not None and discard_mode != 'default': - additionals += "discard='%s' " % discard_mode + additionals += f"discard='{discard_mode}' " if detect_zeroes_mode is not None and detect_zeroes_mode != 'default': - additionals += "detect_zeroes='%s' " % detect_zeroes_mode + additionals += f"detect_zeroes='{detect_zeroes_mode}' " - xml_disk = "" % (disk_type, disk_device) + xml_disk = f"" if disk_device == 'cdrom': - xml_disk += "" % (driver_name, driver_type) + xml_disk += f"" elif disk_device == 'disk': - xml_disk += "" % (driver_name, driver_type, additionals) - xml_disk += """ - """ % (source, target_dev, target_bus) + xml_disk += f"" + xml_disk += f""" + """ if readonly or disk_device == 'cdrom': xml_disk += """""" if shareable: xml_disk += """""" if serial is not None and serial != 'None' and serial != '': - xml_disk += """%s""" % serial + xml_disk += f"""{serial}""" xml_disk += """""" if self.get_status() == 1: self.instance.attachDeviceFlags(xml_disk, VIR_DOMAIN_AFFECT_LIVE) @@ -703,28 +703,28 @@ class wvmInstance(wvmConnect): additionals = '' if cache_mode is not None and cache_mode != 'default': - additionals += "cache='%s' " % cache_mode + additionals += f"cache='{cache_mode}' " if io_mode is not None and io_mode != 'default': - additionals += "io='%s' " % io_mode + additionals += f"io='{io_mode}' " if discard_mode is not None and discard_mode != 'default': - additionals += "discard='%s' " % discard_mode + additionals += f"discard='{discard_mode}' " if detect_zeroes_mode is not None and detect_zeroes_mode != 'default': - additionals += "detect_zeroes='%s' " % detect_zeroes_mode + additionals += f"detect_zeroes='{detect_zeroes_mode}' " - xml_disk = "" % (old_disk_type, old_disk_device) + xml_disk = f"" if old_disk_device == 'cdrom': - xml_disk += "" % (old_driver_name, format) + xml_disk += f"" elif old_disk_device == 'disk': - xml_disk += "" % (old_driver_name, format, additionals) + xml_disk += f"" - xml_disk += """ - """ % (source, target_dev, target_bus) + xml_disk += f""" + """ if readonly: xml_disk += """""" if shareable: xml_disk += """""" if serial is not None and serial != 'None' and serial != '': - xml_disk += """%s""" % serial + xml_disk += f"""{serial}""" xml_disk += """""" self.instance.updateDeviceFlags(xml_disk, VIR_DOMAIN_AFFECT_CONFIG) @@ -753,7 +753,7 @@ class wvmInstance(wvmConnect): xml = """ """ xml += """""" for i in range(1, vcpus_hotplug): - xml += """""".format(i, i+1) + xml += f"""""" xml += """""" tree = etree.fromstring(self._XMLDesc(0)) @@ -914,7 +914,7 @@ class wvmInstance(wvmConnect): xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) root = ElementTree.fromstring(xml) try: - graphic = root.find("devices/graphics[@type='%s']" % current_type) + graphic = root.find(f"devices/graphics[@type='{current_type}']") except SyntaxError: # Little fix for old version ElementTree graphic = root.find("devices/graphics") @@ -942,7 +942,7 @@ class wvmInstance(wvmConnect): root = ElementTree.fromstring(xml) console_type = self.get_console_type() try: - graphic = root.find("devices/graphics[@type='%s']" % console_type) + graphic = root.find(f"devices/graphics[@type='{console_type}']") except SyntaxError: # Little fix for old version ElementTree graphic = root.find("devices/graphics") @@ -1161,7 +1161,7 @@ class wvmInstance(wvmConnect): stg_conn = self.get_wvmStorages() stg_conn.create_storage('dir', nvram_pool_name, None, nvram_dir) - new_nvram_name = "%s_VARS" % clone_data['name'] + new_nvram_name = f"{clone_data['name']}_VARS" nvram_stg = self.get_wvmStorage(nvram_pool_name) nvram_stg.clone_volume(src_nvram_name, new_nvram_name, file_suffix='fd') @@ -1195,16 +1195,16 @@ class wvmInstance(wvmConnect): if vol_format == 'qcow2' and meta_prealloc: meta_prealloc = True - vol_clone_xml = """ + vol_clone_xml = f""" - %s + {target_file} 0 0 - + - %s - %s + {OWNER['uid']} + {OWNER['guid']} 0644 @@ -1213,7 +1213,7 @@ class wvmInstance(wvmConnect): - """ % (target_file, vol_format, OWNER['uid'], OWNER['guid']) + """ stg = vol.storagePoolLookupByVolume() stg.createXMLFrom(vol_clone_xml, vol, meta_prealloc) @@ -1227,15 +1227,15 @@ class wvmInstance(wvmConnect): vol = self.get_volume_by_path(source_name) vol_format = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type") - vol_clone_xml = """ + vol_clone_xml = f""" - %s + {target_file} 0 0 - + - """ % (target_file, vol_format) + """ stg = vol.storagePoolLookupByVolume() stg.createXMLFrom(vol_clone_xml, vol, meta_prealloc) @@ -1275,18 +1275,16 @@ class wvmInstance(wvmConnect): else: interface_type = 'bridge' - xml_iface = """ - - """ % (interface_type, mac_address) + xml_iface = f""" + + """ if interface_type == 'network': - xml_iface += """""" % source + xml_iface += f"""""" else: - xml_iface += """""" % bridge_name - xml_iface += """""" % model + xml_iface += f"""""" + xml_iface += f"""""" if nwfilter: - xml_iface += """ - - """ % nwfilter + xml_iface += f"""""" xml_iface += """""" if self.get_status() == 1: @@ -1438,9 +1436,9 @@ class wvmInstance(wvmConnect): def set_qos(self, mac, direction, average, peak, burst): if direction == "inbound": - xml = "".format(average, peak, burst) + xml = f"" elif direction == "outbound": - xml = "".format(average, peak, burst) + xml = f"" else: raise Exception('Direction must be inbound or outbound') diff --git a/vrtManager/interface.py b/vrtManager/interface.py index f769b6c..149c600 100644 --- a/vrtManager/interface.py +++ b/vrtManager/interface.py @@ -19,32 +19,32 @@ class wvmInterfaces(wvmConnect): def create_iface(self, name, itype, mode, netdev, ipv4_type, ipv4_addr, ipv4_gw, ipv6_type, ipv6_addr, ipv6_gw, stp, delay): - xml = """ - """ % (itype, name, mode) + xml = f""" + """ if ipv4_type == 'dhcp': xml += """ """ if ipv4_type == 'static': address, prefix = ipv4_addr.split('/') - xml += """ - - - """ % (address, prefix, ipv4_gw) + xml += f""" + + + """ if ipv6_type == 'dhcp': xml += """ """ if ipv6_type == 'static': address, prefix = ipv6_addr.split('/') - xml += """ - - - """ % (address, prefix, ipv6_gw) + xml += f""" + + + """ if itype == 'bridge': - xml += """ - - """ % (stp, delay, netdev) + xml += f""" + + """ xml += """""" self.define_iface(xml) iface = self.get_iface(name) diff --git a/vrtManager/network.py b/vrtManager/network.py index f5b04b0..d42f864 100644 --- a/vrtManager/network.py +++ b/vrtManager/network.py @@ -45,37 +45,37 @@ class wvmNetworks(wvmConnect): ipv4, gateway, mask, dhcp4, ipv6, gateway6, prefix6, dhcp6, bridge, openvswitch, fixed=False): - xml = """ + xml = f""" - %s""" % name + {name}""" if forward in ['nat', 'route', 'bridge']: - xml += """""" % forward + xml += f"""""" xml += """""" if openvswitch is True: xml += """""" if forward != 'bridge': if ipv4: - xml += """""" % (gateway, mask) + xml += f"""""" if dhcp4: - xml += """ - """ % (dhcp4[0], dhcp4[1]) + xml += f""" + """ if fixed: fist_oct = int(dhcp4[0].strip().split('.')[3]) last_oct = int(dhcp4[1].strip().split('.')[3]) for ip in range(fist_oct, last_oct + 1): - xml += """""" % (util.randomMAC(), gateway[:-2], ip) + xml += f"""""" xml += """""" xml += """""" if ipv6: - xml += """""" % (gateway6, prefix6) + xml += f"""""" if dhcp6: - xml += """ - """ % (dhcp6[0], dhcp6[1]) + xml += f""" + """ xml += """""" xml += """""" xml += """""" @@ -314,9 +314,9 @@ class wvmNetwork(wvmConnect): def set_qos(self, direction, average, peak, burst): if direction == "inbound": - xml = "".format(average, peak, burst) + xml = f"" elif direction == "outbound": - xml = "".format(average, peak, burst) + xml = f"" else: raise Exception('Direction must be inbound or outbound') @@ -324,7 +324,7 @@ class wvmNetwork(wvmConnect): band = tree.xpath("/network/bandwidth") if len(band) == 0: - xml = "" + xml + "" + xml = f"{xml}" tree.append(etree.fromstring(xml)) else: direct = band[0].find(direction) @@ -339,7 +339,7 @@ class wvmNetwork(wvmConnect): def unset_qos(self, direction): tree = etree.fromstring(self._XMLDesc(0)) - for direct in tree.xpath("/network/bandwidth/{}".format(direction)): + for direct in tree.xpath(f"/network/bandwidth/{direction}"): parent = direct.getparent() parent.remove(direct) @@ -353,7 +353,7 @@ class wvmNetwork(wvmConnect): self.leases = self.net.DHCPLeases() except Exception as e: self.leases = [] - raise "Error getting %s DHCP leases: %s" % (self, e) + raise f"Error getting {self} DHCP leases: {e}" def get_dhcp_leases(self): if self.leases is None: diff --git a/vrtManager/secrets.py b/vrtManager/secrets.py index 1f92850..de7b3f4 100644 --- a/vrtManager/secrets.py +++ b/vrtManager/secrets.py @@ -4,14 +4,14 @@ from vrtManager.connection import wvmConnect class wvmSecrets(wvmConnect): def create_secret(self, ephemeral, private, secret_type, data): - xml = """ - """ % (ephemeral, private, secret_type) + xml = f""" + """ if secret_type == 'ceph': - xml += """%s""" % data + xml += f"""{data}""" if secret_type == 'volume': - xml += """%s""" % data + xml += f"""{data}""" if secret_type == 'iscsi': - xml += """%s""" % data + xml += f"""{data}""" xml += """ """ self.wvm.secretDefineXML(xml) diff --git a/vrtManager/storage.py b/vrtManager/storage.py index 84a5c13..ff3e05f 100644 --- a/vrtManager/storage.py +++ b/vrtManager/storage.py @@ -25,23 +25,21 @@ class wvmStorages(wvmConnect): self.wvm.storagePoolDefineXML(xml, flag) def create_storage(self, stg_type, name, source, target): - xml = """ - - %s""" % (stg_type, name) + xml = f""" + {name}""" if stg_type == 'logical': - xml += """ - - - %s - - """ % (source, name) + xml += f""" + + {name} + + """ if stg_type == 'logical': target = '/dev/' + name - xml += """ + xml += f""" - %s + {target} - """ % target + """ self.define_storage(xml, 0) stg = self.get_storage(name) if stg_type == 'logical': @@ -52,35 +50,37 @@ class wvmStorages(wvmConnect): return stg def create_storage_ceph(self, stg_type, name, ceph_pool, ceph_host, ceph_user, secret): - xml = """ - - %s + xml = f""" + + {name} - %s - - - + {ceph_pool} + + + - """ % (stg_type, name, ceph_pool, ceph_host, ceph_user, secret) + + """ self.define_storage(xml, 0) stg = self.get_storage(name) stg.create(0) stg.setAutostart(1) def create_storage_netfs(self, stg_type, name, netfs_host, source, source_format, target): - xml = """ - - %s + xml = f""" + + {name} - - - + + + - %s + {target} - """ % (stg_type, name, netfs_host, source, source_format, target) + + """ self.define_storage(xml, 0) stg = self.get_storage(name) stg.create(0) @@ -222,27 +222,26 @@ class wvmStorage(wvmConnect): else: name += '.img' alloc = 0 - xml = """ + xml = f""" - %s - %s - %s + {name} + {size} + {alloc} - + - %s - %s + {owner['uid']} + {owner['guid']} 0644 - """ % (name, size, alloc, vol_fmt, owner['uid'], owner['guid']) + """ if vol_fmt == 'qcow2': xml += """ 1.1 """ - xml += """ - + xml += """ """ self._createXML(xml, metadata) return name @@ -260,19 +259,19 @@ class wvmStorage(wvmConnect): suffix = '.' + file_suffix target_file += suffix if len(suffix) > 1 else '' - xml = """ + xml = f""" - %s + {target_file} 0 0 - + - %s - %s - %s + {owner['uid']} + {owner['guid']} + {mode} - """ % (target_file, vol_fmt, owner['uid'], owner['guid'], mode) + """ if vol_fmt == 'qcow2': xml += """ 1.1