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