blob: e2a4204f579fa0aa1e0a0e480903c657c835816b [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 Veillardc6d4a932002-09-12 15:00:57 +0000271 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
272 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000273}
274
275py_return_types = {
276 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000277}
278
279unknown_types = {}
280
Daniel Veillard1971ee22002-01-31 20:29:19 +0000281#######################################################################
282#
283# This part writes the C <-> Python stubs libxml2-py.[ch] and
284# the table libxml2-export.c to add when registrering the Python module
285#
286#######################################################################
287
288def skip_function(name):
289 if name[0:12] == "xmlXPathWrap":
290 return 1
291# if name[0:11] == "xmlXPathNew":
292# return 1
293 return 0
294
Daniel Veillard96fe0952002-01-30 20:52:23 +0000295def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000296 global py_types
297 global unknown_types
298 global functions
299 global skipped_modules
300
301 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000302 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000303 except:
304 print "failed to get function %s infos"
305 return
306
307 if skipped_modules.has_key(file):
308 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000309 if skip_function(name) == 1:
310 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000311
312 c_call = "";
313 format=""
314 format_args=""
315 c_args=""
316 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000317 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000318 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000319 # This should be correct
320 if arg[1][0:6] == "const ":
321 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000322 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000323 if py_types.has_key(arg[1]):
324 (f, t, n, c) = py_types[arg[1]]
325 if f != None:
326 format = format + f
327 if t != None:
328 format_args = format_args + ", &pyobj_%s" % (arg[0])
329 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
330 c_convert = c_convert + \
331 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
332 arg[1], t, arg[0]);
333 else:
334 format_args = format_args + ", &%s" % (arg[0])
335 if c_call != "":
336 c_call = c_call + ", ";
337 c_call = c_call + "%s" % (arg[0])
338 else:
339 if skipped_types.has_key(arg[1]):
340 return 0
341 if unknown_types.has_key(arg[1]):
342 lst = unknown_types[arg[1]]
343 lst.append(name)
344 else:
345 unknown_types[arg[1]] = [name]
346 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000347 if format != "":
348 format = format + ":%s" % (name)
349
350 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000351 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000352 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
353 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
354 args[0][0], args[1][0], args[0][0], args[1][0])
Daniel Veillardd2379012002-03-15 22:24:56 +0000355 c_call = c_call + " %s->%s = xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
Daniel Veillard6361da02002-02-23 10:10:33 +0000356 args[1][0], args[1][0])
357 else:
358 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
359 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000360 else:
361 c_call = "\n %s(%s);\n" % (name, c_call);
362 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000363 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000364 (f, t, n, c) = py_types[ret[0]]
365 c_return = " %s c_retval;\n" % (ret[0])
366 if file == "python_accessor" and ret[2] != None:
367 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
368 else:
369 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
370 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
371 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000372 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000373 (f, t, n, c) = py_return_types[ret[0]]
374 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000375 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000376 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
377 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000378 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000379 if skipped_types.has_key(ret[0]):
380 return 0
381 if unknown_types.has_key(ret[0]):
382 lst = unknown_types[ret[0]]
383 lst.append(name)
384 else:
385 unknown_types[ret[0]] = [name]
386 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000387
Daniel Veillard42766c02002-08-22 20:52:17 +0000388 if file == "debugXML":
389 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
390 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
391 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
392 elif file == "HTMLtree" or file == "HTMLparser":
393 include.write("#ifdef LIBXML_HTML_ENABLED\n");
394 export.write("#ifdef LIBXML_HTML_ENABLED\n");
395 output.write("#ifdef LIBXML_HTML_ENABLED\n");
396 elif file == "c14n":
397 include.write("#ifdef LIBXML_C14N_ENABLED\n");
398 export.write("#ifdef LIBXML_C14N_ENABLED\n");
399 output.write("#ifdef LIBXML_C14N_ENABLED\n");
400 elif file == "xpathInternals" or file == "xpath":
401 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
402 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
403 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
404 elif file == "xpointer":
405 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
406 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
407 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
408 elif file == "xinclude":
409 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
410 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
411 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
412
Daniel Veillard96fe0952002-01-30 20:52:23 +0000413 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000414 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000415
Daniel Veillardd2379012002-03-15 22:24:56 +0000416 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000417 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000418
419 if file == "python":
420 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000421 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000422 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000423 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000424 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000425
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000426 output.write("PyObject *\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000427 output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name))
428 if format == "":
429 output.write("ATTRIBUTE_UNUSED ")
430 output.write(" PyObject *args) {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000431 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000432 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000433 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000434 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000435 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000436 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000437 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000438 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000439 (format, format_args))
440 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000441 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000442 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000443
444 output.write(c_call)
445 output.write(ret_convert)
446 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000447 if file == "debugXML":
448 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
449 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
450 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
451 elif file == "HTMLtree" or file == "HTMLparser":
452 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
453 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
454 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
455 elif file == "c14n":
456 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
457 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
458 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
459 elif file == "xpathInternals" or file == "xpath":
460 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
461 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
462 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
463 elif file == "xpointer":
464 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
465 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
466 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
467 elif file == "xinclude":
468 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
469 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
470 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000471 return 1
472
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000473def buildStubs():
474 global py_types
475 global py_return_types
476 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000477
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000478 try:
479 f = open("libxml2-api.xml")
480 data = f.read()
481 (parser, target) = getparser()
482 parser.feed(data)
483 parser.close()
484 except IOError, msg:
485 try:
486 f = open("../doc/libxml2-api.xml")
487 data = f.read()
488 (parser, target) = getparser()
489 parser.feed(data)
490 parser.close()
491 except IOError, msg:
492 print file, ":", msg
493 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000494
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000495 n = len(functions.keys())
496 print "Found %d functions in libxml2-api.xml" % (n)
497
498 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
499 try:
500 f = open("libxml2-python-api.xml")
501 data = f.read()
502 (parser, target) = getparser()
503 parser.feed(data)
504 parser.close()
505 except IOError, msg:
506 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000507
508
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000509 print "Found %d functions in libxml2-python-api.xml" % (
510 len(functions.keys()) - n)
511 nb_wrap = 0
512 failed = 0
513 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000514
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000515 include = open("libxml2-py.h", "w")
516 include.write("/* Generated */\n\n")
517 export = open("libxml2-export.c", "w")
518 export.write("/* Generated */\n\n")
519 wrapper = open("libxml2-py.c", "w")
520 wrapper.write("/* Generated */\n\n")
521 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000522 wrapper.write("#include \"config.h\"\n")
523 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000524 wrapper.write("#include <libxml/tree.h>\n")
525 wrapper.write("#include \"libxml_wrap.h\"\n")
526 wrapper.write("#include \"libxml2-py.h\"\n\n")
527 for function in functions.keys():
528 ret = print_function_wrapper(function, wrapper, export, include)
529 if ret < 0:
530 failed = failed + 1
531 del functions[function]
532 if ret == 0:
533 skipped = skipped + 1
534 del functions[function]
535 if ret == 1:
536 nb_wrap = nb_wrap + 1
537 include.close()
538 export.close()
539 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000540
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000541 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
542 failed, skipped);
543 print "Missing type converters: "
544 for type in unknown_types.keys():
545 print "%s:%d " % (type, len(unknown_types[type])),
546 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000547
Daniel Veillard1971ee22002-01-31 20:29:19 +0000548#######################################################################
549#
550# This part writes part of the Python front-end classes based on
551# mapping rules between types and classes and also based on function
552# renaming to get consistent function names at the Python level
553#
554#######################################################################
555
556#
557# The type automatically remapped to generated classes
558#
559classes_type = {
560 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
561 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
562 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
563 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
564 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
565 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
566 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
567 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
568 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
569 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
570 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
571 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
572 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
573 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
574 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
575 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
576 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
577 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
578 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000579 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
580 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
581 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000582 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
583 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000584 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
585 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000586 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000587 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000588 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
589 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000590}
591
592converter_type = {
593 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
594}
595
596primary_classes = ["xmlNode", "xmlDoc"]
597
598classes_ancestor = {
599 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000600 "xmlDtd" : "xmlNode",
601 "xmlDoc" : "xmlNode",
602 "xmlAttr" : "xmlNode",
603 "xmlNs" : "xmlNode",
604 "xmlEntity" : "xmlNode",
605 "xmlElement" : "xmlNode",
606 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000607 "outputBuffer": "ioWriteWrapper",
608 "inputBuffer": "ioReadWrapper",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000609}
610classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000611 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000612 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000613 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000614# "outputBuffer": "xmlOutputBufferClose",
615 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000616}
617
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000618functions_noexcept = {
619 "xmlHasProp": 1,
620 "xmlHasNsProp": 1,
621}
622
Daniel Veillard36ed5292002-01-30 23:49:06 +0000623function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000624
625function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000626
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000627def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000628 listname = classe + "List"
629 ll = len(listname)
630 l = len(classe)
631 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000632 func = name[l:]
633 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000634 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
635 func = name[12:]
636 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000637 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
638 func = name[12:]
639 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000640 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
641 func = name[10:]
642 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000643 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
644 func = name[9:]
645 func = string.lower(func[0:1]) + func[1:]
646 elif name[0:9] == "xmlURISet" and file == "python_accessor":
647 func = name[6:]
648 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000649 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
650 func = name[17:]
651 func = string.lower(func[0:1]) + func[1:]
652 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
653 func = name[11:]
654 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000655 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
656 func = name[8:]
657 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000658 elif name[0:15] == "xmlOutputBuffer" and file != "python":
659 func = name[15:]
660 func = string.lower(func[0:1]) + func[1:]
661 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
662 func = name[20:]
663 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000664 elif name[0:11] == "xmlACatalog":
665 func = name[11:]
666 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000667 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000668 func = name[l:]
669 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000670 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000671 func = name[7:]
672 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000673 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000674 func = name[6:]
675 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000676 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000677 func = name[3:]
678 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000679 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000680 func = name
681 if func[0:5] == "xPath":
682 func = "xpath" + func[5:]
683 elif func[0:4] == "xPtr":
684 func = "xpointer" + func[4:]
685 elif func[0:8] == "xInclude":
686 func = "xinclude" + func[8:]
687 elif func[0:2] == "iD":
688 func = "ID" + func[2:]
689 elif func[0:3] == "uRI":
690 func = "URI" + func[3:]
691 elif func[0:4] == "uTF8":
692 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000693 elif func[0:3] == 'sAX':
694 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000695 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000696
Daniel Veillard36ed5292002-01-30 23:49:06 +0000697
Daniel Veillard1971ee22002-01-31 20:29:19 +0000698def functionCompare(info1, info2):
699 (index1, func1, name1, ret1, args1, file1) = info1
700 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000701 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000702 if func1 < func2:
703 return -1
704 if func1 > func2:
705 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000706 if file1 == "python_accessor":
707 return -1
708 if file2 == "python_accessor":
709 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000710 if file1 < file2:
711 return -1
712 if file1 > file2:
713 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000714 return 0
715
716def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000717 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000718 return
719 val = functions[name][0]
720 val = string.replace(val, "NULL", "None");
721 output.write(indent)
722 output.write('"""')
723 while len(val) > 60:
724 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000725 i = string.rfind(str, " ");
726 if i < 0:
727 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000728 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000729 val = val[i:]
730 output.write(str)
731 output.write('\n ');
732 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000733 output.write(val);
734 output.write('"""\n')
735
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000736def buildWrappers():
737 global ctypes
738 global py_types
739 global py_return_types
740 global unknown_types
741 global functions
742 global function_classes
743 global classes_type
744 global classes_list
745 global converter_type
746 global primary_classes
747 global converter_type
748 global classes_ancestor
749 global converter_type
750 global primary_classes
751 global classes_ancestor
752 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000753 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000754
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000755 for type in classes_type.keys():
756 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000757
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000758 #
759 # Build the list of C types to look for ordered to start
760 # with primary classes
761 #
762 ctypes = []
763 classes_list = []
764 ctypes_processed = {}
765 classes_processed = {}
766 for classe in primary_classes:
767 classes_list.append(classe)
768 classes_processed[classe] = ()
769 for type in classes_type.keys():
770 tinfo = classes_type[type]
771 if tinfo[2] == classe:
772 ctypes.append(type)
773 ctypes_processed[type] = ()
774 for type in classes_type.keys():
775 if ctypes_processed.has_key(type):
776 continue
777 tinfo = classes_type[type]
778 if not classes_processed.has_key(tinfo[2]):
779 classes_list.append(tinfo[2])
780 classes_processed[tinfo[2]] = ()
781
782 ctypes.append(type)
783 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000784
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000785 for name in functions.keys():
786 found = 0;
787 (desc, ret, args, file) = functions[name]
788 for type in ctypes:
789 classe = classes_type[type][2]
790
791 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
792 found = 1
793 func = nameFixup(name, classe, type, file)
794 info = (0, func, name, ret, args, file)
795 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000796 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
797 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000798 found = 1
799 func = nameFixup(name, classe, type, file)
800 info = (1, func, name, ret, args, file)
801 function_classes[classe].append(info)
802 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
803 found = 1
804 func = nameFixup(name, classe, type, file)
805 info = (0, func, name, ret, args, file)
806 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000807 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
808 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000809 found = 1
810 func = nameFixup(name, classe, type, file)
811 info = (1, func, name, ret, args, file)
812 function_classes[classe].append(info)
813 if found == 1:
814 break
815 if found == 1:
816 continue
817 if name[0:8] == "xmlXPath":
818 continue
819 if name[0:6] == "xmlStr":
820 continue
821 if name[0:10] == "xmlCharStr":
822 continue
823 func = nameFixup(name, "None", file, file)
824 info = (0, func, name, ret, args, file)
825 function_classes['None'].append(info)
826
827 classes = open("libxml2class.py", "w")
828 txt = open("libxml2class.txt", "w")
829 txt.write(" Generated Classes for libxml2-python\n\n")
830
831 txt.write("#\n# Global functions of the module\n#\n\n")
832 if function_classes.has_key("None"):
833 flist = function_classes["None"]
834 flist.sort(functionCompare)
835 oldfile = ""
836 for info in flist:
837 (index, func, name, ret, args, file) = info
838 if file != oldfile:
839 classes.write("#\n# Functions from module %s\n#\n\n" % file)
840 txt.write("\n# functions from module %s\n" % file)
841 oldfile = file
842 classes.write("def %s(" % func)
843 txt.write("%s()\n" % func);
844 n = 0
845 for arg in args:
846 if n != 0:
847 classes.write(", ")
848 classes.write("%s" % arg[0])
849 n = n + 1
850 classes.write("):\n")
851 writeDoc(name, args, ' ', classes);
852
853 for arg in args:
854 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000855 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000856 (arg[0], arg[0]))
857 classes.write(" else: %s__o = %s%s\n" %
858 (arg[0], arg[0], classes_type[arg[1]][0]))
859 if ret[0] != "void":
860 classes.write(" ret = ");
861 else:
862 classes.write(" ");
863 classes.write("libxml2mod.%s(" % name)
864 n = 0
865 for arg in args:
866 if n != 0:
867 classes.write(", ");
868 classes.write("%s" % arg[0])
869 if classes_type.has_key(arg[1]):
870 classes.write("__o");
871 n = n + 1
872 classes.write(")\n");
873 if ret[0] != "void":
874 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000875 #
876 # Raise an exception
877 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000878 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000879 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000880 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000881 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000882 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000883 % (name))
884 elif string.find(name, "XPath") >= 0:
885 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000886 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000887 % (name))
888 elif string.find(name, "Parse") >= 0:
889 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000890 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000891 % (name))
892 else:
893 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000894 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000895 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000896 classes.write(" return ");
897 classes.write(classes_type[ret[0]][1] % ("ret"));
898 classes.write("\n");
899 else:
900 classes.write(" return ret\n");
901 classes.write("\n");
902
903 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
904 for classname in classes_list:
905 if classname == "None":
906 pass
907 else:
908 if classes_ancestor.has_key(classname):
909 txt.write("\n\nClass %s(%s)\n" % (classname,
910 classes_ancestor[classname]))
911 classes.write("class %s(%s):\n" % (classname,
912 classes_ancestor[classname]))
913 classes.write(" def __init__(self, _obj=None):\n")
914 classes.write(" self._o = None\n")
915 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
916 classes_ancestor[classname]))
917 if classes_ancestor[classname] == "xmlCore" or \
918 classes_ancestor[classname] == "xmlNode":
919 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +0000920 format = "<%s (%%s) object at 0x%%x>" % (classname)
921 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000922 format))
923 else:
924 txt.write("Class %s()\n" % (classname))
925 classes.write("class %s:\n" % (classname))
926 classes.write(" def __init__(self, _obj=None):\n")
927 classes.write(" if _obj != None:self._o = _obj;return\n")
928 classes.write(" self._o = None\n\n");
929 if classes_destructors.has_key(classname):
930 classes.write(" def __del__(self):\n")
931 classes.write(" if self._o != None:\n")
932 classes.write(" libxml2mod.%s(self._o)\n" %
933 classes_destructors[classname]);
934 classes.write(" self._o = None\n\n");
935 flist = function_classes[classname]
936 flist.sort(functionCompare)
937 oldfile = ""
938 for info in flist:
939 (index, func, name, ret, args, file) = info
940 if file != oldfile:
941 if file == "python_accessor":
942 classes.write(" # accessors for %s\n" % (classname))
943 txt.write(" # accessors\n")
944 else:
945 classes.write(" #\n")
946 classes.write(" # %s functions from module %s\n" % (
947 classname, file))
948 txt.write("\n # functions from module %s\n" % file)
949 classes.write(" #\n\n")
950 oldfile = file
951 classes.write(" def %s(self" % func)
952 txt.write(" %s()\n" % func);
953 n = 0
954 for arg in args:
955 if n != index:
956 classes.write(", %s" % arg[0])
957 n = n + 1
958 classes.write("):\n")
959 writeDoc(name, args, ' ', classes);
960 n = 0
961 for arg in args:
962 if classes_type.has_key(arg[1]):
963 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000964 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000965 (arg[0], arg[0]))
966 classes.write(" else: %s__o = %s%s\n" %
967 (arg[0], arg[0], classes_type[arg[1]][0]))
968 n = n + 1
969 if ret[0] != "void":
970 classes.write(" ret = ");
971 else:
972 classes.write(" ");
973 classes.write("libxml2mod.%s(" % name)
974 n = 0
975 for arg in args:
976 if n != 0:
977 classes.write(", ");
978 if n != index:
979 classes.write("%s" % arg[0])
980 if classes_type.has_key(arg[1]):
981 classes.write("__o");
982 else:
983 classes.write("self");
984 if classes_type.has_key(arg[1]):
985 classes.write(classes_type[arg[1]][0])
986 n = n + 1
987 classes.write(")\n");
988 if ret[0] != "void":
989 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000990 #
991 # Raise an exception
992 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000993 if functions_noexcept.has_key(name):
994 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000995 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000996 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000997 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000998 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000999 % (name))
1000 elif string.find(name, "XPath") >= 0:
1001 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001002 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001003 % (name))
1004 elif string.find(name, "Parse") >= 0:
1005 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001006 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001007 % (name))
1008 else:
1009 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001010 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001011 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001012 classes.write(" return ");
1013 classes.write(classes_type[ret[0]][1] % ("ret"));
1014 classes.write("\n");
1015 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001016 #
1017 # Raise an exception
1018 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001019 if functions_noexcept.has_key(name):
1020 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001021 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001022 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001023 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001024 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001025 % (name))
1026 elif string.find(name, "XPath") >= 0:
1027 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001028 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001029 % (name))
1030 elif string.find(name, "Parse") >= 0:
1031 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001032 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001033 % (name))
1034 else:
1035 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001036 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001037 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001038 classes.write(" return ");
1039 classes.write(converter_type[ret[0]] % ("ret"));
1040 classes.write("\n");
1041 else:
1042 classes.write(" return ret\n");
1043 classes.write("\n");
1044
1045 txt.close()
1046 classes.close()
1047
1048
1049buildStubs()
1050buildWrappers()