CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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