blob: 603206525127de30e73df2fbe68a8f9b4fb25868 [file] [log] [blame]
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001#!/usr/bin/python -u
2#
3# generate python wrappers from the XML API description
4#
5
6functions = {}
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00007enums = {} # { enumType: { enumConstant: enumValue } }
Daniel Veillardd2897fd2002-01-30 16:37:32 +00008
Daniel Veillard0fea6f42002-02-22 22:51:13 +00009import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +000010import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000011
12#######################################################################
13#
14# That part if purely the API acquisition phase from the
15# XML API description
16#
17#######################################################################
18import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000019import xmllib
20try:
21 import sgmlop
22except ImportError:
23 sgmlop = None # accelerator not available
24
25debug = 0
26
27if sgmlop:
28 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000029 """sgmlop based XML parser. this is typically 15x faster
30 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000031
Daniel Veillard01a6d412002-02-11 18:42:20 +000032 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000033
Daniel Veillard01a6d412002-02-11 18:42:20 +000034 # setup callbacks
35 self.finish_starttag = target.start
36 self.finish_endtag = target.end
37 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000038
Daniel Veillard01a6d412002-02-11 18:42:20 +000039 # activate parser
40 self.parser = sgmlop.XMLParser()
41 self.parser.register(self)
42 self.feed = self.parser.feed
43 self.entity = {
44 "amp": "&", "gt": ">", "lt": "<",
45 "apos": "'", "quot": '"'
46 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000047
Daniel Veillard01a6d412002-02-11 18:42:20 +000048 def close(self):
49 try:
50 self.parser.close()
51 finally:
52 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000053
Daniel Veillard01a6d412002-02-11 18:42:20 +000054 def handle_entityref(self, entity):
55 # <string> entity
56 try:
57 self.handle_data(self.entity[entity])
58 except KeyError:
59 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000060
61else:
62 FastParser = None
63
64
65class SlowParser(xmllib.XMLParser):
66 """slow but safe standard parser, based on the XML parser in
67 Python's standard library."""
68
69 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000070 self.unknown_starttag = target.start
71 self.handle_data = target.data
72 self.unknown_endtag = target.end
73 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000074
75def getparser(target = None):
76 # get the fastest available parser, and attach it to an
77 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000078 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000079 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000080 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000081 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000082 return SlowParser(target), target
83
84class docParser:
85 def __init__(self):
86 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000087 self._data = []
88 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000089
90 def close(self):
91 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000092 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000093
94 def getmethodname(self):
95 return self._methodname
96
97 def data(self, text):
98 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000099 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000100 self._data.append(text)
101
102 def start(self, tag, attrs):
103 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000104 print "start %s, %s" % (tag, attrs)
105 if tag == 'function':
106 self._data = []
107 self.in_function = 1
108 self.function = None
109 self.function_args = []
110 self.function_descr = None
111 self.function_return = None
112 self.function_file = None
113 if attrs.has_key('name'):
114 self.function = attrs['name']
115 if attrs.has_key('file'):
116 self.function_file = attrs['file']
117 elif tag == 'info':
118 self._data = []
119 elif tag == 'arg':
120 if self.in_function == 1:
121 self.function_arg_name = None
122 self.function_arg_type = None
123 self.function_arg_info = None
124 if attrs.has_key('name'):
125 self.function_arg_name = attrs['name']
126 if attrs.has_key('type'):
127 self.function_arg_type = attrs['type']
128 if attrs.has_key('info'):
129 self.function_arg_info = attrs['info']
130 elif tag == 'return':
131 if self.in_function == 1:
132 self.function_return_type = None
133 self.function_return_info = None
134 self.function_return_field = None
135 if attrs.has_key('type'):
136 self.function_return_type = attrs['type']
137 if attrs.has_key('info'):
138 self.function_return_info = attrs['info']
139 if attrs.has_key('field'):
140 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000141 elif tag == 'enum':
142 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000143
144 def end(self, tag):
145 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000146 print "end %s" % tag
147 if tag == 'function':
148 if self.function != None:
149 function(self.function, self.function_descr,
150 self.function_return, self.function_args,
151 self.function_file)
152 self.in_function = 0
153 elif tag == 'arg':
154 if self.in_function == 1:
155 self.function_args.append([self.function_arg_name,
156 self.function_arg_type,
157 self.function_arg_info])
158 elif tag == 'return':
159 if self.in_function == 1:
160 self.function_return = [self.function_return_type,
161 self.function_return_info,
162 self.function_return_field]
163 elif tag == 'info':
164 str = ''
165 for c in self._data:
166 str = str + c
167 if self.in_function == 1:
168 self.function_descr = str
169
170
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000171def function(name, desc, ret, args, file):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000172 functions[name] = (desc, ret, args, file)
173
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000174def enum(type, name, value):
175 if not enums.has_key(type):
176 enums[type] = {}
177 enums[type][name] = value
178
Daniel Veillard1971ee22002-01-31 20:29:19 +0000179#######################################################################
180#
181# Some filtering rukes to drop functions/types which should not
182# be exposed as-is on the Python interface
183#
184#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000185
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000186skipped_modules = {
187 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000188 'DOCBparser': None,
189 'SAX': None,
190 'hash': None,
191 'list': None,
192 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000193# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000194}
195skipped_types = {
196 'int *': "usually a return type",
197 'xmlSAXHandlerPtr': "not the proper interface for SAX",
198 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000199 'xmlRMutexPtr': "thread specific, skipped",
200 'xmlMutexPtr': "thread specific, skipped",
201 'xmlGlobalStatePtr': "thread specific, skipped",
202 'xmlListPtr': "internal representation not suitable for python",
203 'xmlBufferPtr': "internal representation not suitable for python",
204 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000205}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000206
207#######################################################################
208#
209# Table of remapping to/from the python type or class to the C
210# counterpart.
211#
212#######################################################################
213
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000214py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000215 'void': (None, None, None, None),
216 'int': ('i', None, "int", "int"),
217 'long': ('i', None, "int", "int"),
218 'double': ('d', None, "double", "double"),
219 'unsigned int': ('i', None, "int", "int"),
220 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000221 'unsigned char *': ('z', None, "charPtr", "char *"),
222 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000223 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000224 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000225 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000226 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
237 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
238 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
239 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
240 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
241 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000242 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
243 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
244 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
245 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
246 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
247 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
248 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
249 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
250 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
251 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
252 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
253 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000254 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
257 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
258 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
259 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
260 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
261 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
262 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
263 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
264 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
265 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000266 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
267 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000268 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000269 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
270 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
271 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
272 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000273 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000274 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
275 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000276 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000277 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000278 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
279 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000280 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000281 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000282 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000283 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
284 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
285 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000286 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
287 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
288 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000289}
290
291py_return_types = {
292 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000293}
294
295unknown_types = {}
296
Daniel Veillard1971ee22002-01-31 20:29:19 +0000297#######################################################################
298#
299# This part writes the C <-> Python stubs libxml2-py.[ch] and
300# the table libxml2-export.c to add when registrering the Python module
301#
302#######################################################################
303
Daniel Veillard263ec862004-10-04 10:26:54 +0000304# Class methods which are written by hand in libxml.c but the Python-level
305# code is still automatically generated (so they are not in skip_function()).
306skip_impl = (
307 'xmlSaveFileTo',
308 'xmlSaveFormatFileTo',
309)
310
Daniel Veillard1971ee22002-01-31 20:29:19 +0000311def skip_function(name):
312 if name[0:12] == "xmlXPathWrap":
313 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000314 if name == "xmlFreeParserCtxt":
315 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000316 if name == "xmlCleanupParser":
317 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000318 if name == "xmlFreeTextReader":
319 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000320# if name[0:11] == "xmlXPathNew":
321# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000322 # the next function is defined in libxml.c
323 if name == "xmlRelaxNGFreeValidCtxt":
324 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000325#
326# Those are skipped because the Const version is used of the bindings
327# instead.
328#
329 if name == "xmlTextReaderBaseUri":
330 return 1
331 if name == "xmlTextReaderLocalName":
332 return 1
333 if name == "xmlTextReaderName":
334 return 1
335 if name == "xmlTextReaderNamespaceUri":
336 return 1
337 if name == "xmlTextReaderPrefix":
338 return 1
339 if name == "xmlTextReaderXmlLang":
340 return 1
341 if name == "xmlTextReaderValue":
342 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000343 if name == "xmlOutputBufferClose": # handled by by the superclass
344 return 1
345 if name == "xmlOutputBufferFlush": # handled by by the superclass
346 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000347 if name == "xmlErrMemory":
348 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000349
350 if name == "xmlValidBuildContentModel":
351 return 1
352 if name == "xmlValidateElementDecl":
353 return 1
354 if name == "xmlValidateAttributeDecl":
355 return 1
356
Daniel Veillard1971ee22002-01-31 20:29:19 +0000357 return 0
358
Daniel Veillard96fe0952002-01-30 20:52:23 +0000359def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000360 global py_types
361 global unknown_types
362 global functions
363 global skipped_modules
364
365 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000366 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000367 except:
368 print "failed to get function %s infos"
369 return
370
371 if skipped_modules.has_key(file):
372 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000373 if skip_function(name) == 1:
374 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000375 if name in skip_impl:
376 # Don't delete the function entry in the caller.
377 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000378
379 c_call = "";
380 format=""
381 format_args=""
382 c_args=""
383 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000384 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000385 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000386 # This should be correct
387 if arg[1][0:6] == "const ":
388 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000389 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000390 if py_types.has_key(arg[1]):
391 (f, t, n, c) = py_types[arg[1]]
392 if f != None:
393 format = format + f
394 if t != None:
395 format_args = format_args + ", &pyobj_%s" % (arg[0])
396 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
397 c_convert = c_convert + \
398 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
399 arg[1], t, arg[0]);
400 else:
401 format_args = format_args + ", &%s" % (arg[0])
402 if c_call != "":
403 c_call = c_call + ", ";
404 c_call = c_call + "%s" % (arg[0])
405 else:
406 if skipped_types.has_key(arg[1]):
407 return 0
408 if unknown_types.has_key(arg[1]):
409 lst = unknown_types[arg[1]]
410 lst.append(name)
411 else:
412 unknown_types[arg[1]] = [name]
413 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000414 if format != "":
415 format = format + ":%s" % (name)
416
417 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000418 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000419 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
420 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
421 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000422 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
423 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000424 else:
425 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
426 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000427 else:
428 c_call = "\n %s(%s);\n" % (name, c_call);
429 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000430 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000431 (f, t, n, c) = py_types[ret[0]]
432 c_return = " %s c_retval;\n" % (ret[0])
433 if file == "python_accessor" and ret[2] != None:
434 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
435 else:
436 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
437 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
438 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000439 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000440 (f, t, n, c) = py_return_types[ret[0]]
441 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000442 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000443 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
444 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000445 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000446 if skipped_types.has_key(ret[0]):
447 return 0
448 if unknown_types.has_key(ret[0]):
449 lst = unknown_types[ret[0]]
450 lst.append(name)
451 else:
452 unknown_types[ret[0]] = [name]
453 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000454
Daniel Veillard42766c02002-08-22 20:52:17 +0000455 if file == "debugXML":
456 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
457 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
458 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000459 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000460 include.write("#ifdef LIBXML_HTML_ENABLED\n");
461 export.write("#ifdef LIBXML_HTML_ENABLED\n");
462 output.write("#ifdef LIBXML_HTML_ENABLED\n");
463 elif file == "c14n":
464 include.write("#ifdef LIBXML_C14N_ENABLED\n");
465 export.write("#ifdef LIBXML_C14N_ENABLED\n");
466 output.write("#ifdef LIBXML_C14N_ENABLED\n");
467 elif file == "xpathInternals" or file == "xpath":
468 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
469 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
470 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
471 elif file == "xpointer":
472 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
473 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
474 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
475 elif file == "xinclude":
476 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
477 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
478 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000479 elif file == "xmlregexp":
480 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
481 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
482 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000483 elif file == "xmlschemas" or file == "xmlschemastypes" or \
484 file == "relaxng":
485 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
486 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
487 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000488
Daniel Veillard96fe0952002-01-30 20:52:23 +0000489 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000490 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000491
Daniel Veillardd2379012002-03-15 22:24:56 +0000492 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000493 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000494
495 if file == "python":
496 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000497 if name[0:4] == "html":
498 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
499 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
500 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000501 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000502 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000503 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000504 if name[0:4] == "html":
505 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
506 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
507 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000508 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000509
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000510 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000511 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
512 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000513 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000514 output.write(" ATTRIBUTE_UNUSED")
515 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000516 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000517 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000518 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000519 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000520 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000521 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000522 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000523 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000524 (format, format_args))
525 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000526 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000527 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000528
529 output.write(c_call)
530 output.write(ret_convert)
531 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000532 if file == "debugXML":
533 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
534 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
535 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000536 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000537 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
538 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
539 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
540 elif file == "c14n":
541 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
542 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
543 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
544 elif file == "xpathInternals" or file == "xpath":
545 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
546 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
547 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
548 elif file == "xpointer":
549 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
550 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
551 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
552 elif file == "xinclude":
553 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
554 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
555 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000556 elif file == "xmlregexp":
557 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
558 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
559 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000560 elif file == "xmlschemas" or file == "xmlschemastypes" or \
561 file == "relaxng":
562 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
563 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
564 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000565 return 1
566
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000567def buildStubs():
568 global py_types
569 global py_return_types
570 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000571
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000572 try:
573 f = open("libxml2-api.xml")
574 data = f.read()
575 (parser, target) = getparser()
576 parser.feed(data)
577 parser.close()
578 except IOError, msg:
579 try:
580 f = open("../doc/libxml2-api.xml")
581 data = f.read()
582 (parser, target) = getparser()
583 parser.feed(data)
584 parser.close()
585 except IOError, msg:
586 print file, ":", msg
587 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000588
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000589 n = len(functions.keys())
590 print "Found %d functions in libxml2-api.xml" % (n)
591
592 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
593 try:
594 f = open("libxml2-python-api.xml")
595 data = f.read()
596 (parser, target) = getparser()
597 parser.feed(data)
598 parser.close()
599 except IOError, msg:
600 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000601
602
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000603 print "Found %d functions in libxml2-python-api.xml" % (
604 len(functions.keys()) - n)
605 nb_wrap = 0
606 failed = 0
607 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000608
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000609 include = open("libxml2-py.h", "w")
610 include.write("/* Generated */\n\n")
611 export = open("libxml2-export.c", "w")
612 export.write("/* Generated */\n\n")
613 wrapper = open("libxml2-py.c", "w")
614 wrapper.write("/* Generated */\n\n")
615 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000616 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000617 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000618 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000619 wrapper.write("#include \"libxml_wrap.h\"\n")
620 wrapper.write("#include \"libxml2-py.h\"\n\n")
621 for function in functions.keys():
622 ret = print_function_wrapper(function, wrapper, export, include)
623 if ret < 0:
624 failed = failed + 1
625 del functions[function]
626 if ret == 0:
627 skipped = skipped + 1
628 del functions[function]
629 if ret == 1:
630 nb_wrap = nb_wrap + 1
631 include.close()
632 export.close()
633 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000634
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000635 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
636 failed, skipped);
637 print "Missing type converters: "
638 for type in unknown_types.keys():
639 print "%s:%d " % (type, len(unknown_types[type])),
640 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000641
Daniel Veillard1971ee22002-01-31 20:29:19 +0000642#######################################################################
643#
644# This part writes part of the Python front-end classes based on
645# mapping rules between types and classes and also based on function
646# renaming to get consistent function names at the Python level
647#
648#######################################################################
649
650#
651# The type automatically remapped to generated classes
652#
653classes_type = {
654 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
655 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
656 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
657 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
658 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
659 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
660 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
661 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
662 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
663 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
664 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
665 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
666 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
667 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
668 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
669 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
670 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
671 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
672 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000673 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
674 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
675 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000676 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
677 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000678 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
679 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000680 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000681 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000682 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000683 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000684 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
685 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000686 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000687 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000688 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000689 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
690 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
691 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000692 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
693 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
694 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000695}
696
697converter_type = {
698 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
699}
700
701primary_classes = ["xmlNode", "xmlDoc"]
702
703classes_ancestor = {
704 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000705 "xmlDtd" : "xmlNode",
706 "xmlDoc" : "xmlNode",
707 "xmlAttr" : "xmlNode",
708 "xmlNs" : "xmlNode",
709 "xmlEntity" : "xmlNode",
710 "xmlElement" : "xmlNode",
711 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000712 "outputBuffer": "ioWriteWrapper",
713 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000714 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000715 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000716}
717classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000718 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000719 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000720 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000721# "outputBuffer": "xmlOutputBufferClose",
722 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000723 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000724 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000725 "relaxNgSchema": "xmlRelaxNGFree",
726 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
727 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000728 "Schema": "xmlSchemaFree",
729 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
730 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000731 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000732}
733
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000734functions_noexcept = {
735 "xmlHasProp": 1,
736 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000737 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000738}
739
Daniel Veillarddc85f282002-12-31 11:18:37 +0000740reference_keepers = {
741 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000742 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000743 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000744}
745
Daniel Veillard36ed5292002-01-30 23:49:06 +0000746function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000747
748function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000749
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000750def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000751 listname = classe + "List"
752 ll = len(listname)
753 l = len(classe)
754 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000755 func = name[l:]
756 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000757 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
758 func = name[12:]
759 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000760 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
761 func = name[12:]
762 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000763 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
764 func = name[10:]
765 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000766 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
767 func = name[9:]
768 func = string.lower(func[0:1]) + func[1:]
769 elif name[0:9] == "xmlURISet" and file == "python_accessor":
770 func = name[6:]
771 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000772 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
773 func = name[11:]
774 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000775 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
776 func = name[17:]
777 func = string.lower(func[0:1]) + func[1:]
778 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
779 func = name[11:]
780 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000781 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
782 func = name[8:]
783 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000784 elif name[0:15] == "xmlOutputBuffer" and file != "python":
785 func = name[15:]
786 func = string.lower(func[0:1]) + func[1:]
787 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
788 func = name[20:]
789 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000790 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000791 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000792 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000793 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000794 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
795 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000796 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
797 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000798 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
799 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000800 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
801 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000802 elif name[0:11] == "xmlACatalog":
803 func = name[11:]
804 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000805 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000806 func = name[l:]
807 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000808 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000809 func = name[7:]
810 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000811 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000812 func = name[6:]
813 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000814 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000815 func = name[3:]
816 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000817 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000818 func = name
819 if func[0:5] == "xPath":
820 func = "xpath" + func[5:]
821 elif func[0:4] == "xPtr":
822 func = "xpointer" + func[4:]
823 elif func[0:8] == "xInclude":
824 func = "xinclude" + func[8:]
825 elif func[0:2] == "iD":
826 func = "ID" + func[2:]
827 elif func[0:3] == "uRI":
828 func = "URI" + func[3:]
829 elif func[0:4] == "uTF8":
830 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000831 elif func[0:3] == 'sAX':
832 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000833 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000834
Daniel Veillard36ed5292002-01-30 23:49:06 +0000835
Daniel Veillard1971ee22002-01-31 20:29:19 +0000836def functionCompare(info1, info2):
837 (index1, func1, name1, ret1, args1, file1) = info1
838 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000839 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000840 if func1 < func2:
841 return -1
842 if func1 > func2:
843 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000844 if file1 == "python_accessor":
845 return -1
846 if file2 == "python_accessor":
847 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000848 if file1 < file2:
849 return -1
850 if file1 > file2:
851 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000852 return 0
853
854def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000855 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000856 return
857 val = functions[name][0]
858 val = string.replace(val, "NULL", "None");
859 output.write(indent)
860 output.write('"""')
861 while len(val) > 60:
862 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000863 i = string.rfind(str, " ");
864 if i < 0:
865 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000866 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000867 val = val[i:]
868 output.write(str)
869 output.write('\n ');
870 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000871 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000872 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000873
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000874def buildWrappers():
875 global ctypes
876 global py_types
877 global py_return_types
878 global unknown_types
879 global functions
880 global function_classes
881 global classes_type
882 global classes_list
883 global converter_type
884 global primary_classes
885 global converter_type
886 global classes_ancestor
887 global converter_type
888 global primary_classes
889 global classes_ancestor
890 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000891 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000892
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000893 for type in classes_type.keys():
894 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000895
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000896 #
897 # Build the list of C types to look for ordered to start
898 # with primary classes
899 #
900 ctypes = []
901 classes_list = []
902 ctypes_processed = {}
903 classes_processed = {}
904 for classe in primary_classes:
905 classes_list.append(classe)
906 classes_processed[classe] = ()
907 for type in classes_type.keys():
908 tinfo = classes_type[type]
909 if tinfo[2] == classe:
910 ctypes.append(type)
911 ctypes_processed[type] = ()
912 for type in classes_type.keys():
913 if ctypes_processed.has_key(type):
914 continue
915 tinfo = classes_type[type]
916 if not classes_processed.has_key(tinfo[2]):
917 classes_list.append(tinfo[2])
918 classes_processed[tinfo[2]] = ()
919
920 ctypes.append(type)
921 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000922
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000923 for name in functions.keys():
924 found = 0;
925 (desc, ret, args, file) = functions[name]
926 for type in ctypes:
927 classe = classes_type[type][2]
928
929 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
930 found = 1
931 func = nameFixup(name, classe, type, file)
932 info = (0, func, name, ret, args, file)
933 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000934 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
935 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000936 found = 1
937 func = nameFixup(name, classe, type, file)
938 info = (1, func, name, ret, args, file)
939 function_classes[classe].append(info)
940 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
941 found = 1
942 func = nameFixup(name, classe, type, file)
943 info = (0, func, name, ret, args, file)
944 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000945 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
946 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000947 found = 1
948 func = nameFixup(name, classe, type, file)
949 info = (1, func, name, ret, args, file)
950 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000951 if found == 1:
952 continue
953 if name[0:8] == "xmlXPath":
954 continue
955 if name[0:6] == "xmlStr":
956 continue
957 if name[0:10] == "xmlCharStr":
958 continue
959 func = nameFixup(name, "None", file, file)
960 info = (0, func, name, ret, args, file)
961 function_classes['None'].append(info)
962
963 classes = open("libxml2class.py", "w")
964 txt = open("libxml2class.txt", "w")
965 txt.write(" Generated Classes for libxml2-python\n\n")
966
967 txt.write("#\n# Global functions of the module\n#\n\n")
968 if function_classes.has_key("None"):
969 flist = function_classes["None"]
970 flist.sort(functionCompare)
971 oldfile = ""
972 for info in flist:
973 (index, func, name, ret, args, file) = info
974 if file != oldfile:
975 classes.write("#\n# Functions from module %s\n#\n\n" % file)
976 txt.write("\n# functions from module %s\n" % file)
977 oldfile = file
978 classes.write("def %s(" % func)
979 txt.write("%s()\n" % func);
980 n = 0
981 for arg in args:
982 if n != 0:
983 classes.write(", ")
984 classes.write("%s" % arg[0])
985 n = n + 1
986 classes.write("):\n")
987 writeDoc(name, args, ' ', classes);
988
989 for arg in args:
990 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000991 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000992 (arg[0], arg[0]))
993 classes.write(" else: %s__o = %s%s\n" %
994 (arg[0], arg[0], classes_type[arg[1]][0]))
995 if ret[0] != "void":
996 classes.write(" ret = ");
997 else:
998 classes.write(" ");
999 classes.write("libxml2mod.%s(" % name)
1000 n = 0
1001 for arg in args:
1002 if n != 0:
1003 classes.write(", ");
1004 classes.write("%s" % arg[0])
1005 if classes_type.has_key(arg[1]):
1006 classes.write("__o");
1007 n = n + 1
1008 classes.write(")\n");
1009 if ret[0] != "void":
1010 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001011 #
1012 # Raise an exception
1013 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001014 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001015 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001016 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001017 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001018 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001019 % (name))
1020 elif string.find(name, "XPath") >= 0:
1021 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001022 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001023 % (name))
1024 elif string.find(name, "Parse") >= 0:
1025 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001026 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001027 % (name))
1028 else:
1029 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001030 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001031 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001032 classes.write(" return ");
1033 classes.write(classes_type[ret[0]][1] % ("ret"));
1034 classes.write("\n");
1035 else:
1036 classes.write(" return ret\n");
1037 classes.write("\n");
1038
1039 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1040 for classname in classes_list:
1041 if classname == "None":
1042 pass
1043 else:
1044 if classes_ancestor.has_key(classname):
1045 txt.write("\n\nClass %s(%s)\n" % (classname,
1046 classes_ancestor[classname]))
1047 classes.write("class %s(%s):\n" % (classname,
1048 classes_ancestor[classname]))
1049 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001050 if classes_ancestor[classname] == "xmlCore" or \
1051 classes_ancestor[classname] == "xmlNode":
1052 classes.write(" if type(_obj).__name__ != ")
1053 classes.write("'PyCObject':\n")
1054 classes.write(" raise TypeError, ")
1055 classes.write("'%s needs a PyCObject argument'\n" % \
1056 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001057 if reference_keepers.has_key(classname):
1058 rlist = reference_keepers[classname]
1059 for ref in rlist:
1060 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001061 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001062 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1063 classes_ancestor[classname]))
1064 if classes_ancestor[classname] == "xmlCore" or \
1065 classes_ancestor[classname] == "xmlNode":
1066 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001067 format = "<%s (%%s) object at 0x%%x>" % (classname)
1068 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001069 format))
1070 else:
1071 txt.write("Class %s()\n" % (classname))
1072 classes.write("class %s:\n" % (classname))
1073 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001074 if reference_keepers.has_key(classname):
1075 list = reference_keepers[classname]
1076 for ref in list:
1077 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001078 classes.write(" if _obj != None:self._o = _obj;return\n")
1079 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001080 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001081 if classes_destructors.has_key(classname):
1082 classes.write(" def __del__(self):\n")
1083 classes.write(" if self._o != None:\n")
1084 classes.write(" libxml2mod.%s(self._o)\n" %
1085 classes_destructors[classname]);
1086 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001087 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001088 flist = function_classes[classname]
1089 flist.sort(functionCompare)
1090 oldfile = ""
1091 for info in flist:
1092 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001093 #
1094 # Do not provide as method the destructors for the class
1095 # to avoid double free
1096 #
1097 if name == destruct:
1098 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001099 if file != oldfile:
1100 if file == "python_accessor":
1101 classes.write(" # accessors for %s\n" % (classname))
1102 txt.write(" # accessors\n")
1103 else:
1104 classes.write(" #\n")
1105 classes.write(" # %s functions from module %s\n" % (
1106 classname, file))
1107 txt.write("\n # functions from module %s\n" % file)
1108 classes.write(" #\n\n")
1109 oldfile = file
1110 classes.write(" def %s(self" % func)
1111 txt.write(" %s()\n" % func);
1112 n = 0
1113 for arg in args:
1114 if n != index:
1115 classes.write(", %s" % arg[0])
1116 n = n + 1
1117 classes.write("):\n")
1118 writeDoc(name, args, ' ', classes);
1119 n = 0
1120 for arg in args:
1121 if classes_type.has_key(arg[1]):
1122 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001123 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001124 (arg[0], arg[0]))
1125 classes.write(" else: %s__o = %s%s\n" %
1126 (arg[0], arg[0], classes_type[arg[1]][0]))
1127 n = n + 1
1128 if ret[0] != "void":
1129 classes.write(" ret = ");
1130 else:
1131 classes.write(" ");
1132 classes.write("libxml2mod.%s(" % name)
1133 n = 0
1134 for arg in args:
1135 if n != 0:
1136 classes.write(", ");
1137 if n != index:
1138 classes.write("%s" % arg[0])
1139 if classes_type.has_key(arg[1]):
1140 classes.write("__o");
1141 else:
1142 classes.write("self");
1143 if classes_type.has_key(arg[1]):
1144 classes.write(classes_type[arg[1]][0])
1145 n = n + 1
1146 classes.write(")\n");
1147 if ret[0] != "void":
1148 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001149 #
1150 # Raise an exception
1151 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001152 if functions_noexcept.has_key(name):
1153 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001154 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001155 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001156 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001157 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001158 % (name))
1159 elif string.find(name, "XPath") >= 0:
1160 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001161 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001162 % (name))
1163 elif string.find(name, "Parse") >= 0:
1164 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001165 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001166 % (name))
1167 else:
1168 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001169 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001170 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001171
1172 #
1173 # generate the returned class wrapper for the object
1174 #
1175 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001176 classes.write(classes_type[ret[0]][1] % ("ret"));
1177 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001178
1179 #
1180 # Sometime one need to keep references of the source
1181 # class in the returned class object.
1182 # See reference_keepers for the list
1183 #
1184 tclass = classes_type[ret[0]][2]
1185 if reference_keepers.has_key(tclass):
1186 list = reference_keepers[tclass]
1187 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001188 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001189 classes.write(" __tmp.%s = self\n" %
1190 pref[1])
1191 #
1192 # return the class
1193 #
1194 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001195 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001196 #
1197 # Raise an exception
1198 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001199 if functions_noexcept.has_key(name):
1200 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001201 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001202 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001203 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001204 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001205 % (name))
1206 elif string.find(name, "XPath") >= 0:
1207 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001208 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001209 % (name))
1210 elif string.find(name, "Parse") >= 0:
1211 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001212 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001213 % (name))
1214 else:
1215 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001216 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001217 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001218 classes.write(" return ");
1219 classes.write(converter_type[ret[0]] % ("ret"));
1220 classes.write("\n");
1221 else:
1222 classes.write(" return ret\n");
1223 classes.write("\n");
1224
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001225 #
1226 # Generate enum constants
1227 #
1228 for type,enum in enums.items():
1229 classes.write("# %s\n" % type)
1230 items = enum.items()
1231 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1232 for name,value in items:
1233 classes.write("%s = %s\n" % (name,value))
1234 classes.write("\n");
1235
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001236 txt.close()
1237 classes.close()
1238
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001239buildStubs()
1240buildWrappers()