mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-08 17:08:28 -04:00
Tools: use lxml for xml generation
fix caracters fix indentation validate xml
This commit is contained in:
parent
253ce18184
commit
69fab70582
@ -2,48 +2,50 @@
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
from lxml import etree
|
||||||
import emitter
|
import emitter
|
||||||
|
|
||||||
class XMLEmitter(emitter.Emitter):
|
class XMLEmitter(emitter.Emitter):
|
||||||
def preface(self):
|
def preface(self):
|
||||||
return """<?xml version="1.0" encoding="utf-8"?>
|
return """<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!-- Dynamically generated list of documented logfile messages (generated by parse.py) -->
|
<!-- Dynamically generated list of documented logfile messages (generated by parse.py) -->
|
||||||
<loggermessagefile>
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def postface(self):
|
def postface(self):
|
||||||
return "</loggermessagefile>"
|
return
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
self.logname = "LogMessages.xml"
|
||||||
self.fh = open("LogMessages.xml", mode='w')
|
self.fh = open("LogMessages.xml", mode='w')
|
||||||
print(self.preface(), file=self.fh)
|
print(self.preface(), file=self.fh)
|
||||||
|
self.loggermessagefile = etree.Element('loggermessagefile')
|
||||||
|
|
||||||
def emit(self, doccos):
|
def emit(self, doccos):
|
||||||
self.start()
|
self.start()
|
||||||
for docco in doccos:
|
for docco in doccos:
|
||||||
print(' <logformat name="%s">' % docco.name, file=self.fh)
|
xml_logformat = etree.SubElement(self.loggermessagefile, 'logformat', name=docco.name)
|
||||||
if docco.url is not None:
|
if docco.url is not None:
|
||||||
print(' <url>%s</url>' % docco.url, file=self.fh)
|
xml_url = etree.SubElement(xml_logformat, 'url')
|
||||||
|
xml_url.text = docco.url
|
||||||
if docco.description is not None:
|
if docco.description is not None:
|
||||||
print(' <description>%s</description>' %
|
xml_description = etree.SubElement(xml_logformat, 'description')
|
||||||
docco.description, file=self.fh)
|
xml_description.text = docco.description
|
||||||
print(' <fields>', file=self.fh)
|
|
||||||
|
xml_fields = etree.SubElement(xml_logformat, 'fields')
|
||||||
for f in docco.fields_order:
|
for f in docco.fields_order:
|
||||||
print(' <field name="%s">' % f, file=self.fh)
|
xml_field = etree.SubElement(xml_fields, 'field', name=f)
|
||||||
if "description" in docco.fields[f]:
|
if "description" in docco.fields[f]:
|
||||||
print(' <description>%s</description>' %
|
xml_description2 = etree.SubElement(xml_field, 'description')
|
||||||
docco.fields[f]["description"], file=self.fh)
|
xml_description2.text = docco.fields[f]["description"]
|
||||||
if "bits" in docco.fields[f]:
|
if "bits" in docco.fields[f]:
|
||||||
print(' <bits>%s</bits>' %
|
xml_bits = etree.SubElement(xml_field, 'bits')
|
||||||
docco.fields[f]["bits"], file=self.fh)
|
xml_bits.text = docco.fields[f]["bits"]
|
||||||
print(' </field>', file=self.fh)
|
if xml_fields.text is None and not len(xml_fields):
|
||||||
|
xml_fields.text = '\n' # add </param> on next line in case of empty element.
|
||||||
print(' </fields>', file=self.fh)
|
|
||||||
|
|
||||||
print(' </logformat>', file=self.fh)
|
|
||||||
|
|
||||||
print("", file=self.fh)
|
|
||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
print(self.postface(), file=self.fh)
|
etree.indent(self.loggermessagefile)
|
||||||
|
pretty_xml = etree.tostring(self.loggermessagefile, pretty_print=True, encoding='unicode')
|
||||||
|
self.fh.write(pretty_xml)
|
||||||
self.fh.close()
|
self.fh.close()
|
||||||
|
@ -1,16 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from xml.sax.saxutils import escape, quoteattr
|
from lxml import etree
|
||||||
|
|
||||||
from emit import Emit
|
from emit import Emit
|
||||||
from param import known_param_fields, known_units
|
from param import known_param_fields, known_units
|
||||||
|
|
||||||
INDENT2 = " "
|
|
||||||
INDENT4 = " "
|
|
||||||
INDENT6 = " "
|
|
||||||
INDENT8 = " "
|
|
||||||
INDENT10 = " "
|
|
||||||
|
|
||||||
# Emit APM documentation in an machine readable XML format
|
# Emit APM documentation in an machine readable XML format
|
||||||
class XmlEmit(Emit):
|
class XmlEmit(Emit):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -21,66 +15,66 @@ class XmlEmit(Emit):
|
|||||||
<!-- Dynamically generated list of documented parameters (generated by param_parse.py) -->
|
<!-- Dynamically generated list of documented parameters (generated by param_parse.py) -->
|
||||||
'''
|
'''
|
||||||
self.f.write(self.preamble)
|
self.f.write(self.preamble)
|
||||||
self.f.write('''<paramfile>
|
self.paramfile = etree.Element('paramfile')
|
||||||
<vehicles>\n''')
|
self.vehicles = etree.SubElement(self.paramfile, 'vehicles')
|
||||||
|
self.libraries = etree.SubElement(self.paramfile, 'libraries')
|
||||||
|
self.current_element = self.vehicles
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.f.write(INDENT2 + '</libraries>\n')
|
etree.indent(self.paramfile)
|
||||||
self.f.write('''</paramfile>\n''')
|
pretty_xml = etree.tostring(self.paramfile, pretty_print=True, encoding='unicode')
|
||||||
|
self.f.write(pretty_xml)
|
||||||
self.f.close()
|
self.f.close()
|
||||||
|
|
||||||
def emit_comment(self, s):
|
def emit_comment(self, s):
|
||||||
self.f.write("<!-- " + s + " -->")
|
self.f.write("<!-- " + s + " -->")
|
||||||
|
|
||||||
def start_libraries(self):
|
def start_libraries(self):
|
||||||
self.f.write(INDENT2 + '</vehicles>\n')
|
self.current_element = self.libraries
|
||||||
self.f.write(INDENT2 + '<libraries>\n')
|
|
||||||
|
|
||||||
def emit(self, g):
|
def emit(self, g):
|
||||||
t = INDENT4 + '''<parameters name=%s>\n''' % quoteattr(g.name) # i.e. ArduPlane
|
xml_parameters = etree.SubElement(self.current_element, 'parameters', name=g.name) # i.e. ArduPlane
|
||||||
|
|
||||||
for param in g.params:
|
for param in g.params:
|
||||||
# Begin our parameter node
|
# Begin our parameter node
|
||||||
if hasattr(param, 'DisplayName'):
|
if hasattr(param, 'DisplayName'):
|
||||||
t += INDENT6 + '<param humanName=%s name=%s' % (quoteattr(param.DisplayName), quoteattr(param.name)) # i.e. ArduPlane (ArduPlane:FOOPARM)
|
xml_param = etree.SubElement(xml_parameters, 'param', humanName=param.DisplayName, name=param.name) # i.e. ArduPlane (ArduPlane:FOOPARM)
|
||||||
else:
|
else:
|
||||||
t += INDENT6 + '<param name=%s' % quoteattr(param.name)
|
xml_param = etree.SubElement(xml_parameters, 'param', name=param.name)
|
||||||
|
|
||||||
if hasattr(param, 'Description'):
|
if hasattr(param, 'Description'):
|
||||||
t += ' documentation=%s' % quoteattr(param.Description) # i.e. parameter docs
|
xml_param.set('documentation', param.Description) # i.e. parameter docs
|
||||||
if hasattr(param, 'User'):
|
if hasattr(param, 'User'):
|
||||||
t += ' user=%s' % quoteattr(param.User) # i.e. Standard or Advanced
|
xml_param.set('user', param.User) # i.e. Standard or Advanced
|
||||||
|
|
||||||
if hasattr(param, 'Calibration'):
|
if hasattr(param, 'Calibration'):
|
||||||
t += ' calibration=%s' % quoteattr(param.Calibration) # i.e. Standard or Advanced
|
xml_param.set('calibration', param.Calibration)
|
||||||
|
|
||||||
t += ">\n"
|
|
||||||
|
|
||||||
# Add values as chidren of this node
|
# Add values as chidren of this node
|
||||||
for field in param.__dict__.keys():
|
for field in param.__dict__.keys():
|
||||||
if field not in ['name', 'DisplayName', 'Description', 'User'] and field in known_param_fields:
|
if field not in ['name', 'DisplayName', 'Description', 'User'] and field in known_param_fields:
|
||||||
if field == 'Values' and Emit.prog_values_field.match(param.__dict__[field]):
|
if field == 'Values' and Emit.prog_values_field.match(param.__dict__[field]):
|
||||||
t += INDENT8 + "<values>\n"
|
xml_values = etree.SubElement(xml_param, 'values')
|
||||||
|
|
||||||
values = (param.__dict__[field]).split(',')
|
values = (param.__dict__[field]).split(',')
|
||||||
for value in values:
|
for value in values:
|
||||||
v = value.split(':')
|
v = value.split(':')
|
||||||
if len(v) != 2:
|
if len(v) != 2:
|
||||||
raise ValueError("Bad value (%s)" % v)
|
raise ValueError("Bad value (%s)" % v)
|
||||||
t += INDENT10 + '''<value code=%s>%s</value>\n''' % (quoteattr(v[0]), escape(v[1])) # i.e. numeric value, string label
|
# i.e. numeric value, string label
|
||||||
|
xml_value = etree.SubElement(xml_values, 'value', code=v[0])
|
||||||
|
xml_value.text = v[1]
|
||||||
|
|
||||||
t += INDENT8 + "</values>\n"
|
|
||||||
elif field == 'Units':
|
elif field == 'Units':
|
||||||
abreviated_units = param.__dict__[field]
|
abreviated_units = param.__dict__[field]
|
||||||
if abreviated_units != '':
|
if abreviated_units != '':
|
||||||
units = known_units[abreviated_units] # use the known_units dictionary to convert the abreviated unit into a full textual one
|
units = known_units[abreviated_units] # use the known_units dictionary to convert the abreviated unit into a full textual one
|
||||||
t += INDENT8 + '''<field name=%s>%s</field>\n''' % (quoteattr(field), escape(abreviated_units)) # i.e. A/s
|
xml_field1 = etree.SubElement(xml_param, 'field', name=field) # i.e. A/s
|
||||||
t += INDENT8 + '''<field name=%s>%s</field>\n''' % (quoteattr('UnitText'), escape(units)) # i.e. ampere per second
|
xml_field1.text = abreviated_units
|
||||||
|
xml_field2 = etree.SubElement(xml_param, 'field', name='UnitText') # i.e. ampere per second
|
||||||
|
xml_field2.text = units
|
||||||
else:
|
else:
|
||||||
t += INDENT8 + '''<field name=%s>%s</field>\n''' % (quoteattr(field), escape(param.__dict__[field])) # i.e. Range: 0 10
|
xml_field = etree.SubElement(xml_param, 'field', name=field)
|
||||||
|
xml_field.text = param.__dict__[field]
|
||||||
|
|
||||||
t += INDENT6 + '''</param>\n'''
|
if xml_param.text is None and not len(xml_param):
|
||||||
t += INDENT4 + '''</parameters>\n'''
|
xml_param.text = '\n' # add </param> on next line in case of empty element.
|
||||||
|
|
||||||
# print t
|
|
||||||
self.f.write(t)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user