scanner: parse the copyright tag

This commit is contained in:
Peter Hutterer 2023-02-23 09:21:00 +10:00
parent 8692e2592a
commit 2227690287

View file

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