blob: c0cb3addcaf55950fcbc133ec01c6f1eb6818ced [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 Veillard3798c4a2013-03-29 13:46:24 +0800136
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=""
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800373 c_release=""
William M. Brack106cad62004-12-23 15:56:12 +0000374 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000375 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000376 # This should be correct
377 if arg[1][0:6] == "const ":
378 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000379 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800380 if arg[1] in py_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000381 (f, t, n, c) = py_types[arg[1]]
Daniel Veillard39801e52008-06-03 16:08:54 +0000382 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800383 f = 's#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000384 if f != None:
385 format = format + f
386 if t != None:
387 format_args = format_args + ", &pyobj_%s" % (arg[0])
388 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
389 c_convert = c_convert + \
390 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
Daniel Veillard39801e52008-06-03 16:08:54 +0000391 arg[1], t, arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000392 else:
393 format_args = format_args + ", &%s" % (arg[0])
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800394 if f == 's#':
Daniel Veillard39801e52008-06-03 16:08:54 +0000395 format_args = format_args + ", &py_buffsize%d" % num_bufs
396 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
397 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000398 if c_call != "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000399 c_call = c_call + ", "
Daniel Veillard01a6d412002-02-11 18:42:20 +0000400 c_call = c_call + "%s" % (arg[0])
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800401 if t == "File":
402 c_release = c_release + \
403 " PyFile_Release(%s);\n" % (arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000404 else:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800405 if arg[1] in skipped_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000406 return 0
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800407 if arg[1] in unknown_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000408 lst = unknown_types[arg[1]]
409 lst.append(name)
410 else:
411 unknown_types[arg[1]] = [name]
412 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000413 if format != "":
414 format = format + ":%s" % (name)
415
416 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000417 if file == "python_accessor":
Daniel Veillard39801e52008-06-03 16:08:54 +0000418 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
419 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
420 args[0][0], args[1][0], args[0][0], args[1][0])
421 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
422 args[1][0], args[1][1], args[1][0])
423 else:
424 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
425 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000426 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000427 c_call = "\n %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000428 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800429 elif ret[0] in py_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000430 (f, t, n, c) = py_types[ret[0]]
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800431 c_return = c_return + " %s c_retval;\n" % (ret[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000432 if file == "python_accessor" and ret[2] != None:
433 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
434 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000435 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000436 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
437 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800438 elif ret[0] in py_return_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000439 (f, t, n, c) = py_return_types[ret[0]]
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800440 c_return = c_return + " %s c_retval;\n" % (ret[0])
Daniel Veillard39801e52008-06-03 16:08:54 +0000441 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000442 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
443 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000444 else:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800445 if ret[0] in skipped_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000446 return 0
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800447 if ret[0] in unknown_types:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000448 lst = unknown_types[ret[0]]
449 lst.append(name)
450 else:
451 unknown_types[ret[0]] = [name]
452 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000453
Daniel Veillard95175012005-07-03 16:09:51 +0000454 if cond != None and cond != "":
455 include.write("#if %s\n" % cond)
456 export.write("#if %s\n" % cond)
457 output.write("#if %s\n" % cond)
Daniel Veillard42766c02002-08-22 20:52:17 +0000458
Daniel Veillard96fe0952002-01-30 20:52:23 +0000459 include.write("PyObject * ")
Daniel Veillard39801e52008-06-03 16:08:54 +0000460 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000461
Daniel Veillardd2379012002-03-15 22:24:56 +0000462 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000463 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000464
465 if file == "python":
466 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000467 if cond != None and cond != "":
468 include.write("#endif\n")
469 export.write("#endif\n")
470 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000471 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000472 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000473 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000474 if cond != None and cond != "":
475 include.write("#endif\n")
476 export.write("#endif\n")
477 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000478 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000479
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000480 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000481 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
482 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000483 if format == "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000484 output.write(" ATTRIBUTE_UNUSED")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000485 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000486 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000487 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000489 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000490 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000491 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000492 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000493 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000494 (format, format_args))
495 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000496 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000497 output.write(c_convert)
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800498
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000499 output.write(c_call)
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800500 if c_release != "":
501 output.write(c_release)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000502 output.write(ret_convert)
503 output.write("}\n\n")
Daniel Veillard95175012005-07-03 16:09:51 +0000504 if cond != None and cond != "":
505 include.write("#endif /* %s */\n" % cond)
506 export.write("#endif /* %s */\n" % cond)
507 output.write("#endif /* %s */\n" % cond)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000508 return 1
509
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000510def buildStubs():
511 global py_types
512 global py_return_types
513 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000514
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000515 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000516 f = open(os.path.join(srcPref,"libxml2-api.xml"))
517 data = f.read()
518 (parser, target) = getparser()
519 parser.feed(data)
520 parser.close()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800521 except IOError as msg:
Daniel Veillard39801e52008-06-03 16:08:54 +0000522 try:
523 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
524 data = f.read()
525 (parser, target) = getparser()
526 parser.feed(data)
527 parser.close()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800528 except IOError as msg:
529 print(file, ":", msg)
Daniel Veillard39801e52008-06-03 16:08:54 +0000530 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000531
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800532 n = len(list(functions.keys()))
533 print("Found %d functions in libxml2-api.xml" % (n))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000534
535 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
536 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000537 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
538 data = f.read()
539 (parser, target) = getparser()
540 parser.feed(data)
541 parser.close()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800542 except IOError as msg:
543 print(file, ":", msg)
Daniel Veillard9589d452002-02-02 10:28:17 +0000544
545
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800546 print("Found %d functions in libxml2-python-api.xml" % (
547 len(list(functions.keys())) - n))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000548 nb_wrap = 0
549 failed = 0
550 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000551
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000552 include = open("libxml2-py.h", "w")
553 include.write("/* Generated */\n\n")
554 export = open("libxml2-export.c", "w")
555 export.write("/* Generated */\n\n")
556 wrapper = open("libxml2-py.c", "w")
557 wrapper.write("/* Generated */\n\n")
558 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000559 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000560 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000561 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000562 wrapper.write("#include \"libxml_wrap.h\"\n")
563 wrapper.write("#include \"libxml2-py.h\"\n\n")
Mike Hommey10455bb2010-10-15 18:39:50 +0200564 for function in sorted(functions.keys()):
Daniel Veillard39801e52008-06-03 16:08:54 +0000565 ret = print_function_wrapper(function, wrapper, export, include)
566 if ret < 0:
567 failed = failed + 1
568 del functions[function]
569 if ret == 0:
570 skipped = skipped + 1
571 del functions[function]
572 if ret == 1:
573 nb_wrap = nb_wrap + 1
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000574 include.close()
575 export.close()
576 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000577
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800578 print("Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
579 failed, skipped))
580 print("Missing type converters: ")
581 for type in list(unknown_types.keys()):
582 print("%s:%d " % (type, len(unknown_types[type])))
583 print()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000584
Daniel Veillard1971ee22002-01-31 20:29:19 +0000585#######################################################################
586#
587# This part writes part of the Python front-end classes based on
588# mapping rules between types and classes and also based on function
589# renaming to get consistent function names at the Python level
590#
591#######################################################################
592
593#
594# The type automatically remapped to generated classes
595#
596classes_type = {
597 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
598 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
599 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
Daniel Veillardaf62eb42014-10-13 16:40:56 +0800600 "xmlDoc *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000601 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
602 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
603 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
604 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
605 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
606 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
607 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
608 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
609 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
610 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
611 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
612 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
613 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
614 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
615 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000616 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
617 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
618 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000619 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
620 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000621 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
622 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000623 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000624 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000625 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000626 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000627 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
628 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000629 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000630 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000631 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000632 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
633 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
634 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000635 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
636 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
637 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000638}
639
640converter_type = {
641 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
642}
643
644primary_classes = ["xmlNode", "xmlDoc"]
645
646classes_ancestor = {
647 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000648 "xmlDtd" : "xmlNode",
649 "xmlDoc" : "xmlNode",
650 "xmlAttr" : "xmlNode",
651 "xmlNs" : "xmlNode",
652 "xmlEntity" : "xmlNode",
653 "xmlElement" : "xmlNode",
654 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000655 "outputBuffer": "ioWriteWrapper",
656 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000657 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000658 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard0e460da2005-03-30 22:47:10 +0000659 "ValidCtxt": "ValidCtxtCore",
660 "SchemaValidCtxt": "SchemaValidCtxtCore",
661 "relaxNgValidCtxt": "relaxNgValidCtxtCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000662}
663classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000664 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000665 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000666 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000667# "outputBuffer": "xmlOutputBufferClose",
668 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000669 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000670 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000671 "relaxNgSchema": "xmlRelaxNGFree",
672 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
673 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard39801e52008-06-03 16:08:54 +0000674 "Schema": "xmlSchemaFree",
675 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
676 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000677 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000678}
679
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000680functions_noexcept = {
681 "xmlHasProp": 1,
682 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000683 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000684 "xmlNodeGetNs": 1,
685 "xmlNodeGetNsDefs": 1,
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +0000686 "xmlNextElementSibling": 1,
687 "xmlPreviousElementSibling": 1,
688 "xmlFirstElementChild": 1,
689 "xmlLastElementChild": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000690}
691
Daniel Veillarddc85f282002-12-31 11:18:37 +0000692reference_keepers = {
693 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000694 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard39801e52008-06-03 16:08:54 +0000695 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000696}
697
Daniel Veillard36ed5292002-01-30 23:49:06 +0000698function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000699
700function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000701
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000702def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000703 listname = classe + "List"
704 ll = len(listname)
705 l = len(classe)
706 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000707 func = name[l:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800708 func = func[0:1].lower() + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000709 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
710 func = name[12:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800711 func = func[0:1].lower() + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000712 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
713 func = name[12:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800714 func = func[0:1].lower() + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000715 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
716 func = name[10:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800717 func = func[0:1].lower() + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000718 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
719 func = name[9:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800720 func = func[0:1].lower() + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000721 elif name[0:9] == "xmlURISet" and file == "python_accessor":
722 func = name[6:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800723 func = func[0:1].lower() + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000724 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
725 func = name[11:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800726 func = func[0:1].lower() + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000727 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
728 func = name[17:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800729 func = func[0:1].lower() + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000730 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
731 func = name[11:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800732 func = func[0:1].lower() + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000733 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
734 func = name[8:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800735 func = func[0:1].lower() + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000736 elif name[0:15] == "xmlOutputBuffer" and file != "python":
737 func = name[15:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800738 func = func[0:1].lower() + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000739 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
740 func = name[20:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800741 func = func[0:1].lower() + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000742 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000743 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000744 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000745 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000746 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
747 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000748 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
749 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000750 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
751 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000752 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
753 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000754 elif name[0:11] == "xmlACatalog":
755 func = name[11:]
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:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000758 func = name[l:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800759 func = func[0:1].lower() + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000760 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000761 func = name[7:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800762 func = func[0:1].lower() + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000763 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000764 func = name[6:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800765 func = func[0:1].lower() + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000766 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000767 func = name[3:]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800768 func = func[0:1].lower() + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000769 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000770 func = name
771 if func[0:5] == "xPath":
772 func = "xpath" + func[5:]
773 elif func[0:4] == "xPtr":
774 func = "xpointer" + func[4:]
775 elif func[0:8] == "xInclude":
776 func = "xinclude" + func[8:]
777 elif func[0:2] == "iD":
778 func = "ID" + func[2:]
779 elif func[0:3] == "uRI":
780 func = "URI" + func[3:]
781 elif func[0:4] == "uTF8":
782 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000783 elif func[0:3] == 'sAX':
784 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000785 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000786
Daniel Veillard36ed5292002-01-30 23:49:06 +0000787
Daniel Veillard1971ee22002-01-31 20:29:19 +0000788def functionCompare(info1, info2):
789 (index1, func1, name1, ret1, args1, file1) = info1
790 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000791 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000792 if func1 < func2:
793 return -1
794 if func1 > func2:
795 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000796 if file1 == "python_accessor":
797 return -1
798 if file2 == "python_accessor":
799 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000800 if file1 < file2:
801 return -1
802 if file1 > file2:
803 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000804 return 0
805
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800806def cmp_to_key(mycmp):
807 'Convert a cmp= function into a key= function'
808 class K(object):
809 def __init__(self, obj, *args):
810 self.obj = obj
811 def __lt__(self, other):
812 return mycmp(self.obj, other.obj) < 0
813 def __gt__(self, other):
814 return mycmp(self.obj, other.obj) > 0
815 def __eq__(self, other):
816 return mycmp(self.obj, other.obj) == 0
817 def __le__(self, other):
818 return mycmp(self.obj, other.obj) <= 0
819 def __ge__(self, other):
820 return mycmp(self.obj, other.obj) >= 0
821 def __ne__(self, other):
822 return mycmp(self.obj, other.obj) != 0
823 return K
Daniel Veillard1971ee22002-01-31 20:29:19 +0000824def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000825 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000826 return
827 val = functions[name][0]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800828 val = val.replace("NULL", "None")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000829 output.write(indent)
830 output.write('"""')
831 while len(val) > 60:
Daniel Veillarded939f82008-04-08 08:20:08 +0000832 if val[0] == " ":
Daniel Veillard39801e52008-06-03 16:08:54 +0000833 val = val[1:]
834 continue
Daniel Veillard1971ee22002-01-31 20:29:19 +0000835 str = val[0:60]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800836 i = str.rfind(" ")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000837 if i < 0:
838 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000839 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000840 val = val[i:]
841 output.write(str)
Daniel Veillard39801e52008-06-03 16:08:54 +0000842 output.write('\n ')
Daniel Veillard01a6d412002-02-11 18:42:20 +0000843 output.write(indent)
Daniel Veillard39801e52008-06-03 16:08:54 +0000844 output.write(val)
Daniel Veillardd076a202002-11-20 13:28:31 +0000845 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000846
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000847def buildWrappers():
848 global ctypes
849 global py_types
850 global py_return_types
851 global unknown_types
852 global functions
853 global function_classes
854 global classes_type
855 global classes_list
856 global converter_type
857 global primary_classes
858 global converter_type
859 global classes_ancestor
860 global converter_type
861 global primary_classes
862 global classes_ancestor
863 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000864 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000865
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000866 for type in classes_type.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000867 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000868
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000869 #
870 # Build the list of C types to look for ordered to start
871 # with primary classes
872 #
873 ctypes = []
874 classes_list = []
875 ctypes_processed = {}
876 classes_processed = {}
877 for classe in primary_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000878 classes_list.append(classe)
879 classes_processed[classe] = ()
880 for type in classes_type.keys():
881 tinfo = classes_type[type]
882 if tinfo[2] == classe:
883 ctypes.append(type)
884 ctypes_processed[type] = ()
Mike Hommey10455bb2010-10-15 18:39:50 +0200885 for type in sorted(classes_type.keys()):
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800886 if type in ctypes_processed:
Daniel Veillard39801e52008-06-03 16:08:54 +0000887 continue
888 tinfo = classes_type[type]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800889 if tinfo[2] not in classes_processed:
Daniel Veillard39801e52008-06-03 16:08:54 +0000890 classes_list.append(tinfo[2])
891 classes_processed[tinfo[2]] = ()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800892
Daniel Veillard39801e52008-06-03 16:08:54 +0000893 ctypes.append(type)
894 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000895
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000896 for name in functions.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000897 found = 0
898 (desc, ret, args, file, cond) = functions[name]
899 for type in ctypes:
900 classe = classes_type[type][2]
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000901
Daniel Veillard39801e52008-06-03 16:08:54 +0000902 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
903 found = 1
904 func = nameFixup(name, classe, type, file)
905 info = (0, func, name, ret, args, file)
906 function_classes[classe].append(info)
907 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
908 and file != "python_accessor":
909 found = 1
910 func = nameFixup(name, classe, type, file)
911 info = (1, func, name, ret, args, file)
912 function_classes[classe].append(info)
913 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
914 found = 1
915 func = nameFixup(name, classe, type, file)
916 info = (0, func, name, ret, args, file)
917 function_classes[classe].append(info)
918 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
919 and file != "python_accessor":
920 found = 1
921 func = nameFixup(name, classe, type, file)
922 info = (1, func, name, ret, args, file)
923 function_classes[classe].append(info)
924 if found == 1:
925 continue
926 if name[0:8] == "xmlXPath":
927 continue
928 if name[0:6] == "xmlStr":
929 continue
930 if name[0:10] == "xmlCharStr":
931 continue
932 func = nameFixup(name, "None", file, file)
933 info = (0, func, name, ret, args, file)
934 function_classes['None'].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000935
936 classes = open("libxml2class.py", "w")
937 txt = open("libxml2class.txt", "w")
938 txt.write(" Generated Classes for libxml2-python\n\n")
939
940 txt.write("#\n# Global functions of the module\n#\n\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800941 if "None" in function_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000942 flist = function_classes["None"]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800943 flist = sorted(flist, key=cmp_to_key(functionCompare))
Daniel Veillard39801e52008-06-03 16:08:54 +0000944 oldfile = ""
945 for info in flist:
946 (index, func, name, ret, args, file) = info
947 if file != oldfile:
948 classes.write("#\n# Functions from module %s\n#\n\n" % file)
949 txt.write("\n# functions from module %s\n" % file)
950 oldfile = file
951 classes.write("def %s(" % func)
952 txt.write("%s()\n" % func)
953 n = 0
954 for arg in args:
955 if n != 0:
956 classes.write(", ")
957 classes.write("%s" % arg[0])
958 n = n + 1
959 classes.write("):\n")
960 writeDoc(name, args, ' ', classes)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000961
Daniel Veillard39801e52008-06-03 16:08:54 +0000962 for arg in args:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800963 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +0000964 classes.write(" if %s is None: %s__o = None\n" %
965 (arg[0], arg[0]))
966 classes.write(" else: %s__o = %s%s\n" %
967 (arg[0], arg[0], classes_type[arg[1]][0]))
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800968 if arg[1] in py_types:
969 (f, t, n, c) = py_types[arg[1]]
970 if t == "File":
971 classes.write(" if %s is not None: %s.flush()\n" % (
972 arg[0], arg[0]))
973
Daniel Veillard39801e52008-06-03 16:08:54 +0000974 if ret[0] != "void":
975 classes.write(" ret = ")
976 else:
977 classes.write(" ")
978 classes.write("libxml2mod.%s(" % name)
979 n = 0
980 for arg in args:
981 if n != 0:
982 classes.write(", ")
983 classes.write("%s" % arg[0])
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800984 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +0000985 classes.write("__o")
986 n = n + 1
987 classes.write(")\n")
Daniel Veillard3798c4a2013-03-29 13:46:24 +0800988
989# This may be needed to reposition the I/O, but likely to cause more harm
990# than good. Those changes in Python3 really break the model.
991# for arg in args:
992# if arg[1] in py_types:
993# (f, t, n, c) = py_types[arg[1]]
994# if t == "File":
995# classes.write(" if %s is not None: %s.seek(0,0)\n"%(
996# arg[0], arg[0]))
997
Daniel Veillard39801e52008-06-03 16:08:54 +0000998 if ret[0] != "void":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800999 if ret[0] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001000 #
1001 # Raise an exception
1002 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001003 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +00001004 classes.write(" if ret is None:return None\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001005 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001006 classes.write(
1007 " if ret is None:raise uriError('%s() failed')\n"
1008 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001009 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001010 classes.write(
1011 " if ret is None:raise xpathError('%s() failed')\n"
1012 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001013 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001014 classes.write(
1015 " if ret is None:raise parserError('%s() failed')\n"
1016 % (name))
1017 else:
1018 classes.write(
1019 " if ret is None:raise treeError('%s() failed')\n"
1020 % (name))
1021 classes.write(" return ")
1022 classes.write(classes_type[ret[0]][1] % ("ret"))
1023 classes.write("\n")
1024 else:
1025 classes.write(" return ret\n")
1026 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001027
1028 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1029 for classname in classes_list:
Daniel Veillard39801e52008-06-03 16:08:54 +00001030 if classname == "None":
1031 pass
1032 else:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001033 if classname in classes_ancestor:
Daniel Veillard39801e52008-06-03 16:08:54 +00001034 txt.write("\n\nClass %s(%s)\n" % (classname,
1035 classes_ancestor[classname]))
1036 classes.write("class %s(%s):\n" % (classname,
1037 classes_ancestor[classname]))
1038 classes.write(" def __init__(self, _obj=None):\n")
1039 if classes_ancestor[classname] == "xmlCore" or \
1040 classes_ancestor[classname] == "xmlNode":
Daniel Veillard6f184652013-03-29 15:17:40 +08001041 classes.write(" if checkWrapper(_obj) != 0:")
1042 classes.write(" raise TypeError")
1043 classes.write("('%s got a wrong wrapper object type')\n" % \
Daniel Veillard39801e52008-06-03 16:08:54 +00001044 classname)
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001045 if classname in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001046 rlist = reference_keepers[classname]
1047 for ref in rlist:
1048 classes.write(" self.%s = None\n" % ref[1])
1049 classes.write(" self._o = _obj\n")
1050 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1051 classes_ancestor[classname]))
1052 if classes_ancestor[classname] == "xmlCore" or \
1053 classes_ancestor[classname] == "xmlNode":
1054 classes.write(" def __repr__(self):\n")
1055 format = "<%s (%%s) object at 0x%%x>" % (classname)
Daniel Veillard6f184652013-03-29 15:17:40 +08001056 classes.write(" return \"%s\" %% (self.name, int(pos_id (self)))\n\n" % (
Daniel Veillard39801e52008-06-03 16:08:54 +00001057 format))
1058 else:
1059 txt.write("Class %s()\n" % (classname))
1060 classes.write("class %s:\n" % (classname))
1061 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001062 if classname in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001063 list = reference_keepers[classname]
1064 for ref in list:
1065 classes.write(" self.%s = None\n" % ref[1])
1066 classes.write(" if _obj != None:self._o = _obj;return\n")
1067 classes.write(" self._o = None\n\n")
1068 destruct=None
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001069 if classname in classes_destructors:
Daniel Veillard39801e52008-06-03 16:08:54 +00001070 classes.write(" def __del__(self):\n")
1071 classes.write(" if self._o != None:\n")
1072 classes.write(" libxml2mod.%s(self._o)\n" %
1073 classes_destructors[classname])
1074 classes.write(" self._o = None\n\n")
1075 destruct=classes_destructors[classname]
1076 flist = function_classes[classname]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001077 flist = sorted(flist, key=cmp_to_key(functionCompare))
Daniel Veillard39801e52008-06-03 16:08:54 +00001078 oldfile = ""
1079 for info in flist:
1080 (index, func, name, ret, args, file) = info
1081 #
1082 # Do not provide as method the destructors for the class
1083 # to avoid double free
1084 #
1085 if name == destruct:
1086 continue
1087 if file != oldfile:
1088 if file == "python_accessor":
1089 classes.write(" # accessors for %s\n" % (classname))
1090 txt.write(" # accessors\n")
1091 else:
1092 classes.write(" #\n")
1093 classes.write(" # %s functions from module %s\n" % (
1094 classname, file))
1095 txt.write("\n # functions from module %s\n" % file)
1096 classes.write(" #\n\n")
1097 oldfile = file
1098 classes.write(" def %s(self" % func)
1099 txt.write(" %s()\n" % func)
1100 n = 0
1101 for arg in args:
1102 if n != index:
1103 classes.write(", %s" % arg[0])
1104 n = n + 1
1105 classes.write("):\n")
1106 writeDoc(name, args, ' ', classes)
1107 n = 0
1108 for arg in args:
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001109 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001110 if n != index:
1111 classes.write(" if %s is None: %s__o = None\n" %
1112 (arg[0], arg[0]))
1113 classes.write(" else: %s__o = %s%s\n" %
1114 (arg[0], arg[0], classes_type[arg[1]][0]))
1115 n = n + 1
1116 if ret[0] != "void":
1117 classes.write(" ret = ")
1118 else:
1119 classes.write(" ")
1120 classes.write("libxml2mod.%s(" % name)
1121 n = 0
1122 for arg in args:
1123 if n != 0:
1124 classes.write(", ")
1125 if n != index:
1126 classes.write("%s" % arg[0])
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001127 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001128 classes.write("__o")
1129 else:
1130 classes.write("self")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001131 if arg[1] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001132 classes.write(classes_type[arg[1]][0])
1133 n = n + 1
1134 classes.write(")\n")
1135 if ret[0] != "void":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001136 if ret[0] in classes_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001137 #
1138 # Raise an exception
1139 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001140 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +00001141 classes.write(
1142 " if ret is None:return None\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001143 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001144 classes.write(
1145 " if ret is None:raise uriError('%s() failed')\n"
1146 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001147 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001148 classes.write(
1149 " if ret is None:raise xpathError('%s() failed')\n"
1150 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001151 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001152 classes.write(
1153 " if ret is None:raise parserError('%s() failed')\n"
1154 % (name))
1155 else:
1156 classes.write(
1157 " if ret is None:raise treeError('%s() failed')\n"
1158 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001159
1160 #
Daniel Veillard39801e52008-06-03 16:08:54 +00001161 # generate the returned class wrapper for the object
1162 #
1163 classes.write(" __tmp = ")
1164 classes.write(classes_type[ret[0]][1] % ("ret"))
1165 classes.write("\n")
1166
1167 #
1168 # Sometime one need to keep references of the source
1169 # class in the returned class object.
1170 # See reference_keepers for the list
1171 #
1172 tclass = classes_type[ret[0]][2]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001173 if tclass in reference_keepers:
Daniel Veillard39801e52008-06-03 16:08:54 +00001174 list = reference_keepers[tclass]
1175 for pref in list:
1176 if pref[0] == classname:
1177 classes.write(" __tmp.%s = self\n" %
1178 pref[1])
1179 #
1180 # return the class
1181 #
1182 classes.write(" return __tmp\n")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001183 elif ret[0] in converter_type:
Daniel Veillard39801e52008-06-03 16:08:54 +00001184 #
1185 # Raise an exception
1186 #
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001187 if name in functions_noexcept:
Daniel Veillard39801e52008-06-03 16:08:54 +00001188 classes.write(
1189 " if ret is None:return None")
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001190 elif name.find("URI") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001191 classes.write(
1192 " if ret is None:raise uriError('%s() failed')\n"
1193 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001194 elif name.find("XPath") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001195 classes.write(
1196 " if ret is None:raise xpathError('%s() failed')\n"
1197 % (name))
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001198 elif name.find("Parse") >= 0:
Daniel Veillard39801e52008-06-03 16:08:54 +00001199 classes.write(
1200 " if ret is None:raise parserError('%s() failed')\n"
1201 % (name))
1202 else:
1203 classes.write(
1204 " if ret is None:raise treeError('%s() failed')\n"
1205 % (name))
1206 classes.write(" return ")
1207 classes.write(converter_type[ret[0]] % ("ret"))
1208 classes.write("\n")
1209 else:
1210 classes.write(" return ret\n")
1211 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001212
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001213 #
1214 # Generate enum constants
1215 #
1216 for type,enum in enums.items():
1217 classes.write("# %s\n" % type)
1218 items = enum.items()
Daniel Veillard3cb1ae22013-03-27 22:40:54 +08001219 items = sorted(items, key=(lambda i: int(i[1])))
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001220 for name,value in items:
1221 classes.write("%s = %s\n" % (name,value))
Daniel Veillard39801e52008-06-03 16:08:54 +00001222 classes.write("\n")
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001223
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001224 txt.close()
1225 classes.close()
1226
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001227buildStubs()
1228buildWrappers()