blob: c65728c6aa156700b6c06c017ff55d5786b98616 [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 Veillardff12c492003-01-23 16:42:55 +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 Veillardbd9afb52002-09-25 22:25:35 +0000273 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000274 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000275 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000276 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
277 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
278 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000279}
280
281py_return_types = {
282 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000283}
284
285unknown_types = {}
286
Daniel Veillard1971ee22002-01-31 20:29:19 +0000287#######################################################################
288#
289# This part writes the C <-> Python stubs libxml2-py.[ch] and
290# the table libxml2-export.c to add when registrering the Python module
291#
292#######################################################################
293
294def skip_function(name):
295 if name[0:12] == "xmlXPathWrap":
296 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000297 if name == "xmlFreeParserCtxt":
298 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000299 if name == "xmlFreeTextReader":
300 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000301# if name[0:11] == "xmlXPathNew":
302# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000303 # the next function is defined in libxml.c
304 if name == "xmlRelaxNGFreeValidCtxt":
305 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000306 return 0
307
Daniel Veillard96fe0952002-01-30 20:52:23 +0000308def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000309 global py_types
310 global unknown_types
311 global functions
312 global skipped_modules
313
314 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000315 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000316 except:
317 print "failed to get function %s infos"
318 return
319
320 if skipped_modules.has_key(file):
321 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000322 if skip_function(name) == 1:
323 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000324
325 c_call = "";
326 format=""
327 format_args=""
328 c_args=""
329 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000330 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000331 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000332 # This should be correct
333 if arg[1][0:6] == "const ":
334 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000335 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000336 if py_types.has_key(arg[1]):
337 (f, t, n, c) = py_types[arg[1]]
338 if f != None:
339 format = format + f
340 if t != None:
341 format_args = format_args + ", &pyobj_%s" % (arg[0])
342 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
343 c_convert = c_convert + \
344 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
345 arg[1], t, arg[0]);
346 else:
347 format_args = format_args + ", &%s" % (arg[0])
348 if c_call != "":
349 c_call = c_call + ", ";
350 c_call = c_call + "%s" % (arg[0])
351 else:
352 if skipped_types.has_key(arg[1]):
353 return 0
354 if unknown_types.has_key(arg[1]):
355 lst = unknown_types[arg[1]]
356 lst.append(name)
357 else:
358 unknown_types[arg[1]] = [name]
359 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000360 if format != "":
361 format = format + ":%s" % (name)
362
363 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000364 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000365 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
366 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
367 args[0][0], args[1][0], args[0][0], args[1][0])
Daniel Veillardd2379012002-03-15 22:24:56 +0000368 c_call = c_call + " %s->%s = xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
Daniel Veillard6361da02002-02-23 10:10:33 +0000369 args[1][0], args[1][0])
370 else:
371 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
372 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000373 else:
374 c_call = "\n %s(%s);\n" % (name, c_call);
375 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000376 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000377 (f, t, n, c) = py_types[ret[0]]
378 c_return = " %s c_retval;\n" % (ret[0])
379 if file == "python_accessor" and ret[2] != None:
380 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
381 else:
382 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
383 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
384 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000385 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000386 (f, t, n, c) = py_return_types[ret[0]]
387 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000388 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000389 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
390 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000391 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000392 if skipped_types.has_key(ret[0]):
393 return 0
394 if unknown_types.has_key(ret[0]):
395 lst = unknown_types[ret[0]]
396 lst.append(name)
397 else:
398 unknown_types[ret[0]] = [name]
399 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000400
Daniel Veillard42766c02002-08-22 20:52:17 +0000401 if file == "debugXML":
402 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
403 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
404 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
405 elif file == "HTMLtree" or file == "HTMLparser":
406 include.write("#ifdef LIBXML_HTML_ENABLED\n");
407 export.write("#ifdef LIBXML_HTML_ENABLED\n");
408 output.write("#ifdef LIBXML_HTML_ENABLED\n");
409 elif file == "c14n":
410 include.write("#ifdef LIBXML_C14N_ENABLED\n");
411 export.write("#ifdef LIBXML_C14N_ENABLED\n");
412 output.write("#ifdef LIBXML_C14N_ENABLED\n");
413 elif file == "xpathInternals" or file == "xpath":
414 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
415 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
416 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
417 elif file == "xpointer":
418 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
419 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
420 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
421 elif file == "xinclude":
422 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
423 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
424 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000425 elif file == "xmlregexp":
426 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
427 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
428 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000429 elif file == "xmlschemas" or file == "xmlschemastypes" or \
430 file == "relaxng":
431 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
432 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
433 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000434
Daniel Veillard96fe0952002-01-30 20:52:23 +0000435 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000436 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000437
Daniel Veillardd2379012002-03-15 22:24:56 +0000438 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000439 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000440
441 if file == "python":
442 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000443 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000444 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000445 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000446 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000447
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000448 output.write("PyObject *\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000449 output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name))
450 if format == "":
451 output.write("ATTRIBUTE_UNUSED ")
452 output.write(" PyObject *args) {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000453 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000454 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000455 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000456 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000457 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000458 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000459 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000460 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000461 (format, format_args))
462 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000463 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000464 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000465
466 output.write(c_call)
467 output.write(ret_convert)
468 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000469 if file == "debugXML":
470 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
471 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
472 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
473 elif file == "HTMLtree" or file == "HTMLparser":
474 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
475 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
476 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
477 elif file == "c14n":
478 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
479 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
480 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
481 elif file == "xpathInternals" or file == "xpath":
482 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
483 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
484 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
485 elif file == "xpointer":
486 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
487 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
488 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
489 elif file == "xinclude":
490 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
491 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
492 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000493 elif file == "xmlregexp":
494 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
495 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
496 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000497 elif file == "xmlschemas" or file == "xmlschemastypes" or \
498 file == "relaxng":
499 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
500 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
501 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000502 return 1
503
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000504def buildStubs():
505 global py_types
506 global py_return_types
507 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000508
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000509 try:
510 f = open("libxml2-api.xml")
511 data = f.read()
512 (parser, target) = getparser()
513 parser.feed(data)
514 parser.close()
515 except IOError, msg:
516 try:
517 f = open("../doc/libxml2-api.xml")
518 data = f.read()
519 (parser, target) = getparser()
520 parser.feed(data)
521 parser.close()
522 except IOError, msg:
523 print file, ":", msg
524 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000525
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000526 n = len(functions.keys())
527 print "Found %d functions in libxml2-api.xml" % (n)
528
529 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
530 try:
531 f = open("libxml2-python-api.xml")
532 data = f.read()
533 (parser, target) = getparser()
534 parser.feed(data)
535 parser.close()
536 except IOError, msg:
537 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000538
539
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000540 print "Found %d functions in libxml2-python-api.xml" % (
541 len(functions.keys()) - n)
542 nb_wrap = 0
543 failed = 0
544 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000545
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000546 include = open("libxml2-py.h", "w")
547 include.write("/* Generated */\n\n")
548 export = open("libxml2-export.c", "w")
549 export.write("/* Generated */\n\n")
550 wrapper = open("libxml2-py.c", "w")
551 wrapper.write("/* Generated */\n\n")
552 wrapper.write("#include <Python.h>\n")
Daniel Veillarda1196ed2002-11-23 11:22:49 +0000553# wrapper.write("#include \"config.h\"\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000554 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000555 wrapper.write("#include <libxml/tree.h>\n")
556 wrapper.write("#include \"libxml_wrap.h\"\n")
557 wrapper.write("#include \"libxml2-py.h\"\n\n")
558 for function in functions.keys():
559 ret = print_function_wrapper(function, wrapper, export, include)
560 if ret < 0:
561 failed = failed + 1
562 del functions[function]
563 if ret == 0:
564 skipped = skipped + 1
565 del functions[function]
566 if ret == 1:
567 nb_wrap = nb_wrap + 1
568 include.close()
569 export.close()
570 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000571
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000572 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
573 failed, skipped);
574 print "Missing type converters: "
575 for type in unknown_types.keys():
576 print "%s:%d " % (type, len(unknown_types[type])),
577 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000578
Daniel Veillard1971ee22002-01-31 20:29:19 +0000579#######################################################################
580#
581# This part writes part of the Python front-end classes based on
582# mapping rules between types and classes and also based on function
583# renaming to get consistent function names at the Python level
584#
585#######################################################################
586
587#
588# The type automatically remapped to generated classes
589#
590classes_type = {
591 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
592 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
593 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
594 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
595 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
596 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
597 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
598 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
599 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
600 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
601 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
602 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
603 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
604 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
605 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
606 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
607 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
608 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
609 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000610 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
611 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
612 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000613 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
614 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000615 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
616 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000617 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000618 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000619 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
620 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000621 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000622 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000623 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000624 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
625 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
626 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000627}
628
629converter_type = {
630 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
631}
632
633primary_classes = ["xmlNode", "xmlDoc"]
634
635classes_ancestor = {
636 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000637 "xmlDtd" : "xmlNode",
638 "xmlDoc" : "xmlNode",
639 "xmlAttr" : "xmlNode",
640 "xmlNs" : "xmlNode",
641 "xmlEntity" : "xmlNode",
642 "xmlElement" : "xmlNode",
643 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000644 "outputBuffer": "ioWriteWrapper",
645 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000646 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000647 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000648}
649classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000650 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000651 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000652 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000653# "outputBuffer": "xmlOutputBufferClose",
654 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000655 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000656 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000657 "relaxNgSchema": "xmlRelaxNGFree",
658 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
659 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000660}
661
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000662functions_noexcept = {
663 "xmlHasProp": 1,
664 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000665 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000666}
667
Daniel Veillarddc85f282002-12-31 11:18:37 +0000668reference_keepers = {
669 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000670 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000671}
672
Daniel Veillard36ed5292002-01-30 23:49:06 +0000673function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000674
675function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000676
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000677def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000678 listname = classe + "List"
679 ll = len(listname)
680 l = len(classe)
681 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000682 func = name[l:]
683 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000684 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
685 func = name[12:]
686 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000687 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
688 func = name[12:]
689 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000690 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
691 func = name[10:]
692 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000693 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
694 func = name[9:]
695 func = string.lower(func[0:1]) + func[1:]
696 elif name[0:9] == "xmlURISet" and file == "python_accessor":
697 func = name[6:]
698 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000699 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
700 func = name[17:]
701 func = string.lower(func[0:1]) + func[1:]
702 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
703 func = name[11:]
704 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000705 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
706 func = name[8:]
707 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000708 elif name[0:15] == "xmlOutputBuffer" and file != "python":
709 func = name[15:]
710 func = string.lower(func[0:1]) + func[1:]
711 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
712 func = name[20:]
713 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000714 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000715 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000716 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000717 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000718 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
719 func = name[20:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000720 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
721 func = name[13:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000722 elif name[0:11] == "xmlACatalog":
723 func = name[11:]
724 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000725 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000726 func = name[l:]
727 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000728 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000729 func = name[7:]
730 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000731 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000732 func = name[6:]
733 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000734 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000735 func = name[3:]
736 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000737 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000738 func = name
739 if func[0:5] == "xPath":
740 func = "xpath" + func[5:]
741 elif func[0:4] == "xPtr":
742 func = "xpointer" + func[4:]
743 elif func[0:8] == "xInclude":
744 func = "xinclude" + func[8:]
745 elif func[0:2] == "iD":
746 func = "ID" + func[2:]
747 elif func[0:3] == "uRI":
748 func = "URI" + func[3:]
749 elif func[0:4] == "uTF8":
750 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000751 elif func[0:3] == 'sAX':
752 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000753 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000754
Daniel Veillard36ed5292002-01-30 23:49:06 +0000755
Daniel Veillard1971ee22002-01-31 20:29:19 +0000756def functionCompare(info1, info2):
757 (index1, func1, name1, ret1, args1, file1) = info1
758 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000759 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000760 if func1 < func2:
761 return -1
762 if func1 > func2:
763 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000764 if file1 == "python_accessor":
765 return -1
766 if file2 == "python_accessor":
767 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000768 if file1 < file2:
769 return -1
770 if file1 > file2:
771 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000772 return 0
773
774def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000775 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000776 return
777 val = functions[name][0]
778 val = string.replace(val, "NULL", "None");
779 output.write(indent)
780 output.write('"""')
781 while len(val) > 60:
782 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000783 i = string.rfind(str, " ");
784 if i < 0:
785 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000786 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000787 val = val[i:]
788 output.write(str)
789 output.write('\n ');
790 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000791 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000792 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000793
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000794def buildWrappers():
795 global ctypes
796 global py_types
797 global py_return_types
798 global unknown_types
799 global functions
800 global function_classes
801 global classes_type
802 global classes_list
803 global converter_type
804 global primary_classes
805 global converter_type
806 global classes_ancestor
807 global converter_type
808 global primary_classes
809 global classes_ancestor
810 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000811 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000812
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000813 for type in classes_type.keys():
814 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000815
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000816 #
817 # Build the list of C types to look for ordered to start
818 # with primary classes
819 #
820 ctypes = []
821 classes_list = []
822 ctypes_processed = {}
823 classes_processed = {}
824 for classe in primary_classes:
825 classes_list.append(classe)
826 classes_processed[classe] = ()
827 for type in classes_type.keys():
828 tinfo = classes_type[type]
829 if tinfo[2] == classe:
830 ctypes.append(type)
831 ctypes_processed[type] = ()
832 for type in classes_type.keys():
833 if ctypes_processed.has_key(type):
834 continue
835 tinfo = classes_type[type]
836 if not classes_processed.has_key(tinfo[2]):
837 classes_list.append(tinfo[2])
838 classes_processed[tinfo[2]] = ()
839
840 ctypes.append(type)
841 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000842
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000843 for name in functions.keys():
844 found = 0;
845 (desc, ret, args, file) = functions[name]
846 for type in ctypes:
847 classe = classes_type[type][2]
848
849 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
850 found = 1
851 func = nameFixup(name, classe, type, file)
852 info = (0, func, name, ret, args, file)
853 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000854 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
855 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000856 found = 1
857 func = nameFixup(name, classe, type, file)
858 info = (1, func, name, ret, args, file)
859 function_classes[classe].append(info)
860 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
861 found = 1
862 func = nameFixup(name, classe, type, file)
863 info = (0, func, name, ret, args, file)
864 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000865 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
866 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000867 found = 1
868 func = nameFixup(name, classe, type, file)
869 info = (1, func, name, ret, args, file)
870 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000871 if found == 1:
872 continue
873 if name[0:8] == "xmlXPath":
874 continue
875 if name[0:6] == "xmlStr":
876 continue
877 if name[0:10] == "xmlCharStr":
878 continue
879 func = nameFixup(name, "None", file, file)
880 info = (0, func, name, ret, args, file)
881 function_classes['None'].append(info)
882
883 classes = open("libxml2class.py", "w")
884 txt = open("libxml2class.txt", "w")
885 txt.write(" Generated Classes for libxml2-python\n\n")
886
887 txt.write("#\n# Global functions of the module\n#\n\n")
888 if function_classes.has_key("None"):
889 flist = function_classes["None"]
890 flist.sort(functionCompare)
891 oldfile = ""
892 for info in flist:
893 (index, func, name, ret, args, file) = info
894 if file != oldfile:
895 classes.write("#\n# Functions from module %s\n#\n\n" % file)
896 txt.write("\n# functions from module %s\n" % file)
897 oldfile = file
898 classes.write("def %s(" % func)
899 txt.write("%s()\n" % func);
900 n = 0
901 for arg in args:
902 if n != 0:
903 classes.write(", ")
904 classes.write("%s" % arg[0])
905 n = n + 1
906 classes.write("):\n")
907 writeDoc(name, args, ' ', classes);
908
909 for arg in args:
910 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000911 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000912 (arg[0], arg[0]))
913 classes.write(" else: %s__o = %s%s\n" %
914 (arg[0], arg[0], classes_type[arg[1]][0]))
915 if ret[0] != "void":
916 classes.write(" ret = ");
917 else:
918 classes.write(" ");
919 classes.write("libxml2mod.%s(" % name)
920 n = 0
921 for arg in args:
922 if n != 0:
923 classes.write(", ");
924 classes.write("%s" % arg[0])
925 if classes_type.has_key(arg[1]):
926 classes.write("__o");
927 n = n + 1
928 classes.write(")\n");
929 if ret[0] != "void":
930 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000931 #
932 # Raise an exception
933 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000934 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000935 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000936 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000937 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000938 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000939 % (name))
940 elif string.find(name, "XPath") >= 0:
941 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000942 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000943 % (name))
944 elif string.find(name, "Parse") >= 0:
945 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000946 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000947 % (name))
948 else:
949 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000950 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000951 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000952 classes.write(" return ");
953 classes.write(classes_type[ret[0]][1] % ("ret"));
954 classes.write("\n");
955 else:
956 classes.write(" return ret\n");
957 classes.write("\n");
958
959 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
960 for classname in classes_list:
961 if classname == "None":
962 pass
963 else:
964 if classes_ancestor.has_key(classname):
965 txt.write("\n\nClass %s(%s)\n" % (classname,
966 classes_ancestor[classname]))
967 classes.write("class %s(%s):\n" % (classname,
968 classes_ancestor[classname]))
969 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +0000970 if reference_keepers.has_key(classname):
971 rlist = reference_keepers[classname]
972 for ref in rlist:
973 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000974 classes.write(" self._o = None\n")
975 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
976 classes_ancestor[classname]))
977 if classes_ancestor[classname] == "xmlCore" or \
978 classes_ancestor[classname] == "xmlNode":
979 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +0000980 format = "<%s (%%s) object at 0x%%x>" % (classname)
981 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000982 format))
983 else:
984 txt.write("Class %s()\n" % (classname))
985 classes.write("class %s:\n" % (classname))
986 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +0000987 if reference_keepers.has_key(classname):
988 list = reference_keepers[classname]
989 for ref in list:
990 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000991 classes.write(" if _obj != None:self._o = _obj;return\n")
992 classes.write(" self._o = None\n\n");
993 if classes_destructors.has_key(classname):
994 classes.write(" def __del__(self):\n")
995 classes.write(" if self._o != None:\n")
996 classes.write(" libxml2mod.%s(self._o)\n" %
997 classes_destructors[classname]);
998 classes.write(" self._o = None\n\n");
999 flist = function_classes[classname]
1000 flist.sort(functionCompare)
1001 oldfile = ""
1002 for info in flist:
1003 (index, func, name, ret, args, file) = info
1004 if file != oldfile:
1005 if file == "python_accessor":
1006 classes.write(" # accessors for %s\n" % (classname))
1007 txt.write(" # accessors\n")
1008 else:
1009 classes.write(" #\n")
1010 classes.write(" # %s functions from module %s\n" % (
1011 classname, file))
1012 txt.write("\n # functions from module %s\n" % file)
1013 classes.write(" #\n\n")
1014 oldfile = file
1015 classes.write(" def %s(self" % func)
1016 txt.write(" %s()\n" % func);
1017 n = 0
1018 for arg in args:
1019 if n != index:
1020 classes.write(", %s" % arg[0])
1021 n = n + 1
1022 classes.write("):\n")
1023 writeDoc(name, args, ' ', classes);
1024 n = 0
1025 for arg in args:
1026 if classes_type.has_key(arg[1]):
1027 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001028 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001029 (arg[0], arg[0]))
1030 classes.write(" else: %s__o = %s%s\n" %
1031 (arg[0], arg[0], classes_type[arg[1]][0]))
1032 n = n + 1
1033 if ret[0] != "void":
1034 classes.write(" ret = ");
1035 else:
1036 classes.write(" ");
1037 classes.write("libxml2mod.%s(" % name)
1038 n = 0
1039 for arg in args:
1040 if n != 0:
1041 classes.write(", ");
1042 if n != index:
1043 classes.write("%s" % arg[0])
1044 if classes_type.has_key(arg[1]):
1045 classes.write("__o");
1046 else:
1047 classes.write("self");
1048 if classes_type.has_key(arg[1]):
1049 classes.write(classes_type[arg[1]][0])
1050 n = n + 1
1051 classes.write(")\n");
1052 if ret[0] != "void":
1053 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001054 #
1055 # Raise an exception
1056 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001057 if functions_noexcept.has_key(name):
1058 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001059 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001060 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001061 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001062 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001063 % (name))
1064 elif string.find(name, "XPath") >= 0:
1065 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001066 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001067 % (name))
1068 elif string.find(name, "Parse") >= 0:
1069 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001070 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001071 % (name))
1072 else:
1073 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001074 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001075 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001076
1077 #
1078 # generate the returned class wrapper for the object
1079 #
1080 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001081 classes.write(classes_type[ret[0]][1] % ("ret"));
1082 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001083
1084 #
1085 # Sometime one need to keep references of the source
1086 # class in the returned class object.
1087 # See reference_keepers for the list
1088 #
1089 tclass = classes_type[ret[0]][2]
1090 if reference_keepers.has_key(tclass):
1091 list = reference_keepers[tclass]
1092 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001093 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001094 classes.write(" __tmp.%s = self\n" %
1095 pref[1])
1096 #
1097 # return the class
1098 #
1099 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001100 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001101 #
1102 # Raise an exception
1103 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001104 if functions_noexcept.has_key(name):
1105 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001106 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001107 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001108 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001109 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001110 % (name))
1111 elif string.find(name, "XPath") >= 0:
1112 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001113 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001114 % (name))
1115 elif string.find(name, "Parse") >= 0:
1116 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001117 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001118 % (name))
1119 else:
1120 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001121 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001122 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001123 classes.write(" return ");
1124 classes.write(converter_type[ret[0]] % ("ret"));
1125 classes.write("\n");
1126 else:
1127 classes.write(" return ret\n");
1128 classes.write("\n");
1129
1130 txt.close()
1131 classes.close()
1132
1133
1134buildStubs()
1135buildWrappers()