blob: 3ae57292d1b75786e227cf7876efd15d7dc072a5 [file] [log] [blame]
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001#!/usr/bin/python -u
2#
3# generate python wrappers from the XML API description
4#
5
6functions = {}
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00007enums = {} # { enumType: { enumConstant: enumValue } }
Daniel Veillardd2897fd2002-01-30 16:37:32 +00008
Daniel Veillard0fea6f42002-02-22 22:51:13 +00009import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +000010import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000011
12#######################################################################
13#
14# That part if purely the API acquisition phase from the
15# XML API description
16#
17#######################################################################
18import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000019import xmllib
20try:
21 import sgmlop
22except ImportError:
23 sgmlop = None # accelerator not available
24
25debug = 0
26
27if sgmlop:
28 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000029 """sgmlop based XML parser. this is typically 15x faster
30 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000031
Daniel Veillard01a6d412002-02-11 18:42:20 +000032 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000033
Daniel Veillard01a6d412002-02-11 18:42:20 +000034 # setup callbacks
35 self.finish_starttag = target.start
36 self.finish_endtag = target.end
37 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000038
Daniel Veillard01a6d412002-02-11 18:42:20 +000039 # activate parser
40 self.parser = sgmlop.XMLParser()
41 self.parser.register(self)
42 self.feed = self.parser.feed
43 self.entity = {
44 "amp": "&", "gt": ">", "lt": "<",
45 "apos": "'", "quot": '"'
46 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000047
Daniel Veillard01a6d412002-02-11 18:42:20 +000048 def close(self):
49 try:
50 self.parser.close()
51 finally:
52 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000053
Daniel Veillard01a6d412002-02-11 18:42:20 +000054 def handle_entityref(self, entity):
55 # <string> entity
56 try:
57 self.handle_data(self.entity[entity])
58 except KeyError:
59 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000060
61else:
62 FastParser = None
63
64
65class SlowParser(xmllib.XMLParser):
66 """slow but safe standard parser, based on the XML parser in
67 Python's standard library."""
68
69 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000070 self.unknown_starttag = target.start
71 self.handle_data = target.data
72 self.unknown_endtag = target.end
73 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000074
75def getparser(target = None):
76 # get the fastest available parser, and attach it to an
77 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000078 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000079 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000080 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000081 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000082 return SlowParser(target), target
83
84class docParser:
85 def __init__(self):
86 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000087 self._data = []
88 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000089
90 def close(self):
91 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000092 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000093
94 def getmethodname(self):
95 return self._methodname
96
97 def data(self, text):
98 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000099 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000100 self._data.append(text)
101
102 def start(self, tag, attrs):
103 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000104 print "start %s, %s" % (tag, attrs)
105 if tag == 'function':
106 self._data = []
107 self.in_function = 1
108 self.function = None
109 self.function_args = []
110 self.function_descr = None
111 self.function_return = None
112 self.function_file = None
113 if attrs.has_key('name'):
114 self.function = attrs['name']
115 if attrs.has_key('file'):
116 self.function_file = attrs['file']
117 elif tag == 'info':
118 self._data = []
119 elif tag == 'arg':
120 if self.in_function == 1:
121 self.function_arg_name = None
122 self.function_arg_type = None
123 self.function_arg_info = None
124 if attrs.has_key('name'):
125 self.function_arg_name = attrs['name']
126 if attrs.has_key('type'):
127 self.function_arg_type = attrs['type']
128 if attrs.has_key('info'):
129 self.function_arg_info = attrs['info']
130 elif tag == 'return':
131 if self.in_function == 1:
132 self.function_return_type = None
133 self.function_return_info = None
134 self.function_return_field = None
135 if attrs.has_key('type'):
136 self.function_return_type = attrs['type']
137 if attrs.has_key('info'):
138 self.function_return_info = attrs['info']
139 if attrs.has_key('field'):
140 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000141 elif tag == 'enum':
142 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000143
144 def end(self, tag):
145 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000146 print "end %s" % tag
147 if tag == 'function':
148 if self.function != None:
149 function(self.function, self.function_descr,
150 self.function_return, self.function_args,
151 self.function_file)
152 self.in_function = 0
153 elif tag == 'arg':
154 if self.in_function == 1:
155 self.function_args.append([self.function_arg_name,
156 self.function_arg_type,
157 self.function_arg_info])
158 elif tag == 'return':
159 if self.in_function == 1:
160 self.function_return = [self.function_return_type,
161 self.function_return_info,
162 self.function_return_field]
163 elif tag == 'info':
164 str = ''
165 for c in self._data:
166 str = str + c
167 if self.in_function == 1:
168 self.function_descr = str
169
170
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000171def function(name, desc, ret, args, file):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000172 functions[name] = (desc, ret, args, file)
173
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000174def enum(type, name, value):
175 if not enums.has_key(type):
176 enums[type] = {}
177 enums[type][name] = value
178
Daniel Veillard1971ee22002-01-31 20:29:19 +0000179#######################################################################
180#
181# Some filtering rukes to drop functions/types which should not
182# be exposed as-is on the Python interface
183#
184#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000185
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000186skipped_modules = {
187 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000188 'DOCBparser': None,
189 'SAX': None,
190 'hash': None,
191 'list': None,
192 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000193# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000194}
195skipped_types = {
196 'int *': "usually a return type",
197 'xmlSAXHandlerPtr': "not the proper interface for SAX",
198 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000199 'xmlRMutexPtr': "thread specific, skipped",
200 'xmlMutexPtr': "thread specific, skipped",
201 'xmlGlobalStatePtr': "thread specific, skipped",
202 'xmlListPtr': "internal representation not suitable for python",
203 'xmlBufferPtr': "internal representation not suitable for python",
204 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000205}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000206
207#######################################################################
208#
209# Table of remapping to/from the python type or class to the C
210# counterpart.
211#
212#######################################################################
213
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000214py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000215 'void': (None, None, None, None),
216 'int': ('i', None, "int", "int"),
217 'long': ('i', None, "int", "int"),
218 'double': ('d', None, "double", "double"),
219 'unsigned int': ('i', None, "int", "int"),
220 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000221 'unsigned char *': ('z', None, "charPtr", "char *"),
222 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000223 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000224 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000225 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000226 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
237 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
238 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
239 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
240 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
241 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000242 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
243 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
244 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
245 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
246 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
247 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
248 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
249 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
250 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
251 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
252 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
253 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000254 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
257 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
258 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
259 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
260 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
261 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
262 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
263 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
264 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
265 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000266 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
267 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000268 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000269 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
270 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
271 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
272 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000273 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
274 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000275 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000276 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000277 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
278 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000279 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000280 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000281 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000282 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
283 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
284 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000285}
286
287py_return_types = {
288 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000289}
290
291unknown_types = {}
292
Daniel Veillard1971ee22002-01-31 20:29:19 +0000293#######################################################################
294#
295# This part writes the C <-> Python stubs libxml2-py.[ch] and
296# the table libxml2-export.c to add when registrering the Python module
297#
298#######################################################################
299
300def skip_function(name):
301 if name[0:12] == "xmlXPathWrap":
302 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000303 if name == "xmlFreeParserCtxt":
304 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000305 if name == "xmlCleanupParser":
306 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000307 if name == "xmlFreeTextReader":
308 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000309# if name[0:11] == "xmlXPathNew":
310# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000311 # the next function is defined in libxml.c
312 if name == "xmlRelaxNGFreeValidCtxt":
313 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000314#
315# Those are skipped because the Const version is used of the bindings
316# instead.
317#
318 if name == "xmlTextReaderBaseUri":
319 return 1
320 if name == "xmlTextReaderLocalName":
321 return 1
322 if name == "xmlTextReaderName":
323 return 1
324 if name == "xmlTextReaderNamespaceUri":
325 return 1
326 if name == "xmlTextReaderPrefix":
327 return 1
328 if name == "xmlTextReaderXmlLang":
329 return 1
330 if name == "xmlTextReaderValue":
331 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000332 if name == "xmlOutputBufferClose": # handled by by the superclass
333 return 1
334 if name == "xmlOutputBufferFlush": # handled by by the superclass
335 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000336 if name == "xmlErrMemory":
337 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000338 return 0
339
Daniel Veillard96fe0952002-01-30 20:52:23 +0000340def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000341 global py_types
342 global unknown_types
343 global functions
344 global skipped_modules
345
346 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000347 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000348 except:
349 print "failed to get function %s infos"
350 return
351
352 if skipped_modules.has_key(file):
353 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000354 if skip_function(name) == 1:
355 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000356
357 c_call = "";
358 format=""
359 format_args=""
360 c_args=""
361 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000362 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000363 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000364 # This should be correct
365 if arg[1][0:6] == "const ":
366 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000367 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000368 if py_types.has_key(arg[1]):
369 (f, t, n, c) = py_types[arg[1]]
370 if f != None:
371 format = format + f
372 if t != None:
373 format_args = format_args + ", &pyobj_%s" % (arg[0])
374 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
375 c_convert = c_convert + \
376 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
377 arg[1], t, arg[0]);
378 else:
379 format_args = format_args + ", &%s" % (arg[0])
380 if c_call != "":
381 c_call = c_call + ", ";
382 c_call = c_call + "%s" % (arg[0])
383 else:
384 if skipped_types.has_key(arg[1]):
385 return 0
386 if unknown_types.has_key(arg[1]):
387 lst = unknown_types[arg[1]]
388 lst.append(name)
389 else:
390 unknown_types[arg[1]] = [name]
391 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000392 if format != "":
393 format = format + ":%s" % (name)
394
395 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000396 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000397 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
398 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
399 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000400 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
401 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000402 else:
403 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
404 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000405 else:
406 c_call = "\n %s(%s);\n" % (name, c_call);
407 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000408 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000409 (f, t, n, c) = py_types[ret[0]]
410 c_return = " %s c_retval;\n" % (ret[0])
411 if file == "python_accessor" and ret[2] != None:
412 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
413 else:
414 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
415 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
416 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000417 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000418 (f, t, n, c) = py_return_types[ret[0]]
419 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000420 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000421 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
422 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000423 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000424 if skipped_types.has_key(ret[0]):
425 return 0
426 if unknown_types.has_key(ret[0]):
427 lst = unknown_types[ret[0]]
428 lst.append(name)
429 else:
430 unknown_types[ret[0]] = [name]
431 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000432
Daniel Veillard42766c02002-08-22 20:52:17 +0000433 if file == "debugXML":
434 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
435 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
436 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000437 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000438 include.write("#ifdef LIBXML_HTML_ENABLED\n");
439 export.write("#ifdef LIBXML_HTML_ENABLED\n");
440 output.write("#ifdef LIBXML_HTML_ENABLED\n");
441 elif file == "c14n":
442 include.write("#ifdef LIBXML_C14N_ENABLED\n");
443 export.write("#ifdef LIBXML_C14N_ENABLED\n");
444 output.write("#ifdef LIBXML_C14N_ENABLED\n");
445 elif file == "xpathInternals" or file == "xpath":
446 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
447 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
448 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
449 elif file == "xpointer":
450 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
451 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
452 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
453 elif file == "xinclude":
454 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
455 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
456 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000457 elif file == "xmlregexp":
458 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
459 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
460 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000461 elif file == "xmlschemas" or file == "xmlschemastypes" or \
462 file == "relaxng":
463 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
464 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
465 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000466
Daniel Veillard96fe0952002-01-30 20:52:23 +0000467 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000468 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000469
Daniel Veillardd2379012002-03-15 22:24:56 +0000470 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000471 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000472
473 if file == "python":
474 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000475 if name[0:4] == "html":
476 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
477 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
478 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000479 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000480 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000481 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000482 if name[0:4] == "html":
483 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
484 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
485 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000486 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000487
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000489 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
490 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000491 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000492 output.write(" ATTRIBUTE_UNUSED")
493 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000494 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000495 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000496 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000497 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000498 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000499 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000500 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000501 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000502 (format, format_args))
503 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000504 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000505 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000506
507 output.write(c_call)
508 output.write(ret_convert)
509 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000510 if file == "debugXML":
511 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
512 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
513 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000514 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000515 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
516 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
517 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
518 elif file == "c14n":
519 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
520 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
521 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
522 elif file == "xpathInternals" or file == "xpath":
523 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
524 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
525 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
526 elif file == "xpointer":
527 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
528 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
529 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
530 elif file == "xinclude":
531 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
532 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
533 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000534 elif file == "xmlregexp":
535 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
536 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
537 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000538 elif file == "xmlschemas" or file == "xmlschemastypes" or \
539 file == "relaxng":
540 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
541 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
542 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000543 return 1
544
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000545def buildStubs():
546 global py_types
547 global py_return_types
548 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000549
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000550 try:
551 f = open("libxml2-api.xml")
552 data = f.read()
553 (parser, target) = getparser()
554 parser.feed(data)
555 parser.close()
556 except IOError, msg:
557 try:
558 f = open("../doc/libxml2-api.xml")
559 data = f.read()
560 (parser, target) = getparser()
561 parser.feed(data)
562 parser.close()
563 except IOError, msg:
564 print file, ":", msg
565 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000566
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000567 n = len(functions.keys())
568 print "Found %d functions in libxml2-api.xml" % (n)
569
570 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
571 try:
572 f = open("libxml2-python-api.xml")
573 data = f.read()
574 (parser, target) = getparser()
575 parser.feed(data)
576 parser.close()
577 except IOError, msg:
578 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000579
580
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000581 print "Found %d functions in libxml2-python-api.xml" % (
582 len(functions.keys()) - n)
583 nb_wrap = 0
584 failed = 0
585 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000586
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000587 include = open("libxml2-py.h", "w")
588 include.write("/* Generated */\n\n")
589 export = open("libxml2-export.c", "w")
590 export.write("/* Generated */\n\n")
591 wrapper = open("libxml2-py.c", "w")
592 wrapper.write("/* Generated */\n\n")
593 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000594 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000595 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000596 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000597 wrapper.write("#include \"libxml_wrap.h\"\n")
598 wrapper.write("#include \"libxml2-py.h\"\n\n")
599 for function in functions.keys():
600 ret = print_function_wrapper(function, wrapper, export, include)
601 if ret < 0:
602 failed = failed + 1
603 del functions[function]
604 if ret == 0:
605 skipped = skipped + 1
606 del functions[function]
607 if ret == 1:
608 nb_wrap = nb_wrap + 1
609 include.close()
610 export.close()
611 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000612
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000613 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
614 failed, skipped);
615 print "Missing type converters: "
616 for type in unknown_types.keys():
617 print "%s:%d " % (type, len(unknown_types[type])),
618 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000619
Daniel Veillard1971ee22002-01-31 20:29:19 +0000620#######################################################################
621#
622# This part writes part of the Python front-end classes based on
623# mapping rules between types and classes and also based on function
624# renaming to get consistent function names at the Python level
625#
626#######################################################################
627
628#
629# The type automatically remapped to generated classes
630#
631classes_type = {
632 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
633 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
634 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
635 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
636 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
637 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
638 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
639 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
640 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
641 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
642 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
643 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
644 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
645 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
646 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
647 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
648 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
649 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
650 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000651 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
652 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
653 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000654 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
655 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000656 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
657 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000658 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000659 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000660 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000661 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
662 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000663 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000664 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000665 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000666 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
667 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
668 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000669}
670
671converter_type = {
672 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
673}
674
675primary_classes = ["xmlNode", "xmlDoc"]
676
677classes_ancestor = {
678 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000679 "xmlDtd" : "xmlNode",
680 "xmlDoc" : "xmlNode",
681 "xmlAttr" : "xmlNode",
682 "xmlNs" : "xmlNode",
683 "xmlEntity" : "xmlNode",
684 "xmlElement" : "xmlNode",
685 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000686 "outputBuffer": "ioWriteWrapper",
687 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000688 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000689 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000690}
691classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000692 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000693 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000694 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000695# "outputBuffer": "xmlOutputBufferClose",
696 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000697 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000698 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000699 "relaxNgSchema": "xmlRelaxNGFree",
700 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
701 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000702}
703
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000704functions_noexcept = {
705 "xmlHasProp": 1,
706 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000707 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000708}
709
Daniel Veillarddc85f282002-12-31 11:18:37 +0000710reference_keepers = {
711 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000712 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000713}
714
Daniel Veillard36ed5292002-01-30 23:49:06 +0000715function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000716
717function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000718
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000719def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000720 listname = classe + "List"
721 ll = len(listname)
722 l = len(classe)
723 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000724 func = name[l:]
725 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000726 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
727 func = name[12:]
728 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000729 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
730 func = name[12:]
731 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000732 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
733 func = name[10:]
734 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000735 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
736 func = name[9:]
737 func = string.lower(func[0:1]) + func[1:]
738 elif name[0:9] == "xmlURISet" and file == "python_accessor":
739 func = name[6:]
740 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000741 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
742 func = name[11:]
743 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000744 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
745 func = name[17:]
746 func = string.lower(func[0:1]) + func[1:]
747 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
748 func = name[11:]
749 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000750 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
751 func = name[8:]
752 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000753 elif name[0:15] == "xmlOutputBuffer" and file != "python":
754 func = name[15:]
755 func = string.lower(func[0:1]) + func[1:]
756 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
757 func = name[20:]
758 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000759 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000760 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000761 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000762 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000763 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
764 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000765 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
766 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000767 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
768 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000769 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
770 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000771 elif name[0:11] == "xmlACatalog":
772 func = name[11:]
773 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000774 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000775 func = name[l:]
776 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000777 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000778 func = name[7:]
779 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000780 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000781 func = name[6:]
782 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000783 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000784 func = name[3:]
785 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000786 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000787 func = name
788 if func[0:5] == "xPath":
789 func = "xpath" + func[5:]
790 elif func[0:4] == "xPtr":
791 func = "xpointer" + func[4:]
792 elif func[0:8] == "xInclude":
793 func = "xinclude" + func[8:]
794 elif func[0:2] == "iD":
795 func = "ID" + func[2:]
796 elif func[0:3] == "uRI":
797 func = "URI" + func[3:]
798 elif func[0:4] == "uTF8":
799 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000800 elif func[0:3] == 'sAX':
801 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000802 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000803
Daniel Veillard36ed5292002-01-30 23:49:06 +0000804
Daniel Veillard1971ee22002-01-31 20:29:19 +0000805def functionCompare(info1, info2):
806 (index1, func1, name1, ret1, args1, file1) = info1
807 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000808 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000809 if func1 < func2:
810 return -1
811 if func1 > func2:
812 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000813 if file1 == "python_accessor":
814 return -1
815 if file2 == "python_accessor":
816 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000817 if file1 < file2:
818 return -1
819 if file1 > file2:
820 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000821 return 0
822
823def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000824 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000825 return
826 val = functions[name][0]
827 val = string.replace(val, "NULL", "None");
828 output.write(indent)
829 output.write('"""')
830 while len(val) > 60:
831 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000832 i = string.rfind(str, " ");
833 if i < 0:
834 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000835 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000836 val = val[i:]
837 output.write(str)
838 output.write('\n ');
839 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000840 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000841 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000842
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000843def buildWrappers():
844 global ctypes
845 global py_types
846 global py_return_types
847 global unknown_types
848 global functions
849 global function_classes
850 global classes_type
851 global classes_list
852 global converter_type
853 global primary_classes
854 global converter_type
855 global classes_ancestor
856 global converter_type
857 global primary_classes
858 global classes_ancestor
859 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000860 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000861
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000862 for type in classes_type.keys():
863 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000864
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000865 #
866 # Build the list of C types to look for ordered to start
867 # with primary classes
868 #
869 ctypes = []
870 classes_list = []
871 ctypes_processed = {}
872 classes_processed = {}
873 for classe in primary_classes:
874 classes_list.append(classe)
875 classes_processed[classe] = ()
876 for type in classes_type.keys():
877 tinfo = classes_type[type]
878 if tinfo[2] == classe:
879 ctypes.append(type)
880 ctypes_processed[type] = ()
881 for type in classes_type.keys():
882 if ctypes_processed.has_key(type):
883 continue
884 tinfo = classes_type[type]
885 if not classes_processed.has_key(tinfo[2]):
886 classes_list.append(tinfo[2])
887 classes_processed[tinfo[2]] = ()
888
889 ctypes.append(type)
890 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000891
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000892 for name in functions.keys():
893 found = 0;
894 (desc, ret, args, file) = functions[name]
895 for type in ctypes:
896 classe = classes_type[type][2]
897
898 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
899 found = 1
900 func = nameFixup(name, classe, type, file)
901 info = (0, func, name, ret, args, file)
902 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000903 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
904 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000905 found = 1
906 func = nameFixup(name, classe, type, file)
907 info = (1, func, name, ret, args, file)
908 function_classes[classe].append(info)
909 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
910 found = 1
911 func = nameFixup(name, classe, type, file)
912 info = (0, func, name, ret, args, file)
913 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000914 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
915 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000916 found = 1
917 func = nameFixup(name, classe, type, file)
918 info = (1, func, name, ret, args, file)
919 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000920 if found == 1:
921 continue
922 if name[0:8] == "xmlXPath":
923 continue
924 if name[0:6] == "xmlStr":
925 continue
926 if name[0:10] == "xmlCharStr":
927 continue
928 func = nameFixup(name, "None", file, file)
929 info = (0, func, name, ret, args, file)
930 function_classes['None'].append(info)
931
932 classes = open("libxml2class.py", "w")
933 txt = open("libxml2class.txt", "w")
934 txt.write(" Generated Classes for libxml2-python\n\n")
935
936 txt.write("#\n# Global functions of the module\n#\n\n")
937 if function_classes.has_key("None"):
938 flist = function_classes["None"]
939 flist.sort(functionCompare)
940 oldfile = ""
941 for info in flist:
942 (index, func, name, ret, args, file) = info
943 if file != oldfile:
944 classes.write("#\n# Functions from module %s\n#\n\n" % file)
945 txt.write("\n# functions from module %s\n" % file)
946 oldfile = file
947 classes.write("def %s(" % func)
948 txt.write("%s()\n" % func);
949 n = 0
950 for arg in args:
951 if n != 0:
952 classes.write(", ")
953 classes.write("%s" % arg[0])
954 n = n + 1
955 classes.write("):\n")
956 writeDoc(name, args, ' ', classes);
957
958 for arg in args:
959 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000960 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000961 (arg[0], arg[0]))
962 classes.write(" else: %s__o = %s%s\n" %
963 (arg[0], arg[0], classes_type[arg[1]][0]))
964 if ret[0] != "void":
965 classes.write(" ret = ");
966 else:
967 classes.write(" ");
968 classes.write("libxml2mod.%s(" % name)
969 n = 0
970 for arg in args:
971 if n != 0:
972 classes.write(", ");
973 classes.write("%s" % arg[0])
974 if classes_type.has_key(arg[1]):
975 classes.write("__o");
976 n = n + 1
977 classes.write(")\n");
978 if ret[0] != "void":
979 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000980 #
981 # Raise an exception
982 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000983 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000984 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000985 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000986 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000987 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000988 % (name))
989 elif string.find(name, "XPath") >= 0:
990 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000991 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000992 % (name))
993 elif string.find(name, "Parse") >= 0:
994 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000995 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000996 % (name))
997 else:
998 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000999 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001000 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001001 classes.write(" return ");
1002 classes.write(classes_type[ret[0]][1] % ("ret"));
1003 classes.write("\n");
1004 else:
1005 classes.write(" return ret\n");
1006 classes.write("\n");
1007
1008 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1009 for classname in classes_list:
1010 if classname == "None":
1011 pass
1012 else:
1013 if classes_ancestor.has_key(classname):
1014 txt.write("\n\nClass %s(%s)\n" % (classname,
1015 classes_ancestor[classname]))
1016 classes.write("class %s(%s):\n" % (classname,
1017 classes_ancestor[classname]))
1018 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001019 if reference_keepers.has_key(classname):
1020 rlist = reference_keepers[classname]
1021 for ref in rlist:
1022 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001023 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001024 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1025 classes_ancestor[classname]))
1026 if classes_ancestor[classname] == "xmlCore" or \
1027 classes_ancestor[classname] == "xmlNode":
1028 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001029 format = "<%s (%%s) object at 0x%%x>" % (classname)
1030 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001031 format))
1032 else:
1033 txt.write("Class %s()\n" % (classname))
1034 classes.write("class %s:\n" % (classname))
1035 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001036 if reference_keepers.has_key(classname):
1037 list = reference_keepers[classname]
1038 for ref in list:
1039 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001040 classes.write(" if _obj != None:self._o = _obj;return\n")
1041 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001042 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001043 if classes_destructors.has_key(classname):
1044 classes.write(" def __del__(self):\n")
1045 classes.write(" if self._o != None:\n")
1046 classes.write(" libxml2mod.%s(self._o)\n" %
1047 classes_destructors[classname]);
1048 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001049 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001050 flist = function_classes[classname]
1051 flist.sort(functionCompare)
1052 oldfile = ""
1053 for info in flist:
1054 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001055 #
1056 # Do not provide as method the destructors for the class
1057 # to avoid double free
1058 #
1059 if name == destruct:
1060 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001061 if file != oldfile:
1062 if file == "python_accessor":
1063 classes.write(" # accessors for %s\n" % (classname))
1064 txt.write(" # accessors\n")
1065 else:
1066 classes.write(" #\n")
1067 classes.write(" # %s functions from module %s\n" % (
1068 classname, file))
1069 txt.write("\n # functions from module %s\n" % file)
1070 classes.write(" #\n\n")
1071 oldfile = file
1072 classes.write(" def %s(self" % func)
1073 txt.write(" %s()\n" % func);
1074 n = 0
1075 for arg in args:
1076 if n != index:
1077 classes.write(", %s" % arg[0])
1078 n = n + 1
1079 classes.write("):\n")
1080 writeDoc(name, args, ' ', classes);
1081 n = 0
1082 for arg in args:
1083 if classes_type.has_key(arg[1]):
1084 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001085 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001086 (arg[0], arg[0]))
1087 classes.write(" else: %s__o = %s%s\n" %
1088 (arg[0], arg[0], classes_type[arg[1]][0]))
1089 n = n + 1
1090 if ret[0] != "void":
1091 classes.write(" ret = ");
1092 else:
1093 classes.write(" ");
1094 classes.write("libxml2mod.%s(" % name)
1095 n = 0
1096 for arg in args:
1097 if n != 0:
1098 classes.write(", ");
1099 if n != index:
1100 classes.write("%s" % arg[0])
1101 if classes_type.has_key(arg[1]):
1102 classes.write("__o");
1103 else:
1104 classes.write("self");
1105 if classes_type.has_key(arg[1]):
1106 classes.write(classes_type[arg[1]][0])
1107 n = n + 1
1108 classes.write(")\n");
1109 if ret[0] != "void":
1110 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001111 #
1112 # Raise an exception
1113 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001114 if functions_noexcept.has_key(name):
1115 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001116 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001117 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001118 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001119 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001120 % (name))
1121 elif string.find(name, "XPath") >= 0:
1122 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001123 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001124 % (name))
1125 elif string.find(name, "Parse") >= 0:
1126 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001127 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001128 % (name))
1129 else:
1130 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001131 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001132 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001133
1134 #
1135 # generate the returned class wrapper for the object
1136 #
1137 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001138 classes.write(classes_type[ret[0]][1] % ("ret"));
1139 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001140
1141 #
1142 # Sometime one need to keep references of the source
1143 # class in the returned class object.
1144 # See reference_keepers for the list
1145 #
1146 tclass = classes_type[ret[0]][2]
1147 if reference_keepers.has_key(tclass):
1148 list = reference_keepers[tclass]
1149 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001150 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001151 classes.write(" __tmp.%s = self\n" %
1152 pref[1])
1153 #
1154 # return the class
1155 #
1156 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001157 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001158 #
1159 # Raise an exception
1160 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001161 if functions_noexcept.has_key(name):
1162 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001163 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001164 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001165 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001166 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001167 % (name))
1168 elif string.find(name, "XPath") >= 0:
1169 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001170 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001171 % (name))
1172 elif string.find(name, "Parse") >= 0:
1173 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001174 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001175 % (name))
1176 else:
1177 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001178 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001179 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001180 classes.write(" return ");
1181 classes.write(converter_type[ret[0]] % ("ret"));
1182 classes.write("\n");
1183 else:
1184 classes.write(" return ret\n");
1185 classes.write("\n");
1186
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001187 #
1188 # Generate enum constants
1189 #
1190 for type,enum in enums.items():
1191 classes.write("# %s\n" % type)
1192 items = enum.items()
1193 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1194 for name,value in items:
1195 classes.write("%s = %s\n" % (name,value))
1196 classes.write("\n");
1197
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001198 txt.close()
1199 classes.close()
1200
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001201buildStubs()
1202buildWrappers()