blob: 64baea74981d7d26ecd49bf8c42cbfad37357bfc [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 Veillard1971ee22002-01-31 20:29:19 +0000324 return 0
325
Daniel Veillard96fe0952002-01-30 20:52:23 +0000326def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000327 global py_types
328 global unknown_types
329 global functions
330 global skipped_modules
331
332 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000333 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000334 except:
335 print "failed to get function %s infos"
336 return
337
338 if skipped_modules.has_key(file):
339 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000340 if skip_function(name) == 1:
341 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000342
343 c_call = "";
344 format=""
345 format_args=""
346 c_args=""
347 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000348 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000349 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000350 # This should be correct
351 if arg[1][0:6] == "const ":
352 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000353 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000354 if py_types.has_key(arg[1]):
355 (f, t, n, c) = py_types[arg[1]]
356 if f != None:
357 format = format + f
358 if t != None:
359 format_args = format_args + ", &pyobj_%s" % (arg[0])
360 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
361 c_convert = c_convert + \
362 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
363 arg[1], t, arg[0]);
364 else:
365 format_args = format_args + ", &%s" % (arg[0])
366 if c_call != "":
367 c_call = c_call + ", ";
368 c_call = c_call + "%s" % (arg[0])
369 else:
370 if skipped_types.has_key(arg[1]):
371 return 0
372 if unknown_types.has_key(arg[1]):
373 lst = unknown_types[arg[1]]
374 lst.append(name)
375 else:
376 unknown_types[arg[1]] = [name]
377 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000378 if format != "":
379 format = format + ":%s" % (name)
380
381 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000382 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000383 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
384 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
385 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000386 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
387 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000388 else:
389 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
390 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000391 else:
392 c_call = "\n %s(%s);\n" % (name, c_call);
393 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000394 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000395 (f, t, n, c) = py_types[ret[0]]
396 c_return = " %s c_retval;\n" % (ret[0])
397 if file == "python_accessor" and ret[2] != None:
398 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
399 else:
400 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
401 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
402 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000403 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000404 (f, t, n, c) = py_return_types[ret[0]]
405 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000406 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000407 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
408 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000409 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000410 if skipped_types.has_key(ret[0]):
411 return 0
412 if unknown_types.has_key(ret[0]):
413 lst = unknown_types[ret[0]]
414 lst.append(name)
415 else:
416 unknown_types[ret[0]] = [name]
417 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000418
Daniel Veillard42766c02002-08-22 20:52:17 +0000419 if file == "debugXML":
420 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
421 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
422 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
423 elif file == "HTMLtree" or file == "HTMLparser":
424 include.write("#ifdef LIBXML_HTML_ENABLED\n");
425 export.write("#ifdef LIBXML_HTML_ENABLED\n");
426 output.write("#ifdef LIBXML_HTML_ENABLED\n");
427 elif file == "c14n":
428 include.write("#ifdef LIBXML_C14N_ENABLED\n");
429 export.write("#ifdef LIBXML_C14N_ENABLED\n");
430 output.write("#ifdef LIBXML_C14N_ENABLED\n");
431 elif file == "xpathInternals" or file == "xpath":
432 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
433 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
434 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
435 elif file == "xpointer":
436 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
437 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
438 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
439 elif file == "xinclude":
440 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
441 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
442 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000443 elif file == "xmlregexp":
444 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
445 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
446 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000447 elif file == "xmlschemas" or file == "xmlschemastypes" or \
448 file == "relaxng":
449 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
450 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
451 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000452
Daniel Veillard96fe0952002-01-30 20:52:23 +0000453 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000454 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000455
Daniel Veillardd2379012002-03-15 22:24:56 +0000456 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000457 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000458
459 if file == "python":
460 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000461 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000462 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000463 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000464 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000465
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000466 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000467 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
468 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000469 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000470 output.write(" ATTRIBUTE_UNUSED")
471 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000472 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000473 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000474 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000475 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000476 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000477 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000478 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000479 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000480 (format, format_args))
481 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000482 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000483 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000484
485 output.write(c_call)
486 output.write(ret_convert)
487 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000488 if file == "debugXML":
489 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
490 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
491 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
492 elif file == "HTMLtree" or file == "HTMLparser":
493 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
494 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
495 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
496 elif file == "c14n":
497 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
498 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
499 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
500 elif file == "xpathInternals" or file == "xpath":
501 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
502 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
503 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
504 elif file == "xpointer":
505 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
506 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
507 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
508 elif file == "xinclude":
509 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
510 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
511 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000512 elif file == "xmlregexp":
513 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
514 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
515 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000516 elif file == "xmlschemas" or file == "xmlschemastypes" or \
517 file == "relaxng":
518 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
519 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
520 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000521 return 1
522
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000523def buildStubs():
524 global py_types
525 global py_return_types
526 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000527
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000528 try:
529 f = open("libxml2-api.xml")
530 data = f.read()
531 (parser, target) = getparser()
532 parser.feed(data)
533 parser.close()
534 except IOError, msg:
535 try:
536 f = open("../doc/libxml2-api.xml")
537 data = f.read()
538 (parser, target) = getparser()
539 parser.feed(data)
540 parser.close()
541 except IOError, msg:
542 print file, ":", msg
543 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000544
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000545 n = len(functions.keys())
546 print "Found %d functions in libxml2-api.xml" % (n)
547
548 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
549 try:
550 f = open("libxml2-python-api.xml")
551 data = f.read()
552 (parser, target) = getparser()
553 parser.feed(data)
554 parser.close()
555 except IOError, msg:
556 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000557
558
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000559 print "Found %d functions in libxml2-python-api.xml" % (
560 len(functions.keys()) - n)
561 nb_wrap = 0
562 failed = 0
563 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000564
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000565 include = open("libxml2-py.h", "w")
566 include.write("/* Generated */\n\n")
567 export = open("libxml2-export.c", "w")
568 export.write("/* Generated */\n\n")
569 wrapper = open("libxml2-py.c", "w")
570 wrapper.write("/* Generated */\n\n")
571 wrapper.write("#include <Python.h>\n")
Daniel Veillarda1196ed2002-11-23 11:22:49 +0000572# wrapper.write("#include \"config.h\"\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000573 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000574 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000575 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000576 wrapper.write("#include \"libxml_wrap.h\"\n")
577 wrapper.write("#include \"libxml2-py.h\"\n\n")
578 for function in functions.keys():
579 ret = print_function_wrapper(function, wrapper, export, include)
580 if ret < 0:
581 failed = failed + 1
582 del functions[function]
583 if ret == 0:
584 skipped = skipped + 1
585 del functions[function]
586 if ret == 1:
587 nb_wrap = nb_wrap + 1
588 include.close()
589 export.close()
590 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000591
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000592 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
593 failed, skipped);
594 print "Missing type converters: "
595 for type in unknown_types.keys():
596 print "%s:%d " % (type, len(unknown_types[type])),
597 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000598
Daniel Veillard1971ee22002-01-31 20:29:19 +0000599#######################################################################
600#
601# This part writes part of the Python front-end classes based on
602# mapping rules between types and classes and also based on function
603# renaming to get consistent function names at the Python level
604#
605#######################################################################
606
607#
608# The type automatically remapped to generated classes
609#
610classes_type = {
611 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
612 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
613 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
614 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
615 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
616 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
617 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
618 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
619 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
620 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
621 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
622 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
623 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
624 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
625 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
626 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
627 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
628 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
629 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000630 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
631 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
632 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000633 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
634 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000635 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
636 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000637 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000638 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000639 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
640 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000641 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000642 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000643 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000644 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
645 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
646 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000647}
648
649converter_type = {
650 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
651}
652
653primary_classes = ["xmlNode", "xmlDoc"]
654
655classes_ancestor = {
656 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000657 "xmlDtd" : "xmlNode",
658 "xmlDoc" : "xmlNode",
659 "xmlAttr" : "xmlNode",
660 "xmlNs" : "xmlNode",
661 "xmlEntity" : "xmlNode",
662 "xmlElement" : "xmlNode",
663 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000664 "outputBuffer": "ioWriteWrapper",
665 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000666 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000667 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000668}
669classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000670 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000671 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000672 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000673# "outputBuffer": "xmlOutputBufferClose",
674 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000675 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000676 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000677 "relaxNgSchema": "xmlRelaxNGFree",
678 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
679 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000680}
681
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000682functions_noexcept = {
683 "xmlHasProp": 1,
684 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000685 "xmlDocSetRootElement": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000686}
687
Daniel Veillarddc85f282002-12-31 11:18:37 +0000688reference_keepers = {
689 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000690 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000691}
692
Daniel Veillard36ed5292002-01-30 23:49:06 +0000693function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000694
695function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000696
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000697def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000698 listname = classe + "List"
699 ll = len(listname)
700 l = len(classe)
701 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000702 func = name[l:]
703 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000704 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
705 func = name[12:]
706 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000707 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
708 func = name[12:]
709 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000710 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
711 func = name[10:]
712 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000713 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
714 func = name[9:]
715 func = string.lower(func[0:1]) + func[1:]
716 elif name[0:9] == "xmlURISet" and file == "python_accessor":
717 func = name[6:]
718 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000719 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
720 func = name[17:]
721 func = string.lower(func[0:1]) + func[1:]
722 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
723 func = name[11:]
724 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000725 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
726 func = name[8:]
727 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000728 elif name[0:15] == "xmlOutputBuffer" and file != "python":
729 func = name[15:]
730 func = string.lower(func[0:1]) + func[1:]
731 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
732 func = name[20:]
733 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000734 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000735 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000736 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000737 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000738 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
739 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000740 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
741 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000742 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
743 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000744 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
745 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000746 elif name[0:11] == "xmlACatalog":
747 func = name[11:]
748 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000749 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000750 func = name[l:]
751 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000752 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000753 func = name[7:]
754 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000755 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000756 func = name[6:]
757 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000758 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000759 func = name[3:]
760 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000761 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000762 func = name
763 if func[0:5] == "xPath":
764 func = "xpath" + func[5:]
765 elif func[0:4] == "xPtr":
766 func = "xpointer" + func[4:]
767 elif func[0:8] == "xInclude":
768 func = "xinclude" + func[8:]
769 elif func[0:2] == "iD":
770 func = "ID" + func[2:]
771 elif func[0:3] == "uRI":
772 func = "URI" + func[3:]
773 elif func[0:4] == "uTF8":
774 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000775 elif func[0:3] == 'sAX':
776 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000777 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000778
Daniel Veillard36ed5292002-01-30 23:49:06 +0000779
Daniel Veillard1971ee22002-01-31 20:29:19 +0000780def functionCompare(info1, info2):
781 (index1, func1, name1, ret1, args1, file1) = info1
782 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000783 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000784 if func1 < func2:
785 return -1
786 if func1 > func2:
787 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000788 if file1 == "python_accessor":
789 return -1
790 if file2 == "python_accessor":
791 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000792 if file1 < file2:
793 return -1
794 if file1 > file2:
795 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000796 return 0
797
798def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000799 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000800 return
801 val = functions[name][0]
802 val = string.replace(val, "NULL", "None");
803 output.write(indent)
804 output.write('"""')
805 while len(val) > 60:
806 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000807 i = string.rfind(str, " ");
808 if i < 0:
809 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000810 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000811 val = val[i:]
812 output.write(str)
813 output.write('\n ');
814 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000815 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000816 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000817
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000818def buildWrappers():
819 global ctypes
820 global py_types
821 global py_return_types
822 global unknown_types
823 global functions
824 global function_classes
825 global classes_type
826 global classes_list
827 global converter_type
828 global primary_classes
829 global converter_type
830 global classes_ancestor
831 global converter_type
832 global primary_classes
833 global classes_ancestor
834 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000835 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000836
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000837 for type in classes_type.keys():
838 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000839
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000840 #
841 # Build the list of C types to look for ordered to start
842 # with primary classes
843 #
844 ctypes = []
845 classes_list = []
846 ctypes_processed = {}
847 classes_processed = {}
848 for classe in primary_classes:
849 classes_list.append(classe)
850 classes_processed[classe] = ()
851 for type in classes_type.keys():
852 tinfo = classes_type[type]
853 if tinfo[2] == classe:
854 ctypes.append(type)
855 ctypes_processed[type] = ()
856 for type in classes_type.keys():
857 if ctypes_processed.has_key(type):
858 continue
859 tinfo = classes_type[type]
860 if not classes_processed.has_key(tinfo[2]):
861 classes_list.append(tinfo[2])
862 classes_processed[tinfo[2]] = ()
863
864 ctypes.append(type)
865 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000866
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000867 for name in functions.keys():
868 found = 0;
869 (desc, ret, args, file) = functions[name]
870 for type in ctypes:
871 classe = classes_type[type][2]
872
873 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
874 found = 1
875 func = nameFixup(name, classe, type, file)
876 info = (0, func, name, ret, args, file)
877 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000878 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
879 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000880 found = 1
881 func = nameFixup(name, classe, type, file)
882 info = (1, func, name, ret, args, file)
883 function_classes[classe].append(info)
884 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
885 found = 1
886 func = nameFixup(name, classe, type, file)
887 info = (0, func, name, ret, args, file)
888 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000889 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
890 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000891 found = 1
892 func = nameFixup(name, classe, type, file)
893 info = (1, func, name, ret, args, file)
894 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000895 if found == 1:
896 continue
897 if name[0:8] == "xmlXPath":
898 continue
899 if name[0:6] == "xmlStr":
900 continue
901 if name[0:10] == "xmlCharStr":
902 continue
903 func = nameFixup(name, "None", file, file)
904 info = (0, func, name, ret, args, file)
905 function_classes['None'].append(info)
906
907 classes = open("libxml2class.py", "w")
908 txt = open("libxml2class.txt", "w")
909 txt.write(" Generated Classes for libxml2-python\n\n")
910
911 txt.write("#\n# Global functions of the module\n#\n\n")
912 if function_classes.has_key("None"):
913 flist = function_classes["None"]
914 flist.sort(functionCompare)
915 oldfile = ""
916 for info in flist:
917 (index, func, name, ret, args, file) = info
918 if file != oldfile:
919 classes.write("#\n# Functions from module %s\n#\n\n" % file)
920 txt.write("\n# functions from module %s\n" % file)
921 oldfile = file
922 classes.write("def %s(" % func)
923 txt.write("%s()\n" % func);
924 n = 0
925 for arg in args:
926 if n != 0:
927 classes.write(", ")
928 classes.write("%s" % arg[0])
929 n = n + 1
930 classes.write("):\n")
931 writeDoc(name, args, ' ', classes);
932
933 for arg in args:
934 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000935 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000936 (arg[0], arg[0]))
937 classes.write(" else: %s__o = %s%s\n" %
938 (arg[0], arg[0], classes_type[arg[1]][0]))
939 if ret[0] != "void":
940 classes.write(" ret = ");
941 else:
942 classes.write(" ");
943 classes.write("libxml2mod.%s(" % name)
944 n = 0
945 for arg in args:
946 if n != 0:
947 classes.write(", ");
948 classes.write("%s" % arg[0])
949 if classes_type.has_key(arg[1]):
950 classes.write("__o");
951 n = n + 1
952 classes.write(")\n");
953 if ret[0] != "void":
954 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000955 #
956 # Raise an exception
957 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000958 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000959 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000960 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000961 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000962 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000963 % (name))
964 elif string.find(name, "XPath") >= 0:
965 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000966 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000967 % (name))
968 elif string.find(name, "Parse") >= 0:
969 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000970 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000971 % (name))
972 else:
973 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000974 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000975 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000976 classes.write(" return ");
977 classes.write(classes_type[ret[0]][1] % ("ret"));
978 classes.write("\n");
979 else:
980 classes.write(" return ret\n");
981 classes.write("\n");
982
983 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
984 for classname in classes_list:
985 if classname == "None":
986 pass
987 else:
988 if classes_ancestor.has_key(classname):
989 txt.write("\n\nClass %s(%s)\n" % (classname,
990 classes_ancestor[classname]))
991 classes.write("class %s(%s):\n" % (classname,
992 classes_ancestor[classname]))
993 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +0000994 if reference_keepers.has_key(classname):
995 rlist = reference_keepers[classname]
996 for ref in rlist:
997 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000998 classes.write(" self._o = None\n")
999 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1000 classes_ancestor[classname]))
1001 if classes_ancestor[classname] == "xmlCore" or \
1002 classes_ancestor[classname] == "xmlNode":
1003 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001004 format = "<%s (%%s) object at 0x%%x>" % (classname)
1005 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001006 format))
1007 else:
1008 txt.write("Class %s()\n" % (classname))
1009 classes.write("class %s:\n" % (classname))
1010 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001011 if reference_keepers.has_key(classname):
1012 list = reference_keepers[classname]
1013 for ref in list:
1014 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001015 classes.write(" if _obj != None:self._o = _obj;return\n")
1016 classes.write(" self._o = None\n\n");
1017 if classes_destructors.has_key(classname):
1018 classes.write(" def __del__(self):\n")
1019 classes.write(" if self._o != None:\n")
1020 classes.write(" libxml2mod.%s(self._o)\n" %
1021 classes_destructors[classname]);
1022 classes.write(" self._o = None\n\n");
1023 flist = function_classes[classname]
1024 flist.sort(functionCompare)
1025 oldfile = ""
1026 for info in flist:
1027 (index, func, name, ret, args, file) = info
1028 if file != oldfile:
1029 if file == "python_accessor":
1030 classes.write(" # accessors for %s\n" % (classname))
1031 txt.write(" # accessors\n")
1032 else:
1033 classes.write(" #\n")
1034 classes.write(" # %s functions from module %s\n" % (
1035 classname, file))
1036 txt.write("\n # functions from module %s\n" % file)
1037 classes.write(" #\n\n")
1038 oldfile = file
1039 classes.write(" def %s(self" % func)
1040 txt.write(" %s()\n" % func);
1041 n = 0
1042 for arg in args:
1043 if n != index:
1044 classes.write(", %s" % arg[0])
1045 n = n + 1
1046 classes.write("):\n")
1047 writeDoc(name, args, ' ', classes);
1048 n = 0
1049 for arg in args:
1050 if classes_type.has_key(arg[1]):
1051 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001052 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001053 (arg[0], arg[0]))
1054 classes.write(" else: %s__o = %s%s\n" %
1055 (arg[0], arg[0], classes_type[arg[1]][0]))
1056 n = n + 1
1057 if ret[0] != "void":
1058 classes.write(" ret = ");
1059 else:
1060 classes.write(" ");
1061 classes.write("libxml2mod.%s(" % name)
1062 n = 0
1063 for arg in args:
1064 if n != 0:
1065 classes.write(", ");
1066 if n != index:
1067 classes.write("%s" % arg[0])
1068 if classes_type.has_key(arg[1]):
1069 classes.write("__o");
1070 else:
1071 classes.write("self");
1072 if classes_type.has_key(arg[1]):
1073 classes.write(classes_type[arg[1]][0])
1074 n = n + 1
1075 classes.write(")\n");
1076 if ret[0] != "void":
1077 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001078 #
1079 # Raise an exception
1080 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001081 if functions_noexcept.has_key(name):
1082 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001083 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001084 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001085 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001086 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001087 % (name))
1088 elif string.find(name, "XPath") >= 0:
1089 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001090 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001091 % (name))
1092 elif string.find(name, "Parse") >= 0:
1093 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001094 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001095 % (name))
1096 else:
1097 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001098 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001099 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001100
1101 #
1102 # generate the returned class wrapper for the object
1103 #
1104 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001105 classes.write(classes_type[ret[0]][1] % ("ret"));
1106 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001107
1108 #
1109 # Sometime one need to keep references of the source
1110 # class in the returned class object.
1111 # See reference_keepers for the list
1112 #
1113 tclass = classes_type[ret[0]][2]
1114 if reference_keepers.has_key(tclass):
1115 list = reference_keepers[tclass]
1116 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001117 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001118 classes.write(" __tmp.%s = self\n" %
1119 pref[1])
1120 #
1121 # return the class
1122 #
1123 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001124 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001125 #
1126 # Raise an exception
1127 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001128 if functions_noexcept.has_key(name):
1129 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001130 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001131 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001132 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001133 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001134 % (name))
1135 elif string.find(name, "XPath") >= 0:
1136 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001137 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001138 % (name))
1139 elif string.find(name, "Parse") >= 0:
1140 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001141 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001142 % (name))
1143 else:
1144 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001145 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001146 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001147 classes.write(" return ");
1148 classes.write(converter_type[ret[0]] % ("ret"));
1149 classes.write("\n");
1150 else:
1151 classes.write(" return ret\n");
1152 classes.write("\n");
1153
1154 txt.close()
1155 classes.close()
1156
1157
1158buildStubs()
1159buildWrappers()