from __future__ import print_function from lxml import etree import emitter class XMLEmitter(emitter.Emitter): def preface(self): return """ """ def postface(self): return def start(self): self.logname = "LogMessages.xml" self.fh = open("LogMessages.xml", mode='w') print(self.preface(), file=self.fh) self.loggermessagefile = etree.Element('loggermessagefile') def emit(self, doccos, enumerations): self.start() for docco in doccos: xml_logformat = etree.SubElement(self.loggermessagefile, 'logformat', name=docco.name) if docco.url is not None: xml_url = etree.SubElement(xml_logformat, 'url') xml_url.text = docco.url if docco.description is not None: xml_description = etree.SubElement(xml_logformat, 'description') xml_description.text = docco.description xml_fields = etree.SubElement(xml_logformat, 'fields') for f in docco.fields_order: units = docco.fields[f]['units'] if "units" in docco.fields[f] else "" fmt = docco.fields[f]['fmt'] if "fmt" in docco.fields[f] else "" xml_field = etree.SubElement(xml_fields, 'field', name=f, units=units, type=fmt) if "description" in docco.fields[f]: xml_description2 = etree.SubElement(xml_field, 'description') xml_description2.text = docco.fields[f]["description"] # Check for enum/bitfield fieldnamething = None if "bitmaskenum" in docco.fields[f]: fieldnamething = "bitmaskenum" xmlenumtag = "bitmask" xmlentrytag = "bit" elif "valueenum" in docco.fields[f]: fieldnamething = "valueenum" xmlenumtag = "enum" xmlentrytag = "element" # If an enum/bitmask is defined, include this in the XML if fieldnamething is not None: enum_name = docco.fields[f][fieldnamething] if enum_name not in enumerations: raise Exception("Unknown enum (%s) (have %s)" % (enum_name, "\n".join(sorted(enumerations.keys())))) enum = enumerations[enum_name] xml_enum = etree.SubElement(xml_field, xmlenumtag, name=enum_name) for entry in enum.entries: xml_enum_entry = etree.SubElement(xml_enum, xmlentrytag, name=entry.name) xml_enum_entry_value = etree.SubElement(xml_enum_entry, 'value') xml_enum_entry_value.text = str(entry.value) if entry.comment is not None: xml_enum_entry_comment = etree.SubElement(xml_enum_entry, 'description') xml_enum_entry_comment.text = entry.comment if xml_fields.text is None and not len(xml_fields): xml_fields.text = '\n' # add on next line in case of empty element. self.stop() def stop(self): # etree.indent(self.loggermessagefile) # not available on thor, Ubuntu 16.04 pretty_xml = etree.tostring(self.loggermessagefile, pretty_print=True, encoding='unicode') self.fh.write(pretty_xml) self.fh.close()