From 40678b728e95fe5e153a7222530428551a695714 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Wed, 20 Jul 2022 23:21:36 +0200 Subject: [PATCH 1/4] generate_packets.py: Move packet header parsing into separate method See osdn#45187 Signed-off-by: Alina Lenk --- common/generate_packets.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index e8aa4ea1a3..ef67dfad15 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -1929,23 +1929,34 @@ class Packet: # matches a packet cancel flag (cancelled packet type) CANCEL_PATTERN = re.compile(r"^cancel\((.*)\)$") - def __init__(self, text: str, resolve_type: typing.Callable[[str], str]): + @classmethod + def parse(cls, text: str, resolve_type: typing.Callable[[str], str]) -> "Packet": + """Parse a packet from the given text (without the end line)""" text = text.strip() - lines = text.split("\n") + header, *lines = text.split("\n") - mo = __class__.HEADER_PATTERN.fullmatch(lines[0]) + mo = __class__.HEADER_PATTERN.fullmatch(header) if mo is None: - raise ValueError("not a valid packet header line: %r" % lines[0]) + raise ValueError("not a valid packet header line: %r" % header) + + packet_type, packet_number, flags_text = mo.groups("") + packet_number = int(packet_number) + if packet_number not in range(65536): + raise ValueError("packet number %d for %s outside legal range [0,65536)" % (packet_number, packet_type)) - self.type=mo.group(1) - self.type_number=int(mo.group(2)) - if self.type_number not in range(65536): - raise ValueError("packet number %d for %s outside legal range [0,65536)" % (self.type_number, self.name)) - dummy=mo.group(3) + return cls(packet_type, packet_number, flags_text, lines, resolve_type) - del lines[0] + def __init__(self, packet_type: str, packet_number: int, flags_text: str, + lines: typing.Iterable[str], resolve_type: typing.Callable[[str], str]): + self.type = packet_type + self.type_number = packet_number - arr=list(item.strip() for item in dummy.split(",") if item) + arr = [ + stripped + for flag in flags_text.split(",") + for stripped in (flag.strip(),) + if stripped + ] dirs = set() @@ -2310,7 +2321,7 @@ class PacketsDefinition(typing.Iterable[Packet]): packet_text = packet_text.strip() if not packet_text: continue - packet = Packet(packet_text, self.resolve_type) + packet = Packet.parse(packet_text, self.resolve_type) if packet.type in self.packets_by_type: raise ValueError("Duplicate packet type: " + packet.type) -- 2.34.1