blob: 04b3832fc3663cf4aa74253630735675ecbe29f7 [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 Veillard7db38712002-02-07 16:39:11 +0000273 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
274 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000275 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000276 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000277 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
278 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000279 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000280 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000281 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000282 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
283 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
284 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000285 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
286 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
287 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000288}
289
290py_return_types = {
291 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000292}
293
294unknown_types = {}
295
Daniel Veillard1971ee22002-01-31 20:29:19 +0000296#######################################################################
297#
298# This part writes the C <-> Python stubs libxml2-py.[ch] and
299# the table libxml2-export.c to add when registrering the Python module
300#
301#######################################################################
302
Daniel Veillard263ec862004-10-04 10:26:54 +0000303# Class methods which are written by hand in libxml.c but the Python-level
304# code is still automatically generated (so they are not in skip_function()).
305skip_impl = (
306 'xmlSaveFileTo',
307 'xmlSaveFormatFileTo',
308)
309
Daniel Veillard1971ee22002-01-31 20:29:19 +0000310def skip_function(name):
311 if name[0:12] == "xmlXPathWrap":
312 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000313 if name == "xmlFreeParserCtxt":
314 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000315 if name == "xmlCleanupParser":
316 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000317 if name == "xmlFreeTextReader":
318 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000319# if name[0:11] == "xmlXPathNew":
320# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000321 # the next function is defined in libxml.c
322 if name == "xmlRelaxNGFreeValidCtxt":
323 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000324#
325# Those are skipped because the Const version is used of the bindings
326# instead.
327#
328 if name == "xmlTextReaderBaseUri":
329 return 1
330 if name == "xmlTextReaderLocalName":
331 return 1
332 if name == "xmlTextReaderName":
333 return 1
334 if name == "xmlTextReaderNamespaceUri":
335 return 1
336 if name == "xmlTextReaderPrefix":
337 return 1
338 if name == "xmlTextReaderXmlLang":
339 return 1
340 if name == "xmlTextReaderValue":
341 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000342 if name == "xmlOutputBufferClose": # handled by by the superclass
343 return 1
344 if name == "xmlOutputBufferFlush": # handled by by the superclass
345 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000346 if name == "xmlErrMemory":
347 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000348 return 0
349
Daniel Veillard96fe0952002-01-30 20:52:23 +0000350def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000351 global py_types
352 global unknown_types
353 global functions
354 global skipped_modules
355
356 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000357 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000358 except:
359 print "failed to get function %s infos"
360 return
361
362 if skipped_modules.has_key(file):
363 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000364 if skip_function(name) == 1:
365 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000366 if name in skip_impl:
367 # Don't delete the function entry in the caller.
368 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000369
370 c_call = "";
371 format=""
372 format_args=""
373 c_args=""
374 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000375 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000376 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000377 # This should be correct
378 if arg[1][0:6] == "const ":
379 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000380 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000381 if py_types.has_key(arg[1]):
382 (f, t, n, c) = py_types[arg[1]]
383 if f != None:
384 format = format + f
385 if t != None:
386 format_args = format_args + ", &pyobj_%s" % (arg[0])
387 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
388 c_convert = c_convert + \
389 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
390 arg[1], t, arg[0]);
391 else:
392 format_args = format_args + ", &%s" % (arg[0])
393 if c_call != "":
394 c_call = c_call + ", ";
395 c_call = c_call + "%s" % (arg[0])
396 else:
397 if skipped_types.has_key(arg[1]):
398 return 0
399 if unknown_types.has_key(arg[1]):
400 lst = unknown_types[arg[1]]
401 lst.append(name)
402 else:
403 unknown_types[arg[1]] = [name]
404 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000405 if format != "":
406 format = format + ":%s" % (name)
407
408 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000409 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000410 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
411 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
412 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000413 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
414 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000415 else:
416 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
417 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000418 else:
419 c_call = "\n %s(%s);\n" % (name, c_call);
420 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000421 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000422 (f, t, n, c) = py_types[ret[0]]
423 c_return = " %s c_retval;\n" % (ret[0])
424 if file == "python_accessor" and ret[2] != None:
425 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
426 else:
427 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
428 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
429 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000430 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000431 (f, t, n, c) = py_return_types[ret[0]]
432 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000433 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000434 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
435 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000436 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000437 if skipped_types.has_key(ret[0]):
438 return 0
439 if unknown_types.has_key(ret[0]):
440 lst = unknown_types[ret[0]]
441 lst.append(name)
442 else:
443 unknown_types[ret[0]] = [name]
444 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000445
Daniel Veillard42766c02002-08-22 20:52:17 +0000446 if file == "debugXML":
447 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
448 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
449 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000450 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000451 include.write("#ifdef LIBXML_HTML_ENABLED\n");
452 export.write("#ifdef LIBXML_HTML_ENABLED\n");
453 output.write("#ifdef LIBXML_HTML_ENABLED\n");
454 elif file == "c14n":
455 include.write("#ifdef LIBXML_C14N_ENABLED\n");
456 export.write("#ifdef LIBXML_C14N_ENABLED\n");
457 output.write("#ifdef LIBXML_C14N_ENABLED\n");
458 elif file == "xpathInternals" or file == "xpath":
459 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
460 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
461 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
462 elif file == "xpointer":
463 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
464 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
465 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
466 elif file == "xinclude":
467 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
468 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
469 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000470 elif file == "xmlregexp":
471 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
472 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
473 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000474 elif file == "xmlschemas" or file == "xmlschemastypes" or \
475 file == "relaxng":
476 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
477 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
478 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000479
Daniel Veillard96fe0952002-01-30 20:52:23 +0000480 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000481 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000482
Daniel Veillardd2379012002-03-15 22:24:56 +0000483 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000484 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000485
486 if file == "python":
487 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000488 if name[0:4] == "html":
489 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
490 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
491 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000492 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000493 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000494 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000495 if name[0:4] == "html":
496 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
497 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
498 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000499 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000500
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000501 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000502 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
503 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000504 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000505 output.write(" ATTRIBUTE_UNUSED")
506 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000507 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000508 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000509 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000510 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000511 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000512 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000513 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000514 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000515 (format, format_args))
516 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000517 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000518 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000519
520 output.write(c_call)
521 output.write(ret_convert)
522 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000523 if file == "debugXML":
524 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
525 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
526 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000527 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000528 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
529 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
530 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
531 elif file == "c14n":
532 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
533 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
534 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
535 elif file == "xpathInternals" or file == "xpath":
536 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
537 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
538 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
539 elif file == "xpointer":
540 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
541 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
542 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
543 elif file == "xinclude":
544 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
545 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
546 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000547 elif file == "xmlregexp":
548 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
549 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
550 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000551 elif file == "xmlschemas" or file == "xmlschemastypes" or \
552 file == "relaxng":
553 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
554 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
555 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000556 return 1
557
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000558def buildStubs():
559 global py_types
560 global py_return_types
561 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000562
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000563 try:
564 f = open("libxml2-api.xml")
565 data = f.read()
566 (parser, target) = getparser()
567 parser.feed(data)
568 parser.close()
569 except IOError, msg:
570 try:
571 f = open("../doc/libxml2-api.xml")
572 data = f.read()
573 (parser, target) = getparser()
574 parser.feed(data)
575 parser.close()
576 except IOError, msg:
577 print file, ":", msg
578 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000579
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000580 n = len(functions.keys())
581 print "Found %d functions in libxml2-api.xml" % (n)
582
583 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
584 try:
585 f = open("libxml2-python-api.xml")
586 data = f.read()
587 (parser, target) = getparser()
588 parser.feed(data)
589 parser.close()
590 except IOError, msg:
591 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000592
593
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000594 print "Found %d functions in libxml2-python-api.xml" % (
595 len(functions.keys()) - n)
596 nb_wrap = 0
597 failed = 0
598 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000599
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000600 include = open("libxml2-py.h", "w")
601 include.write("/* Generated */\n\n")
602 export = open("libxml2-export.c", "w")
603 export.write("/* Generated */\n\n")
604 wrapper = open("libxml2-py.c", "w")
605 wrapper.write("/* Generated */\n\n")
606 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000607 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000608 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000609 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000610 wrapper.write("#include \"libxml_wrap.h\"\n")
611 wrapper.write("#include \"libxml2-py.h\"\n\n")
612 for function in functions.keys():
613 ret = print_function_wrapper(function, wrapper, export, include)
614 if ret < 0:
615 failed = failed + 1
616 del functions[function]
617 if ret == 0:
618 skipped = skipped + 1
619 del functions[function]
620 if ret == 1:
621 nb_wrap = nb_wrap + 1
622 include.close()
623 export.close()
624 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000625
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000626 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
627 failed, skipped);
628 print "Missing type converters: "
629 for type in unknown_types.keys():
630 print "%s:%d " % (type, len(unknown_types[type])),
631 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000632
Daniel Veillard1971ee22002-01-31 20:29:19 +0000633#######################################################################
634#
635# This part writes part of the Python front-end classes based on
636# mapping rules between types and classes and also based on function
637# renaming to get consistent function names at the Python level
638#
639#######################################################################
640
641#
642# The type automatically remapped to generated classes
643#
644classes_type = {
645 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
646 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
647 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
648 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
649 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
650 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
651 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
652 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
653 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
654 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
655 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
656 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
657 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
658 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
659 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
660 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
661 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
662 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
663 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000664 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
665 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
666 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000667 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
668 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000669 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
670 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000671 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000672 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000673 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000674 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
675 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000676 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000677 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000678 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000679 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
680 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
681 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000682 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
683 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
684 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000685}
686
687converter_type = {
688 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
689}
690
691primary_classes = ["xmlNode", "xmlDoc"]
692
693classes_ancestor = {
694 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000695 "xmlDtd" : "xmlNode",
696 "xmlDoc" : "xmlNode",
697 "xmlAttr" : "xmlNode",
698 "xmlNs" : "xmlNode",
699 "xmlEntity" : "xmlNode",
700 "xmlElement" : "xmlNode",
701 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000702 "outputBuffer": "ioWriteWrapper",
703 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000704 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000705 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000706}
707classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000708 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000709 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000710 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000711# "outputBuffer": "xmlOutputBufferClose",
712 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000713 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000714 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000715 "relaxNgSchema": "xmlRelaxNGFree",
716 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
717 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000718 "Schema": "xmlSchemaFree",
719 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
720 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000721}
722
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000723functions_noexcept = {
724 "xmlHasProp": 1,
725 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000726 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000727}
728
Daniel Veillarddc85f282002-12-31 11:18:37 +0000729reference_keepers = {
730 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000731 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000732 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000733}
734
Daniel Veillard36ed5292002-01-30 23:49:06 +0000735function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000736
737function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000738
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000739def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000740 listname = classe + "List"
741 ll = len(listname)
742 l = len(classe)
743 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000744 func = name[l:]
745 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000746 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
747 func = name[12:]
748 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000749 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
750 func = name[12:]
751 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000752 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
753 func = name[10:]
754 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000755 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
756 func = name[9:]
757 func = string.lower(func[0:1]) + func[1:]
758 elif name[0:9] == "xmlURISet" and file == "python_accessor":
759 func = name[6:]
760 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000761 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
762 func = name[11:]
763 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000764 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
765 func = name[17:]
766 func = string.lower(func[0:1]) + func[1:]
767 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
768 func = name[11:]
769 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000770 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
771 func = name[8:]
772 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000773 elif name[0:15] == "xmlOutputBuffer" and file != "python":
774 func = name[15:]
775 func = string.lower(func[0:1]) + func[1:]
776 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
777 func = name[20:]
778 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000779 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000780 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000781 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000782 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000783 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
784 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000785 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
786 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000787 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
788 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000789 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
790 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000791 elif name[0:11] == "xmlACatalog":
792 func = name[11:]
793 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000794 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000795 func = name[l:]
796 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000797 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000798 func = name[7:]
799 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000800 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000801 func = name[6:]
802 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000803 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000804 func = name[3:]
805 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000806 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000807 func = name
808 if func[0:5] == "xPath":
809 func = "xpath" + func[5:]
810 elif func[0:4] == "xPtr":
811 func = "xpointer" + func[4:]
812 elif func[0:8] == "xInclude":
813 func = "xinclude" + func[8:]
814 elif func[0:2] == "iD":
815 func = "ID" + func[2:]
816 elif func[0:3] == "uRI":
817 func = "URI" + func[3:]
818 elif func[0:4] == "uTF8":
819 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000820 elif func[0:3] == 'sAX':
821 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000822 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000823
Daniel Veillard36ed5292002-01-30 23:49:06 +0000824
Daniel Veillard1971ee22002-01-31 20:29:19 +0000825def functionCompare(info1, info2):
826 (index1, func1, name1, ret1, args1, file1) = info1
827 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000828 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000829 if func1 < func2:
830 return -1
831 if func1 > func2:
832 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000833 if file1 == "python_accessor":
834 return -1
835 if file2 == "python_accessor":
836 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000837 if file1 < file2:
838 return -1
839 if file1 > file2:
840 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000841 return 0
842
843def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000844 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000845 return
846 val = functions[name][0]
847 val = string.replace(val, "NULL", "None");
848 output.write(indent)
849 output.write('"""')
850 while len(val) > 60:
851 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000852 i = string.rfind(str, " ");
853 if i < 0:
854 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000855 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000856 val = val[i:]
857 output.write(str)
858 output.write('\n ');
859 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000860 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000861 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000862
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000863def buildWrappers():
864 global ctypes
865 global py_types
866 global py_return_types
867 global unknown_types
868 global functions
869 global function_classes
870 global classes_type
871 global classes_list
872 global converter_type
873 global primary_classes
874 global converter_type
875 global classes_ancestor
876 global converter_type
877 global primary_classes
878 global classes_ancestor
879 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000880 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000881
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000882 for type in classes_type.keys():
883 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000884
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000885 #
886 # Build the list of C types to look for ordered to start
887 # with primary classes
888 #
889 ctypes = []
890 classes_list = []
891 ctypes_processed = {}
892 classes_processed = {}
893 for classe in primary_classes:
894 classes_list.append(classe)
895 classes_processed[classe] = ()
896 for type in classes_type.keys():
897 tinfo = classes_type[type]
898 if tinfo[2] == classe:
899 ctypes.append(type)
900 ctypes_processed[type] = ()
901 for type in classes_type.keys():
902 if ctypes_processed.has_key(type):
903 continue
904 tinfo = classes_type[type]
905 if not classes_processed.has_key(tinfo[2]):
906 classes_list.append(tinfo[2])
907 classes_processed[tinfo[2]] = ()
908
909 ctypes.append(type)
910 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000911
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000912 for name in functions.keys():
913 found = 0;
914 (desc, ret, args, file) = functions[name]
915 for type in ctypes:
916 classe = classes_type[type][2]
917
918 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
919 found = 1
920 func = nameFixup(name, classe, type, file)
921 info = (0, func, name, ret, args, file)
922 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000923 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
924 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000925 found = 1
926 func = nameFixup(name, classe, type, file)
927 info = (1, func, name, ret, args, file)
928 function_classes[classe].append(info)
929 elif name[0:4] == "html" 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:4] == "html" 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)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000940 if found == 1:
941 continue
942 if name[0:8] == "xmlXPath":
943 continue
944 if name[0:6] == "xmlStr":
945 continue
946 if name[0:10] == "xmlCharStr":
947 continue
948 func = nameFixup(name, "None", file, file)
949 info = (0, func, name, ret, args, file)
950 function_classes['None'].append(info)
951
952 classes = open("libxml2class.py", "w")
953 txt = open("libxml2class.txt", "w")
954 txt.write(" Generated Classes for libxml2-python\n\n")
955
956 txt.write("#\n# Global functions of the module\n#\n\n")
957 if function_classes.has_key("None"):
958 flist = function_classes["None"]
959 flist.sort(functionCompare)
960 oldfile = ""
961 for info in flist:
962 (index, func, name, ret, args, file) = info
963 if file != oldfile:
964 classes.write("#\n# Functions from module %s\n#\n\n" % file)
965 txt.write("\n# functions from module %s\n" % file)
966 oldfile = file
967 classes.write("def %s(" % func)
968 txt.write("%s()\n" % func);
969 n = 0
970 for arg in args:
971 if n != 0:
972 classes.write(", ")
973 classes.write("%s" % arg[0])
974 n = n + 1
975 classes.write("):\n")
976 writeDoc(name, args, ' ', classes);
977
978 for arg in args:
979 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000980 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000981 (arg[0], arg[0]))
982 classes.write(" else: %s__o = %s%s\n" %
983 (arg[0], arg[0], classes_type[arg[1]][0]))
984 if ret[0] != "void":
985 classes.write(" ret = ");
986 else:
987 classes.write(" ");
988 classes.write("libxml2mod.%s(" % name)
989 n = 0
990 for arg in args:
991 if n != 0:
992 classes.write(", ");
993 classes.write("%s" % arg[0])
994 if classes_type.has_key(arg[1]):
995 classes.write("__o");
996 n = n + 1
997 classes.write(")\n");
998 if ret[0] != "void":
999 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001000 #
1001 # Raise an exception
1002 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001003 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001004 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001005 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001006 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001007 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001008 % (name))
1009 elif string.find(name, "XPath") >= 0:
1010 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001011 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001012 % (name))
1013 elif string.find(name, "Parse") >= 0:
1014 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001015 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001016 % (name))
1017 else:
1018 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001019 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001020 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001021 classes.write(" return ");
1022 classes.write(classes_type[ret[0]][1] % ("ret"));
1023 classes.write("\n");
1024 else:
1025 classes.write(" return ret\n");
1026 classes.write("\n");
1027
1028 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1029 for classname in classes_list:
1030 if classname == "None":
1031 pass
1032 else:
1033 if classes_ancestor.has_key(classname):
1034 txt.write("\n\nClass %s(%s)\n" % (classname,
1035 classes_ancestor[classname]))
1036 classes.write("class %s(%s):\n" % (classname,
1037 classes_ancestor[classname]))
1038 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001039 if classes_ancestor[classname] == "xmlCore" or \
1040 classes_ancestor[classname] == "xmlNode":
1041 classes.write(" if type(_obj).__name__ != ")
1042 classes.write("'PyCObject':\n")
1043 classes.write(" raise TypeError, ")
1044 classes.write("'%s needs a PyCObject argument'\n" % \
1045 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001046 if reference_keepers.has_key(classname):
1047 rlist = reference_keepers[classname]
1048 for ref in rlist:
1049 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001050 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001051 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1052 classes_ancestor[classname]))
1053 if classes_ancestor[classname] == "xmlCore" or \
1054 classes_ancestor[classname] == "xmlNode":
1055 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001056 format = "<%s (%%s) object at 0x%%x>" % (classname)
1057 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001058 format))
1059 else:
1060 txt.write("Class %s()\n" % (classname))
1061 classes.write("class %s:\n" % (classname))
1062 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001063 if reference_keepers.has_key(classname):
1064 list = reference_keepers[classname]
1065 for ref in list:
1066 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001067 classes.write(" if _obj != None:self._o = _obj;return\n")
1068 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001069 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001070 if classes_destructors.has_key(classname):
1071 classes.write(" def __del__(self):\n")
1072 classes.write(" if self._o != None:\n")
1073 classes.write(" libxml2mod.%s(self._o)\n" %
1074 classes_destructors[classname]);
1075 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001076 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001077 flist = function_classes[classname]
1078 flist.sort(functionCompare)
1079 oldfile = ""
1080 for info in flist:
1081 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001082 #
1083 # Do not provide as method the destructors for the class
1084 # to avoid double free
1085 #
1086 if name == destruct:
1087 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001088 if file != oldfile:
1089 if file == "python_accessor":
1090 classes.write(" # accessors for %s\n" % (classname))
1091 txt.write(" # accessors\n")
1092 else:
1093 classes.write(" #\n")
1094 classes.write(" # %s functions from module %s\n" % (
1095 classname, file))
1096 txt.write("\n # functions from module %s\n" % file)
1097 classes.write(" #\n\n")
1098 oldfile = file
1099 classes.write(" def %s(self" % func)
1100 txt.write(" %s()\n" % func);
1101 n = 0
1102 for arg in args:
1103 if n != index:
1104 classes.write(", %s" % arg[0])
1105 n = n + 1
1106 classes.write("):\n")
1107 writeDoc(name, args, ' ', classes);
1108 n = 0
1109 for arg in args:
1110 if classes_type.has_key(arg[1]):
1111 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001112 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001113 (arg[0], arg[0]))
1114 classes.write(" else: %s__o = %s%s\n" %
1115 (arg[0], arg[0], classes_type[arg[1]][0]))
1116 n = n + 1
1117 if ret[0] != "void":
1118 classes.write(" ret = ");
1119 else:
1120 classes.write(" ");
1121 classes.write("libxml2mod.%s(" % name)
1122 n = 0
1123 for arg in args:
1124 if n != 0:
1125 classes.write(", ");
1126 if n != index:
1127 classes.write("%s" % arg[0])
1128 if classes_type.has_key(arg[1]):
1129 classes.write("__o");
1130 else:
1131 classes.write("self");
1132 if classes_type.has_key(arg[1]):
1133 classes.write(classes_type[arg[1]][0])
1134 n = n + 1
1135 classes.write(")\n");
1136 if ret[0] != "void":
1137 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001138 #
1139 # Raise an exception
1140 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001141 if functions_noexcept.has_key(name):
1142 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001143 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001144 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001145 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001146 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001147 % (name))
1148 elif string.find(name, "XPath") >= 0:
1149 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001150 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001151 % (name))
1152 elif string.find(name, "Parse") >= 0:
1153 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001154 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001155 % (name))
1156 else:
1157 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001158 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001159 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001160
1161 #
1162 # generate the returned class wrapper for the object
1163 #
1164 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001165 classes.write(classes_type[ret[0]][1] % ("ret"));
1166 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001167
1168 #
1169 # Sometime one need to keep references of the source
1170 # class in the returned class object.
1171 # See reference_keepers for the list
1172 #
1173 tclass = classes_type[ret[0]][2]
1174 if reference_keepers.has_key(tclass):
1175 list = reference_keepers[tclass]
1176 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001177 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001178 classes.write(" __tmp.%s = self\n" %
1179 pref[1])
1180 #
1181 # return the class
1182 #
1183 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001184 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001185 #
1186 # Raise an exception
1187 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001188 if functions_noexcept.has_key(name):
1189 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001190 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001191 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001192 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001193 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001194 % (name))
1195 elif string.find(name, "XPath") >= 0:
1196 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001197 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001198 % (name))
1199 elif string.find(name, "Parse") >= 0:
1200 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001201 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001202 % (name))
1203 else:
1204 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001205 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001206 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001207 classes.write(" return ");
1208 classes.write(converter_type[ret[0]] % ("ret"));
1209 classes.write("\n");
1210 else:
1211 classes.write(" return ret\n");
1212 classes.write("\n");
1213
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001214 #
1215 # Generate enum constants
1216 #
1217 for type,enum in enums.items():
1218 classes.write("# %s\n" % type)
1219 items = enum.items()
1220 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1221 for name,value in items:
1222 classes.write("%s = %s\n" % (name,value))
1223 classes.write("\n");
1224
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001225 txt.close()
1226 classes.close()
1227
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001228buildStubs()
1229buildWrappers()