Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 167 lines (140 sloc) 5.444 kb
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
1 #!/usr/bin/env python
2
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
3 import sys, os, os.path, re, codecs
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
4
5 BUILD_SOURCE_FILE = os.path.join("src", "lxml", "xmlerror.pxi")
327cdbc @scoder renamed 'include' package to 'includes' because 'include' is a keyword i...
scoder authored
6 BUILD_DEF_FILE = os.path.join("src", "lxml", "includes", "xmlerror.pxd")
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
7
8 if len(sys.argv) < 2 or sys.argv[1].lower() in ('-h', '--help'):
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
9 print("This script generates the constants in file %s" % BUILD_SOURCE_FILE)
10 print("Call as")
11 print(sys.argv[0], "/path/to/libxml2-doc-dir")
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
12 sys.exit(len(sys.argv) > 1)
13
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
14 HTML_DIR = os.path.join(sys.argv[1], 'html')
15 os.stat(HTML_DIR) # raise an error if we can't find it
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
16
17 sys.path.insert(0, 'src')
18 from lxml import etree
19
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
20 # map enum name to Python variable name and alignment for constant name
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
21 ENUM_MAP = {
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
22 'xmlErrorLevel' : ('__ERROR_LEVELS', 'XML_ERR_'),
23 'xmlErrorDomain' : ('__ERROR_DOMAINS', 'XML_FROM_'),
24 'xmlParserErrors' : ('__PARSER_ERROR_TYPES', 'XML_'),
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
25 # 'xmlXPathError' : ('__XPATH_ERROR_TYPES', ''),
26 # 'xmlSchemaValidError' : ('__XMLSCHEMA_ERROR_TYPES', 'XML_'),
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
27 'xmlRelaxNGValidErr' : ('__RELAXNG_ERROR_TYPES', 'XML_'),
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
28 }
29
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
30 ENUM_ORDER = (
31 'xmlErrorLevel',
32 'xmlErrorDomain',
33 'xmlParserErrors',
34 # 'xmlXPathError',
35 # 'xmlSchemaValidError',
36 'xmlRelaxNGValidErr')
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
37
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
38 COMMENT = """
39 # This section is generated by the script '%s'.
40
41 """ % os.path.basename(sys.argv[0])
42
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
43 def split(lines):
44 lines = iter(lines)
45 pre = []
46 for line in lines:
47 pre.append(line)
48 if line.startswith('#') and "BEGIN: GENERATED CONSTANTS" in line:
49 break
50 pre.append('')
51 for line in lines:
52 if line.startswith('#') and "END: GENERATED CONSTANTS" in line:
53 break
54 post = ['', line]
55 post.extend(lines)
56 post.append('')
57 return pre, post
58
59 def regenerate_file(filename, result):
60 # read .pxi source file
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
61 f = codecs.open(filename, 'r', encoding="utf-8")
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
62 pre, post = split(f)
63 f.close()
64
65 # write .pxi source file
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
66 f = codecs.open(filename, 'w', encoding="utf-8")
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
67 f.write(''.join(pre))
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
68 f.write(COMMENT)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
69 f.write('\n'.join(result))
70 f.write(''.join(post))
71 f.close()
72
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
73 collect_text = etree.XPath("string()")
74 find_enums = etree.XPath(
75 "//html:pre[@class = 'programlisting' and contains(text(), 'Enum')]",
76 namespaces = {'html' : 'http://www.w3.org/1999/xhtml'})
77
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
78 def parse_enums(html_dir, html_filename, enum_dict):
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
79 PARSE_ENUM_NAME = re.compile('\s*enum\s+(\w+)\s*{', re.I).match
80 PARSE_ENUM_VALUE = re.compile('\s*=\s+([0-9]+)\s*(?::\s*(.*))?').match
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
81 tree = etree.parse(os.path.join(html_dir, html_filename))
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
82 enums = find_enums(tree)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
83 for enum in enums:
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
84 enum_name = PARSE_ENUM_NAME(collect_text(enum))
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
85 if not enum_name:
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
86 continue
87 enum_name = enum_name.group(1)
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
88 if enum_name not in ENUM_MAP:
89 continue
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
90 print("Found enum", enum_name)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
91 entries = []
92 for child in enum:
93 name = child.text
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
94 match = PARSE_ENUM_VALUE(child.tail)
95 if not match:
96 print("Ignoring enum %s (failed to parse field '%s')" % (
97 enum_name, name))
98 break
99 value, descr = match.groups()
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
100 entries.append((name, int(value), descr))
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
101 else:
102 enum_dict[enum_name] = entries
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
103 return enum_dict
104
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
105 enum_dict = {}
106 parse_enums(HTML_DIR, 'libxml-xmlerror.html', enum_dict)
512dfc5 [svn r3305] r3525@delle: sbehnel | 2008-02-15 15:08:18 +0100
scoder authored
107 #parse_enums(HTML_DIR, 'libxml-xpath.html', enum_dict)
108 #parse_enums(HTML_DIR, 'libxml-xmlschemas.html', enum_dict)
e056284 [svn r3304] r3524@delle: sbehnel | 2008-02-15 13:39:53 +0100
scoder authored
109 parse_enums(HTML_DIR, 'libxml-relaxng.html', enum_dict)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
110
111 # regenerate source files
112 pxi_result = []
113 append_pxi = pxi_result.append
114 pxd_result = []
115 append_pxd = pxd_result.append
116
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
117 append_pxd('cdef extern from "libxml/xmlerror.h":')
118 append_pxi('''\
7d56771 @scoder minor simplifications in error constant setup code, regenerated constant...
scoder authored
119 # Constants are stored in tuples of strings, for which Cython generates very
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
120 # efficient setup code. To parse them, iterate over the tuples and parse each
8c171e0 [svn r2291] comment
scoder authored
121 # line in each string independently. Tuples of strings (instead of a plain
122 # string) are required as some C-compilers of a certain well-known OS vendor
123 # cannot handle strings that are a few thousand bytes in length.
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
124 ''')
125
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
126 ctypedef_indent = ' '*4
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
127 constant_indent = ctypedef_indent*2
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
128
129 for enum_name in ENUM_ORDER:
130 constants = enum_dict[enum_name]
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
131 pxi_name, prefix = ENUM_MAP[enum_name]
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
132
133 append_pxd(ctypedef_indent + 'ctypedef enum %s:' % enum_name)
7d56771 @scoder minor simplifications in error constant setup code, regenerated constant...
scoder authored
134 append_pxi('cdef object %s = (u"""\\' % pxi_name)
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
135
136 prefix_len = len(prefix)
137 length = 2 # each string ends with '\n\0'
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
138 for name, val, descr in constants:
7d56771 @scoder minor simplifications in error constant setup code, regenerated constant...
scoder authored
139 if descr and descr != str(val):
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
140 line = '%-50s = %7d # %s' % (name, val, descr)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
141 else:
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
142 line = '%-50s = %7d' % (name, val)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
143 append_pxd(constant_indent + line)
144
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
145 if name[:prefix_len] == prefix and len(name) > prefix_len:
146 name = name[prefix_len:]
147 line = '%s=%d' % (name, val)
148 if length + len(line) >= 2040: # max string length in MSVC is 2048
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
149 append_pxi('""",')
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
150 append_pxi('u"""\\')
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
151 length = 2 # each string ends with '\n\0'
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
152 append_pxi(line)
0790a8f [svn r1390] update-error-constants.py: strip prefixes and indentation be...
scoder authored
153 length += len(line) + 2 # + '\n\0'
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
154
155 append_pxd('')
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
156 append_pxi('""",)')
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
157 append_pxi('')
158
159 # write source files
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
160 print("Updating file %s" % BUILD_SOURCE_FILE)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
161 regenerate_file(BUILD_SOURCE_FILE, pxi_result)
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
162
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
163 print("Updating file %s" % BUILD_DEF_FILE)
938c96c [svn r1379] support regenerating constants in xmlerror.px? automatically...
scoder authored
164 regenerate_file(BUILD_DEF_FILE, pxd_result)
12b4454 [svn r1389] store error constant strings in tuples instead of using stri...
scoder authored
165
a570821 [svn r3680] r4226@delle: sbehnel | 2008-05-19 00:52:10 +0200
scoder authored
166 print("Done")
Something went wrong with that request. Please try again.