blob: 83d100cf59730ddf510d87e48194a64fcf6f2ac1 [file] [log] [blame]
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001#!/usr/bin/python -u
2#
3# generate python wrappers from the XML API description
4#
5
6functions = {}
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00007enums = {} # { enumType: { enumConstant: enumValue } }
Daniel Veillardd2897fd2002-01-30 16:37:32 +00008
Daniel Veillard2fc6df92005-01-30 18:42:55 +00009import os
Daniel Veillard0fea6f42002-02-22 22:51:13 +000010import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +000011import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000012
Daniel Veillard2fc6df92005-01-30 18:42:55 +000013if __name__ == "__main__":
14 # launched as a script
15 srcPref = os.path.dirname(sys.argv[0])
William M. Brack106cad62004-12-23 15:56:12 +000016else:
Daniel Veillard2fc6df92005-01-30 18:42:55 +000017 # imported
18 srcPref = os.path.dirname(__file__)
William M. Brack106cad62004-12-23 15:56:12 +000019
Daniel Veillard1971ee22002-01-31 20:29:19 +000020#######################################################################
21#
22# That part if purely the API acquisition phase from the
23# XML API description
24#
25#######################################################################
26import os
Daniel Veillard79461372010-01-13 15:34:50 +010027import xml.sax
Daniel Veillardd2897fd2002-01-30 16:37:32 +000028
29debug = 0
30
Daniel Veillard79461372010-01-13 15:34:50 +010031def getparser():
32 # Attach parser to an unmarshalling object. return both objects.
33 target = docParser()
34 parser = xml.sax.make_parser()
35 parser.setContentHandler(target)
36 return parser, target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000037
Daniel Veillard79461372010-01-13 15:34:50 +010038class docParser(xml.sax.handler.ContentHandler):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000039 def __init__(self):
40 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000041 self._data = []
42 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000043
Daniel Veillard79461372010-01-13 15:34:50 +010044 self.startElement = self.start
45 self.endElement = self.end
46 self.characters = self.data
47
Daniel Veillardd2897fd2002-01-30 16:37:32 +000048 def close(self):
49 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000050 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000051
52 def getmethodname(self):
53 return self._methodname
54
55 def data(self, text):
56 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000057 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +000058 self._data.append(text)
59
60 def start(self, tag, attrs):
61 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000062 print "start %s, %s" % (tag, attrs)
63 if tag == 'function':
64 self._data = []
65 self.in_function = 1
66 self.function = None
Daniel Veillard95175012005-07-03 16:09:51 +000067 self.function_cond = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000068 self.function_args = []
69 self.function_descr = None
70 self.function_return = None
71 self.function_file = None
72 if attrs.has_key('name'):
73 self.function = attrs['name']
74 if attrs.has_key('file'):
75 self.function_file = attrs['file']
Daniel Veillard95175012005-07-03 16:09:51 +000076 elif tag == 'cond':
77 self._data = []
Daniel Veillard01a6d412002-02-11 18:42:20 +000078 elif tag == 'info':
79 self._data = []
80 elif tag == 'arg':
81 if self.in_function == 1:
82 self.function_arg_name = None
83 self.function_arg_type = None
84 self.function_arg_info = None
85 if attrs.has_key('name'):
86 self.function_arg_name = attrs['name']
87 if attrs.has_key('type'):
88 self.function_arg_type = attrs['type']
89 if attrs.has_key('info'):
90 self.function_arg_info = attrs['info']
91 elif tag == 'return':
92 if self.in_function == 1:
93 self.function_return_type = None
94 self.function_return_info = None
95 self.function_return_field = None
96 if attrs.has_key('type'):
97 self.function_return_type = attrs['type']
98 if attrs.has_key('info'):
99 self.function_return_info = attrs['info']
100 if attrs.has_key('field'):
101 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000102 elif tag == 'enum':
103 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000104
105 def end(self, tag):
106 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000107 print "end %s" % tag
108 if tag == 'function':
109 if self.function != None:
110 function(self.function, self.function_descr,
111 self.function_return, self.function_args,
Daniel Veillard95175012005-07-03 16:09:51 +0000112 self.function_file, self.function_cond)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000113 self.in_function = 0
114 elif tag == 'arg':
115 if self.in_function == 1:
116 self.function_args.append([self.function_arg_name,
117 self.function_arg_type,
118 self.function_arg_info])
119 elif tag == 'return':
120 if self.in_function == 1:
121 self.function_return = [self.function_return_type,
122 self.function_return_info,
123 self.function_return_field]
124 elif tag == 'info':
125 str = ''
126 for c in self._data:
127 str = str + c
128 if self.in_function == 1:
129 self.function_descr = str
Daniel Veillard95175012005-07-03 16:09:51 +0000130 elif tag == 'cond':
131 str = ''
132 for c in self._data:
133 str = str + c
134 if self.in_function == 1:
135 self.function_cond = str
Daniel Veillard01a6d412002-02-11 18:42:20 +0000136
137
Daniel Veillard95175012005-07-03 16:09:51 +0000138def function(name, desc, ret, args, file, cond):
139 functions[name] = (desc, ret, args, file, cond)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000140
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000141def enum(type, name, value):
142 if not enums.has_key(type):
143 enums[type] = {}
144 enums[type][name] = value
145
Daniel Veillard1971ee22002-01-31 20:29:19 +0000146#######################################################################
147#
148# Some filtering rukes to drop functions/types which should not
149# be exposed as-is on the Python interface
150#
151#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000152
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000153skipped_modules = {
154 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000155 'DOCBparser': None,
156 'SAX': None,
157 'hash': None,
158 'list': None,
159 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000160# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000161}
162skipped_types = {
163 'int *': "usually a return type",
164 'xmlSAXHandlerPtr': "not the proper interface for SAX",
165 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000166 'xmlRMutexPtr': "thread specific, skipped",
167 'xmlMutexPtr': "thread specific, skipped",
168 'xmlGlobalStatePtr': "thread specific, skipped",
169 'xmlListPtr': "internal representation not suitable for python",
170 'xmlBufferPtr': "internal representation not suitable for python",
171 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000172}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000173
174#######################################################################
175#
176# Table of remapping to/from the python type or class to the C
177# counterpart.
178#
179#######################################################################
180
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000181py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000182 'void': (None, None, None, None),
183 'int': ('i', None, "int", "int"),
Daniel Veillardddefe9c2006-08-04 12:44:24 +0000184 'long': ('l', None, "long", "long"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000185 'double': ('d', None, "double", "double"),
186 'unsigned int': ('i', None, "int", "int"),
187 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000188 'unsigned char *': ('z', None, "charPtr", "char *"),
189 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000190 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000191 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000192 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000193 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
194 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
195 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
196 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
197 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
198 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
199 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
200 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
201 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
202 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
203 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
204 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
205 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
206 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
207 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
208 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000209 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
210 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
211 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
212 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
213 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
214 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
215 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
216 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
217 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
218 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
219 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
220 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000221 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
222 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
223 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
224 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
225 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
226 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
227 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
228 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
229 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000233 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
234 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000235 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000236 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
237 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
238 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
239 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000240 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000241 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
242 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000243 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000244 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000245 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
246 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000247 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000248 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000249 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000250 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
251 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
252 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000253 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
254 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
255 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000256}
257
258py_return_types = {
259 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000260}
261
262unknown_types = {}
263
William M. Brack106cad62004-12-23 15:56:12 +0000264foreign_encoding_args = (
William M. Brackff349112004-12-24 08:39:13 +0000265 'htmlCreateMemoryParserCtxt',
266 'htmlCtxtReadMemory',
267 'htmlParseChunk',
268 'htmlReadMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000269 'xmlCreateMemoryParserCtxt',
William M. Brackff349112004-12-24 08:39:13 +0000270 'xmlCtxtReadMemory',
271 'xmlCtxtResetPush',
272 'xmlParseChunk',
273 'xmlParseMemory',
274 'xmlReadMemory',
275 'xmlRecoverMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000276)
277
Daniel Veillard1971ee22002-01-31 20:29:19 +0000278#######################################################################
279#
280# This part writes the C <-> Python stubs libxml2-py.[ch] and
281# the table libxml2-export.c to add when registrering the Python module
282#
283#######################################################################
284
Daniel Veillard263ec862004-10-04 10:26:54 +0000285# Class methods which are written by hand in libxml.c but the Python-level
286# code is still automatically generated (so they are not in skip_function()).
287skip_impl = (
288 'xmlSaveFileTo',
289 'xmlSaveFormatFileTo',
290)
291
Daniel Veillard1971ee22002-01-31 20:29:19 +0000292def skip_function(name):
293 if name[0:12] == "xmlXPathWrap":
294 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000295 if name == "xmlFreeParserCtxt":
296 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000297 if name == "xmlCleanupParser":
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 Veillard25c90c52005-03-02 10:47:41 +0000306 if name == "xmlFreeValidCtxt":
307 return 1
Daniel Veillardbb8502c2005-03-30 07:40:35 +0000308 if name == "xmlSchemaFreeValidCtxt":
309 return 1
310
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000311#
312# Those are skipped because the Const version is used of the bindings
313# instead.
314#
315 if name == "xmlTextReaderBaseUri":
316 return 1
317 if name == "xmlTextReaderLocalName":
318 return 1
319 if name == "xmlTextReaderName":
320 return 1
321 if name == "xmlTextReaderNamespaceUri":
322 return 1
323 if name == "xmlTextReaderPrefix":
324 return 1
325 if name == "xmlTextReaderXmlLang":
326 return 1
327 if name == "xmlTextReaderValue":
328 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000329 if name == "xmlOutputBufferClose": # handled by by the superclass
330 return 1
331 if name == "xmlOutputBufferFlush": # handled by by the superclass
332 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000333 if name == "xmlErrMemory":
334 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000335
336 if name == "xmlValidBuildContentModel":
337 return 1
338 if name == "xmlValidateElementDecl":
339 return 1
340 if name == "xmlValidateAttributeDecl":
341 return 1
Alexey Neyman48da90b2013-02-25 15:54:25 +0800342 if name == "xmlPopInputCallbacks":
343 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000344
Daniel Veillard1971ee22002-01-31 20:29:19 +0000345 return 0
346
Daniel Veillard96fe0952002-01-30 20:52:23 +0000347def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000348 global py_types
349 global unknown_types
350 global functions
351 global skipped_modules
352
353 try:
Daniel Veillard95175012005-07-03 16:09:51 +0000354 (desc, ret, args, file, cond) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000355 except:
356 print "failed to get function %s infos"
357 return
358
359 if skipped_modules.has_key(file):
360 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000361 if skip_function(name) == 1:
362 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000363 if name in skip_impl:
Daniel Veillard39801e52008-06-03 16:08:54 +0000364 # Don't delete the function entry in the caller.
365 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000366
Daniel Veillard39801e52008-06-03 16:08:54 +0000367 c_call = ""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000368 format=""
369 format_args=""
370 c_args=""
371 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000372 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000373 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000374 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000375 # This should be correct
376 if arg[1][0:6] == "const ":
377 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000378 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000379 if py_types.has_key(arg[1]):
380 (f, t, n, c) = py_types[arg[1]]
Daniel Veillard39801e52008-06-03 16:08:54 +0000381 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
382 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000383 if f != None:
384 format = format + f
385 if t != None:
386 format_args = format_args + ", &pyobj_%s" % (arg[0])
387 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
388 c_convert = c_convert + \
389 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
Daniel Veillard39801e52008-06-03 16:08:54 +0000390 arg[1], t, arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000391 else:
392 format_args = format_args + ", &%s" % (arg[0])
Daniel Veillard39801e52008-06-03 16:08:54 +0000393 if f == 't#':
394 format_args = format_args + ", &py_buffsize%d" % num_bufs
395 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
396 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000397 if c_call != "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000398 c_call = c_call + ", "
Daniel Veillard01a6d412002-02-11 18:42:20 +0000399 c_call = c_call + "%s" % (arg[0])
400 else:
401 if skipped_types.has_key(arg[1]):
402 return 0
403 if unknown_types.has_key(arg[1]):
404 lst = unknown_types[arg[1]]
405 lst.append(name)
406 else:
407 unknown_types[arg[1]] = [name]
408 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000409 if format != "":
410 format = format + ":%s" % (name)
411
412 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000413 if file == "python_accessor":
Daniel Veillard39801e52008-06-03 16:08:54 +0000414 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
415 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
416 args[0][0], args[1][0], args[0][0], args[1][0])
417 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
418 args[1][0], args[1][1], args[1][0])
419 else:
420 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
421 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000422 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000423 c_call = "\n %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000424 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000425 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000426 (f, t, n, c) = py_types[ret[0]]
427 c_return = " %s c_retval;\n" % (ret[0])
428 if file == "python_accessor" and ret[2] != None:
429 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
430 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000431 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000432 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
433 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000434 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000435 (f, t, n, c) = py_return_types[ret[0]]
436 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard39801e52008-06-03 16:08:54 +0000437 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000438 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
439 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000440 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000441 if skipped_types.has_key(ret[0]):
442 return 0
443 if unknown_types.has_key(ret[0]):
444 lst = unknown_types[ret[0]]
445 lst.append(name)
446 else:
447 unknown_types[ret[0]] = [name]
448 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000449
Daniel Veillard95175012005-07-03 16:09:51 +0000450 if cond != None and cond != "":
451 include.write("#if %s\n" % cond)
452 export.write("#if %s\n" % cond)
453 output.write("#if %s\n" % cond)
Daniel Veillard42766c02002-08-22 20:52:17 +0000454
Daniel Veillard96fe0952002-01-30 20:52:23 +0000455 include.write("PyObject * ")
Daniel Veillard39801e52008-06-03 16:08:54 +0000456 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000457
Daniel Veillardd2379012002-03-15 22:24:56 +0000458 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000459 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000460
461 if file == "python":
462 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000463 if cond != None and cond != "":
464 include.write("#endif\n")
465 export.write("#endif\n")
466 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000467 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000468 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000469 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000470 if cond != None and cond != "":
471 include.write("#endif\n")
472 export.write("#endif\n")
473 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000474 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000475
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000476 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000477 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
478 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000479 if format == "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000480 output.write(" ATTRIBUTE_UNUSED")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000481 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000482 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000483 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000484 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000485 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000486 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000487 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000489 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000490 (format, format_args))
491 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000492 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000493 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000494
495 output.write(c_call)
496 output.write(ret_convert)
497 output.write("}\n\n")
Daniel Veillard95175012005-07-03 16:09:51 +0000498 if cond != None and cond != "":
499 include.write("#endif /* %s */\n" % cond)
500 export.write("#endif /* %s */\n" % cond)
501 output.write("#endif /* %s */\n" % cond)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000502 return 1
503
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000504def buildStubs():
505 global py_types
506 global py_return_types
507 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000508
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000509 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000510 f = open(os.path.join(srcPref,"libxml2-api.xml"))
511 data = f.read()
512 (parser, target) = getparser()
513 parser.feed(data)
514 parser.close()
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000515 except IOError, msg:
Daniel Veillard39801e52008-06-03 16:08:54 +0000516 try:
517 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
518 data = f.read()
519 (parser, target) = getparser()
520 parser.feed(data)
521 parser.close()
522 except IOError, msg:
523 print file, ":", msg
524 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000525
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000526 n = len(functions.keys())
527 print "Found %d functions in libxml2-api.xml" % (n)
528
529 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
530 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000531 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
532 data = f.read()
533 (parser, target) = getparser()
534 parser.feed(data)
535 parser.close()
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000536 except IOError, msg:
Daniel Veillard39801e52008-06-03 16:08:54 +0000537 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000538
539
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000540 print "Found %d functions in libxml2-python-api.xml" % (
Daniel Veillard39801e52008-06-03 16:08:54 +0000541 len(functions.keys()) - n)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000542 nb_wrap = 0
543 failed = 0
544 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000545
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000546 include = open("libxml2-py.h", "w")
547 include.write("/* Generated */\n\n")
548 export = open("libxml2-export.c", "w")
549 export.write("/* Generated */\n\n")
550 wrapper = open("libxml2-py.c", "w")
551 wrapper.write("/* Generated */\n\n")
552 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000553 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000554 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000555 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000556 wrapper.write("#include \"libxml_wrap.h\"\n")
557 wrapper.write("#include \"libxml2-py.h\"\n\n")
Mike Hommey10455bb2010-10-15 18:39:50 +0200558 for function in sorted(functions.keys()):
Daniel Veillard39801e52008-06-03 16:08:54 +0000559 ret = print_function_wrapper(function, wrapper, export, include)
560 if ret < 0:
561 failed = failed + 1
562 del functions[function]
563 if ret == 0:
564 skipped = skipped + 1
565 del functions[function]
566 if ret == 1:
567 nb_wrap = nb_wrap + 1
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000568 include.close()
569 export.close()
570 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000571
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000572 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
Daniel Veillard39801e52008-06-03 16:08:54 +0000573 failed, skipped)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000574 print "Missing type converters: "
575 for type in unknown_types.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000576 print "%s:%d " % (type, len(unknown_types[type])),
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000577 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000578
Daniel Veillard1971ee22002-01-31 20:29:19 +0000579#######################################################################
580#
581# This part writes part of the Python front-end classes based on
582# mapping rules between types and classes and also based on function
583# renaming to get consistent function names at the Python level
584#
585#######################################################################
586
587#
588# The type automatically remapped to generated classes
589#
590classes_type = {
591 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
592 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
593 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
594 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
595 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
596 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
597 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
598 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
599 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
600 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
601 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
602 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
603 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
604 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
605 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
606 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
607 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
608 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
609 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000610 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
611 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
612 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000613 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
614 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000615 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
616 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000617 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000618 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000619 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000620 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000621 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
622 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000623 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000624 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000625 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000626 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
627 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
628 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000629 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
630 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
631 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000632}
633
634converter_type = {
635 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
636}
637
638primary_classes = ["xmlNode", "xmlDoc"]
639
640classes_ancestor = {
641 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000642 "xmlDtd" : "xmlNode",
643 "xmlDoc" : "xmlNode",
644 "xmlAttr" : "xmlNode",
645 "xmlNs" : "xmlNode",
646 "xmlEntity" : "xmlNode",
647 "xmlElement" : "xmlNode",
648 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000649 "outputBuffer": "ioWriteWrapper",
650 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000651 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000652 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard0e460da2005-03-30 22:47:10 +0000653 "ValidCtxt": "ValidCtxtCore",
654 "SchemaValidCtxt": "SchemaValidCtxtCore",
655 "relaxNgValidCtxt": "relaxNgValidCtxtCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000656}
657classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000658 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000659 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000660 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000661# "outputBuffer": "xmlOutputBufferClose",
662 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000663 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000664 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000665 "relaxNgSchema": "xmlRelaxNGFree",
666 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
667 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard39801e52008-06-03 16:08:54 +0000668 "Schema": "xmlSchemaFree",
669 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
670 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000671 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000672}
673
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000674functions_noexcept = {
675 "xmlHasProp": 1,
676 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000677 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000678 "xmlNodeGetNs": 1,
679 "xmlNodeGetNsDefs": 1,
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +0000680 "xmlNextElementSibling": 1,
681 "xmlPreviousElementSibling": 1,
682 "xmlFirstElementChild": 1,
683 "xmlLastElementChild": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000684}
685
Daniel Veillarddc85f282002-12-31 11:18:37 +0000686reference_keepers = {
687 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000688 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard39801e52008-06-03 16:08:54 +0000689 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000690}
691
Daniel Veillard36ed5292002-01-30 23:49:06 +0000692function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000693
694function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000695
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000696def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000697 listname = classe + "List"
698 ll = len(listname)
699 l = len(classe)
700 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000701 func = name[l:]
702 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000703 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
704 func = name[12:]
705 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000706 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
707 func = name[12:]
708 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000709 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
710 func = name[10:]
711 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000712 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
713 func = name[9:]
714 func = string.lower(func[0:1]) + func[1:]
715 elif name[0:9] == "xmlURISet" and file == "python_accessor":
716 func = name[6:]
717 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000718 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
719 func = name[11:]
720 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000721 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
722 func = name[17:]
723 func = string.lower(func[0:1]) + func[1:]
724 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
725 func = name[11:]
726 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000727 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
728 func = name[8:]
729 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000730 elif name[0:15] == "xmlOutputBuffer" and file != "python":
731 func = name[15:]
732 func = string.lower(func[0:1]) + func[1:]
733 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
734 func = name[20:]
735 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000736 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000737 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000738 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000739 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000740 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
741 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000742 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
743 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000744 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
745 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000746 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
747 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000748 elif name[0:11] == "xmlACatalog":
749 func = name[11:]
750 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000751 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000752 func = name[l:]
753 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000754 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000755 func = name[7:]
756 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000757 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000758 func = name[6:]
759 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000760 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000761 func = name[3:]
762 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000763 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000764 func = name
765 if func[0:5] == "xPath":
766 func = "xpath" + func[5:]
767 elif func[0:4] == "xPtr":
768 func = "xpointer" + func[4:]
769 elif func[0:8] == "xInclude":
770 func = "xinclude" + func[8:]
771 elif func[0:2] == "iD":
772 func = "ID" + func[2:]
773 elif func[0:3] == "uRI":
774 func = "URI" + func[3:]
775 elif func[0:4] == "uTF8":
776 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000777 elif func[0:3] == 'sAX':
778 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000779 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000780
Daniel Veillard36ed5292002-01-30 23:49:06 +0000781
Daniel Veillard1971ee22002-01-31 20:29:19 +0000782def functionCompare(info1, info2):
783 (index1, func1, name1, ret1, args1, file1) = info1
784 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000785 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000786 if func1 < func2:
787 return -1
788 if func1 > func2:
789 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000790 if file1 == "python_accessor":
791 return -1
792 if file2 == "python_accessor":
793 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000794 if file1 < file2:
795 return -1
796 if file1 > file2:
797 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000798 return 0
799
800def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000801 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000802 return
803 val = functions[name][0]
Daniel Veillard39801e52008-06-03 16:08:54 +0000804 val = string.replace(val, "NULL", "None")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000805 output.write(indent)
806 output.write('"""')
807 while len(val) > 60:
Daniel Veillarded939f82008-04-08 08:20:08 +0000808 if val[0] == " ":
Daniel Veillard39801e52008-06-03 16:08:54 +0000809 val = val[1:]
810 continue
Daniel Veillard1971ee22002-01-31 20:29:19 +0000811 str = val[0:60]
Daniel Veillard39801e52008-06-03 16:08:54 +0000812 i = string.rfind(str, " ")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000813 if i < 0:
814 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000815 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000816 val = val[i:]
817 output.write(str)
Daniel Veillard39801e52008-06-03 16:08:54 +0000818 output.write('\n ')
Daniel Veillard01a6d412002-02-11 18:42:20 +0000819 output.write(indent)
Daniel Veillard39801e52008-06-03 16:08:54 +0000820 output.write(val)
Daniel Veillardd076a202002-11-20 13:28:31 +0000821 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000822
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000823def buildWrappers():
824 global ctypes
825 global py_types
826 global py_return_types
827 global unknown_types
828 global functions
829 global function_classes
830 global classes_type
831 global classes_list
832 global converter_type
833 global primary_classes
834 global converter_type
835 global classes_ancestor
836 global converter_type
837 global primary_classes
838 global classes_ancestor
839 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000840 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000841
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000842 for type in classes_type.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000843 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000844
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000845 #
846 # Build the list of C types to look for ordered to start
847 # with primary classes
848 #
849 ctypes = []
850 classes_list = []
851 ctypes_processed = {}
852 classes_processed = {}
853 for classe in primary_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000854 classes_list.append(classe)
855 classes_processed[classe] = ()
856 for type in classes_type.keys():
857 tinfo = classes_type[type]
858 if tinfo[2] == classe:
859 ctypes.append(type)
860 ctypes_processed[type] = ()
Mike Hommey10455bb2010-10-15 18:39:50 +0200861 for type in sorted(classes_type.keys()):
Daniel Veillard39801e52008-06-03 16:08:54 +0000862 if ctypes_processed.has_key(type):
863 continue
864 tinfo = classes_type[type]
865 if not classes_processed.has_key(tinfo[2]):
866 classes_list.append(tinfo[2])
867 classes_processed[tinfo[2]] = ()
868
869 ctypes.append(type)
870 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000871
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000872 for name in functions.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000873 found = 0
874 (desc, ret, args, file, cond) = functions[name]
875 for type in ctypes:
876 classe = classes_type[type][2]
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000877
Daniel Veillard39801e52008-06-03 16:08:54 +0000878 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
879 found = 1
880 func = nameFixup(name, classe, type, file)
881 info = (0, func, name, ret, args, file)
882 function_classes[classe].append(info)
883 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
884 and file != "python_accessor":
885 found = 1
886 func = nameFixup(name, classe, type, file)
887 info = (1, func, name, ret, args, file)
888 function_classes[classe].append(info)
889 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
890 found = 1
891 func = nameFixup(name, classe, type, file)
892 info = (0, func, name, ret, args, file)
893 function_classes[classe].append(info)
894 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
895 and file != "python_accessor":
896 found = 1
897 func = nameFixup(name, classe, type, file)
898 info = (1, func, name, ret, args, file)
899 function_classes[classe].append(info)
900 if found == 1:
901 continue
902 if name[0:8] == "xmlXPath":
903 continue
904 if name[0:6] == "xmlStr":
905 continue
906 if name[0:10] == "xmlCharStr":
907 continue
908 func = nameFixup(name, "None", file, file)
909 info = (0, func, name, ret, args, file)
910 function_classes['None'].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000911
912 classes = open("libxml2class.py", "w")
913 txt = open("libxml2class.txt", "w")
914 txt.write(" Generated Classes for libxml2-python\n\n")
915
916 txt.write("#\n# Global functions of the module\n#\n\n")
917 if function_classes.has_key("None"):
Daniel Veillard39801e52008-06-03 16:08:54 +0000918 flist = function_classes["None"]
919 flist.sort(functionCompare)
920 oldfile = ""
921 for info in flist:
922 (index, func, name, ret, args, file) = info
923 if file != oldfile:
924 classes.write("#\n# Functions from module %s\n#\n\n" % file)
925 txt.write("\n# functions from module %s\n" % file)
926 oldfile = file
927 classes.write("def %s(" % func)
928 txt.write("%s()\n" % func)
929 n = 0
930 for arg in args:
931 if n != 0:
932 classes.write(", ")
933 classes.write("%s" % arg[0])
934 n = n + 1
935 classes.write("):\n")
936 writeDoc(name, args, ' ', classes)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000937
Daniel Veillard39801e52008-06-03 16:08:54 +0000938 for arg in args:
939 if classes_type.has_key(arg[1]):
940 classes.write(" if %s is None: %s__o = None\n" %
941 (arg[0], arg[0]))
942 classes.write(" else: %s__o = %s%s\n" %
943 (arg[0], arg[0], classes_type[arg[1]][0]))
944 if ret[0] != "void":
945 classes.write(" ret = ")
946 else:
947 classes.write(" ")
948 classes.write("libxml2mod.%s(" % name)
949 n = 0
950 for arg in args:
951 if n != 0:
952 classes.write(", ")
953 classes.write("%s" % arg[0])
954 if classes_type.has_key(arg[1]):
955 classes.write("__o")
956 n = n + 1
957 classes.write(")\n")
958 if ret[0] != "void":
959 if classes_type.has_key(ret[0]):
960 #
961 # Raise an exception
962 #
963 if functions_noexcept.has_key(name):
964 classes.write(" if ret is None:return None\n")
965 elif string.find(name, "URI") >= 0:
966 classes.write(
967 " if ret is None:raise uriError('%s() failed')\n"
968 % (name))
969 elif string.find(name, "XPath") >= 0:
970 classes.write(
971 " if ret is None:raise xpathError('%s() failed')\n"
972 % (name))
973 elif string.find(name, "Parse") >= 0:
974 classes.write(
975 " if ret is None:raise parserError('%s() failed')\n"
976 % (name))
977 else:
978 classes.write(
979 " if ret is None:raise treeError('%s() failed')\n"
980 % (name))
981 classes.write(" return ")
982 classes.write(classes_type[ret[0]][1] % ("ret"))
983 classes.write("\n")
984 else:
985 classes.write(" return ret\n")
986 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000987
988 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
989 for classname in classes_list:
Daniel Veillard39801e52008-06-03 16:08:54 +0000990 if classname == "None":
991 pass
992 else:
993 if classes_ancestor.has_key(classname):
994 txt.write("\n\nClass %s(%s)\n" % (classname,
995 classes_ancestor[classname]))
996 classes.write("class %s(%s):\n" % (classname,
997 classes_ancestor[classname]))
998 classes.write(" def __init__(self, _obj=None):\n")
999 if classes_ancestor[classname] == "xmlCore" or \
1000 classes_ancestor[classname] == "xmlNode":
1001 classes.write(" if type(_obj).__name__ != ")
1002 classes.write("'PyCObject':\n")
1003 classes.write(" raise TypeError, ")
1004 classes.write("'%s needs a PyCObject argument'\n" % \
1005 classname)
1006 if reference_keepers.has_key(classname):
1007 rlist = reference_keepers[classname]
1008 for ref in rlist:
1009 classes.write(" self.%s = None\n" % ref[1])
1010 classes.write(" self._o = _obj\n")
1011 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1012 classes_ancestor[classname]))
1013 if classes_ancestor[classname] == "xmlCore" or \
1014 classes_ancestor[classname] == "xmlNode":
1015 classes.write(" def __repr__(self):\n")
1016 format = "<%s (%%s) object at 0x%%x>" % (classname)
1017 classes.write(" return \"%s\" %% (self.name, long(pos_id (self)))\n\n" % (
1018 format))
1019 else:
1020 txt.write("Class %s()\n" % (classname))
1021 classes.write("class %s:\n" % (classname))
1022 classes.write(" def __init__(self, _obj=None):\n")
1023 if reference_keepers.has_key(classname):
1024 list = reference_keepers[classname]
1025 for ref in list:
1026 classes.write(" self.%s = None\n" % ref[1])
1027 classes.write(" if _obj != None:self._o = _obj;return\n")
1028 classes.write(" self._o = None\n\n")
1029 destruct=None
1030 if classes_destructors.has_key(classname):
1031 classes.write(" def __del__(self):\n")
1032 classes.write(" if self._o != None:\n")
1033 classes.write(" libxml2mod.%s(self._o)\n" %
1034 classes_destructors[classname])
1035 classes.write(" self._o = None\n\n")
1036 destruct=classes_destructors[classname]
1037 flist = function_classes[classname]
1038 flist.sort(functionCompare)
1039 oldfile = ""
1040 for info in flist:
1041 (index, func, name, ret, args, file) = info
1042 #
1043 # Do not provide as method the destructors for the class
1044 # to avoid double free
1045 #
1046 if name == destruct:
1047 continue
1048 if file != oldfile:
1049 if file == "python_accessor":
1050 classes.write(" # accessors for %s\n" % (classname))
1051 txt.write(" # accessors\n")
1052 else:
1053 classes.write(" #\n")
1054 classes.write(" # %s functions from module %s\n" % (
1055 classname, file))
1056 txt.write("\n # functions from module %s\n" % file)
1057 classes.write(" #\n\n")
1058 oldfile = file
1059 classes.write(" def %s(self" % func)
1060 txt.write(" %s()\n" % func)
1061 n = 0
1062 for arg in args:
1063 if n != index:
1064 classes.write(", %s" % arg[0])
1065 n = n + 1
1066 classes.write("):\n")
1067 writeDoc(name, args, ' ', classes)
1068 n = 0
1069 for arg in args:
1070 if classes_type.has_key(arg[1]):
1071 if n != index:
1072 classes.write(" if %s is None: %s__o = None\n" %
1073 (arg[0], arg[0]))
1074 classes.write(" else: %s__o = %s%s\n" %
1075 (arg[0], arg[0], classes_type[arg[1]][0]))
1076 n = n + 1
1077 if ret[0] != "void":
1078 classes.write(" ret = ")
1079 else:
1080 classes.write(" ")
1081 classes.write("libxml2mod.%s(" % name)
1082 n = 0
1083 for arg in args:
1084 if n != 0:
1085 classes.write(", ")
1086 if n != index:
1087 classes.write("%s" % arg[0])
1088 if classes_type.has_key(arg[1]):
1089 classes.write("__o")
1090 else:
1091 classes.write("self")
1092 if classes_type.has_key(arg[1]):
1093 classes.write(classes_type[arg[1]][0])
1094 n = n + 1
1095 classes.write(")\n")
1096 if ret[0] != "void":
1097 if classes_type.has_key(ret[0]):
1098 #
1099 # Raise an exception
1100 #
1101 if functions_noexcept.has_key(name):
1102 classes.write(
1103 " if ret is None:return None\n")
1104 elif string.find(name, "URI") >= 0:
1105 classes.write(
1106 " if ret is None:raise uriError('%s() failed')\n"
1107 % (name))
1108 elif string.find(name, "XPath") >= 0:
1109 classes.write(
1110 " if ret is None:raise xpathError('%s() failed')\n"
1111 % (name))
1112 elif string.find(name, "Parse") >= 0:
1113 classes.write(
1114 " if ret is None:raise parserError('%s() failed')\n"
1115 % (name))
1116 else:
1117 classes.write(
1118 " if ret is None:raise treeError('%s() failed')\n"
1119 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001120
1121 #
Daniel Veillard39801e52008-06-03 16:08:54 +00001122 # generate the returned class wrapper for the object
1123 #
1124 classes.write(" __tmp = ")
1125 classes.write(classes_type[ret[0]][1] % ("ret"))
1126 classes.write("\n")
1127
1128 #
1129 # Sometime one need to keep references of the source
1130 # class in the returned class object.
1131 # See reference_keepers for the list
1132 #
1133 tclass = classes_type[ret[0]][2]
1134 if reference_keepers.has_key(tclass):
1135 list = reference_keepers[tclass]
1136 for pref in list:
1137 if pref[0] == classname:
1138 classes.write(" __tmp.%s = self\n" %
1139 pref[1])
1140 #
1141 # return the class
1142 #
1143 classes.write(" return __tmp\n")
1144 elif converter_type.has_key(ret[0]):
1145 #
1146 # Raise an exception
1147 #
1148 if functions_noexcept.has_key(name):
1149 classes.write(
1150 " if ret is None:return None")
1151 elif string.find(name, "URI") >= 0:
1152 classes.write(
1153 " if ret is None:raise uriError('%s() failed')\n"
1154 % (name))
1155 elif string.find(name, "XPath") >= 0:
1156 classes.write(
1157 " if ret is None:raise xpathError('%s() failed')\n"
1158 % (name))
1159 elif string.find(name, "Parse") >= 0:
1160 classes.write(
1161 " if ret is None:raise parserError('%s() failed')\n"
1162 % (name))
1163 else:
1164 classes.write(
1165 " if ret is None:raise treeError('%s() failed')\n"
1166 % (name))
1167 classes.write(" return ")
1168 classes.write(converter_type[ret[0]] % ("ret"))
1169 classes.write("\n")
1170 else:
1171 classes.write(" return ret\n")
1172 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001173
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001174 #
1175 # Generate enum constants
1176 #
1177 for type,enum in enums.items():
1178 classes.write("# %s\n" % type)
1179 items = enum.items()
1180 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1181 for name,value in items:
1182 classes.write("%s = %s\n" % (name,value))
Daniel Veillard39801e52008-06-03 16:08:54 +00001183 classes.write("\n")
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001184
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001185 txt.close()
1186 classes.close()
1187
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001188buildStubs()
1189buildWrappers()