diff --git a/daemon/core/api/tlv/coreapi.py b/daemon/core/api/tlv/coreapi.py index ba737fd4..0fd16bf5 100644 --- a/daemon/core/api/tlv/coreapi.py +++ b/daemon/core/api/tlv/coreapi.py @@ -180,7 +180,7 @@ class CoreTlvDataString(CoreTlvData): :rtype: tuple """ if not isinstance(value, str): - raise ValueError("value not a string: %s" % type(value)) + raise ValueError(f"value not a string: {type(value)}") value = value.encode("utf-8") if len(value) < 256: @@ -220,7 +220,7 @@ class CoreTlvDataUint16List(CoreTlvData): :rtype: tuple """ if not isinstance(values, tuple): - raise ValueError("value not a tuple: %s" % values) + raise ValueError(f"value not a tuple: {values}") data = b"" for value in values: @@ -237,7 +237,8 @@ class CoreTlvDataUint16List(CoreTlvData): :param data: data to unpack :return: unpacked data """ - data_format = "!%dH" % (len(data) / 2) + size = int(len(data) / 2) + data_format = f"!{size}H" return struct.unpack(data_format, data) @classmethod @@ -435,7 +436,7 @@ class CoreTlv(object): try: return self.tlv_type_map(self.tlv_type).name except ValueError: - return "unknown tlv type: %s" % str(self.tlv_type) + return f"unknown tlv type: {self.tlv_type}" def __str__(self): """ @@ -444,11 +445,7 @@ class CoreTlv(object): :return: string representation :rtype: str """ - return "%s " % ( - self.__class__.__name__, - self.type_str(), - self.value, - ) + return f"{self.__class__.__name__} " class CoreNodeTlv(CoreTlv): @@ -734,7 +731,7 @@ class CoreMessage(object): :return: nothing """ if key in self.tlv_data: - raise KeyError("key already exists: %s (val=%s)" % (key, value)) + raise KeyError(f"key already exists: {key} (val={value})") self.tlv_data[key] = value @@ -793,7 +790,7 @@ class CoreMessage(object): try: return MessageTypes(self.message_type).name except ValueError: - return "unknown message type: %s" % str(self.message_type) + return f"unknown message type: {self.message_type}" def flag_str(self): """ @@ -810,12 +807,13 @@ class CoreMessage(object): try: message_flags.append(self.flag_map(flag).name) except ValueError: - message_flags.append("0x%x" % flag) + message_flags.append(f"0x{flag:x}") flag <<= 1 if not (self.flags & ~(flag - 1)): break - return "0x%x <%s>" % (self.flags, " | ".join(message_flags)) + message_flags = " | ".join(message_flags) + return f"0x{self.flags:x} <{message_flags}>" def __str__(self): """ @@ -824,20 +822,16 @@ class CoreMessage(object): :return: string representation :rtype: str """ - result = "%s " % ( - self.__class__.__name__, - self.type_str(), - self.flag_str(), - ) + result = f"{self.__class__.__name__} " for key in self.tlv_data: value = self.tlv_data[key] try: tlv_type = self.tlv_class.tlv_type_map(key).name except ValueError: - tlv_type = "tlv type %s" % key + tlv_type = f"tlv type {key}" - result += "\n %s: %s" % (tlv_type, value) + result += f"\n {tlv_type}: {value}" return result diff --git a/daemon/core/api/tlv/corehandlers.py b/daemon/core/api/tlv/corehandlers.py index f3966ba1..85b7b831 100644 --- a/daemon/core/api/tlv/corehandlers.py +++ b/daemon/core/api/tlv/corehandlers.py @@ -75,7 +75,7 @@ class CoreHandler(socketserver.BaseRequestHandler): self.handler_threads = [] num_threads = int(server.config["numthreads"]) if num_threads < 1: - raise ValueError("invalid number of threads: %s" % num_threads) + raise ValueError(f"invalid number of threads: {num_threads}") logging.debug("launching core server handler threads: %s", num_threads) for _ in range(num_threads): @@ -460,7 +460,7 @@ class CoreHandler(socketserver.BaseRequestHandler): try: header = self.request.recv(coreapi.CoreMessage.header_len) except IOError as e: - raise IOError("error receiving header (%s)" % e) + raise IOError(f"error receiving header ({e})") if len(header) != coreapi.CoreMessage.header_len: if len(header) == 0: @@ -478,10 +478,7 @@ class CoreHandler(socketserver.BaseRequestHandler): while len(data) < message_len: data += self.request.recv(message_len - len(data)) if len(data) > message_len: - error_message = ( - "received message length does not match received data (%s != %s)" - % (len(data), message_len) - ) + error_message = f"received message length does not match received data ({len(data)} != {message_len})" logging.error(error_message) raise IOError(error_message) @@ -573,11 +570,7 @@ class CoreHandler(socketserver.BaseRequestHandler): ) except KeyError: # multiple TLVs of same type cause KeyError exception - reply_message = "CoreMessage (type %d flags %d length %d)" % ( - message_type, - message_flags, - message_length, - ) + reply_message = f"CoreMessage (type {message_type} flags {message_flags} length {message_length})" logging.debug("sending reply:\n%s", reply_message) @@ -999,7 +992,7 @@ class CoreHandler(socketserver.BaseRequestHandler): RegisterTlvs.EXECUTE_SERVER.value, execute_server ) tlv_data += coreapi.CoreRegisterTlv.pack( - RegisterTlvs.SESSION.value, "%s" % sid + RegisterTlvs.SESSION.value, str(sid) ) message = coreapi.CoreRegMessage.pack(0, tlv_data) replies.append(message) @@ -1104,7 +1097,7 @@ class CoreHandler(socketserver.BaseRequestHandler): self.session.mobility.config_reset(node_id) self.session.emane.config_reset(node_id) else: - raise Exception("cant handle config all: %s" % message_type) + raise Exception(f"cant handle config all: {message_type}") return replies @@ -1158,7 +1151,7 @@ class CoreHandler(socketserver.BaseRequestHandler): if metadata_configs is None: metadata_configs = {} data_values = "|".join( - ["%s=%s" % (x, metadata_configs[x]) for x in metadata_configs] + [f"{x}={metadata_configs[x]}" for x in metadata_configs] ) data_types = tuple(ConfigDataTypes.STRING.value for _ in metadata_configs) config_response = ConfigData( @@ -1239,7 +1232,7 @@ class CoreHandler(socketserver.BaseRequestHandler): services = sorted(group_map[group], key=lambda x: x.name.lower()) logging.debug("sorted services for group(%s): %s", group, services) end_index = start_index + len(services) - 1 - group_strings.append("%s:%s-%s" % (group, start_index, end_index)) + group_strings.append(f"{group}:{start_index}-{end_index}") start_index += len(services) for service_name in services: captions.append(service_name.name) @@ -1714,24 +1707,24 @@ class CoreHandler(socketserver.BaseRequestHandler): ): status = self.session.services.stop_service(node, service) if status: - fail += "Stop %s," % service.name + fail += f"Stop {service.name}," if ( event_type == EventTypes.START.value or event_type == EventTypes.RESTART.value ): status = self.session.services.startup_service(node, service) if status: - fail += "Start %s(%s)," % service.name + fail += f"Start ({service.name})," if event_type == EventTypes.PAUSE.value: status = self.session.services.validate_service(node, service) if status: - fail += "%s," % service.name + fail += f"{service.name}," if event_type == EventTypes.RECONFIGURE.value: self.session.services.service_reconfigure(node, service) fail_data = "" if len(fail) > 0: - fail_data += "Fail:" + fail + fail_data += f"Fail:{fail}" unknown_data = "" num = len(unknown) if num > 0: @@ -1741,14 +1734,14 @@ class CoreHandler(socketserver.BaseRequestHandler): unknown_data += ", " num -= 1 logging.warning("Event requested for unknown service(s): %s", unknown_data) - unknown_data = "Unknown:" + unknown_data + unknown_data = f"Unknown:{unknown_data}" event_data = EventData( node=node_id, event_type=event_type, name=name, data=fail_data + ";" + unknown_data, - time="%s" % time.time(), + time=str(time.time()), ) self.session.broadcast_event(event_data) @@ -1769,7 +1762,7 @@ class CoreHandler(socketserver.BaseRequestHandler): thumb = message.get_tlv(SessionTlvs.THUMB.value) user = message.get_tlv(SessionTlvs.USER.value) logging.debug( - "SESSION message flags=0x%x sessions=%s" % (message.flags, session_id_str) + "SESSION message flags=0x%x sessions=%s", message.flags, session_id_str ) if message.flags == 0: @@ -1940,7 +1933,7 @@ class CoreHandler(socketserver.BaseRequestHandler): # service customizations service_configs = self.session.services.all_configs() for node_id, service in service_configs: - opaque = "service:%s" % service.name + opaque = f"service:{service.name}" data_types = tuple( repeat(ConfigDataTypes.STRING.value, len(ServiceShim.keys)) ) @@ -1976,7 +1969,7 @@ class CoreHandler(socketserver.BaseRequestHandler): file_data = FileData( message_type=MessageFlags.ADD.value, name=str(file_name), - type="hook:%s" % state, + type=f"hook:{state}", data=str(config_data), ) self.session.broadcast_file(file_data) @@ -1992,7 +1985,7 @@ class CoreHandler(socketserver.BaseRequestHandler): metadata_configs = self.session.metadata.get_configs() if metadata_configs: data_values = "|".join( - ["%s=%s" % (x, metadata_configs[x]) for x in metadata_configs] + [f"{x}={metadata_configs[x]}" for x in metadata_configs] ) data_types = tuple( ConfigDataTypes.STRING.value @@ -2041,7 +2034,7 @@ class CoreUdpHandler(CoreHandler): data = self.request[0] header = data[: coreapi.CoreMessage.header_len] if len(header) < coreapi.CoreMessage.header_len: - raise IOError("error receiving header (received %d bytes)" % len(header)) + raise IOError(f"error receiving header (received {len(header)} bytes)") message_type, message_flags, message_len = coreapi.CoreMessage.unpack_header( header @@ -2136,7 +2129,7 @@ class CoreUdpHandler(CoreHandler): :return: """ raise Exception( - "Unable to queue %s message for later processing using UDP!" % msg + f"Unable to queue {msg} message for later processing using UDP!" ) def sendall(self, data): diff --git a/daemon/core/nodes/network.py b/daemon/core/nodes/network.py index 98bec198..229005c4 100644 --- a/daemon/core/nodes/network.py +++ b/daemon/core/nodes/network.py @@ -97,7 +97,7 @@ class EbtablesQueue(object): :return: ebtable atomic command :rtype: list[str] """ - return "%s --atomic-file %s %s" % (EBTABLES_BIN, self.atomic_file, cmd) + return f"{EBTABLES_BIN} --atomic-file {self.atomic_file} {cmd}" def lastupdate(self, wlan): """ @@ -175,7 +175,7 @@ class EbtablesQueue(object): wlan.net_cmd(args) try: - wlan.net_cmd("rm -f %s" % self.atomic_file) + wlan.net_cmd(f"rm -f {self.atomic_file}") except CoreCommandError: logging.exception("error removing atomic file: %s", self.atomic_file) @@ -198,26 +198,22 @@ class EbtablesQueue(object): """ with wlan._linked_lock: # flush the chain - self.cmds.append("-F %s" % wlan.brname) + self.cmds.append(f"-F {wlan.brname}") # rebuild the chain for netif1, v in wlan._linked.items(): for netif2, linked in v.items(): if wlan.policy == "DROP" and linked: self.cmds.extend( [ - "-A %s -i %s -o %s -j ACCEPT" - % (wlan.brname, netif1.localname, netif2.localname), - "-A %s -o %s -i %s -j ACCEPT" - % (wlan.brname, netif1.localname, netif2.localname), + f"-A {wlan.brname} -i {netif1.localname} -o {netif2.localname} -j ACCEPT", + f"-A {wlan.brname} -o {netif1.localname} -i {netif2.localname} -j ACCEPT", ] ) elif wlan.policy == "ACCEPT" and not linked: self.cmds.extend( [ - "-A %s -i %s -o %s -j DROP" - % (wlan.brname, netif1.localname, netif2.localname), - "-A %s -o %s -i %s -j DROP" - % (wlan.brname, netif1.localname, netif2.localname), + f"-A {wlan.brname} -i {netif1.localname} -o {netif2.localname} -j DROP", + f"-A {wlan.brname} -o {netif1.localname} -i {netif2.localname} -j DROP", ] ) @@ -268,7 +264,7 @@ class CoreNetwork(CoreNetworkBase): self.policy = policy self.name = name sessionid = self.session.short_session_id() - self.brname = "b.%s.%s" % (str(self.id), sessionid) + self.brname = f"b.{self.id}.{sessionid}" self.up = False if start: self.startup() @@ -303,9 +299,8 @@ class CoreNetwork(CoreNetworkBase): # create a new ebtables chain for this bridge cmds = [ - "%s -N %s -P %s" % (EBTABLES_BIN, self.brname, self.policy), - "%s -A FORWARD --logical-in %s -j %s" - % (EBTABLES_BIN, self.brname, self.brname), + f"{EBTABLES_BIN} -N {self.brname} -P {self.policy}", + f"{EBTABLES_BIN} -A FORWARD --logical-in {self.brname} -j {self.brname}", ] ebtablescmds(self.net_cmd, cmds) @@ -325,9 +320,8 @@ class CoreNetwork(CoreNetworkBase): try: self.net_client.delete_bridge(self.brname) cmds = [ - "%s -D FORWARD --logical-in %s -j %s" - % (EBTABLES_BIN, self.brname, self.brname), - "%s -X %s" % (EBTABLES_BIN, self.brname), + f"{EBTABLES_BIN} -D FORWARD --logical-in {self.brname} -j {self.brname}", + f"{EBTABLES_BIN} -X {self.brname}", ] ebtablescmds(self.net_cmd, cmds) except CoreCommandError: @@ -379,10 +373,10 @@ class CoreNetwork(CoreNetworkBase): """ # check if the network interfaces are attached to this network if self._netif[netif1.netifi] != netif1: - raise ValueError("inconsistency for netif %s" % netif1.name) + raise ValueError(f"inconsistency for netif {netif1.name}") if self._netif[netif2.netifi] != netif2: - raise ValueError("inconsistency for netif %s" % netif2.name) + raise ValueError(f"inconsistency for netif {netif2.name}") try: linked = self._linked[netif1][netif2] @@ -392,7 +386,7 @@ class CoreNetwork(CoreNetworkBase): elif self.policy == "DROP": linked = False else: - raise Exception("unknown policy: %s" % self.policy) + raise Exception(f"unknown policy: {self.policy}") self._linked[netif1][netif2] = linked return linked @@ -455,7 +449,7 @@ class CoreNetwork(CoreNetworkBase): """ if devname is None: devname = netif.localname - tc = "%s qdisc replace dev %s" % (TC_BIN, devname) + tc = f"{TC_BIN} qdisc replace dev {devname}" parent = "root" changed = False if netif.setparam("bw", bw): @@ -464,16 +458,16 @@ class CoreNetwork(CoreNetworkBase): burst = max(2 * netif.mtu, bw / 1000) # max IP payload limit = 0xFFFF - tbf = "tbf rate %s burst %s limit %s" % (bw, burst, limit) + tbf = f"tbf rate {bw} burst {burst} limit {limit}" if bw > 0: if self.up: - cmd = "%s %s handle 1: %s" % (tc, parent, tbf) + cmd = f"{tc} {parent} handle 1: {tbf}" netif.net_cmd(cmd) netif.setparam("has_tbf", True) changed = True elif netif.getparam("has_tbf") and bw <= 0: if self.up: - cmd = "%s qdisc delete dev %s %s" % (TC_BIN, devname, parent) + cmd = f"{TC_BIN} qdisc delete dev {devname} {parent}" netif.net_cmd(cmd) netif.setparam("has_tbf", False) # removing the parent removes the child @@ -494,17 +488,17 @@ class CoreNetwork(CoreNetworkBase): return # jitter and delay use the same delay statement if delay is not None: - netem += " delay %sus" % delay + netem += f" delay {delay}us" if jitter is not None: if delay is None: - netem += " delay 0us %sus 25%%" % jitter + netem += f" delay 0us {jitter}us 25%" else: - netem += " %sus 25%%" % jitter + netem += f" {jitter}us 25%" if loss is not None and loss > 0: - netem += " loss %s%%" % min(loss, 100) + netem += f" loss {min(loss, 100)}%" if duplicate is not None and duplicate > 0: - netem += " duplicate %s%%" % min(duplicate, 100) + netem += f" duplicate {min(duplicate, 100)}%" delay_check = delay is None or delay <= 0 jitter_check = jitter is None or jitter <= 0 @@ -515,16 +509,13 @@ class CoreNetwork(CoreNetworkBase): if not netif.getparam("has_netem"): return if self.up: - cmd = "%s qdisc delete dev %s %s handle 10:" % (TC_BIN, devname, parent) + cmd = f"{TC_BIN} qdisc delete dev {devname} {parent} handle 10:" netif.net_cmd(cmd) netif.setparam("has_netem", False) elif len(netem) > 1: if self.up: - cmd = "%s qdisc replace dev %s %s handle 10: %s" % ( - TC_BIN, - devname, - parent, - netem, + cmd = ( + f"{TC_BIN} qdisc replace dev {devname} {parent} handle 10: {netem}" ) netif.net_cmd(cmd) netif.setparam("has_netem", True) @@ -540,22 +531,22 @@ class CoreNetwork(CoreNetworkBase): """ sessionid = self.session.short_session_id() try: - _id = "%x" % self.id + _id = f"{self.id:x}" except TypeError: - _id = "%s" % self.id + _id = str(self.id) try: - net_id = "%x" % net.id + net_id = f"{net.id:x}" except TypeError: - net_id = "%s" % net.id + net_id = str(net.id) - localname = "veth%s.%s.%s" % (_id, net_id, sessionid) + localname = f"veth{_id}.{net_id}.{sessionid}" if len(localname) >= 16: - raise ValueError("interface local name %s too long" % localname) + raise ValueError(f"interface local name {localname} too long") - name = "veth%s.%s.%s" % (net_id, _id, sessionid) + name = f"veth{net_id}.{_id}.{sessionid}" if len(name) >= 16: - raise ValueError("interface name %s too long" % name) + raise ValueError(f"interface name {name} too long") netif = Veth(self.session, None, name, localname, start=self.up) self.attach(netif) @@ -689,7 +680,7 @@ class GreTapBridge(CoreNetwork): :return: nothing """ if self.gretap: - raise ValueError("gretap already exists for %s" % self.name) + raise ValueError(f"gretap already exists for {self.name}") remoteip = addrlist[0].split("/")[0] localip = None if len(addrlist) > 1: @@ -773,14 +764,14 @@ class CtrlNet(CoreNetwork): :return: """ use_ovs = self.session.options.get_config("ovs") == "True" - current = "%s/%s" % (address, self.prefix.prefixlen) + current = f"{address}/{self.prefix.prefixlen}" net_client = get_net_client(use_ovs, utils.check_cmd) net_client.create_address(self.brname, current) servers = self.session.distributed.servers for name in servers: server = servers[name] address -= 1 - current = "%s/%s" % (address, self.prefix.prefixlen) + current = f"{address}/{self.prefix.prefixlen}" net_client = get_net_client(use_ovs, server.remote_cmd) net_client.create_address(self.brname, current) @@ -792,7 +783,7 @@ class CtrlNet(CoreNetwork): :raises CoreCommandError: when there is a command exception """ if self.net_client.existing_bridges(self.id): - raise CoreError("old bridges exist for node: %s" % self.id) + raise CoreError(f"old bridges exist for node: {self.id}") CoreNetwork.startup(self) @@ -811,7 +802,7 @@ class CtrlNet(CoreNetwork): self.brname, self.updown_script, ) - self.net_cmd("%s %s startup" % (self.updown_script, self.brname)) + self.net_cmd(f"{self.updown_script} {self.brname} startup") if self.serverintf: self.net_client.create_interface(self.brname, self.serverintf) @@ -839,7 +830,7 @@ class CtrlNet(CoreNetwork): self.brname, self.updown_script, ) - self.net_cmd("%s %s shutdown" % (self.updown_script, self.brname)) + self.net_cmd(f"{self.updown_script} {self.brname} shutdown") except CoreCommandError: logging.exception("error issuing shutdown script shutdown")