Path: blob/master/Tools/autotest/logger_metadata/emit_xml.py
9717 views
'''1AP_FLAKE8_CLEAN2'''3from lxml import etree4import emitter567class XMLEmitter(emitter.Emitter):8def preface(self):9return """<?xml version="1.0" encoding="utf-8"?>10<!-- Dynamically generated list of documented logfile messages (generated by parse.py) -->11"""1213def postface(self):14return1516def start(self):17self.logname = "LogMessages.xml"18self.fh = open("LogMessages.xml", mode='w')19print(self.preface(), file=self.fh)20self.loggermessagefile = etree.Element('loggermessagefile')2122def emit(self, doccos, enumerations):23self.start()24for docco in doccos:25xml_logformat = etree.SubElement(self.loggermessagefile, 'logformat', name=docco.name)26if docco.url is not None:27xml_url = etree.SubElement(xml_logformat, 'url')28xml_url.text = docco.url29if docco.description is not None:30xml_description = etree.SubElement(xml_logformat, 'description')31xml_description.text = docco.description3233xml_fields = etree.SubElement(xml_logformat, 'fields')34for f in docco.fields_order:35units = docco.fields[f]['units'] if "units" in docco.fields[f] else ""36fmt = docco.fields[f]['fmt'] if "fmt" in docco.fields[f] else ""37xml_field = etree.SubElement(xml_fields, 'field', name=f, units=units, type=fmt)38if "description" in docco.fields[f]:39xml_description2 = etree.SubElement(xml_field, 'description')40xml_description2.text = docco.fields[f]["description"]41# Check for enum/bitfield42fieldnamething = None43if "bitmaskenum" in docco.fields[f]:44fieldnamething = "bitmaskenum"45xmlenumtag = "bitmask"46xmlentrytag = "bit"47elif "valueenum" in docco.fields[f]:48fieldnamething = "valueenum"49xmlenumtag = "enum"50xmlentrytag = "element"51# If an enum/bitmask is defined, include this in the XML52if fieldnamething is not None:53enum_name = docco.fields[f][fieldnamething]54if enum_name not in enumerations:55raise Exception("Unknown enum (%s) (have %s)" %56(enum_name, "\n".join(sorted(enumerations.keys()))))57enum = enumerations[enum_name]58xml_enum = etree.SubElement(xml_field, xmlenumtag, name=enum_name)59for entry in enum.entries:60xml_enum_entry = etree.SubElement(xml_enum, xmlentrytag, name=entry.name)61xml_enum_entry_value = etree.SubElement(xml_enum_entry, 'value')62xml_enum_entry_value.text = str(entry.value)63if entry.comment is not None:64xml_enum_entry_comment = etree.SubElement(xml_enum_entry, 'description')65xml_enum_entry_comment.text = entry.comment66if xml_fields.text is None and not len(xml_fields):67xml_fields.text = '\n' # add </param> on next line in case of empty element.68self.stop()6970def stop(self):71# etree.indent(self.loggermessagefile) # not available on thor, Ubuntu 16.0472pretty_xml = etree.tostring(self.loggermessagefile, pretty_print=True, encoding='unicode')73self.fh.write(pretty_xml)74self.fh.close()757677