blob: b864b16c0944278aa1951da1196d48e5827cea95 [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 Veillard3cb1ae22013-03-27 22:40:54 +080050 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 Veillard3cb1ae22013-03-27 22:40:54 +080057 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 Veillard3cb1ae22013-03-27 22:40:54 +080062 print("start %s, %s" % (tag, attrs))
Daniel Veillard01a6d412002-02-11 18:42:20 +000063 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
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080072 if 'name' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000073 self.function = attrs['name']
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080074 if 'file' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000075 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
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080085 if 'name' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000086 self.function_arg_name = attrs['name']
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080087 if 'type' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000088 self.function_arg_type = attrs['type']
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080089 if 'info' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000090 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
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080096 if 'type' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000097 self.function_return_type = attrs['type']
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080098 if 'info' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +000099 self.function_return_info = attrs['info']
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800100 if 'field' in attrs.keys():
Daniel Veillard01a6d412002-02-11 18:42:20 +0000101 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 Veillard3cb1ae22013-03-27 22:40:54 +0800107 print("end %s" % tag)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000108 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):
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800142 if type not in enums:
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000143 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":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800343 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:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800356 print("failed to get function %s infos")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000357 return
358
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800359 if file in skipped_modules:
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000360 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 Veillard3cb1ae22013-03-27 22:40:54 +0800379 if arg[1] in py_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000380 (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:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800401 if arg[1] in skipped_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000402 return 0
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800403 if arg[1] in unknown_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000404 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 Veillard3cb1ae22013-03-27 22:40:54 +0800425 elif ret[0] in py_types:
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 Veillard3cb1ae22013-03-27 22:40:54 +0800434 elif ret[0] in py_return_types:
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 Veillard3cb1ae22013-03-27 22:40:54 +0800441 if ret[0] in skipped_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000442 return 0
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800443 if ret[0] in unknown_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000444 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 Veillard3cb1ae22013-03-27 22:40:54 +0800515 except IOError as 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()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800522 except IOError as msg:
523 print(file, ":", msg)
Daniel Veillard39801e52008-06-03 16:08:54 +0000524 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000525
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800526 n = len(list(functions.keys()))
527 print("Found %d functions in libxml2-api.xml" % (n))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000528
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 Veillard3cb1ae22013-03-27 22:40:54 +0800536 except IOError as msg:
537 print(file, ":", msg)
Daniel Veillard9589d452002-02-02 10:28:17 +0000538
539
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800540 print("Found %d functions in libxml2-python-api.xml" % (
541 len(list(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 Veillard3cb1ae22013-03-27 22:40:54 +0800572 print("Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
573 failed, skipped))
574 print("Missing type converters: ")
575 for type in list(unknown_types.keys()):
576 print("%s:%d " % (type, len(unknown_types[type])))
577 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800702 func = func[0:1].lower() + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000703 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
704 func = name[12:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800705 func = func[0:1].lower() + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000706 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
707 func = name[12:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800708 func = func[0:1].lower() + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000709 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
710 func = name[10:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800711 func = func[0:1].lower() + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000712 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
713 func = name[9:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800714 func = func[0:1].lower() + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000715 elif name[0:9] == "xmlURISet" and file == "python_accessor":
716 func = name[6:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800717 func = func[0:1].lower() + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000718 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
719 func = name[11:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800720 func = func[0:1].lower() + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000721 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
722 func = name[17:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800723 func = func[0:1].lower() + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000724 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
725 func = name[11:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800726 func = func[0:1].lower() + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000727 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
728 func = name[8:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800729 func = func[0:1].lower() + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000730 elif name[0:15] == "xmlOutputBuffer" and file != "python":
731 func = name[15:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800732 func = func[0:1].lower() + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000733 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
734 func = name[20:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800735 func = func[0:1].lower() + 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800750 func = func[0:1].lower() + 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800753 func = func[0:1].lower() + 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800756 func = func[0:1].lower() + 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800759 func = func[0:1].lower() + 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:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800762 func = func[0:1].lower() + 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
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800800def cmp_to_key(mycmp):
801 'Convert a cmp= function into a key= function'
802 class K(object):
803 def __init__(self, obj, *args):
804 self.obj = obj
805 def __lt__(self, other):
806 return mycmp(self.obj, other.obj) < 0
807 def __gt__(self, other):
808 return mycmp(self.obj, other.obj) > 0
809 def __eq__(self, other):
810 return mycmp(self.obj, other.obj) == 0
811 def __le__(self, other):
812 return mycmp(self.obj, other.obj) <= 0
813 def __ge__(self, other):
814 return mycmp(self.obj, other.obj) >= 0
815 def __ne__(self, other):
816 return mycmp(self.obj, other.obj) != 0
817 return K
Daniel Veillard1971ee22002-01-31 20:29:19 +0000818def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000819 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000820 return
821 val = functions[name][0]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800822 val = val.replace("NULL", "None")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000823 output.write(indent)
824 output.write('"""')
825 while len(val) > 60:
Daniel Veillarded939f82008-04-08 08:20:08 +0000826 if val[0] == " ":
Daniel Veillard39801e52008-06-03 16:08:54 +0000827 val = val[1:]
828 continue
Daniel Veillard1971ee22002-01-31 20:29:19 +0000829 str = val[0:60]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800830 i = str.rfind(" ")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000831 if i < 0:
832 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000833 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000834 val = val[i:]
835 output.write(str)
Daniel Veillard39801e52008-06-03 16:08:54 +0000836 output.write('\n ')
Daniel Veillard01a6d412002-02-11 18:42:20 +0000837 output.write(indent)
Daniel Veillard39801e52008-06-03 16:08:54 +0000838 output.write(val)
Daniel Veillardd076a202002-11-20 13:28:31 +0000839 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000840
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000841def buildWrappers():
842 global ctypes
843 global py_types
844 global py_return_types
845 global unknown_types
846 global functions
847 global function_classes
848 global classes_type
849 global classes_list
850 global converter_type
851 global primary_classes
852 global converter_type
853 global classes_ancestor
854 global converter_type
855 global primary_classes
856 global classes_ancestor
857 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000858 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000859
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000860 for type in classes_type.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000861 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000862
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000863 #
864 # Build the list of C types to look for ordered to start
865 # with primary classes
866 #
867 ctypes = []
868 classes_list = []
869 ctypes_processed = {}
870 classes_processed = {}
871 for classe in primary_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000872 classes_list.append(classe)
873 classes_processed[classe] = ()
874 for type in classes_type.keys():
875 tinfo = classes_type[type]
876 if tinfo[2] == classe:
877 ctypes.append(type)
878 ctypes_processed[type] = ()
Mike Hommey10455bb2010-10-15 18:39:50 +0200879 for type in sorted(classes_type.keys()):
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800880 if type in ctypes_processed:
Daniel Veillard39801e52008-06-03 16:08:54 +0000881 continue
882 tinfo = classes_type[type]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800883 if tinfo[2] not in classes_processed:
Daniel Veillard39801e52008-06-03 16:08:54 +0000884 classes_list.append(tinfo[2])
885 classes_processed[tinfo[2]] = ()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800886
Daniel Veillard39801e52008-06-03 16:08:54 +0000887 ctypes.append(type)
888 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000889
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000890 for name in functions.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000891 found = 0
892 (desc, ret, args, file, cond) = functions[name]
893 for type in ctypes:
894 classe = classes_type[type][2]
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000895
Daniel Veillard39801e52008-06-03 16:08:54 +0000896 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
897 found = 1
898 func = nameFixup(name, classe, type, file)
899 info = (0, func, name, ret, args, file)
900 function_classes[classe].append(info)
901 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
902 and file != "python_accessor":
903 found = 1
904 func = nameFixup(name, classe, type, file)
905 info = (1, func, name, ret, args, file)
906 function_classes[classe].append(info)
907 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
908 found = 1
909 func = nameFixup(name, classe, type, file)
910 info = (0, func, name, ret, args, file)
911 function_classes[classe].append(info)
912 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
913 and file != "python_accessor":
914 found = 1
915 func = nameFixup(name, classe, type, file)
916 info = (1, func, name, ret, args, file)
917 function_classes[classe].append(info)
918 if found == 1:
919 continue
920 if name[0:8] == "xmlXPath":
921 continue
922 if name[0:6] == "xmlStr":
923 continue
924 if name[0:10] == "xmlCharStr":
925 continue
926 func = nameFixup(name, "None", file, file)
927 info = (0, func, name, ret, args, file)
928 function_classes['None'].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000929
930 classes = open("libxml2class.py", "w")
931 txt = open("libxml2class.txt", "w")
932 txt.write(" Generated Classes for libxml2-python\n\n")
933
934 txt.write("#\n# Global functions of the module\n#\n\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800935 if "None" in function_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000936 flist = function_classes["None"]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800937 flist = sorted(flist, key=cmp_to_key(functionCompare))
Daniel Veillard39801e52008-06-03 16:08:54 +0000938 oldfile = ""
939 for info in flist:
940 (index, func, name, ret, args, file) = info
941 if file != oldfile:
942 classes.write("#\n# Functions from module %s\n#\n\n" % file)
943 txt.write("\n# functions from module %s\n" % file)
944 oldfile = file
945 classes.write("def %s(" % func)
946 txt.write("%s()\n" % func)
947 n = 0
948 for arg in args:
949 if n != 0:
950 classes.write(", ")
951 classes.write("%s" % arg[0])
952 n = n + 1
953 classes.write("):\n")
954 writeDoc(name, args, ' ', classes)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000955
Daniel Veillard39801e52008-06-03 16:08:54 +0000956 for arg in args:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800957 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +0000958 classes.write(" if %s is None: %s__o = None\n" %
959 (arg[0], arg[0]))
960 classes.write(" else: %s__o = %s%s\n" %
961 (arg[0], arg[0], classes_type[arg[1]][0]))
962 if ret[0] != "void":
963 classes.write(" ret = ")
964 else:
965 classes.write(" ")
966 classes.write("libxml2mod.%s(" % name)
967 n = 0
968 for arg in args:
969 if n != 0:
970 classes.write(", ")
971 classes.write("%s" % arg[0])
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800972 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +0000973 classes.write("__o")
974 n = n + 1
975 classes.write(")\n")
976 if ret[0] != "void":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800977 if ret[0] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +0000978 #
979 # Raise an exception
980 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800981 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +0000982 classes.write(" if ret is None:return None\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800983 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +0000984 classes.write(
985 " if ret is None:raise uriError('%s() failed')\n"
986 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800987 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +0000988 classes.write(
989 " if ret is None:raise xpathError('%s() failed')\n"
990 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800991 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +0000992 classes.write(
993 " if ret is None:raise parserError('%s() failed')\n"
994 % (name))
995 else:
996 classes.write(
997 " if ret is None:raise treeError('%s() failed')\n"
998 % (name))
999 classes.write(" return ")
1000 classes.write(classes_type[ret[0]][1] % ("ret"))
1001 classes.write("\n")
1002 else:
1003 classes.write(" return ret\n")
1004 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001005
1006 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1007 for classname in classes_list:
Daniel Veillard39801e52008-06-03 16:08:54 +00001008 if classname == "None":
1009 pass
1010 else:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001011 if classname in classes_ancestor:
Daniel Veillard39801e52008-06-03 16:08:54 +00001012 txt.write("\n\nClass %s(%s)\n" % (classname,
1013 classes_ancestor[classname]))
1014 classes.write("class %s(%s):\n" % (classname,
1015 classes_ancestor[classname]))
1016 classes.write(" def __init__(self, _obj=None):\n")
1017 if classes_ancestor[classname] == "xmlCore" or \
1018 classes_ancestor[classname] == "xmlNode":
1019 classes.write(" if type(_obj).__name__ != ")
1020 classes.write("'PyCObject':\n")
1021 classes.write(" raise TypeError, ")
1022 classes.write("'%s needs a PyCObject argument'\n" % \
1023 classname)
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001024 if classname in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001025 rlist = reference_keepers[classname]
1026 for ref in rlist:
1027 classes.write(" self.%s = None\n" % ref[1])
1028 classes.write(" self._o = _obj\n")
1029 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1030 classes_ancestor[classname]))
1031 if classes_ancestor[classname] == "xmlCore" or \
1032 classes_ancestor[classname] == "xmlNode":
1033 classes.write(" def __repr__(self):\n")
1034 format = "<%s (%%s) object at 0x%%x>" % (classname)
1035 classes.write(" return \"%s\" %% (self.name, long(pos_id (self)))\n\n" % (
1036 format))
1037 else:
1038 txt.write("Class %s()\n" % (classname))
1039 classes.write("class %s:\n" % (classname))
1040 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001041 if classname in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001042 list = reference_keepers[classname]
1043 for ref in list:
1044 classes.write(" self.%s = None\n" % ref[1])
1045 classes.write(" if _obj != None:self._o = _obj;return\n")
1046 classes.write(" self._o = None\n\n")
1047 destruct=None
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001048 if classname in classes_destructors:
Daniel Veillard39801e52008-06-03 16:08:54 +00001049 classes.write(" def __del__(self):\n")
1050 classes.write(" if self._o != None:\n")
1051 classes.write(" libxml2mod.%s(self._o)\n" %
1052 classes_destructors[classname])
1053 classes.write(" self._o = None\n\n")
1054 destruct=classes_destructors[classname]
1055 flist = function_classes[classname]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001056 flist = sorted(flist, key=cmp_to_key(functionCompare))
Daniel Veillard39801e52008-06-03 16:08:54 +00001057 oldfile = ""
1058 for info in flist:
1059 (index, func, name, ret, args, file) = info
1060 #
1061 # Do not provide as method the destructors for the class
1062 # to avoid double free
1063 #
1064 if name == destruct:
1065 continue
1066 if file != oldfile:
1067 if file == "python_accessor":
1068 classes.write(" # accessors for %s\n" % (classname))
1069 txt.write(" # accessors\n")
1070 else:
1071 classes.write(" #\n")
1072 classes.write(" # %s functions from module %s\n" % (
1073 classname, file))
1074 txt.write("\n # functions from module %s\n" % file)
1075 classes.write(" #\n\n")
1076 oldfile = file
1077 classes.write(" def %s(self" % func)
1078 txt.write(" %s()\n" % func)
1079 n = 0
1080 for arg in args:
1081 if n != index:
1082 classes.write(", %s" % arg[0])
1083 n = n + 1
1084 classes.write("):\n")
1085 writeDoc(name, args, ' ', classes)
1086 n = 0
1087 for arg in args:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001088 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001089 if n != index:
1090 classes.write(" if %s is None: %s__o = None\n" %
1091 (arg[0], arg[0]))
1092 classes.write(" else: %s__o = %s%s\n" %
1093 (arg[0], arg[0], classes_type[arg[1]][0]))
1094 n = n + 1
1095 if ret[0] != "void":
1096 classes.write(" ret = ")
1097 else:
1098 classes.write(" ")
1099 classes.write("libxml2mod.%s(" % name)
1100 n = 0
1101 for arg in args:
1102 if n != 0:
1103 classes.write(", ")
1104 if n != index:
1105 classes.write("%s" % arg[0])
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001106 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001107 classes.write("__o")
1108 else:
1109 classes.write("self")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001110 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001111 classes.write(classes_type[arg[1]][0])
1112 n = n + 1
1113 classes.write(")\n")
1114 if ret[0] != "void":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001115 if ret[0] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001116 #
1117 # Raise an exception
1118 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001119 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +00001120 classes.write(
1121 " if ret is None:return None\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001122 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001123 classes.write(
1124 " if ret is None:raise uriError('%s() failed')\n"
1125 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001126 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001127 classes.write(
1128 " if ret is None:raise xpathError('%s() failed')\n"
1129 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001130 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001131 classes.write(
1132 " if ret is None:raise parserError('%s() failed')\n"
1133 % (name))
1134 else:
1135 classes.write(
1136 " if ret is None:raise treeError('%s() failed')\n"
1137 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001138
1139 #
Daniel Veillard39801e52008-06-03 16:08:54 +00001140 # generate the returned class wrapper for the object
1141 #
1142 classes.write(" __tmp = ")
1143 classes.write(classes_type[ret[0]][1] % ("ret"))
1144 classes.write("\n")
1145
1146 #
1147 # Sometime one need to keep references of the source
1148 # class in the returned class object.
1149 # See reference_keepers for the list
1150 #
1151 tclass = classes_type[ret[0]][2]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001152 if tclass in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001153 list = reference_keepers[tclass]
1154 for pref in list:
1155 if pref[0] == classname:
1156 classes.write(" __tmp.%s = self\n" %
1157 pref[1])
1158 #
1159 # return the class
1160 #
1161 classes.write(" return __tmp\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001162 elif ret[0] in converter_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001163 #
1164 # Raise an exception
1165 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001166 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +00001167 classes.write(
1168 " if ret is None:return None")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001169 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001170 classes.write(
1171 " if ret is None:raise uriError('%s() failed')\n"
1172 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001173 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001174 classes.write(
1175 " if ret is None:raise xpathError('%s() failed')\n"
1176 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001177 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001178 classes.write(
1179 " if ret is None:raise parserError('%s() failed')\n"
1180 % (name))
1181 else:
1182 classes.write(
1183 " if ret is None:raise treeError('%s() failed')\n"
1184 % (name))
1185 classes.write(" return ")
1186 classes.write(converter_type[ret[0]] % ("ret"))
1187 classes.write("\n")
1188 else:
1189 classes.write(" return ret\n")
1190 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001191
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001192 #
1193 # Generate enum constants
1194 #
1195 for type,enum in enums.items():
1196 classes.write("# %s\n" % type)
1197 items = enum.items()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001198 items = sorted(items, key=(lambda i: int(i[1])))
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001199 for name,value in items:
1200 classes.write("%s = %s\n" % (name,value))
Daniel Veillard39801e52008-06-03 16:08:54 +00001201 classes.write("\n")
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001202
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001203 txt.close()
1204 classes.close()
1205
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001206buildStubs()
1207buildWrappers()