3 from __future__ import print_function, absolute_import
5 import sys, os, os.path, re, codecs
7 BUILD_SOURCE_FILE = os.path.join("src", "lxml", "xmlerror.pxi")
8 BUILD_DEF_FILE = os.path.join("src", "lxml", "includes", "xmlerror.pxd")
10 if len(sys.argv) < 2 or sys.argv[1].lower() in ('-h', '--help'):
11 print("This script generates the constants in file %s" % BUILD_SOURCE_FILE)
13 print(sys.argv[0], "/path/to/libxml2-doc-dir")
14 sys.exit(len(sys.argv) > 1)
16 HTML_DIR = os.path.join(sys.argv[1], 'html')
17 os.stat(HTML_DIR) # raise an error if we can't find it
19 sys.path.insert(0, 'src')
20 from lxml import etree
22 # map enum name to Python variable name and alignment for constant name
24 'xmlErrorLevel' : ('__ERROR_LEVELS', 'XML_ERR_'),
25 'xmlErrorDomain' : ('__ERROR_DOMAINS', 'XML_FROM_'),
26 'xmlParserErrors' : ('__PARSER_ERROR_TYPES', 'XML_'),
27 # 'xmlXPathError' : ('__XPATH_ERROR_TYPES', ''),
28 # 'xmlSchemaValidError' : ('__XMLSCHEMA_ERROR_TYPES', 'XML_'),
29 'xmlRelaxNGValidErr' : ('__RELAXNG_ERROR_TYPES', 'XML_'),
37 # 'xmlSchemaValidError',
41 # This section is generated by the script '%s'.
43 """ % os.path.basename(sys.argv[0])
50 if line.startswith('#') and "BEGIN: GENERATED CONSTANTS" in line:
54 if line.startswith('#') and "END: GENERATED CONSTANTS" in line:
61 def regenerate_file(filename, result):
62 # read .pxi source file
63 f = codecs.open(filename, 'r', encoding="utf-8")
67 # write .pxi source file
68 f = codecs.open(filename, 'w', encoding="utf-8")
71 f.write('\n'.join(result))
72 f.write(''.join(post))
75 collect_text = etree.XPath("string()")
76 find_enums = etree.XPath(
77 "//html:pre[@class = 'programlisting' and contains(text(), 'Enum')]",
78 namespaces = {'html' : 'http://www.w3.org/1999/xhtml'})
80 def parse_enums(html_dir, html_filename, enum_dict):
81 PARSE_ENUM_NAME = re.compile(r'\s*enum\s+(\w+)\s*{', re.I).match
82 PARSE_ENUM_VALUE = re.compile(r'\s*=\s+([0-9]+)\s*(?::\s*(.*))?').match
83 tree = etree.parse(os.path.join(html_dir, html_filename))
84 enums = find_enums(tree)
86 enum_name = PARSE_ENUM_NAME(collect_text(enum))
89 enum_name = enum_name.group(1)
90 if enum_name not in ENUM_MAP:
92 print("Found enum", enum_name)
96 match = PARSE_ENUM_VALUE(child.tail)
98 print("Ignoring enum %s (failed to parse field '%s')" % (
101 value, descr = match.groups()
102 entries.append((name, int(value), descr))
104 enum_dict[enum_name] = entries
108 parse_enums(HTML_DIR, 'libxml-xmlerror.html', enum_dict)
109 #parse_enums(HTML_DIR, 'libxml-xpath.html', enum_dict)
110 #parse_enums(HTML_DIR, 'libxml-xmlschemas.html', enum_dict)
111 parse_enums(HTML_DIR, 'libxml-relaxng.html', enum_dict)
113 # regenerate source files
115 append_pxi = pxi_result.append
117 append_pxd = pxd_result.append
119 append_pxd('cdef extern from "libxml/xmlerror.h":')
121 ctypedef_indent = ' '*4
122 constant_indent = ctypedef_indent*2
124 for enum_name in ENUM_ORDER:
125 constants = enum_dict[enum_name]
126 pxi_name, prefix = ENUM_MAP[enum_name]
128 append_pxd(ctypedef_indent + 'ctypedef enum %s:' % enum_name)
129 append_pxi('cdef object %s = """\\' % pxi_name)
131 prefix_len = len(prefix)
132 length = 2 # each string ends with '\n\0'
133 for name, val, descr in constants:
134 if descr and descr != str(val):
135 line = '%-50s = %7d # %s' % (name, val, descr)
137 line = '%-50s = %7d' % (name, val)
138 append_pxd(constant_indent + line)
140 if name[:prefix_len] == prefix and len(name) > prefix_len:
141 name = name[prefix_len:]
142 line = '%s=%d' % (name, val)
144 length += len(line) + 2 # + '\n\0'
151 print("Updating file %s" % BUILD_SOURCE_FILE)
152 regenerate_file(BUILD_SOURCE_FILE, pxi_result)
154 print("Updating file %s" % BUILD_DEF_FILE)
155 regenerate_file(BUILD_DEF_FILE, pxd_result)