blob: 3896177c8b26de12baae6479848d43bf162062d7 [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 Veillard198c1bf2003-10-20 17:07:41 +0000306#
307# Those are skipped because the Const version is used of the bindings
308# instead.
309#
310 if name == "xmlTextReaderBaseUri":
311 return 1
312 if name == "xmlTextReaderLocalName":
313 return 1
314 if name == "xmlTextReaderName":
315 return 1
316 if name == "xmlTextReaderNamespaceUri":
317 return 1
318 if name == "xmlTextReaderPrefix":
319 return 1
320 if name == "xmlTextReaderXmlLang":
321 return 1
322 if name == "xmlTextReaderValue":
323 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000324 if name == "xmlOutputBufferClose": # handled by by the superclass
325 return 1
326 if name == "xmlOutputBufferFlush": # handled by by the superclass
327 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000328 return 0
329
Daniel Veillard96fe0952002-01-30 20:52:23 +0000330def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000331 global py_types
332 global unknown_types
333 global functions
334 global skipped_modules
335
336 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000337 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000338 except:
339 print "failed to get function %s infos"
340 return
341
342 if skipped_modules.has_key(file):
343 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000344 if skip_function(name) == 1:
345 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000346
347 c_call = "";
348 format=""
349 format_args=""
350 c_args=""
351 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000352 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000353 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000354 # This should be correct
355 if arg[1][0:6] == "const ":
356 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000357 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000358 if py_types.has_key(arg[1]):
359 (f, t, n, c) = py_types[arg[1]]
360 if f != None:
361 format = format + f
362 if t != None:
363 format_args = format_args + ", &pyobj_%s" % (arg[0])
364 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
365 c_convert = c_convert + \
366 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
367 arg[1], t, arg[0]);
368 else:
369 format_args = format_args + ", &%s" % (arg[0])
370 if c_call != "":
371 c_call = c_call + ", ";
372 c_call = c_call + "%s" % (arg[0])
373 else:
374 if skipped_types.has_key(arg[1]):
375 return 0
376 if unknown_types.has_key(arg[1]):
377 lst = unknown_types[arg[1]]
378 lst.append(name)
379 else:
380 unknown_types[arg[1]] = [name]
381 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000382 if format != "":
383 format = format + ":%s" % (name)
384
385 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000386 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000387 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
388 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
389 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000390 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
391 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000392 else:
393 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
394 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000395 else:
396 c_call = "\n %s(%s);\n" % (name, c_call);
397 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000398 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000399 (f, t, n, c) = py_types[ret[0]]
400 c_return = " %s c_retval;\n" % (ret[0])
401 if file == "python_accessor" and ret[2] != None:
402 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
403 else:
404 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
405 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
406 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000407 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000408 (f, t, n, c) = py_return_types[ret[0]]
409 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000410 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000411 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
412 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000413 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000414 if skipped_types.has_key(ret[0]):
415 return 0
416 if unknown_types.has_key(ret[0]):
417 lst = unknown_types[ret[0]]
418 lst.append(name)
419 else:
420 unknown_types[ret[0]] = [name]
421 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000422
Daniel Veillard42766c02002-08-22 20:52:17 +0000423 if file == "debugXML":
424 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
425 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
426 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
427 elif file == "HTMLtree" or file == "HTMLparser":
428 include.write("#ifdef LIBXML_HTML_ENABLED\n");
429 export.write("#ifdef LIBXML_HTML_ENABLED\n");
430 output.write("#ifdef LIBXML_HTML_ENABLED\n");
431 elif file == "c14n":
432 include.write("#ifdef LIBXML_C14N_ENABLED\n");
433 export.write("#ifdef LIBXML_C14N_ENABLED\n");
434 output.write("#ifdef LIBXML_C14N_ENABLED\n");
435 elif file == "xpathInternals" or file == "xpath":
436 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
437 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
438 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
439 elif file == "xpointer":
440 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
441 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
442 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
443 elif file == "xinclude":
444 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
445 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
446 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000447 elif file == "xmlregexp":
448 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
449 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
450 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000451 elif file == "xmlschemas" or file == "xmlschemastypes" or \
452 file == "relaxng":
453 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
454 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
455 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000456
Daniel Veillard96fe0952002-01-30 20:52:23 +0000457 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000458 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000459
Daniel Veillardd2379012002-03-15 22:24:56 +0000460 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000461 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000462
463 if file == "python":
464 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000465 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000466 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000467 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000468 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000469
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000470 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000471 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
472 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000473 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000474 output.write(" ATTRIBUTE_UNUSED")
475 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000476 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000477 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000478 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000479 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000480 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000481 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000482 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000483 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000484 (format, format_args))
485 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000486 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000487 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488
489 output.write(c_call)
490 output.write(ret_convert)
491 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000492 if file == "debugXML":
493 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
494 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
495 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
496 elif file == "HTMLtree" or file == "HTMLparser":
497 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
498 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
499 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
500 elif file == "c14n":
501 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
502 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
503 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
504 elif file == "xpathInternals" or file == "xpath":
505 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
506 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
507 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
508 elif file == "xpointer":
509 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
510 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
511 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
512 elif file == "xinclude":
513 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
514 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
515 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000516 elif file == "xmlregexp":
517 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
518 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
519 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000520 elif file == "xmlschemas" or file == "xmlschemastypes" or \
521 file == "relaxng":
522 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
523 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
524 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000525 return 1
526
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000527def buildStubs():
528 global py_types
529 global py_return_types
530 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000531
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000532 try:
533 f = open("libxml2-api.xml")
534 data = f.read()
535 (parser, target) = getparser()
536 parser.feed(data)
537 parser.close()
538 except IOError, msg:
539 try:
540 f = open("../doc/libxml2-api.xml")
541 data = f.read()
542 (parser, target) = getparser()
543 parser.feed(data)
544 parser.close()
545 except IOError, msg:
546 print file, ":", msg
547 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000548
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000549 n = len(functions.keys())
550 print "Found %d functions in libxml2-api.xml" % (n)
551
552 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
553 try:
554 f = open("libxml2-python-api.xml")
555 data = f.read()
556 (parser, target) = getparser()
557 parser.feed(data)
558 parser.close()
559 except IOError, msg:
560 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000561
562
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000563 print "Found %d functions in libxml2-python-api.xml" % (
564 len(functions.keys()) - n)
565 nb_wrap = 0
566 failed = 0
567 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000568
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000569 include = open("libxml2-py.h", "w")
570 include.write("/* Generated */\n\n")
571 export = open("libxml2-export.c", "w")
572 export.write("/* Generated */\n\n")
573 wrapper = open("libxml2-py.c", "w")
574 wrapper.write("/* Generated */\n\n")
575 wrapper.write("#include <Python.h>\n")
Daniel Veillarda1196ed2002-11-23 11:22:49 +0000576# wrapper.write("#include \"config.h\"\n")
William M. Brackea939082003-11-09 12:45:26 +0000577 wrapper.write("#define IN_LIBXML\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000578 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000579 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000580 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000581 wrapper.write("#include \"libxml_wrap.h\"\n")
582 wrapper.write("#include \"libxml2-py.h\"\n\n")
583 for function in functions.keys():
584 ret = print_function_wrapper(function, wrapper, export, include)
585 if ret < 0:
586 failed = failed + 1
587 del functions[function]
588 if ret == 0:
589 skipped = skipped + 1
590 del functions[function]
591 if ret == 1:
592 nb_wrap = nb_wrap + 1
593 include.close()
594 export.close()
595 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000596
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000597 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
598 failed, skipped);
599 print "Missing type converters: "
600 for type in unknown_types.keys():
601 print "%s:%d " % (type, len(unknown_types[type])),
602 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000603
Daniel Veillard1971ee22002-01-31 20:29:19 +0000604#######################################################################
605#
606# This part writes part of the Python front-end classes based on
607# mapping rules between types and classes and also based on function
608# renaming to get consistent function names at the Python level
609#
610#######################################################################
611
612#
613# The type automatically remapped to generated classes
614#
615classes_type = {
616 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
617 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
618 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
619 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
620 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
621 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
622 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
623 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
624 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
625 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
626 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
627 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
628 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
629 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
630 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
631 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
632 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
633 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
634 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000635 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
636 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
637 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000638 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
639 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000640 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
641 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000642 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000643 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000644 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
645 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000646 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000647 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000648 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000649 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
650 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
651 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000652}
653
654converter_type = {
655 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
656}
657
658primary_classes = ["xmlNode", "xmlDoc"]
659
660classes_ancestor = {
661 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000662 "xmlDtd" : "xmlNode",
663 "xmlDoc" : "xmlNode",
664 "xmlAttr" : "xmlNode",
665 "xmlNs" : "xmlNode",
666 "xmlEntity" : "xmlNode",
667 "xmlElement" : "xmlNode",
668 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000669 "outputBuffer": "ioWriteWrapper",
670 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000671 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000672 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000673}
674classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000675 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000676 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000677 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000678# "outputBuffer": "xmlOutputBufferClose",
679 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000680 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000681 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000682 "relaxNgSchema": "xmlRelaxNGFree",
683 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
684 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000685}
686
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000687functions_noexcept = {
688 "xmlHasProp": 1,
689 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000690 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000691}
692
Daniel Veillarddc85f282002-12-31 11:18:37 +0000693reference_keepers = {
694 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000695 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000696}
697
Daniel Veillard36ed5292002-01-30 23:49:06 +0000698function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000699
700function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000701
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000702def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000703 listname = classe + "List"
704 ll = len(listname)
705 l = len(classe)
706 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000707 func = name[l:]
708 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000709 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
710 func = name[12:]
711 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000712 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
713 func = name[12:]
714 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000715 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
716 func = name[10:]
717 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000718 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
719 func = name[9:]
720 func = string.lower(func[0:1]) + func[1:]
721 elif name[0:9] == "xmlURISet" and file == "python_accessor":
722 func = name[6:]
723 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000724 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
725 func = name[17:]
726 func = string.lower(func[0:1]) + func[1:]
727 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
728 func = name[11:]
729 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000730 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
731 func = name[8:]
732 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000733 elif name[0:15] == "xmlOutputBuffer" and file != "python":
734 func = name[15:]
735 func = string.lower(func[0:1]) + func[1:]
736 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
737 func = name[20:]
738 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000739 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000740 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000741 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000742 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000743 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
744 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000745 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
746 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000747 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
748 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000749 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
750 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000751 elif name[0:11] == "xmlACatalog":
752 func = name[11:]
753 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000754 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000755 func = name[l:]
756 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000757 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000758 func = name[7:]
759 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000760 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000761 func = name[6:]
762 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000763 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000764 func = name[3:]
765 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000766 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000767 func = name
768 if func[0:5] == "xPath":
769 func = "xpath" + func[5:]
770 elif func[0:4] == "xPtr":
771 func = "xpointer" + func[4:]
772 elif func[0:8] == "xInclude":
773 func = "xinclude" + func[8:]
774 elif func[0:2] == "iD":
775 func = "ID" + func[2:]
776 elif func[0:3] == "uRI":
777 func = "URI" + func[3:]
778 elif func[0:4] == "uTF8":
779 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000780 elif func[0:3] == 'sAX':
781 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000782 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000783
Daniel Veillard36ed5292002-01-30 23:49:06 +0000784
Daniel Veillard1971ee22002-01-31 20:29:19 +0000785def functionCompare(info1, info2):
786 (index1, func1, name1, ret1, args1, file1) = info1
787 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000788 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000789 if func1 < func2:
790 return -1
791 if func1 > func2:
792 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000793 if file1 == "python_accessor":
794 return -1
795 if file2 == "python_accessor":
796 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000797 if file1 < file2:
798 return -1
799 if file1 > file2:
800 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000801 return 0
802
803def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000804 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000805 return
806 val = functions[name][0]
807 val = string.replace(val, "NULL", "None");
808 output.write(indent)
809 output.write('"""')
810 while len(val) > 60:
811 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000812 i = string.rfind(str, " ");
813 if i < 0:
814 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000815 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000816 val = val[i:]
817 output.write(str)
818 output.write('\n ');
819 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000820 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000821 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000822
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000823def buildWrappers():
824 global ctypes
825 global py_types
826 global py_return_types
827 global unknown_types
828 global functions
829 global function_classes
830 global classes_type
831 global classes_list
832 global converter_type
833 global primary_classes
834 global converter_type
835 global classes_ancestor
836 global converter_type
837 global primary_classes
838 global classes_ancestor
839 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000840 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000841
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000842 for type in classes_type.keys():
843 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000844
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000845 #
846 # Build the list of C types to look for ordered to start
847 # with primary classes
848 #
849 ctypes = []
850 classes_list = []
851 ctypes_processed = {}
852 classes_processed = {}
853 for classe in primary_classes:
854 classes_list.append(classe)
855 classes_processed[classe] = ()
856 for type in classes_type.keys():
857 tinfo = classes_type[type]
858 if tinfo[2] == classe:
859 ctypes.append(type)
860 ctypes_processed[type] = ()
861 for type in classes_type.keys():
862 if ctypes_processed.has_key(type):
863 continue
864 tinfo = classes_type[type]
865 if not classes_processed.has_key(tinfo[2]):
866 classes_list.append(tinfo[2])
867 classes_processed[tinfo[2]] = ()
868
869 ctypes.append(type)
870 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000871
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000872 for name in functions.keys():
873 found = 0;
874 (desc, ret, args, file) = functions[name]
875 for type in ctypes:
876 classe = classes_type[type][2]
877
878 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
879 found = 1
880 func = nameFixup(name, classe, type, file)
881 info = (0, func, name, ret, args, file)
882 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000883 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
884 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000885 found = 1
886 func = nameFixup(name, classe, type, file)
887 info = (1, func, name, ret, args, file)
888 function_classes[classe].append(info)
889 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
890 found = 1
891 func = nameFixup(name, classe, type, file)
892 info = (0, func, name, ret, args, file)
893 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000894 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
895 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000896 found = 1
897 func = nameFixup(name, classe, type, file)
898 info = (1, func, name, ret, args, file)
899 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000900 if found == 1:
901 continue
902 if name[0:8] == "xmlXPath":
903 continue
904 if name[0:6] == "xmlStr":
905 continue
906 if name[0:10] == "xmlCharStr":
907 continue
908 func = nameFixup(name, "None", file, file)
909 info = (0, func, name, ret, args, file)
910 function_classes['None'].append(info)
911
912 classes = open("libxml2class.py", "w")
913 txt = open("libxml2class.txt", "w")
914 txt.write(" Generated Classes for libxml2-python\n\n")
915
916 txt.write("#\n# Global functions of the module\n#\n\n")
917 if function_classes.has_key("None"):
918 flist = function_classes["None"]
919 flist.sort(functionCompare)
920 oldfile = ""
921 for info in flist:
922 (index, func, name, ret, args, file) = info
923 if file != oldfile:
924 classes.write("#\n# Functions from module %s\n#\n\n" % file)
925 txt.write("\n# functions from module %s\n" % file)
926 oldfile = file
927 classes.write("def %s(" % func)
928 txt.write("%s()\n" % func);
929 n = 0
930 for arg in args:
931 if n != 0:
932 classes.write(", ")
933 classes.write("%s" % arg[0])
934 n = n + 1
935 classes.write("):\n")
936 writeDoc(name, args, ' ', classes);
937
938 for arg in args:
939 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000940 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000941 (arg[0], arg[0]))
942 classes.write(" else: %s__o = %s%s\n" %
943 (arg[0], arg[0], classes_type[arg[1]][0]))
944 if ret[0] != "void":
945 classes.write(" ret = ");
946 else:
947 classes.write(" ");
948 classes.write("libxml2mod.%s(" % name)
949 n = 0
950 for arg in args:
951 if n != 0:
952 classes.write(", ");
953 classes.write("%s" % arg[0])
954 if classes_type.has_key(arg[1]):
955 classes.write("__o");
956 n = n + 1
957 classes.write(")\n");
958 if ret[0] != "void":
959 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000960 #
961 # Raise an exception
962 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000963 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000964 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000965 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000966 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000967 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000968 % (name))
969 elif string.find(name, "XPath") >= 0:
970 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000971 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000972 % (name))
973 elif string.find(name, "Parse") >= 0:
974 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000975 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000976 % (name))
977 else:
978 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000979 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000980 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000981 classes.write(" return ");
982 classes.write(classes_type[ret[0]][1] % ("ret"));
983 classes.write("\n");
984 else:
985 classes.write(" return ret\n");
986 classes.write("\n");
987
988 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
989 for classname in classes_list:
990 if classname == "None":
991 pass
992 else:
993 if classes_ancestor.has_key(classname):
994 txt.write("\n\nClass %s(%s)\n" % (classname,
995 classes_ancestor[classname]))
996 classes.write("class %s(%s):\n" % (classname,
997 classes_ancestor[classname]))
998 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +0000999 if reference_keepers.has_key(classname):
1000 rlist = reference_keepers[classname]
1001 for ref in rlist:
1002 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001003 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001004 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1005 classes_ancestor[classname]))
1006 if classes_ancestor[classname] == "xmlCore" or \
1007 classes_ancestor[classname] == "xmlNode":
1008 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001009 format = "<%s (%%s) object at 0x%%x>" % (classname)
1010 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001011 format))
1012 else:
1013 txt.write("Class %s()\n" % (classname))
1014 classes.write("class %s:\n" % (classname))
1015 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001016 if reference_keepers.has_key(classname):
1017 list = reference_keepers[classname]
1018 for ref in list:
1019 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001020 classes.write(" if _obj != None:self._o = _obj;return\n")
1021 classes.write(" self._o = None\n\n");
1022 if classes_destructors.has_key(classname):
1023 classes.write(" def __del__(self):\n")
1024 classes.write(" if self._o != None:\n")
1025 classes.write(" libxml2mod.%s(self._o)\n" %
1026 classes_destructors[classname]);
1027 classes.write(" self._o = None\n\n");
1028 flist = function_classes[classname]
1029 flist.sort(functionCompare)
1030 oldfile = ""
1031 for info in flist:
1032 (index, func, name, ret, args, file) = info
1033 if file != oldfile:
1034 if file == "python_accessor":
1035 classes.write(" # accessors for %s\n" % (classname))
1036 txt.write(" # accessors\n")
1037 else:
1038 classes.write(" #\n")
1039 classes.write(" # %s functions from module %s\n" % (
1040 classname, file))
1041 txt.write("\n # functions from module %s\n" % file)
1042 classes.write(" #\n\n")
1043 oldfile = file
1044 classes.write(" def %s(self" % func)
1045 txt.write(" %s()\n" % func);
1046 n = 0
1047 for arg in args:
1048 if n != index:
1049 classes.write(", %s" % arg[0])
1050 n = n + 1
1051 classes.write("):\n")
1052 writeDoc(name, args, ' ', classes);
1053 n = 0
1054 for arg in args:
1055 if classes_type.has_key(arg[1]):
1056 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001057 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001058 (arg[0], arg[0]))
1059 classes.write(" else: %s__o = %s%s\n" %
1060 (arg[0], arg[0], classes_type[arg[1]][0]))
1061 n = n + 1
1062 if ret[0] != "void":
1063 classes.write(" ret = ");
1064 else:
1065 classes.write(" ");
1066 classes.write("libxml2mod.%s(" % name)
1067 n = 0
1068 for arg in args:
1069 if n != 0:
1070 classes.write(", ");
1071 if n != index:
1072 classes.write("%s" % arg[0])
1073 if classes_type.has_key(arg[1]):
1074 classes.write("__o");
1075 else:
1076 classes.write("self");
1077 if classes_type.has_key(arg[1]):
1078 classes.write(classes_type[arg[1]][0])
1079 n = n + 1
1080 classes.write(")\n");
1081 if ret[0] != "void":
1082 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001083 #
1084 # Raise an exception
1085 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001086 if functions_noexcept.has_key(name):
1087 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001088 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001089 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001090 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001091 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001092 % (name))
1093 elif string.find(name, "XPath") >= 0:
1094 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001095 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001096 % (name))
1097 elif string.find(name, "Parse") >= 0:
1098 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001099 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001100 % (name))
1101 else:
1102 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001103 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001104 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001105
1106 #
1107 # generate the returned class wrapper for the object
1108 #
1109 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001110 classes.write(classes_type[ret[0]][1] % ("ret"));
1111 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001112
1113 #
1114 # Sometime one need to keep references of the source
1115 # class in the returned class object.
1116 # See reference_keepers for the list
1117 #
1118 tclass = classes_type[ret[0]][2]
1119 if reference_keepers.has_key(tclass):
1120 list = reference_keepers[tclass]
1121 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001122 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001123 classes.write(" __tmp.%s = self\n" %
1124 pref[1])
1125 #
1126 # return the class
1127 #
1128 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001129 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001130 #
1131 # Raise an exception
1132 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001133 if functions_noexcept.has_key(name):
1134 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001135 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001136 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001137 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001138 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001139 % (name))
1140 elif string.find(name, "XPath") >= 0:
1141 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001142 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001143 % (name))
1144 elif string.find(name, "Parse") >= 0:
1145 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001146 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001147 % (name))
1148 else:
1149 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001150 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001151 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001152 classes.write(" return ");
1153 classes.write(converter_type[ret[0]] % ("ret"));
1154 classes.write("\n");
1155 else:
1156 classes.write(" return ret\n");
1157 classes.write("\n");
1158
1159 txt.close()
1160 classes.close()
1161
1162
1163buildStubs()
1164buildWrappers()