From 5e6e4d8fc9f48a7a6e96bcb4f7ab9f159aeec8c8 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 22 Feb 2023 09:29:47 +1000 Subject: [PATCH] scanner: separate the parser from the actual protocol Let's not leak anything related to XML parsing into the class we use as the actual protocol description. --- proto/ei-scanner | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/proto/ei-scanner b/proto/ei-scanner index bb11996..00af3b6 100755 --- a/proto/ei-scanner +++ b/proto/ei-scanner @@ -361,7 +361,12 @@ class XmlError(Exception): @attr.s -class Protocol(xml.sax.handler.ContentHandler): +class Protocol: + interfaces: list[Interface] = attr.ib(factory=list) + + +@attr.s +class ProtocolParser(xml.sax.handler.ContentHandler): component: str = attr.ib() interfaces: list[Interface] = attr.ib(factory=list) @@ -613,17 +618,17 @@ class Protocol(xml.sax.handler.ContentHandler): pass @classmethod - def create(cls, component: str) -> "Protocol": + def create(cls, component: str) -> "ProtocolParser": h = cls(component=component) return h def parse(protofile: Path, component: str) -> Protocol: - proto = Protocol.create(component=component) + proto = ProtocolParser.create(component=component) xml.sax.parse(os.fspath(protofile), proto) # We parse two times, once to fetch all the interfaces, then to parse the details xml.sax.parse(os.fspath(protofile), proto) - return proto + return Protocol(proto.interfaces) def generate_source(