On Tue, Apr 16, 2024 at 2:24 PM Luca Vizzarro <luca.vizza...@arm.com> wrote: > > On 16/04/2024 10:03, Juraj Linkeš wrote: > >> +@dataclass > >> +class TestPmdPort(TextParser): > > > > This and the classes above are missing docstrings. > > Ack. > > >> @@ -225,6 +664,38 @@ def set_forward_mode(self, mode: > >> TestPmdForwardingModes, verify: bool = True): > >> f"Test pmd failed to set fwd mode to {mode.value}" > >> ) > >> > >> + def show_port_info_all(self) -> list[TestPmdPort]: > >> + """Returns the information of all the ports.""" > > > > Can we add sample output so that the format of what we're trying to > > parse is clear? > > Ack. > > >> + output = self.send_command("show port info all") > >> + > >> + ports = [] > >> + iter = re.finditer(r"\*+.+\*+", output) > >> + if next(iter, False): # we are slicing retrospectively, skip > >> first block > >> + start_pos = 0 > >> + for block in iter: > >> + end_pos = block.start() > >> + ports.append(TestPmdPort.parse(output[start_pos:end_pos])) > >> + start_pos = end_pos > >> + > >> + ports.append(TestPmdPort.parse(output[start_pos:])) > >> + > >> + return ports > > > > Can this be done the same way it's done in the last commit? > > > > iter = re.finditer(r"(^ #*.+#*$[^#]+)^ #*$", output, re.MULTILINE) > > return [TestPmdPortStats.parse(block.group(1)) for block in iter] > > > > Looks much better. > > I agree that it looks much better. I gave it a first attempt to come up > with a regular expression that is not too complicated and is able to > match blocks individually. I've noticed that blocks start with: > > \n********* Infos for port X ************ > > but don't have an actual ending delimiter, unlike for the stats.
Ah, so that's the difference and the reason. I guess the ending delimiter is either the start of the next section of the prompt (or the end of the string). > I'll > experiment with some look ahead constructs. The easiest solution is to > match everything that is not * ([^*]+) but can we be certain that there > won't be any asterisk in the actual information? We can't. But we can be reasonably certain there won't be five consecutive asterisks, so maybe we can work with that.