mirror of https://github.com/ArduPilot/ardupilot
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 lxml import etree
|
||||
import emitter
|
||||
|
||||
class XMLEmitter(emitter.Emitter):
|
||||
def preface(self):
|
||||
return """<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Dynamically generated list of documented logfile messages (generated by parse.py) -->
|
||||
<loggermessagefile>
|
||||
"""
|
||||
|
||||
def postface(self):
|
||||
return "</loggermessagefile>"
|
||||
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):
|
||||
self.start()
|
||||
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:
|
||||
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:
|
||||
print(' <description>%s</description>' %
|
||||
docco.description, file=self.fh)
|
||||
print(' <fields>', file=self.fh)
|
||||
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:
|
||||
print(' <field name="%s">' % f, file=self.fh)
|
||||
xml_field = etree.SubElement(xml_fields, 'field', name=f)
|
||||
if "description" in docco.fields[f]:
|
||||
print(' <description>%s</description>' %
|
||||
docco.fields[f]["description"], file=self.fh)
|
||||
xml_description2 = etree.SubElement(xml_field, 'description')
|
||||
xml_description2.text = docco.fields[f]["description"]
|
||||
if "bits" in docco.fields[f]:
|
||||
print(' <bits>%s</bits>' %
|
||||
docco.fields[f]["bits"], file=self.fh)
|
||||
print(' </field>', file=self.fh)
|
||||
|
||||
print(' </fields>', file=self.fh)
|
||||
|
||||
print(' </logformat>', file=self.fh)
|
||||
|
||||
print("", file=self.fh)
|
||||
xml_bits = etree.SubElement(xml_field, 'bits')
|
||||
xml_bits.text = docco.fields[f]["bits"]
|
||||
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.
|
||||
self.stop()
|
||||
|
||||
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()
|
||||
|
|
|
@ -1,16 +1,10 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from xml.sax.saxutils import escape, quoteattr
|
||||
from lxml import etree
|
||||
|
||||
from emit import Emit
|
||||
from param import known_param_fields, known_units
|
||||
|
||||
INDENT2 = " "
|
||||
INDENT4 = " "
|
||||
INDENT6 = " "
|
||||
INDENT8 = " "
|
||||
INDENT10 = " "
|
||||
|
||||
# Emit APM documentation in an machine readable XML format
|
||||
class XmlEmit(Emit):
|
||||
def __init__(self):
|
||||
|
@ -21,66 +15,66 @@ class XmlEmit(Emit):
|
|||
<!-- Dynamically generated list of documented parameters (generated by param_parse.py) -->
|
||||
'''
|
||||
self.f.write(self.preamble)
|
||||
self.f.write('''<paramfile>
|
||||
<vehicles>\n''')
|
||||
self.paramfile = etree.Element('paramfile')
|
||||
self.vehicles = etree.SubElement(self.paramfile, 'vehicles')
|
||||
self.libraries = etree.SubElement(self.paramfile, 'libraries')
|
||||
self.current_element = self.vehicles
|
||||
|
||||
def close(self):
|
||||
self.f.write(INDENT2 + '</libraries>\n')
|
||||
self.f.write('''</paramfile>\n''')
|
||||
etree.indent(self.paramfile)
|
||||
pretty_xml = etree.tostring(self.paramfile, pretty_print=True, encoding='unicode')
|
||||
self.f.write(pretty_xml)
|
||||
self.f.close()
|
||||
|
||||
def emit_comment(self, s):
|
||||
self.f.write("<!-- " + s + " -->")
|
||||
|
||||
def start_libraries(self):
|
||||
self.f.write(INDENT2 + '</vehicles>\n')
|
||||
self.f.write(INDENT2 + '<libraries>\n')
|
||||
self.current_element = self.libraries
|
||||
|
||||
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:
|
||||
# Begin our parameter node
|
||||
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:
|
||||
t += INDENT6 + '<param name=%s' % quoteattr(param.name)
|
||||
xml_param = etree.SubElement(xml_parameters, 'param', name=param.name)
|
||||
|
||||
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'):
|
||||
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'):
|
||||
t += ' calibration=%s' % quoteattr(param.Calibration) # i.e. Standard or Advanced
|
||||
|
||||
t += ">\n"
|
||||
xml_param.set('calibration', param.Calibration)
|
||||
|
||||
# Add values as chidren of this node
|
||||
for field in param.__dict__.keys():
|
||||
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]):
|
||||
t += INDENT8 + "<values>\n"
|
||||
|
||||
xml_values = etree.SubElement(xml_param, 'values')
|
||||
values = (param.__dict__[field]).split(',')
|
||||
for value in values:
|
||||
v = value.split(':')
|
||||
if len(v) != 2:
|
||||
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':
|
||||
abreviated_units = param.__dict__[field]
|
||||
if abreviated_units != '':
|
||||
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
|
||||
t += INDENT8 + '''<field name=%s>%s</field>\n''' % (quoteattr('UnitText'), escape(units)) # i.e. ampere per second
|
||||
xml_field1 = etree.SubElement(xml_param, 'field', name=field) # i.e. A/s
|
||||
xml_field1.text = abreviated_units
|
||||
xml_field2 = etree.SubElement(xml_param, 'field', name='UnitText') # i.e. ampere per second
|
||||
xml_field2.text = units
|
||||
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'''
|
||||
t += INDENT4 + '''</parameters>\n'''
|
||||
|
||||
# print t
|
||||
self.f.write(t)
|
||||
if xml_param.text is None and not len(xml_param):
|
||||
xml_param.text = '\n' # add </param> on next line in case of empty element.
|
||||
|
|
Loading…
Reference in New Issue