tools: Use ElementTree to write XML in generate-docs-nm-settings-docs-gir.py

Instead of manually writting XML line by line.
Quoting is automatic.
Makes it much easier to modify. (just add new elements)

Generated XML not indented at the moment.
This commit is contained in:
Igor Ponomarev
2022-05-30 18:40:01 +03:00
committed by Thomas Haller
parent a2298d31c0
commit f00e90923c

View File

@@ -4,17 +4,15 @@
# Copyright (C) 2009 - 2017 Red Hat, Inc. # Copyright (C) 2009 - 2017 Red Hat, Inc.
# #
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
import xml.etree.ElementTree as ET
import argparse import argparse
import os import os
import gi import gi
import xml.sax.saxutils as saxutils
import re import re
gi.require_version("GIRepository", "2.0") gi.require_version("GIRepository", "2.0")
from gi.repository import GIRepository from gi.repository import GIRepository
import xml.etree.ElementTree as ET
try: try:
libs = os.environ["LD_LIBRARY_PATH"].split(":") libs = os.environ["LD_LIBRARY_PATH"].split(":")
libs.reverse() libs.reverse()
@@ -186,13 +184,8 @@ def settings_sort_key(x):
return (x_prefix != "setting_connection", x_prefix) return (x_prefix != "setting_connection", x_prefix)
def xml_quoteattr(val):
return saxutils.quoteattr(str(val))
def main(gir_path_str, output_path_str): def main(gir_path_str, output_path_str):
girxml = ET.parse(gir_path_str).getroot() girxml = ET.parse(gir_path_str).getroot()
outfile = open(output_path_str, mode="w")
basexml = girxml.find('./gi:namespace/gi:class[@name="Setting"]', ns_map) basexml = girxml.find('./gi:namespace/gi:class[@name="Setting"]', ns_map)
settings = girxml.findall('./gi:namespace/gi:class[@parent="Setting"]', ns_map) settings = girxml.findall('./gi:namespace/gi:class[@parent="Setting"]', ns_map)
@@ -205,14 +198,8 @@ def main(gir_path_str, output_path_str):
init_constants(girxml, settings) init_constants(girxml, settings)
outfile.write( nm_settings_docs_element = ET.Element("nm-setting-docs")
"""<?xml version=\"1.0\"?> docs_gir = ET.ElementTree(nm_settings_docs_element)
<!DOCTYPE nm-setting-docs [
<!ENTITY quot "&#34;">
]>
<nm-setting-docs>
"""
)
for settingxml in settings: for settingxml in settings:
if "abstract" in settingxml.attrib: if "abstract" in settingxml.attrib:
@@ -227,13 +214,14 @@ def main(gir_path_str, output_path_str):
"%s needs a gtk-doc block with one-line description" "%s needs a gtk-doc block with one-line description"
% setting.props.name % setting.props.name
) )
outfile.write( setting_element = ET.SubElement(
' <setting name="%s" description=%s name_upper="%s" >\n' nm_settings_docs_element,
% ( "setting",
setting.props.name, attrib={
xml_quoteattr(class_desc), "name": setting.props.name,
get_setting_name_define(settingxml), "description": class_desc,
) "name_upper": get_setting_name_define(settingxml),
},
) )
setting_properties = { setting_properties = {
@@ -263,25 +251,28 @@ def main(gir_path_str, output_path_str):
% (setting.props.name, prop) % (setting.props.name, prop)
) )
default_value_as_xml = "" property_attributes = {
if default_value is not None: "name": prop,
default_value_as_xml = " default=%s" % (xml_quoteattr(default_value)) "name_upper": prop_upper,
"type": value_type,
"description": value_desc,
}
outfile.write( if default_value is not None:
' <property name="%s" name_upper="%s" type="%s"%s description=%s />\n' property_attributes["default"] = str(default_value)
% (
prop, ET.SubElement(
prop_upper, setting_element,
value_type, "property",
default_value_as_xml, attrib=property_attributes,
xml_quoteattr(value_desc),
)
) )
outfile.write(" </setting>\n") with open(output_path_str, mode="w") as outfile:
docs_gir.write(
outfile.write("</nm-setting-docs>\n") outfile,
outfile.close() encoding="unicode",
xml_declaration=True,
)
if __name__ == "__main__": if __name__ == "__main__":