1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#!/usr/bin/env python3
import operator
import os.path
import pathlib
import sys
import xml.etree.ElementTree as ET
BUILD_SOURCE_FILE = os.path.join("src", "lxml", "xmlerror.pxi")
BUILD_DEF_FILE = os.path.join("src", "lxml", "includes", "xmlerror.pxd")
# map enum name to Python variable name and alignment for constant name
ENUM_MAP = {
'xmlErrorLevel' : ('__ERROR_LEVELS', 'XML_ERR_'),
'xmlErrorDomain' : ('__ERROR_DOMAINS', 'XML_FROM_'),
'xmlParserErrors' : ('__PARSER_ERROR_TYPES', 'XML_'),
# 'xmlXPathError' : ('__XPATH_ERROR_TYPES', ''),
# 'xmlSchemaValidError' : ('__XMLSCHEMA_ERROR_TYPES', 'XML_'),
'xmlRelaxNGValidErr' : ('__RELAXNG_ERROR_TYPES', 'XML_'),
}
ENUM_ORDER = (
'xmlErrorLevel',
'xmlErrorDomain',
'xmlParserErrors',
# 'xmlXPathError',
# 'xmlSchemaValidError',
'xmlRelaxNGValidErr')
COMMENT = """
# This section is generated by the script '%s'.
""" % os.path.basename(sys.argv[0])
def split(lines):
lines = iter(lines)
pre = []
for line in lines:
pre.append(line)
if line.startswith('#') and "BEGIN: GENERATED CONSTANTS" in line:
break
pre.append('')
old = []
for line in lines:
if line.startswith('#') and "END: GENERATED CONSTANTS" in line:
break
old.append(line.rstrip('\n'))
post = ['', line]
post.extend(lines)
post.append('')
return pre, old, post
def regenerate_file(filename, result):
new = COMMENT + '\n'.join(result)
# read .pxi source file
with open(filename, 'r', encoding="utf-8") as f:
pre, old, post = split(f)
if new.strip() == '\n'.join(old).strip():
# no changes
return False
# write .pxi source file
with open(filename, 'w', encoding="utf-8") as f:
f.write(''.join(pre))
f.write(new)
f.write(''.join(post))
return True
def parse_from_api_xml(api_xml_path, enum_dict):
tree = ET.parse(str(api_xml_path))
for enum in tree.iterfind('symbols/enum'):
enum_type = enum.get('type')
if enum_type not in ENUM_MAP:
continue
entries = enum_dict.get(enum_type)
if not entries:
print("Found enum", enum_type)
entries = enum_dict[enum_type] = []
entries.append((
enum.get('name'),
int(enum.get('value')),
enum.get('info', '').strip(),
))
def parse_from_doxygen_xml(doxygen_xml_path, enum_dict):
for xml_file in doxygen_xml_path.glob("*_8h.xml"):
for _, compound in ET.iterparse(xml_file):
if compound.tag != 'compounddef':
continue
if not compound.findtext('compoundname', '').endswith('.h'):
break
for memberdef in compound.iterfind('sectiondef[@kind = "enum"]/memberdef'):
enum_type = memberdef.findtext('name')
if enum_type not in ENUM_MAP:
continue
entries = enum_dict.get(enum_type)
if not entries:
print("Found enum", enum_type)
entries = enum_dict[enum_type] = []
enum_value = 0
for enum in memberdef.iterfind('enumvalue'):
enum_value = int(enum.findtext('initializer', '').lstrip('= ') or enum_value + 1)
entries.append((
enum.findtext('name'),
enum_value,
enum.findtext('briefdescription/para', '').rstrip('. ').strip(),
))
def generate_source_files(enum_dict):
pxi_result = []
append_pxi = pxi_result.append
pxd_result = []
append_pxd = pxd_result.append
append_pxd('cdef extern from "libxml/xmlerror.h":')
ctypedef_indent = ' '*4
constant_indent = ctypedef_indent*2
for enum_name in ENUM_ORDER:
constants = enum_dict[enum_name]
constants.sort(key=operator.itemgetter(1))
pxi_name, prefix = ENUM_MAP[enum_name]
append_pxd(ctypedef_indent + 'ctypedef enum %s:' % enum_name)
append_pxi('cdef object %s = """\\' % pxi_name)
prefix_len = len(prefix)
length = 2 # each string ends with '\n\0'
for name, val, descr in constants:
if descr and descr != str(val):
line = '%-50s = %7d # %s' % (name, val, descr)
else:
line = '%-50s = %7d' % (name, val)
append_pxd(constant_indent + line)
if name[:prefix_len] == prefix and len(name) > prefix_len:
name = name[prefix_len:]
line = '%s=%d' % (name, val)
append_pxi(line)
length += len(line) + 2 # + '\n\0'
append_pxd('')
append_pxi('"""')
append_pxi('')
# write source files
print("Updating file %s" % BUILD_SOURCE_FILE)
updated = regenerate_file(BUILD_SOURCE_FILE, pxi_result)
if not updated:
print("No changes.")
print("Updating file %s" % BUILD_DEF_FILE)
updated = regenerate_file(BUILD_DEF_FILE, pxd_result)
if not updated:
print("No changes.")
print("Done")
def main(doc_dir):
doc_path = pathlib.Path(doc_dir)
api_xml_path = doc_path / 'libxml2-api.xml'
doxygen_xml_path = doc_path / 'xml'
enum_dict = {}
if api_xml_path.exists():
parse_from_api_xml(api_xml_path, enum_dict)
elif doxygen_xml_path.exists():
parse_from_doxygen_xml(doxygen_xml_path, enum_dict)
else:
print(f"XML files for libxml2 API not found - did you generate the libxml2 documentation in {doc_dir}?")
return
generate_source_files(enum_dict)
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1].lower() in ('-h', '--help'):
print("This script generates the constants in file %s" % BUILD_SOURCE_FILE)
print("Call as")
print(sys.argv[0], "/path/to/libxml2-doc-dir")
sys.exit(len(sys.argv) > 1)
main(sys.argv[1])
|