blob: 9cc3abc5651129810574ee62e946fb6b91ae8c6a [file] [log] [blame]
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001#!/usr/bin/python -u
2#
3# generate python wrappers from the XML API description
4#
5
6functions = {}
7
Daniel Veillard0fea6f42002-02-22 22:51:13 +00008import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +00009import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000010
11#######################################################################
12#
13# That part if purely the API acquisition phase from the
14# XML API description
15#
16#######################################################################
17import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000018import xmllib
19try:
20 import sgmlop
21except ImportError:
22 sgmlop = None # accelerator not available
23
24debug = 0
25
26if sgmlop:
27 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000028 """sgmlop based XML parser. this is typically 15x faster
29 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000030
Daniel Veillard01a6d412002-02-11 18:42:20 +000031 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000032
Daniel Veillard01a6d412002-02-11 18:42:20 +000033 # setup callbacks
34 self.finish_starttag = target.start
35 self.finish_endtag = target.end
36 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000037
Daniel Veillard01a6d412002-02-11 18:42:20 +000038 # activate parser
39 self.parser = sgmlop.XMLParser()
40 self.parser.register(self)
41 self.feed = self.parser.feed
42 self.entity = {
43 "amp": "&", "gt": ">", "lt": "<",
44 "apos": "'", "quot": '"'
45 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000046
Daniel Veillard01a6d412002-02-11 18:42:20 +000047 def close(self):
48 try:
49 self.parser.close()
50 finally:
51 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000052
Daniel Veillard01a6d412002-02-11 18:42:20 +000053 def handle_entityref(self, entity):
54 # <string> entity
55 try:
56 self.handle_data(self.entity[entity])
57 except KeyError:
58 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000059
60else:
61 FastParser = None
62
63
64class SlowParser(xmllib.XMLParser):
65 """slow but safe standard parser, based on the XML parser in
66 Python's standard library."""
67
68 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000069 self.unknown_starttag = target.start
70 self.handle_data = target.data
71 self.unknown_endtag = target.end
72 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000073
74def getparser(target = None):
75 # get the fastest available parser, and attach it to an
76 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000077 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000078 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000079 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000080 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000081 return SlowParser(target), target
82
83class docParser:
84 def __init__(self):
85 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000086 self._data = []
87 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000088
89 def close(self):
90 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000091 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000092
93 def getmethodname(self):
94 return self._methodname
95
96 def data(self, text):
97 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000098 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +000099 self._data.append(text)
100
101 def start(self, tag, attrs):
102 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000103 print "start %s, %s" % (tag, attrs)
104 if tag == 'function':
105 self._data = []
106 self.in_function = 1
107 self.function = None
108 self.function_args = []
109 self.function_descr = None
110 self.function_return = None
111 self.function_file = None
112 if attrs.has_key('name'):
113 self.function = attrs['name']
114 if attrs.has_key('file'):
115 self.function_file = attrs['file']
116 elif tag == 'info':
117 self._data = []
118 elif tag == 'arg':
119 if self.in_function == 1:
120 self.function_arg_name = None
121 self.function_arg_type = None
122 self.function_arg_info = None
123 if attrs.has_key('name'):
124 self.function_arg_name = attrs['name']
125 if attrs.has_key('type'):
126 self.function_arg_type = attrs['type']
127 if attrs.has_key('info'):
128 self.function_arg_info = attrs['info']
129 elif tag == 'return':
130 if self.in_function == 1:
131 self.function_return_type = None
132 self.function_return_info = None
133 self.function_return_field = None
134 if attrs.has_key('type'):
135 self.function_return_type = attrs['type']
136 if attrs.has_key('info'):
137 self.function_return_info = attrs['info']
138 if attrs.has_key('field'):
139 self.function_return_field = attrs['field']
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000140
141
142 def end(self, tag):
143 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000144 print "end %s" % tag
145 if tag == 'function':
146 if self.function != None:
147 function(self.function, self.function_descr,
148 self.function_return, self.function_args,
149 self.function_file)
150 self.in_function = 0
151 elif tag == 'arg':
152 if self.in_function == 1:
153 self.function_args.append([self.function_arg_name,
154 self.function_arg_type,
155 self.function_arg_info])
156 elif tag == 'return':
157 if self.in_function == 1:
158 self.function_return = [self.function_return_type,
159 self.function_return_info,
160 self.function_return_field]
161 elif tag == 'info':
162 str = ''
163 for c in self._data:
164 str = str + c
165 if self.in_function == 1:
166 self.function_descr = str
167
168
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000169def function(name, desc, ret, args, file):
170 global functions
171
172 functions[name] = (desc, ret, args, file)
173
Daniel Veillard1971ee22002-01-31 20:29:19 +0000174#######################################################################
175#
176# Some filtering rukes to drop functions/types which should not
177# be exposed as-is on the Python interface
178#
179#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000180
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000181skipped_modules = {
182 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000183 'DOCBparser': None,
184 'SAX': None,
185 'hash': None,
186 'list': None,
187 'threads': None,
Daniel Veillard1971ee22002-01-31 20:29:19 +0000188 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000189}
190skipped_types = {
191 'int *': "usually a return type",
192 'xmlSAXHandlerPtr': "not the proper interface for SAX",
193 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000194 'xmlRMutexPtr': "thread specific, skipped",
195 'xmlMutexPtr': "thread specific, skipped",
196 'xmlGlobalStatePtr': "thread specific, skipped",
197 'xmlListPtr': "internal representation not suitable for python",
198 'xmlBufferPtr': "internal representation not suitable for python",
199 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000200}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000201
202#######################################################################
203#
204# Table of remapping to/from the python type or class to the C
205# counterpart.
206#
207#######################################################################
208
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000209py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000210 'void': (None, None, None, None),
211 'int': ('i', None, "int", "int"),
212 'long': ('i', None, "int", "int"),
213 'double': ('d', None, "double", "double"),
214 'unsigned int': ('i', None, "int", "int"),
215 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000216 'unsigned char *': ('z', None, "charPtr", "char *"),
217 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000218 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000219 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000220 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000221 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
222 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
223 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
224 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
225 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
226 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000237 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
238 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
239 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
240 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
241 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
242 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
243 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
244 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
245 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
246 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
247 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
248 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000249 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
250 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
251 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
252 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
253 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
254 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
257 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
258 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
259 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
260 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000261 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
262 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000263 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000264 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
265 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
266 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
267 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000268 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
269 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000270 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000271 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
272 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000273 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000274}
275
276py_return_types = {
277 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000278}
279
280unknown_types = {}
281
Daniel Veillard1971ee22002-01-31 20:29:19 +0000282#######################################################################
283#
284# This part writes the C <-> Python stubs libxml2-py.[ch] and
285# the table libxml2-export.c to add when registrering the Python module
286#
287#######################################################################
288
289def skip_function(name):
290 if name[0:12] == "xmlXPathWrap":
291 return 1
292# if name[0:11] == "xmlXPathNew":
293# return 1
294 return 0
295
Daniel Veillard96fe0952002-01-30 20:52:23 +0000296def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000297 global py_types
298 global unknown_types
299 global functions
300 global skipped_modules
301
302 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000303 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000304 except:
305 print "failed to get function %s infos"
306 return
307
308 if skipped_modules.has_key(file):
309 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000310 if skip_function(name) == 1:
311 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000312
313 c_call = "";
314 format=""
315 format_args=""
316 c_args=""
317 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000318 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000319 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000320 # This should be correct
321 if arg[1][0:6] == "const ":
322 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000323 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000324 if py_types.has_key(arg[1]):
325 (f, t, n, c) = py_types[arg[1]]
326 if f != None:
327 format = format + f
328 if t != None:
329 format_args = format_args + ", &pyobj_%s" % (arg[0])
330 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
331 c_convert = c_convert + \
332 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
333 arg[1], t, arg[0]);
334 else:
335 format_args = format_args + ", &%s" % (arg[0])
336 if c_call != "":
337 c_call = c_call + ", ";
338 c_call = c_call + "%s" % (arg[0])
339 else:
340 if skipped_types.has_key(arg[1]):
341 return 0
342 if unknown_types.has_key(arg[1]):
343 lst = unknown_types[arg[1]]
344 lst.append(name)
345 else:
346 unknown_types[arg[1]] = [name]
347 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000348 if format != "":
349 format = format + ":%s" % (name)
350
351 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000352 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000353 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
354 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
355 args[0][0], args[1][0], args[0][0], args[1][0])
Daniel Veillardd2379012002-03-15 22:24:56 +0000356 c_call = c_call + " %s->%s = xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
Daniel Veillard6361da02002-02-23 10:10:33 +0000357 args[1][0], args[1][0])
358 else:
359 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
360 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000361 else:
362 c_call = "\n %s(%s);\n" % (name, c_call);
363 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000364 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000365 (f, t, n, c) = py_types[ret[0]]
366 c_return = " %s c_retval;\n" % (ret[0])
367 if file == "python_accessor" and ret[2] != None:
368 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
369 else:
370 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
371 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
372 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000373 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000374 (f, t, n, c) = py_return_types[ret[0]]
375 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000376 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000377 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
378 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000379 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000380 if skipped_types.has_key(ret[0]):
381 return 0
382 if unknown_types.has_key(ret[0]):
383 lst = unknown_types[ret[0]]
384 lst.append(name)
385 else:
386 unknown_types[ret[0]] = [name]
387 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000388
Daniel Veillard42766c02002-08-22 20:52:17 +0000389 if file == "debugXML":
390 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
391 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
392 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
393 elif file == "HTMLtree" or file == "HTMLparser":
394 include.write("#ifdef LIBXML_HTML_ENABLED\n");
395 export.write("#ifdef LIBXML_HTML_ENABLED\n");
396 output.write("#ifdef LIBXML_HTML_ENABLED\n");
397 elif file == "c14n":
398 include.write("#ifdef LIBXML_C14N_ENABLED\n");
399 export.write("#ifdef LIBXML_C14N_ENABLED\n");
400 output.write("#ifdef LIBXML_C14N_ENABLED\n");
401 elif file == "xpathInternals" or file == "xpath":
402 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
403 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
404 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
405 elif file == "xpointer":
406 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
407 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
408 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
409 elif file == "xinclude":
410 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
411 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
412 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000413 elif file == "xmlregexp":
414 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
415 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
416 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000417
Daniel Veillard96fe0952002-01-30 20:52:23 +0000418 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000419 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000420
Daniel Veillardd2379012002-03-15 22:24:56 +0000421 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000422 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000423
424 if file == "python":
425 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000426 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000427 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000428 # Those have been manually generated
Daniel Veillard01a6d412002-02-11 18:42:20 +0000429 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000430
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000431 output.write("PyObject *\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000432 output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name))
433 if format == "":
434 output.write("ATTRIBUTE_UNUSED ")
435 output.write(" PyObject *args) {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000436 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000437 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000438 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000439 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000440 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000441 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000442 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000443 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000444 (format, format_args))
445 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000446 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000447 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000448
449 output.write(c_call)
450 output.write(ret_convert)
451 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000452 if file == "debugXML":
453 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
454 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
455 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
456 elif file == "HTMLtree" or file == "HTMLparser":
457 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
458 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
459 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
460 elif file == "c14n":
461 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
462 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
463 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
464 elif file == "xpathInternals" or file == "xpath":
465 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
466 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
467 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
468 elif file == "xpointer":
469 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
470 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
471 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
472 elif file == "xinclude":
473 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
474 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
475 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000476 elif file == "xmlregexp":
477 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
478 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
479 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000480 return 1
481
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000482def buildStubs():
483 global py_types
484 global py_return_types
485 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000486
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000487 try:
488 f = open("libxml2-api.xml")
489 data = f.read()
490 (parser, target) = getparser()
491 parser.feed(data)
492 parser.close()
493 except IOError, msg:
494 try:
495 f = open("../doc/libxml2-api.xml")
496 data = f.read()
497 (parser, target) = getparser()
498 parser.feed(data)
499 parser.close()
500 except IOError, msg:
501 print file, ":", msg
502 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000503
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000504 n = len(functions.keys())
505 print "Found %d functions in libxml2-api.xml" % (n)
506
507 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
508 try:
509 f = open("libxml2-python-api.xml")
510 data = f.read()
511 (parser, target) = getparser()
512 parser.feed(data)
513 parser.close()
514 except IOError, msg:
515 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000516
517
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000518 print "Found %d functions in libxml2-python-api.xml" % (
519 len(functions.keys()) - n)
520 nb_wrap = 0
521 failed = 0
522 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000523
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000524 include = open("libxml2-py.h", "w")
525 include.write("/* Generated */\n\n")
526 export = open("libxml2-export.c", "w")
527 export.write("/* Generated */\n\n")
528 wrapper = open("libxml2-py.c", "w")
529 wrapper.write("/* Generated */\n\n")
530 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000531 wrapper.write("#include \"config.h\"\n")
532 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000533 wrapper.write("#include <libxml/tree.h>\n")
534 wrapper.write("#include \"libxml_wrap.h\"\n")
535 wrapper.write("#include \"libxml2-py.h\"\n\n")
536 for function in functions.keys():
537 ret = print_function_wrapper(function, wrapper, export, include)
538 if ret < 0:
539 failed = failed + 1
540 del functions[function]
541 if ret == 0:
542 skipped = skipped + 1
543 del functions[function]
544 if ret == 1:
545 nb_wrap = nb_wrap + 1
546 include.close()
547 export.close()
548 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000549
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000550 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
551 failed, skipped);
552 print "Missing type converters: "
553 for type in unknown_types.keys():
554 print "%s:%d " % (type, len(unknown_types[type])),
555 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000556
Daniel Veillard1971ee22002-01-31 20:29:19 +0000557#######################################################################
558#
559# This part writes part of the Python front-end classes based on
560# mapping rules between types and classes and also based on function
561# renaming to get consistent function names at the Python level
562#
563#######################################################################
564
565#
566# The type automatically remapped to generated classes
567#
568classes_type = {
569 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
570 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
571 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
572 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
573 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
574 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
575 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
576 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
577 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
578 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
579 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
580 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
581 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
582 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
583 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
584 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
585 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
586 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
587 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000588 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
589 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
590 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000591 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
592 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000593 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
594 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000595 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000596 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000597 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
598 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000599 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000600}
601
602converter_type = {
603 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
604}
605
606primary_classes = ["xmlNode", "xmlDoc"]
607
608classes_ancestor = {
609 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000610 "xmlDtd" : "xmlNode",
611 "xmlDoc" : "xmlNode",
612 "xmlAttr" : "xmlNode",
613 "xmlNs" : "xmlNode",
614 "xmlEntity" : "xmlNode",
615 "xmlElement" : "xmlNode",
616 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000617 "outputBuffer": "ioWriteWrapper",
618 "inputBuffer": "ioReadWrapper",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000619}
620classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000621 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000622 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000623 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000624# "outputBuffer": "xmlOutputBufferClose",
625 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000626 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000627}
628
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000629functions_noexcept = {
630 "xmlHasProp": 1,
631 "xmlHasNsProp": 1,
632}
633
Daniel Veillard36ed5292002-01-30 23:49:06 +0000634function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000635
636function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000637
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000638def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000639 listname = classe + "List"
640 ll = len(listname)
641 l = len(classe)
642 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000643 func = name[l:]
644 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000645 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
646 func = name[12:]
647 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000648 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
649 func = name[12:]
650 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000651 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
652 func = name[10:]
653 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000654 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
655 func = name[9:]
656 func = string.lower(func[0:1]) + func[1:]
657 elif name[0:9] == "xmlURISet" and file == "python_accessor":
658 func = name[6:]
659 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000660 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
661 func = name[17:]
662 func = string.lower(func[0:1]) + func[1:]
663 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
664 func = name[11:]
665 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000666 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
667 func = name[8:]
668 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000669 elif name[0:15] == "xmlOutputBuffer" and file != "python":
670 func = name[15:]
671 func = string.lower(func[0:1]) + func[1:]
672 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
673 func = name[20:]
674 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000675 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000676 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000677 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000678 func = "regexp" + name[6:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000679 elif name[0:11] == "xmlACatalog":
680 func = name[11:]
681 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000682 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000683 func = name[l:]
684 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000685 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000686 func = name[7:]
687 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000688 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000689 func = name[6:]
690 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000691 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000692 func = name[3:]
693 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000694 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000695 func = name
696 if func[0:5] == "xPath":
697 func = "xpath" + func[5:]
698 elif func[0:4] == "xPtr":
699 func = "xpointer" + func[4:]
700 elif func[0:8] == "xInclude":
701 func = "xinclude" + func[8:]
702 elif func[0:2] == "iD":
703 func = "ID" + func[2:]
704 elif func[0:3] == "uRI":
705 func = "URI" + func[3:]
706 elif func[0:4] == "uTF8":
707 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000708 elif func[0:3] == 'sAX':
709 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000710 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000711
Daniel Veillard36ed5292002-01-30 23:49:06 +0000712
Daniel Veillard1971ee22002-01-31 20:29:19 +0000713def functionCompare(info1, info2):
714 (index1, func1, name1, ret1, args1, file1) = info1
715 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000716 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000717 if func1 < func2:
718 return -1
719 if func1 > func2:
720 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000721 if file1 == "python_accessor":
722 return -1
723 if file2 == "python_accessor":
724 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000725 if file1 < file2:
726 return -1
727 if file1 > file2:
728 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000729 return 0
730
731def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000732 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000733 return
734 val = functions[name][0]
735 val = string.replace(val, "NULL", "None");
736 output.write(indent)
737 output.write('"""')
738 while len(val) > 60:
739 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000740 i = string.rfind(str, " ");
741 if i < 0:
742 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000743 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000744 val = val[i:]
745 output.write(str)
746 output.write('\n ');
747 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000748 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000749 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000750
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000751def buildWrappers():
752 global ctypes
753 global py_types
754 global py_return_types
755 global unknown_types
756 global functions
757 global function_classes
758 global classes_type
759 global classes_list
760 global converter_type
761 global primary_classes
762 global converter_type
763 global classes_ancestor
764 global converter_type
765 global primary_classes
766 global classes_ancestor
767 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000768 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000769
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000770 for type in classes_type.keys():
771 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000772
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000773 #
774 # Build the list of C types to look for ordered to start
775 # with primary classes
776 #
777 ctypes = []
778 classes_list = []
779 ctypes_processed = {}
780 classes_processed = {}
781 for classe in primary_classes:
782 classes_list.append(classe)
783 classes_processed[classe] = ()
784 for type in classes_type.keys():
785 tinfo = classes_type[type]
786 if tinfo[2] == classe:
787 ctypes.append(type)
788 ctypes_processed[type] = ()
789 for type in classes_type.keys():
790 if ctypes_processed.has_key(type):
791 continue
792 tinfo = classes_type[type]
793 if not classes_processed.has_key(tinfo[2]):
794 classes_list.append(tinfo[2])
795 classes_processed[tinfo[2]] = ()
796
797 ctypes.append(type)
798 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000799
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000800 for name in functions.keys():
801 found = 0;
802 (desc, ret, args, file) = functions[name]
803 for type in ctypes:
804 classe = classes_type[type][2]
805
806 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
807 found = 1
808 func = nameFixup(name, classe, type, file)
809 info = (0, func, name, ret, args, file)
810 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000811 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
812 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000813 found = 1
814 func = nameFixup(name, classe, type, file)
815 info = (1, func, name, ret, args, file)
816 function_classes[classe].append(info)
817 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
818 found = 1
819 func = nameFixup(name, classe, type, file)
820 info = (0, func, name, ret, args, file)
821 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000822 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
823 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000824 found = 1
825 func = nameFixup(name, classe, type, file)
826 info = (1, func, name, ret, args, file)
827 function_classes[classe].append(info)
828 if found == 1:
829 break
830 if found == 1:
831 continue
832 if name[0:8] == "xmlXPath":
833 continue
834 if name[0:6] == "xmlStr":
835 continue
836 if name[0:10] == "xmlCharStr":
837 continue
838 func = nameFixup(name, "None", file, file)
839 info = (0, func, name, ret, args, file)
840 function_classes['None'].append(info)
841
842 classes = open("libxml2class.py", "w")
843 txt = open("libxml2class.txt", "w")
844 txt.write(" Generated Classes for libxml2-python\n\n")
845
846 txt.write("#\n# Global functions of the module\n#\n\n")
847 if function_classes.has_key("None"):
848 flist = function_classes["None"]
849 flist.sort(functionCompare)
850 oldfile = ""
851 for info in flist:
852 (index, func, name, ret, args, file) = info
853 if file != oldfile:
854 classes.write("#\n# Functions from module %s\n#\n\n" % file)
855 txt.write("\n# functions from module %s\n" % file)
856 oldfile = file
857 classes.write("def %s(" % func)
858 txt.write("%s()\n" % func);
859 n = 0
860 for arg in args:
861 if n != 0:
862 classes.write(", ")
863 classes.write("%s" % arg[0])
864 n = n + 1
865 classes.write("):\n")
866 writeDoc(name, args, ' ', classes);
867
868 for arg in args:
869 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000870 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000871 (arg[0], arg[0]))
872 classes.write(" else: %s__o = %s%s\n" %
873 (arg[0], arg[0], classes_type[arg[1]][0]))
874 if ret[0] != "void":
875 classes.write(" ret = ");
876 else:
877 classes.write(" ");
878 classes.write("libxml2mod.%s(" % name)
879 n = 0
880 for arg in args:
881 if n != 0:
882 classes.write(", ");
883 classes.write("%s" % arg[0])
884 if classes_type.has_key(arg[1]):
885 classes.write("__o");
886 n = n + 1
887 classes.write(")\n");
888 if ret[0] != "void":
889 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000890 #
891 # Raise an exception
892 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000893 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000894 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000895 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000896 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000897 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000898 % (name))
899 elif string.find(name, "XPath") >= 0:
900 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000901 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000902 % (name))
903 elif string.find(name, "Parse") >= 0:
904 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000905 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000906 % (name))
907 else:
908 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000909 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000910 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000911 classes.write(" return ");
912 classes.write(classes_type[ret[0]][1] % ("ret"));
913 classes.write("\n");
914 else:
915 classes.write(" return ret\n");
916 classes.write("\n");
917
918 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
919 for classname in classes_list:
920 if classname == "None":
921 pass
922 else:
923 if classes_ancestor.has_key(classname):
924 txt.write("\n\nClass %s(%s)\n" % (classname,
925 classes_ancestor[classname]))
926 classes.write("class %s(%s):\n" % (classname,
927 classes_ancestor[classname]))
928 classes.write(" def __init__(self, _obj=None):\n")
929 classes.write(" self._o = None\n")
930 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
931 classes_ancestor[classname]))
932 if classes_ancestor[classname] == "xmlCore" or \
933 classes_ancestor[classname] == "xmlNode":
934 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +0000935 format = "<%s (%%s) object at 0x%%x>" % (classname)
936 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000937 format))
938 else:
939 txt.write("Class %s()\n" % (classname))
940 classes.write("class %s:\n" % (classname))
941 classes.write(" def __init__(self, _obj=None):\n")
942 classes.write(" if _obj != None:self._o = _obj;return\n")
943 classes.write(" self._o = None\n\n");
944 if classes_destructors.has_key(classname):
945 classes.write(" def __del__(self):\n")
946 classes.write(" if self._o != None:\n")
947 classes.write(" libxml2mod.%s(self._o)\n" %
948 classes_destructors[classname]);
949 classes.write(" self._o = None\n\n");
950 flist = function_classes[classname]
951 flist.sort(functionCompare)
952 oldfile = ""
953 for info in flist:
954 (index, func, name, ret, args, file) = info
955 if file != oldfile:
956 if file == "python_accessor":
957 classes.write(" # accessors for %s\n" % (classname))
958 txt.write(" # accessors\n")
959 else:
960 classes.write(" #\n")
961 classes.write(" # %s functions from module %s\n" % (
962 classname, file))
963 txt.write("\n # functions from module %s\n" % file)
964 classes.write(" #\n\n")
965 oldfile = file
966 classes.write(" def %s(self" % func)
967 txt.write(" %s()\n" % func);
968 n = 0
969 for arg in args:
970 if n != index:
971 classes.write(", %s" % arg[0])
972 n = n + 1
973 classes.write("):\n")
974 writeDoc(name, args, ' ', classes);
975 n = 0
976 for arg in args:
977 if classes_type.has_key(arg[1]):
978 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000979 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000980 (arg[0], arg[0]))
981 classes.write(" else: %s__o = %s%s\n" %
982 (arg[0], arg[0], classes_type[arg[1]][0]))
983 n = n + 1
984 if ret[0] != "void":
985 classes.write(" ret = ");
986 else:
987 classes.write(" ");
988 classes.write("libxml2mod.%s(" % name)
989 n = 0
990 for arg in args:
991 if n != 0:
992 classes.write(", ");
993 if n != index:
994 classes.write("%s" % arg[0])
995 if classes_type.has_key(arg[1]):
996 classes.write("__o");
997 else:
998 classes.write("self");
999 if classes_type.has_key(arg[1]):
1000 classes.write(classes_type[arg[1]][0])
1001 n = n + 1
1002 classes.write(")\n");
1003 if ret[0] != "void":
1004 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001005 #
1006 # Raise an exception
1007 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001008 if functions_noexcept.has_key(name):
1009 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001010 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001011 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001012 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001013 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001014 % (name))
1015 elif string.find(name, "XPath") >= 0:
1016 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001017 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001018 % (name))
1019 elif string.find(name, "Parse") >= 0:
1020 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001021 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001022 % (name))
1023 else:
1024 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001025 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001026 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001027 classes.write(" return ");
1028 classes.write(classes_type[ret[0]][1] % ("ret"));
1029 classes.write("\n");
1030 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001031 #
1032 # Raise an exception
1033 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001034 if functions_noexcept.has_key(name):
1035 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001036 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001037 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001038 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001039 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001040 % (name))
1041 elif string.find(name, "XPath") >= 0:
1042 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001043 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001044 % (name))
1045 elif string.find(name, "Parse") >= 0:
1046 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001047 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001048 % (name))
1049 else:
1050 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001051 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001052 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001053 classes.write(" return ");
1054 classes.write(converter_type[ret[0]] % ("ret"));
1055 classes.write("\n");
1056 else:
1057 classes.write(" return ret\n");
1058 classes.write("\n");
1059
1060 txt.close()
1061 classes.close()
1062
1063
1064buildStubs()
1065buildWrappers()