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