blob: 5533f3464a4ac3e8d375190b0adac9bef6f265b8 [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 = {}
7
Daniel Veillard0fea6f42002-02-22 22:51:13 +00008import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +00009import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000010
11#######################################################################
12#
13# That part if purely the API acquisition phase from the
14# XML API description
15#
16#######################################################################
17import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000018import xmllib
19try:
20 import sgmlop
21except ImportError:
22 sgmlop = None # accelerator not available
23
24debug = 0
25
26if sgmlop:
27 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000028 """sgmlop based XML parser. this is typically 15x faster
29 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000030
Daniel Veillard01a6d412002-02-11 18:42:20 +000031 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000032
Daniel Veillard01a6d412002-02-11 18:42:20 +000033 # setup callbacks
34 self.finish_starttag = target.start
35 self.finish_endtag = target.end
36 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000037
Daniel Veillard01a6d412002-02-11 18:42:20 +000038 # activate parser
39 self.parser = sgmlop.XMLParser()
40 self.parser.register(self)
41 self.feed = self.parser.feed
42 self.entity = {
43 "amp": "&", "gt": ">", "lt": "<",
44 "apos": "'", "quot": '"'
45 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000046
Daniel Veillard01a6d412002-02-11 18:42:20 +000047 def close(self):
48 try:
49 self.parser.close()
50 finally:
51 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000052
Daniel Veillard01a6d412002-02-11 18:42:20 +000053 def handle_entityref(self, entity):
54 # <string> entity
55 try:
56 self.handle_data(self.entity[entity])
57 except KeyError:
58 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000059
60else:
61 FastParser = None
62
63
64class SlowParser(xmllib.XMLParser):
65 """slow but safe standard parser, based on the XML parser in
66 Python's standard library."""
67
68 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000069 self.unknown_starttag = target.start
70 self.handle_data = target.data
71 self.unknown_endtag = target.end
72 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000073
74def getparser(target = None):
75 # get the fastest available parser, and attach it to an
76 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000077 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000078 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000079 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000080 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000081 return SlowParser(target), target
82
83class docParser:
84 def __init__(self):
85 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000086 self._data = []
87 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000088
89 def close(self):
90 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000091 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000092
93 def getmethodname(self):
94 return self._methodname
95
96 def data(self, text):
97 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000098 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +000099 self._data.append(text)
100
101 def start(self, tag, attrs):
102 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000103 print "start %s, %s" % (tag, attrs)
104 if tag == 'function':
105 self._data = []
106 self.in_function = 1
107 self.function = None
108 self.function_args = []
109 self.function_descr = None
110 self.function_return = None
111 self.function_file = None
112 if attrs.has_key('name'):
113 self.function = attrs['name']
114 if attrs.has_key('file'):
115 self.function_file = attrs['file']
116 elif tag == 'info':
117 self._data = []
118 elif tag == 'arg':
119 if self.in_function == 1:
120 self.function_arg_name = None
121 self.function_arg_type = None
122 self.function_arg_info = None
123 if attrs.has_key('name'):
124 self.function_arg_name = attrs['name']
125 if attrs.has_key('type'):
126 self.function_arg_type = attrs['type']
127 if attrs.has_key('info'):
128 self.function_arg_info = attrs['info']
129 elif tag == 'return':
130 if self.in_function == 1:
131 self.function_return_type = None
132 self.function_return_info = None
133 self.function_return_field = None
134 if attrs.has_key('type'):
135 self.function_return_type = attrs['type']
136 if attrs.has_key('info'):
137 self.function_return_info = attrs['info']
138 if attrs.has_key('field'):
139 self.function_return_field = attrs['field']
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000140
141
142 def end(self, tag):
143 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000144 print "end %s" % tag
145 if tag == 'function':
146 if self.function != None:
147 function(self.function, self.function_descr,
148 self.function_return, self.function_args,
149 self.function_file)
150 self.in_function = 0
151 elif tag == 'arg':
152 if self.in_function == 1:
153 self.function_args.append([self.function_arg_name,
154 self.function_arg_type,
155 self.function_arg_info])
156 elif tag == 'return':
157 if self.in_function == 1:
158 self.function_return = [self.function_return_type,
159 self.function_return_info,
160 self.function_return_field]
161 elif tag == 'info':
162 str = ''
163 for c in self._data:
164 str = str + c
165 if self.in_function == 1:
166 self.function_descr = str
167
168
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000169def function(name, desc, ret, args, file):
170 global functions
171
172 functions[name] = (desc, ret, args, file)
173
Daniel Veillard1971ee22002-01-31 20:29:19 +0000174#######################################################################
175#
176# Some filtering rukes to drop functions/types which should not
177# be exposed as-is on the Python interface
178#
179#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000180
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000181skipped_modules = {
182 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000183 'DOCBparser': None,
184 'SAX': None,
185 'hash': None,
186 'list': None,
187 'threads': None,
Daniel Veillard1971ee22002-01-31 20:29:19 +0000188 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000189}
190skipped_types = {
191 'int *': "usually a return type",
192 'xmlSAXHandlerPtr': "not the proper interface for SAX",
193 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000194 'xmlRMutexPtr': "thread specific, skipped",
195 'xmlMutexPtr': "thread specific, skipped",
196 'xmlGlobalStatePtr': "thread specific, skipped",
197 'xmlListPtr': "internal representation not suitable for python",
198 'xmlBufferPtr': "internal representation not suitable for python",
199 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000200}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000201
202#######################################################################
203#
204# Table of remapping to/from the python type or class to the C
205# counterpart.
206#
207#######################################################################
208
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000209py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000210 'void': (None, None, None, None),
211 'int': ('i', None, "int", "int"),
212 'long': ('i', None, "int", "int"),
213 'double': ('d', None, "double", "double"),
214 'unsigned int': ('i', None, "int", "int"),
215 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000216 'unsigned char *': ('z', None, "charPtr", "char *"),
217 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000218 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000219 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000220 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000221 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
222 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
223 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
224 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
225 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
226 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000237 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
238 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
239 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
240 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
241 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
242 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
243 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
244 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
245 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
246 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
247 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
248 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000249 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
250 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
251 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
252 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
253 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
254 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
257 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
258 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
259 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
260 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000261 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
262 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000263 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000264 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
265 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
266 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
267 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000268 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
269 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000270 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000271}
272
273py_return_types = {
274 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000275}
276
277unknown_types = {}
278
Daniel Veillard1971ee22002-01-31 20:29:19 +0000279#######################################################################
280#
281# This part writes the C <-> Python stubs libxml2-py.[ch] and
282# the table libxml2-export.c to add when registrering the Python module
283#
284#######################################################################
285
286def skip_function(name):
287 if name[0:12] == "xmlXPathWrap":
288 return 1
289# if name[0:11] == "xmlXPathNew":
290# return 1
291 return 0
292
Daniel Veillard96fe0952002-01-30 20:52:23 +0000293def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000294 global py_types
295 global unknown_types
296 global functions
297 global skipped_modules
298
299 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000300 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000301 except:
302 print "failed to get function %s infos"
303 return
304
305 if skipped_modules.has_key(file):
306 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000307 if skip_function(name) == 1:
308 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000309
310 c_call = "";
311 format=""
312 format_args=""
313 c_args=""
314 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000315 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000316 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000317 # This should be correct
318 if arg[1][0:6] == "const ":
319 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000320 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000321 if py_types.has_key(arg[1]):
322 (f, t, n, c) = py_types[arg[1]]
323 if f != None:
324 format = format + f
325 if t != None:
326 format_args = format_args + ", &pyobj_%s" % (arg[0])
327 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
328 c_convert = c_convert + \
329 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
330 arg[1], t, arg[0]);
331 else:
332 format_args = format_args + ", &%s" % (arg[0])
333 if c_call != "":
334 c_call = c_call + ", ";
335 c_call = c_call + "%s" % (arg[0])
336 else:
337 if skipped_types.has_key(arg[1]):
338 return 0
339 if unknown_types.has_key(arg[1]):
340 lst = unknown_types[arg[1]]
341 lst.append(name)
342 else:
343 unknown_types[arg[1]] = [name]
344 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000345 if format != "":
346 format = format + ":%s" % (name)
347
348 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000349 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000350 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
351 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
352 args[0][0], args[1][0], args[0][0], args[1][0])
Daniel Veillardd2379012002-03-15 22:24:56 +0000353 c_call = c_call + " %s->%s = xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
Daniel Veillard6361da02002-02-23 10:10:33 +0000354 args[1][0], args[1][0])
355 else:
356 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
357 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000358 else:
359 c_call = "\n %s(%s);\n" % (name, c_call);
360 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000361 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000362 (f, t, n, c) = py_types[ret[0]]
363 c_return = " %s c_retval;\n" % (ret[0])
364 if file == "python_accessor" and ret[2] != None:
365 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
366 else:
367 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
368 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
369 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000370 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000371 (f, t, n, c) = py_return_types[ret[0]]
372 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000373 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000374 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
375 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000376 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000377 if skipped_types.has_key(ret[0]):
378 return 0
379 if unknown_types.has_key(ret[0]):
380 lst = unknown_types[ret[0]]
381 lst.append(name)
382 else:
383 unknown_types[ret[0]] = [name]
384 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000385
Daniel Veillard96fe0952002-01-30 20:52:23 +0000386 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000387 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000388
Daniel Veillardd2379012002-03-15 22:24:56 +0000389 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000390 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000391
392 if file == "python":
393 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000394 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000395 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000396 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000397 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000398
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000399 output.write("PyObject *\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000400 output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name))
401 if format == "":
402 output.write("ATTRIBUTE_UNUSED ")
403 output.write(" PyObject *args) {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000404 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000405 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000406 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000407 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000408 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000409 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000410 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000411 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000412 (format, format_args))
413 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000414 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000415 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000416
417 output.write(c_call)
418 output.write(ret_convert)
419 output.write("}\n\n")
420 return 1
421
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000422def buildStubs():
423 global py_types
424 global py_return_types
425 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000426
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000427 try:
428 f = open("libxml2-api.xml")
429 data = f.read()
430 (parser, target) = getparser()
431 parser.feed(data)
432 parser.close()
433 except IOError, msg:
434 try:
435 f = open("../doc/libxml2-api.xml")
436 data = f.read()
437 (parser, target) = getparser()
438 parser.feed(data)
439 parser.close()
440 except IOError, msg:
441 print file, ":", msg
442 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000443
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000444 n = len(functions.keys())
445 print "Found %d functions in libxml2-api.xml" % (n)
446
447 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
448 try:
449 f = open("libxml2-python-api.xml")
450 data = f.read()
451 (parser, target) = getparser()
452 parser.feed(data)
453 parser.close()
454 except IOError, msg:
455 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000456
457
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000458 print "Found %d functions in libxml2-python-api.xml" % (
459 len(functions.keys()) - n)
460 nb_wrap = 0
461 failed = 0
462 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000463
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000464 include = open("libxml2-py.h", "w")
465 include.write("/* Generated */\n\n")
466 export = open("libxml2-export.c", "w")
467 export.write("/* Generated */\n\n")
468 wrapper = open("libxml2-py.c", "w")
469 wrapper.write("/* Generated */\n\n")
470 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000471 wrapper.write("#include \"config.h\"\n")
472 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000473 wrapper.write("#include <libxml/tree.h>\n")
474 wrapper.write("#include \"libxml_wrap.h\"\n")
475 wrapper.write("#include \"libxml2-py.h\"\n\n")
476 for function in functions.keys():
477 ret = print_function_wrapper(function, wrapper, export, include)
478 if ret < 0:
479 failed = failed + 1
480 del functions[function]
481 if ret == 0:
482 skipped = skipped + 1
483 del functions[function]
484 if ret == 1:
485 nb_wrap = nb_wrap + 1
486 include.close()
487 export.close()
488 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000489
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000490 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
491 failed, skipped);
492 print "Missing type converters: "
493 for type in unknown_types.keys():
494 print "%s:%d " % (type, len(unknown_types[type])),
495 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000496
Daniel Veillard1971ee22002-01-31 20:29:19 +0000497#######################################################################
498#
499# This part writes part of the Python front-end classes based on
500# mapping rules between types and classes and also based on function
501# renaming to get consistent function names at the Python level
502#
503#######################################################################
504
505#
506# The type automatically remapped to generated classes
507#
508classes_type = {
509 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
510 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
511 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
512 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
513 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
514 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
515 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
516 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
517 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
518 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
519 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
520 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
521 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
522 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
523 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
524 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
525 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
526 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
527 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000528 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
529 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
530 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000531 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
532 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000533 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
534 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000535 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000536 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000537}
538
539converter_type = {
540 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
541}
542
543primary_classes = ["xmlNode", "xmlDoc"]
544
545classes_ancestor = {
546 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000547 "xmlDtd" : "xmlNode",
548 "xmlDoc" : "xmlNode",
549 "xmlAttr" : "xmlNode",
550 "xmlNs" : "xmlNode",
551 "xmlEntity" : "xmlNode",
552 "xmlElement" : "xmlNode",
553 "xmlAttribute" : "xmlNode",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000554}
555classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000556 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000557 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000558 "URI": "xmlFreeURI",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000559}
560
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000561functions_noexcept = {
562 "xmlHasProp": 1,
563 "xmlHasNsProp": 1,
564}
565
Daniel Veillard36ed5292002-01-30 23:49:06 +0000566function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000567
568function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000569
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000570def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000571 listname = classe + "List"
572 ll = len(listname)
573 l = len(classe)
574 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000575 func = name[l:]
576 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000577 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
578 func = name[12:]
579 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000580 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
581 func = name[12:]
582 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000583 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
584 func = name[10:]
585 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000586 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
587 func = name[9:]
588 func = string.lower(func[0:1]) + func[1:]
589 elif name[0:9] == "xmlURISet" and file == "python_accessor":
590 func = name[6:]
591 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000592 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
593 func = name[17:]
594 func = string.lower(func[0:1]) + func[1:]
595 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
596 func = name[11:]
597 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000598 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
599 func = name[8:]
600 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000601 elif name[0:11] == "xmlACatalog":
602 func = name[11:]
603 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000604 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000605 func = name[l:]
606 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000607 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000608 func = name[7:]
609 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000610 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000611 func = name[6:]
612 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000613 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000614 func = name[3:]
615 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000616 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000617 func = name
618 if func[0:5] == "xPath":
619 func = "xpath" + func[5:]
620 elif func[0:4] == "xPtr":
621 func = "xpointer" + func[4:]
622 elif func[0:8] == "xInclude":
623 func = "xinclude" + func[8:]
624 elif func[0:2] == "iD":
625 func = "ID" + func[2:]
626 elif func[0:3] == "uRI":
627 func = "URI" + func[3:]
628 elif func[0:4] == "uTF8":
629 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000630 elif func[0:3] == 'sAX':
631 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000632 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000633
Daniel Veillard36ed5292002-01-30 23:49:06 +0000634
Daniel Veillard1971ee22002-01-31 20:29:19 +0000635def functionCompare(info1, info2):
636 (index1, func1, name1, ret1, args1, file1) = info1
637 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000638 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000639 if func1 < func2:
640 return -1
641 if func1 > func2:
642 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000643 if file1 == "python_accessor":
644 return -1
645 if file2 == "python_accessor":
646 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000647 if file1 < file2:
648 return -1
649 if file1 > file2:
650 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000651 return 0
652
653def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000654 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000655 return
656 val = functions[name][0]
657 val = string.replace(val, "NULL", "None");
658 output.write(indent)
659 output.write('"""')
660 while len(val) > 60:
661 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000662 i = string.rfind(str, " ");
663 if i < 0:
664 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000665 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000666 val = val[i:]
667 output.write(str)
668 output.write('\n ');
669 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000670 output.write(val);
671 output.write('"""\n')
672
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000673def buildWrappers():
674 global ctypes
675 global py_types
676 global py_return_types
677 global unknown_types
678 global functions
679 global function_classes
680 global classes_type
681 global classes_list
682 global converter_type
683 global primary_classes
684 global converter_type
685 global classes_ancestor
686 global converter_type
687 global primary_classes
688 global classes_ancestor
689 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000690 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000691
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000692 for type in classes_type.keys():
693 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000694
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000695 #
696 # Build the list of C types to look for ordered to start
697 # with primary classes
698 #
699 ctypes = []
700 classes_list = []
701 ctypes_processed = {}
702 classes_processed = {}
703 for classe in primary_classes:
704 classes_list.append(classe)
705 classes_processed[classe] = ()
706 for type in classes_type.keys():
707 tinfo = classes_type[type]
708 if tinfo[2] == classe:
709 ctypes.append(type)
710 ctypes_processed[type] = ()
711 for type in classes_type.keys():
712 if ctypes_processed.has_key(type):
713 continue
714 tinfo = classes_type[type]
715 if not classes_processed.has_key(tinfo[2]):
716 classes_list.append(tinfo[2])
717 classes_processed[tinfo[2]] = ()
718
719 ctypes.append(type)
720 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000721
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000722 for name in functions.keys():
723 found = 0;
724 (desc, ret, args, file) = functions[name]
725 for type in ctypes:
726 classe = classes_type[type][2]
727
728 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
729 found = 1
730 func = nameFixup(name, classe, type, file)
731 info = (0, func, name, ret, args, file)
732 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000733 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
734 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000735 found = 1
736 func = nameFixup(name, classe, type, file)
737 info = (1, func, name, ret, args, file)
738 function_classes[classe].append(info)
739 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
740 found = 1
741 func = nameFixup(name, classe, type, file)
742 info = (0, func, name, ret, args, file)
743 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000744 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
745 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000746 found = 1
747 func = nameFixup(name, classe, type, file)
748 info = (1, func, name, ret, args, file)
749 function_classes[classe].append(info)
750 if found == 1:
751 break
752 if found == 1:
753 continue
754 if name[0:8] == "xmlXPath":
755 continue
756 if name[0:6] == "xmlStr":
757 continue
758 if name[0:10] == "xmlCharStr":
759 continue
760 func = nameFixup(name, "None", file, file)
761 info = (0, func, name, ret, args, file)
762 function_classes['None'].append(info)
763
764 classes = open("libxml2class.py", "w")
765 txt = open("libxml2class.txt", "w")
766 txt.write(" Generated Classes for libxml2-python\n\n")
767
768 txt.write("#\n# Global functions of the module\n#\n\n")
769 if function_classes.has_key("None"):
770 flist = function_classes["None"]
771 flist.sort(functionCompare)
772 oldfile = ""
773 for info in flist:
774 (index, func, name, ret, args, file) = info
775 if file != oldfile:
776 classes.write("#\n# Functions from module %s\n#\n\n" % file)
777 txt.write("\n# functions from module %s\n" % file)
778 oldfile = file
779 classes.write("def %s(" % func)
780 txt.write("%s()\n" % func);
781 n = 0
782 for arg in args:
783 if n != 0:
784 classes.write(", ")
785 classes.write("%s" % arg[0])
786 n = n + 1
787 classes.write("):\n")
788 writeDoc(name, args, ' ', classes);
789
790 for arg in args:
791 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000792 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000793 (arg[0], arg[0]))
794 classes.write(" else: %s__o = %s%s\n" %
795 (arg[0], arg[0], classes_type[arg[1]][0]))
796 if ret[0] != "void":
797 classes.write(" ret = ");
798 else:
799 classes.write(" ");
800 classes.write("libxml2mod.%s(" % name)
801 n = 0
802 for arg in args:
803 if n != 0:
804 classes.write(", ");
805 classes.write("%s" % arg[0])
806 if classes_type.has_key(arg[1]):
807 classes.write("__o");
808 n = n + 1
809 classes.write(")\n");
810 if ret[0] != "void":
811 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000812 #
813 # Raise an exception
814 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000815 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000816 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000817 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000818 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000819 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000820 % (name))
821 elif string.find(name, "XPath") >= 0:
822 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000823 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000824 % (name))
825 elif string.find(name, "Parse") >= 0:
826 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000827 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000828 % (name))
829 else:
830 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000831 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000832 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000833 classes.write(" return ");
834 classes.write(classes_type[ret[0]][1] % ("ret"));
835 classes.write("\n");
836 else:
837 classes.write(" return ret\n");
838 classes.write("\n");
839
840 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
841 for classname in classes_list:
842 if classname == "None":
843 pass
844 else:
845 if classes_ancestor.has_key(classname):
846 txt.write("\n\nClass %s(%s)\n" % (classname,
847 classes_ancestor[classname]))
848 classes.write("class %s(%s):\n" % (classname,
849 classes_ancestor[classname]))
850 classes.write(" def __init__(self, _obj=None):\n")
851 classes.write(" self._o = None\n")
852 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
853 classes_ancestor[classname]))
854 if classes_ancestor[classname] == "xmlCore" or \
855 classes_ancestor[classname] == "xmlNode":
856 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +0000857 format = "<%s (%%s) object at 0x%%x>" % (classname)
858 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000859 format))
860 else:
861 txt.write("Class %s()\n" % (classname))
862 classes.write("class %s:\n" % (classname))
863 classes.write(" def __init__(self, _obj=None):\n")
864 classes.write(" if _obj != None:self._o = _obj;return\n")
865 classes.write(" self._o = None\n\n");
866 if classes_destructors.has_key(classname):
867 classes.write(" def __del__(self):\n")
868 classes.write(" if self._o != None:\n")
869 classes.write(" libxml2mod.%s(self._o)\n" %
870 classes_destructors[classname]);
871 classes.write(" self._o = None\n\n");
872 flist = function_classes[classname]
873 flist.sort(functionCompare)
874 oldfile = ""
875 for info in flist:
876 (index, func, name, ret, args, file) = info
877 if file != oldfile:
878 if file == "python_accessor":
879 classes.write(" # accessors for %s\n" % (classname))
880 txt.write(" # accessors\n")
881 else:
882 classes.write(" #\n")
883 classes.write(" # %s functions from module %s\n" % (
884 classname, file))
885 txt.write("\n # functions from module %s\n" % file)
886 classes.write(" #\n\n")
887 oldfile = file
888 classes.write(" def %s(self" % func)
889 txt.write(" %s()\n" % func);
890 n = 0
891 for arg in args:
892 if n != index:
893 classes.write(", %s" % arg[0])
894 n = n + 1
895 classes.write("):\n")
896 writeDoc(name, args, ' ', classes);
897 n = 0
898 for arg in args:
899 if classes_type.has_key(arg[1]):
900 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000901 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000902 (arg[0], arg[0]))
903 classes.write(" else: %s__o = %s%s\n" %
904 (arg[0], arg[0], classes_type[arg[1]][0]))
905 n = n + 1
906 if ret[0] != "void":
907 classes.write(" ret = ");
908 else:
909 classes.write(" ");
910 classes.write("libxml2mod.%s(" % name)
911 n = 0
912 for arg in args:
913 if n != 0:
914 classes.write(", ");
915 if n != index:
916 classes.write("%s" % arg[0])
917 if classes_type.has_key(arg[1]):
918 classes.write("__o");
919 else:
920 classes.write("self");
921 if classes_type.has_key(arg[1]):
922 classes.write(classes_type[arg[1]][0])
923 n = n + 1
924 classes.write(")\n");
925 if ret[0] != "void":
926 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000927 #
928 # Raise an exception
929 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000930 if functions_noexcept.has_key(name):
931 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000932 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000933 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000934 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000935 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000936 % (name))
937 elif string.find(name, "XPath") >= 0:
938 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000939 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000940 % (name))
941 elif string.find(name, "Parse") >= 0:
942 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000943 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000944 % (name))
945 else:
946 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000947 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000948 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000949 classes.write(" return ");
950 classes.write(classes_type[ret[0]][1] % ("ret"));
951 classes.write("\n");
952 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000953 #
954 # Raise an exception
955 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000956 if functions_noexcept.has_key(name):
957 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000958 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000959 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000960 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000961 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000962 % (name))
963 elif string.find(name, "XPath") >= 0:
964 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000965 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000966 % (name))
967 elif string.find(name, "Parse") >= 0:
968 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000969 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000970 % (name))
971 else:
972 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000973 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000974 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000975 classes.write(" return ");
976 classes.write(converter_type[ret[0]] % ("ret"));
977 classes.write("\n");
978 else:
979 classes.write(" return ret\n");
980 classes.write("\n");
981
982 txt.close()
983 classes.close()
984
985
986buildStubs()
987buildWrappers()