From 222769028717bacb2d47c5ba33059a275ffe0bdf Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 23 Feb 2023 09:21:00 +1000 Subject: [PATCH] scanner: parse the copyright tag --- proto/ei-scanner | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/proto/ei-scanner b/proto/ei-scanner index 48532f0..55c1d70 100755 --- a/proto/ei-scanner +++ b/proto/ei-scanner @@ -374,8 +374,15 @@ class XmlError(Exception): return cls(line=location[0], column=location[1], message=message) +@attr.s +class Copyright: + text: str = attr.ib(default="") + is_complete: bool = attr.ib(init=False, default=False) + + @attr.s class Protocol: + copyright: Optional[str] = attr.ib(default=None) interfaces: list[Interface] = attr.ib(factory=list) @@ -387,6 +394,7 @@ class ProtocolParser(xml.sax.handler.ContentHandler): current_interface: Optional[Interface] = attr.ib(init=False, default=None) current_message: Optional[Union[Message, Enum]] = attr.ib(init=False, default=None) current_description: Optional[Description] = attr.ib(init=False, default=None) + copyright: Optional[Copyright] = attr.ib(init=False, default=None) _run_counter: int = attr.ib(init=False, default=0, repr=False) @@ -612,6 +620,12 @@ class ProtocolParser(xml.sax.handler.ContentHandler): elif element == "description": summary = attrs.get("summary", "") self.current_description = Description(summary=summary) + elif element == "copyright": + if self.copyright is not None: + raise XmlError.create( + "Multiple tags in file", self.location + ) + self.copyright = Copyright() def endElement(self, name): if name == "interface": @@ -640,11 +654,16 @@ class ProtocolParser(xml.sax.handler.ContentHandler): else: self.current_message.description = self.current_description self.current_description = None + elif name == "copyright": + assert self.copyright is not None + self.copyright.text = dedent(self.copyright.text) + self.copyright.is_complete = True def characters(self, content): if self.current_description is not None: self.current_description.text += content - pass + elif self.copyright is not None and not self.copyright.is_complete: + self.copyright.text += content @classmethod def create(cls, component: str) -> "ProtocolParser": @@ -657,7 +676,8 @@ def parse(protofile: Path, component: str) -> Protocol: 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 Protocol(proto.interfaces) + copyright = proto.copyright.text if proto.copyright else None + return Protocol(copyright=copyright, interfaces=proto.interfaces) def generate_source(