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.
This commit is contained in:
Peter Hutterer 2023-02-22 09:29:47 +10:00
parent 4414c91fca
commit 5e6e4d8fc9

View file

@ -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(