Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/autotest/logger_metadata/emit_xml.py
Views: 1799
from __future__ import print_function12from lxml import etree3import emitter45class XMLEmitter(emitter.Emitter):6def preface(self):7return """<?xml version="1.0" encoding="utf-8"?>8<!-- Dynamically generated list of documented logfile messages (generated by parse.py) -->9"""1011def postface(self):12return1314def start(self):15self.logname = "LogMessages.xml"16self.fh = open("LogMessages.xml", mode='w')17print(self.preface(), file=self.fh)18self.loggermessagefile = etree.Element('loggermessagefile')1920def emit(self, doccos, enumerations):21self.start()22for docco in doccos:23xml_logformat = etree.SubElement(self.loggermessagefile, 'logformat', name=docco.name)24if docco.url is not None:25xml_url = etree.SubElement(xml_logformat, 'url')26xml_url.text = docco.url27if docco.description is not None:28xml_description = etree.SubElement(xml_logformat, 'description')29xml_description.text = docco.description3031xml_fields = etree.SubElement(xml_logformat, 'fields')32for f in docco.fields_order:33units = docco.fields[f]['units'] if "units" in docco.fields[f] else ""34fmt = docco.fields[f]['fmt'] if "fmt" in docco.fields[f] else ""35xml_field = etree.SubElement(xml_fields, 'field', name=f, units=units, type=fmt)36if "description" in docco.fields[f]:37xml_description2 = etree.SubElement(xml_field, 'description')38xml_description2.text = docco.fields[f]["description"]39# Check for enum/bitfield40fieldnamething = None41if "bitmaskenum" in docco.fields[f]:42fieldnamething = "bitmaskenum"43xmlenumtag = "bitmask"44xmlentrytag = "bit"45elif "valueenum" in docco.fields[f]:46fieldnamething = "valueenum"47xmlenumtag = "enum"48xmlentrytag = "element"49# If an enum/bitmask is defined, include this in the XML50if fieldnamething is not None:51enum_name = docco.fields[f][fieldnamething]52if enum_name not in enumerations:53raise Exception("Unknown enum (%s) (have %s)" %54(enum_name, "\n".join(sorted(enumerations.keys()))))55enum = enumerations[enum_name]56xml_enum = etree.SubElement(xml_field, xmlenumtag, name=enum_name)57for entry in enum.entries:58xml_enum_entry = etree.SubElement(xml_enum, xmlentrytag, name=entry.name)59xml_enum_entry_value = etree.SubElement(xml_enum_entry, 'value')60xml_enum_entry_value.text = str(entry.value)61if entry.comment is not None:62xml_enum_entry_comment = etree.SubElement(xml_enum_entry, 'description')63xml_enum_entry_comment.text = entry.comment64if xml_fields.text is None and not len(xml_fields):65xml_fields.text = '\n' # add </param> on next line in case of empty element.66self.stop()6768def stop(self):69# etree.indent(self.loggermessagefile) # not available on thor, Ubuntu 16.0470pretty_xml = etree.tostring(self.loggermessagefile, pretty_print=True, encoding='unicode')71self.fh.write(pretty_xml)72self.fh.close()737475