blob: 92a3440a66f3cbaddc215eff6e015b2ab5d25d9b [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 Veillardd2897fd2002-01-30 16:37:32 +000027import xmllib
28try:
29 import sgmlop
30except ImportError:
31 sgmlop = None # accelerator not available
32
33debug = 0
34
35if sgmlop:
36 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000037 """sgmlop based XML parser. this is typically 15x faster
38 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000039
Daniel Veillard01a6d412002-02-11 18:42:20 +000040 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000041
Daniel Veillard01a6d412002-02-11 18:42:20 +000042 # setup callbacks
43 self.finish_starttag = target.start
44 self.finish_endtag = target.end
45 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000046
Daniel Veillard01a6d412002-02-11 18:42:20 +000047 # activate parser
48 self.parser = sgmlop.XMLParser()
49 self.parser.register(self)
50 self.feed = self.parser.feed
51 self.entity = {
52 "amp": "&", "gt": ">", "lt": "<",
53 "apos": "'", "quot": '"'
54 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000055
Daniel Veillard01a6d412002-02-11 18:42:20 +000056 def close(self):
57 try:
58 self.parser.close()
59 finally:
60 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000061
Daniel Veillard01a6d412002-02-11 18:42:20 +000062 def handle_entityref(self, entity):
63 # <string> entity
64 try:
65 self.handle_data(self.entity[entity])
66 except KeyError:
67 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000068
69else:
70 FastParser = None
71
72
73class SlowParser(xmllib.XMLParser):
74 """slow but safe standard parser, based on the XML parser in
75 Python's standard library."""
76
77 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000078 self.unknown_starttag = target.start
79 self.handle_data = target.data
80 self.unknown_endtag = target.end
81 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000082
83def getparser(target = None):
84 # get the fastest available parser, and attach it to an
85 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000086 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000087 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000088 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000089 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000090 return SlowParser(target), target
91
92class docParser:
93 def __init__(self):
94 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000095 self._data = []
96 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000097
98 def close(self):
99 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000100 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000101
102 def getmethodname(self):
103 return self._methodname
104
105 def data(self, text):
106 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000107 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000108 self._data.append(text)
109
110 def start(self, tag, attrs):
111 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000112 print "start %s, %s" % (tag, attrs)
113 if tag == 'function':
114 self._data = []
115 self.in_function = 1
116 self.function = None
Daniel Veillard95175012005-07-03 16:09:51 +0000117 self.function_cond = None
Daniel Veillard01a6d412002-02-11 18:42:20 +0000118 self.function_args = []
119 self.function_descr = None
120 self.function_return = None
121 self.function_file = None
122 if attrs.has_key('name'):
123 self.function = attrs['name']
124 if attrs.has_key('file'):
125 self.function_file = attrs['file']
Daniel Veillard95175012005-07-03 16:09:51 +0000126 elif tag == 'cond':
127 self._data = []
Daniel Veillard01a6d412002-02-11 18:42:20 +0000128 elif tag == 'info':
129 self._data = []
130 elif tag == 'arg':
131 if self.in_function == 1:
132 self.function_arg_name = None
133 self.function_arg_type = None
134 self.function_arg_info = None
135 if attrs.has_key('name'):
136 self.function_arg_name = attrs['name']
137 if attrs.has_key('type'):
138 self.function_arg_type = attrs['type']
139 if attrs.has_key('info'):
140 self.function_arg_info = attrs['info']
141 elif tag == 'return':
142 if self.in_function == 1:
143 self.function_return_type = None
144 self.function_return_info = None
145 self.function_return_field = None
146 if attrs.has_key('type'):
147 self.function_return_type = attrs['type']
148 if attrs.has_key('info'):
149 self.function_return_info = attrs['info']
150 if attrs.has_key('field'):
151 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000152 elif tag == 'enum':
153 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000154
155 def end(self, tag):
156 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000157 print "end %s" % tag
158 if tag == 'function':
159 if self.function != None:
160 function(self.function, self.function_descr,
161 self.function_return, self.function_args,
Daniel Veillard95175012005-07-03 16:09:51 +0000162 self.function_file, self.function_cond)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000163 self.in_function = 0
164 elif tag == 'arg':
165 if self.in_function == 1:
166 self.function_args.append([self.function_arg_name,
167 self.function_arg_type,
168 self.function_arg_info])
169 elif tag == 'return':
170 if self.in_function == 1:
171 self.function_return = [self.function_return_type,
172 self.function_return_info,
173 self.function_return_field]
174 elif tag == 'info':
175 str = ''
176 for c in self._data:
177 str = str + c
178 if self.in_function == 1:
179 self.function_descr = str
Daniel Veillard95175012005-07-03 16:09:51 +0000180 elif tag == 'cond':
181 str = ''
182 for c in self._data:
183 str = str + c
184 if self.in_function == 1:
185 self.function_cond = str
Daniel Veillard01a6d412002-02-11 18:42:20 +0000186
187
Daniel Veillard95175012005-07-03 16:09:51 +0000188def function(name, desc, ret, args, file, cond):
189 functions[name] = (desc, ret, args, file, cond)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000190
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000191def enum(type, name, value):
192 if not enums.has_key(type):
193 enums[type] = {}
194 enums[type][name] = value
195
Daniel Veillard1971ee22002-01-31 20:29:19 +0000196#######################################################################
197#
198# Some filtering rukes to drop functions/types which should not
199# be exposed as-is on the Python interface
200#
201#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000202
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000203skipped_modules = {
204 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000205 'DOCBparser': None,
206 'SAX': None,
207 'hash': None,
208 'list': None,
209 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000210# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000211}
212skipped_types = {
213 'int *': "usually a return type",
214 'xmlSAXHandlerPtr': "not the proper interface for SAX",
215 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000216 'xmlRMutexPtr': "thread specific, skipped",
217 'xmlMutexPtr': "thread specific, skipped",
218 'xmlGlobalStatePtr': "thread specific, skipped",
219 'xmlListPtr': "internal representation not suitable for python",
220 'xmlBufferPtr': "internal representation not suitable for python",
221 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000222}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000223
224#######################################################################
225#
226# Table of remapping to/from the python type or class to the C
227# counterpart.
228#
229#######################################################################
230
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000231py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000232 'void': (None, None, None, None),
233 'int': ('i', None, "int", "int"),
Daniel Veillardddefe9c2006-08-04 12:44:24 +0000234 'long': ('l', None, "long", "long"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000235 'double': ('d', None, "double", "double"),
236 'unsigned int': ('i', None, "int", "int"),
237 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000238 'unsigned char *': ('z', None, "charPtr", "char *"),
239 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000240 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000241 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000242 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000243 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
244 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
245 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
246 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
247 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
248 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
249 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
250 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
251 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
252 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
253 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
254 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
255 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
256 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
257 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
258 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000259 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
260 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
261 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
262 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
263 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
264 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
265 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
266 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
267 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
268 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
269 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
270 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000271 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
272 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
273 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
274 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
275 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
276 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
277 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
278 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
279 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
280 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
281 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
282 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000283 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
284 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000285 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000286 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
287 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
288 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
289 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000290 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000291 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
292 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000293 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000294 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000295 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
296 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000297 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000298 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000299 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000300 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
301 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
302 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000303 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
304 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
305 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000306}
307
308py_return_types = {
309 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000310}
311
312unknown_types = {}
313
William M. Brack106cad62004-12-23 15:56:12 +0000314foreign_encoding_args = (
William M. Brackff349112004-12-24 08:39:13 +0000315 'htmlCreateMemoryParserCtxt',
316 'htmlCtxtReadMemory',
317 'htmlParseChunk',
318 'htmlReadMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000319 'xmlCreateMemoryParserCtxt',
William M. Brackff349112004-12-24 08:39:13 +0000320 'xmlCtxtReadMemory',
321 'xmlCtxtResetPush',
322 'xmlParseChunk',
323 'xmlParseMemory',
324 'xmlReadMemory',
325 'xmlRecoverMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000326)
327
Daniel Veillard1971ee22002-01-31 20:29:19 +0000328#######################################################################
329#
330# This part writes the C <-> Python stubs libxml2-py.[ch] and
331# the table libxml2-export.c to add when registrering the Python module
332#
333#######################################################################
334
Daniel Veillard263ec862004-10-04 10:26:54 +0000335# Class methods which are written by hand in libxml.c but the Python-level
336# code is still automatically generated (so they are not in skip_function()).
337skip_impl = (
338 'xmlSaveFileTo',
339 'xmlSaveFormatFileTo',
340)
341
Daniel Veillard1971ee22002-01-31 20:29:19 +0000342def skip_function(name):
343 if name[0:12] == "xmlXPathWrap":
344 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000345 if name == "xmlFreeParserCtxt":
346 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000347 if name == "xmlCleanupParser":
348 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000349 if name == "xmlFreeTextReader":
350 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000351# if name[0:11] == "xmlXPathNew":
352# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000353 # the next function is defined in libxml.c
354 if name == "xmlRelaxNGFreeValidCtxt":
355 return 1
Daniel Veillard25c90c52005-03-02 10:47:41 +0000356 if name == "xmlFreeValidCtxt":
357 return 1
Daniel Veillardbb8502c2005-03-30 07:40:35 +0000358 if name == "xmlSchemaFreeValidCtxt":
359 return 1
360
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000361#
362# Those are skipped because the Const version is used of the bindings
363# instead.
364#
365 if name == "xmlTextReaderBaseUri":
366 return 1
367 if name == "xmlTextReaderLocalName":
368 return 1
369 if name == "xmlTextReaderName":
370 return 1
371 if name == "xmlTextReaderNamespaceUri":
372 return 1
373 if name == "xmlTextReaderPrefix":
374 return 1
375 if name == "xmlTextReaderXmlLang":
376 return 1
377 if name == "xmlTextReaderValue":
378 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000379 if name == "xmlOutputBufferClose": # handled by by the superclass
380 return 1
381 if name == "xmlOutputBufferFlush": # handled by by the superclass
382 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000383 if name == "xmlErrMemory":
384 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000385
386 if name == "xmlValidBuildContentModel":
387 return 1
388 if name == "xmlValidateElementDecl":
389 return 1
390 if name == "xmlValidateAttributeDecl":
391 return 1
392
Daniel Veillard1971ee22002-01-31 20:29:19 +0000393 return 0
394
Daniel Veillard96fe0952002-01-30 20:52:23 +0000395def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000396 global py_types
397 global unknown_types
398 global functions
399 global skipped_modules
400
401 try:
Daniel Veillard95175012005-07-03 16:09:51 +0000402 (desc, ret, args, file, cond) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000403 except:
404 print "failed to get function %s infos"
405 return
406
407 if skipped_modules.has_key(file):
408 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000409 if skip_function(name) == 1:
410 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000411 if name in skip_impl:
Daniel Veillard39801e52008-06-03 16:08:54 +0000412 # Don't delete the function entry in the caller.
413 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000414
Daniel Veillard39801e52008-06-03 16:08:54 +0000415 c_call = ""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000416 format=""
417 format_args=""
418 c_args=""
419 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000420 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000421 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000422 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000423 # This should be correct
424 if arg[1][0:6] == "const ":
425 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000426 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000427 if py_types.has_key(arg[1]):
428 (f, t, n, c) = py_types[arg[1]]
Daniel Veillard39801e52008-06-03 16:08:54 +0000429 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
430 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000431 if f != None:
432 format = format + f
433 if t != None:
434 format_args = format_args + ", &pyobj_%s" % (arg[0])
435 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
436 c_convert = c_convert + \
437 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
Daniel Veillard39801e52008-06-03 16:08:54 +0000438 arg[1], t, arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000439 else:
440 format_args = format_args + ", &%s" % (arg[0])
Daniel Veillard39801e52008-06-03 16:08:54 +0000441 if f == 't#':
442 format_args = format_args + ", &py_buffsize%d" % num_bufs
443 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
444 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000445 if c_call != "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000446 c_call = c_call + ", "
Daniel Veillard01a6d412002-02-11 18:42:20 +0000447 c_call = c_call + "%s" % (arg[0])
448 else:
449 if skipped_types.has_key(arg[1]):
450 return 0
451 if unknown_types.has_key(arg[1]):
452 lst = unknown_types[arg[1]]
453 lst.append(name)
454 else:
455 unknown_types[arg[1]] = [name]
456 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000457 if format != "":
458 format = format + ":%s" % (name)
459
460 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000461 if file == "python_accessor":
Daniel Veillard39801e52008-06-03 16:08:54 +0000462 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
463 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
464 args[0][0], args[1][0], args[0][0], args[1][0])
465 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
466 args[1][0], args[1][1], args[1][0])
467 else:
468 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
469 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000470 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000471 c_call = "\n %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000472 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000473 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000474 (f, t, n, c) = py_types[ret[0]]
475 c_return = " %s c_retval;\n" % (ret[0])
476 if file == "python_accessor" and ret[2] != None:
477 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
478 else:
Daniel Veillard39801e52008-06-03 16:08:54 +0000479 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000480 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
481 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000482 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000483 (f, t, n, c) = py_return_types[ret[0]]
484 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard39801e52008-06-03 16:08:54 +0000485 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000486 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
487 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000489 if skipped_types.has_key(ret[0]):
490 return 0
491 if unknown_types.has_key(ret[0]):
492 lst = unknown_types[ret[0]]
493 lst.append(name)
494 else:
495 unknown_types[ret[0]] = [name]
496 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000497
Daniel Veillard95175012005-07-03 16:09:51 +0000498 if cond != None and cond != "":
499 include.write("#if %s\n" % cond)
500 export.write("#if %s\n" % cond)
501 output.write("#if %s\n" % cond)
Daniel Veillard42766c02002-08-22 20:52:17 +0000502
Daniel Veillard96fe0952002-01-30 20:52:23 +0000503 include.write("PyObject * ")
Daniel Veillard39801e52008-06-03 16:08:54 +0000504 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000505
Daniel Veillardd2379012002-03-15 22:24:56 +0000506 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000507 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000508
509 if file == "python":
510 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000511 if cond != None and cond != "":
512 include.write("#endif\n")
513 export.write("#endif\n")
514 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000515 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000516 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000517 # Those have been manually generated
Daniel Veillard39801e52008-06-03 16:08:54 +0000518 if cond != None and cond != "":
519 include.write("#endif\n")
520 export.write("#endif\n")
521 output.write("#endif\n")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000522 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000523
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000524 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000525 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
526 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000527 if format == "":
Daniel Veillard39801e52008-06-03 16:08:54 +0000528 output.write(" ATTRIBUTE_UNUSED")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000529 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000530 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000531 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000532 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000533 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000534 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000535 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000536 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000537 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000538 (format, format_args))
539 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000540 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000541 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000542
543 output.write(c_call)
544 output.write(ret_convert)
545 output.write("}\n\n")
Daniel Veillard95175012005-07-03 16:09:51 +0000546 if cond != None and cond != "":
547 include.write("#endif /* %s */\n" % cond)
548 export.write("#endif /* %s */\n" % cond)
549 output.write("#endif /* %s */\n" % cond)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000550 return 1
551
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000552def buildStubs():
553 global py_types
554 global py_return_types
555 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000556
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000557 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000558 f = open(os.path.join(srcPref,"libxml2-api.xml"))
559 data = f.read()
560 (parser, target) = getparser()
561 parser.feed(data)
562 parser.close()
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000563 except IOError, msg:
Daniel Veillard39801e52008-06-03 16:08:54 +0000564 try:
565 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
566 data = f.read()
567 (parser, target) = getparser()
568 parser.feed(data)
569 parser.close()
570 except IOError, msg:
571 print file, ":", msg
572 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000573
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000574 n = len(functions.keys())
575 print "Found %d functions in libxml2-api.xml" % (n)
576
577 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
578 try:
Daniel Veillard39801e52008-06-03 16:08:54 +0000579 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
580 data = f.read()
581 (parser, target) = getparser()
582 parser.feed(data)
583 parser.close()
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000584 except IOError, msg:
Daniel Veillard39801e52008-06-03 16:08:54 +0000585 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000586
587
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000588 print "Found %d functions in libxml2-python-api.xml" % (
Daniel Veillard39801e52008-06-03 16:08:54 +0000589 len(functions.keys()) - n)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000590 nb_wrap = 0
591 failed = 0
592 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000593
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000594 include = open("libxml2-py.h", "w")
595 include.write("/* Generated */\n\n")
596 export = open("libxml2-export.c", "w")
597 export.write("/* Generated */\n\n")
598 wrapper = open("libxml2-py.c", "w")
599 wrapper.write("/* Generated */\n\n")
600 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000601 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000602 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000603 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000604 wrapper.write("#include \"libxml_wrap.h\"\n")
605 wrapper.write("#include \"libxml2-py.h\"\n\n")
606 for function in functions.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000607 ret = print_function_wrapper(function, wrapper, export, include)
608 if ret < 0:
609 failed = failed + 1
610 del functions[function]
611 if ret == 0:
612 skipped = skipped + 1
613 del functions[function]
614 if ret == 1:
615 nb_wrap = nb_wrap + 1
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000616 include.close()
617 export.close()
618 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000619
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000620 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
Daniel Veillard39801e52008-06-03 16:08:54 +0000621 failed, skipped)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000622 print "Missing type converters: "
623 for type in unknown_types.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000624 print "%s:%d " % (type, len(unknown_types[type])),
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000625 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000626
Daniel Veillard1971ee22002-01-31 20:29:19 +0000627#######################################################################
628#
629# This part writes part of the Python front-end classes based on
630# mapping rules between types and classes and also based on function
631# renaming to get consistent function names at the Python level
632#
633#######################################################################
634
635#
636# The type automatically remapped to generated classes
637#
638classes_type = {
639 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
640 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
641 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
642 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
643 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
644 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
645 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
646 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
647 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
648 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
649 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
650 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
651 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
652 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
653 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
654 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
655 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
656 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
657 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000658 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
659 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
660 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000661 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
662 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000663 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
664 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000665 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000666 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000667 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000668 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000669 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
670 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000671 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000672 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000673 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000674 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
675 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
676 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000677 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
678 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
679 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000680}
681
682converter_type = {
683 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
684}
685
686primary_classes = ["xmlNode", "xmlDoc"]
687
688classes_ancestor = {
689 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000690 "xmlDtd" : "xmlNode",
691 "xmlDoc" : "xmlNode",
692 "xmlAttr" : "xmlNode",
693 "xmlNs" : "xmlNode",
694 "xmlEntity" : "xmlNode",
695 "xmlElement" : "xmlNode",
696 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000697 "outputBuffer": "ioWriteWrapper",
698 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000699 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000700 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard0e460da2005-03-30 22:47:10 +0000701 "ValidCtxt": "ValidCtxtCore",
702 "SchemaValidCtxt": "SchemaValidCtxtCore",
703 "relaxNgValidCtxt": "relaxNgValidCtxtCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000704}
705classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000706 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000707 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000708 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000709# "outputBuffer": "xmlOutputBufferClose",
710 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000711 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000712 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000713 "relaxNgSchema": "xmlRelaxNGFree",
714 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
715 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard39801e52008-06-03 16:08:54 +0000716 "Schema": "xmlSchemaFree",
717 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
718 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000719 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000720}
721
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000722functions_noexcept = {
723 "xmlHasProp": 1,
724 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000725 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000726 "xmlNodeGetNs": 1,
727 "xmlNodeGetNsDefs": 1,
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +0000728 "xmlNextElementSibling": 1,
729 "xmlPreviousElementSibling": 1,
730 "xmlFirstElementChild": 1,
731 "xmlLastElementChild": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000732}
733
Daniel Veillarddc85f282002-12-31 11:18:37 +0000734reference_keepers = {
735 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000736 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard39801e52008-06-03 16:08:54 +0000737 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000738}
739
Daniel Veillard36ed5292002-01-30 23:49:06 +0000740function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000741
742function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000743
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000744def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000745 listname = classe + "List"
746 ll = len(listname)
747 l = len(classe)
748 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000749 func = name[l:]
750 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000751 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
752 func = name[12:]
753 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000754 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
755 func = name[12:]
756 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000757 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
758 func = name[10:]
759 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000760 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
761 func = name[9:]
762 func = string.lower(func[0:1]) + func[1:]
763 elif name[0:9] == "xmlURISet" and file == "python_accessor":
764 func = name[6:]
765 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000766 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
767 func = name[11:]
768 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000769 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
770 func = name[17:]
771 func = string.lower(func[0:1]) + func[1:]
772 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
773 func = name[11:]
774 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000775 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
776 func = name[8:]
777 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000778 elif name[0:15] == "xmlOutputBuffer" and file != "python":
779 func = name[15:]
780 func = string.lower(func[0:1]) + func[1:]
781 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
782 func = name[20:]
783 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000784 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000785 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000786 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000787 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000788 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
789 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000790 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
791 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000792 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
793 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000794 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
795 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000796 elif name[0:11] == "xmlACatalog":
797 func = name[11:]
798 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000799 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000800 func = name[l:]
801 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000802 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000803 func = name[7:]
804 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000805 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000806 func = name[6:]
807 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000808 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000809 func = name[3:]
810 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000811 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000812 func = name
813 if func[0:5] == "xPath":
814 func = "xpath" + func[5:]
815 elif func[0:4] == "xPtr":
816 func = "xpointer" + func[4:]
817 elif func[0:8] == "xInclude":
818 func = "xinclude" + func[8:]
819 elif func[0:2] == "iD":
820 func = "ID" + func[2:]
821 elif func[0:3] == "uRI":
822 func = "URI" + func[3:]
823 elif func[0:4] == "uTF8":
824 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000825 elif func[0:3] == 'sAX':
826 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000827 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000828
Daniel Veillard36ed5292002-01-30 23:49:06 +0000829
Daniel Veillard1971ee22002-01-31 20:29:19 +0000830def functionCompare(info1, info2):
831 (index1, func1, name1, ret1, args1, file1) = info1
832 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000833 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000834 if func1 < func2:
835 return -1
836 if func1 > func2:
837 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000838 if file1 == "python_accessor":
839 return -1
840 if file2 == "python_accessor":
841 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000842 if file1 < file2:
843 return -1
844 if file1 > file2:
845 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000846 return 0
847
848def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000849 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000850 return
851 val = functions[name][0]
Daniel Veillard39801e52008-06-03 16:08:54 +0000852 val = string.replace(val, "NULL", "None")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000853 output.write(indent)
854 output.write('"""')
855 while len(val) > 60:
Daniel Veillarded939f82008-04-08 08:20:08 +0000856 if val[0] == " ":
Daniel Veillard39801e52008-06-03 16:08:54 +0000857 val = val[1:]
858 continue
Daniel Veillard1971ee22002-01-31 20:29:19 +0000859 str = val[0:60]
Daniel Veillard39801e52008-06-03 16:08:54 +0000860 i = string.rfind(str, " ")
Daniel Veillard01a6d412002-02-11 18:42:20 +0000861 if i < 0:
862 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000863 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000864 val = val[i:]
865 output.write(str)
Daniel Veillard39801e52008-06-03 16:08:54 +0000866 output.write('\n ')
Daniel Veillard01a6d412002-02-11 18:42:20 +0000867 output.write(indent)
Daniel Veillard39801e52008-06-03 16:08:54 +0000868 output.write(val)
Daniel Veillardd076a202002-11-20 13:28:31 +0000869 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000870
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000871def buildWrappers():
872 global ctypes
873 global py_types
874 global py_return_types
875 global unknown_types
876 global functions
877 global function_classes
878 global classes_type
879 global classes_list
880 global converter_type
881 global primary_classes
882 global converter_type
883 global classes_ancestor
884 global converter_type
885 global primary_classes
886 global classes_ancestor
887 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000888 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000889
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000890 for type in classes_type.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000891 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000892
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000893 #
894 # Build the list of C types to look for ordered to start
895 # with primary classes
896 #
897 ctypes = []
898 classes_list = []
899 ctypes_processed = {}
900 classes_processed = {}
901 for classe in primary_classes:
Daniel Veillard39801e52008-06-03 16:08:54 +0000902 classes_list.append(classe)
903 classes_processed[classe] = ()
904 for type in classes_type.keys():
905 tinfo = classes_type[type]
906 if tinfo[2] == classe:
907 ctypes.append(type)
908 ctypes_processed[type] = ()
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000909 for type in classes_type.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000910 if ctypes_processed.has_key(type):
911 continue
912 tinfo = classes_type[type]
913 if not classes_processed.has_key(tinfo[2]):
914 classes_list.append(tinfo[2])
915 classes_processed[tinfo[2]] = ()
916
917 ctypes.append(type)
918 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000919
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000920 for name in functions.keys():
Daniel Veillard39801e52008-06-03 16:08:54 +0000921 found = 0
922 (desc, ret, args, file, cond) = functions[name]
923 for type in ctypes:
924 classe = classes_type[type][2]
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000925
Daniel Veillard39801e52008-06-03 16:08:54 +0000926 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
927 found = 1
928 func = nameFixup(name, classe, type, file)
929 info = (0, func, name, ret, args, file)
930 function_classes[classe].append(info)
931 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
932 and file != "python_accessor":
933 found = 1
934 func = nameFixup(name, classe, type, file)
935 info = (1, func, name, ret, args, file)
936 function_classes[classe].append(info)
937 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
938 found = 1
939 func = nameFixup(name, classe, type, file)
940 info = (0, func, name, ret, args, file)
941 function_classes[classe].append(info)
942 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
943 and file != "python_accessor":
944 found = 1
945 func = nameFixup(name, classe, type, file)
946 info = (1, func, name, ret, args, file)
947 function_classes[classe].append(info)
948 if found == 1:
949 continue
950 if name[0:8] == "xmlXPath":
951 continue
952 if name[0:6] == "xmlStr":
953 continue
954 if name[0:10] == "xmlCharStr":
955 continue
956 func = nameFixup(name, "None", file, file)
957 info = (0, func, name, ret, args, file)
958 function_classes['None'].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000959
960 classes = open("libxml2class.py", "w")
961 txt = open("libxml2class.txt", "w")
962 txt.write(" Generated Classes for libxml2-python\n\n")
963
964 txt.write("#\n# Global functions of the module\n#\n\n")
965 if function_classes.has_key("None"):
Daniel Veillard39801e52008-06-03 16:08:54 +0000966 flist = function_classes["None"]
967 flist.sort(functionCompare)
968 oldfile = ""
969 for info in flist:
970 (index, func, name, ret, args, file) = info
971 if file != oldfile:
972 classes.write("#\n# Functions from module %s\n#\n\n" % file)
973 txt.write("\n# functions from module %s\n" % file)
974 oldfile = file
975 classes.write("def %s(" % func)
976 txt.write("%s()\n" % func)
977 n = 0
978 for arg in args:
979 if n != 0:
980 classes.write(", ")
981 classes.write("%s" % arg[0])
982 n = n + 1
983 classes.write("):\n")
984 writeDoc(name, args, ' ', classes)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000985
Daniel Veillard39801e52008-06-03 16:08:54 +0000986 for arg in args:
987 if classes_type.has_key(arg[1]):
988 classes.write(" if %s is None: %s__o = None\n" %
989 (arg[0], arg[0]))
990 classes.write(" else: %s__o = %s%s\n" %
991 (arg[0], arg[0], classes_type[arg[1]][0]))
992 if ret[0] != "void":
993 classes.write(" ret = ")
994 else:
995 classes.write(" ")
996 classes.write("libxml2mod.%s(" % name)
997 n = 0
998 for arg in args:
999 if n != 0:
1000 classes.write(", ")
1001 classes.write("%s" % arg[0])
1002 if classes_type.has_key(arg[1]):
1003 classes.write("__o")
1004 n = n + 1
1005 classes.write(")\n")
1006 if ret[0] != "void":
1007 if classes_type.has_key(ret[0]):
1008 #
1009 # Raise an exception
1010 #
1011 if functions_noexcept.has_key(name):
1012 classes.write(" if ret is None:return None\n")
1013 elif string.find(name, "URI") >= 0:
1014 classes.write(
1015 " if ret is None:raise uriError('%s() failed')\n"
1016 % (name))
1017 elif string.find(name, "XPath") >= 0:
1018 classes.write(
1019 " if ret is None:raise xpathError('%s() failed')\n"
1020 % (name))
1021 elif string.find(name, "Parse") >= 0:
1022 classes.write(
1023 " if ret is None:raise parserError('%s() failed')\n"
1024 % (name))
1025 else:
1026 classes.write(
1027 " if ret is None:raise treeError('%s() failed')\n"
1028 % (name))
1029 classes.write(" return ")
1030 classes.write(classes_type[ret[0]][1] % ("ret"))
1031 classes.write("\n")
1032 else:
1033 classes.write(" return ret\n")
1034 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001035
1036 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1037 for classname in classes_list:
Daniel Veillard39801e52008-06-03 16:08:54 +00001038 if classname == "None":
1039 pass
1040 else:
1041 if classes_ancestor.has_key(classname):
1042 txt.write("\n\nClass %s(%s)\n" % (classname,
1043 classes_ancestor[classname]))
1044 classes.write("class %s(%s):\n" % (classname,
1045 classes_ancestor[classname]))
1046 classes.write(" def __init__(self, _obj=None):\n")
1047 if classes_ancestor[classname] == "xmlCore" or \
1048 classes_ancestor[classname] == "xmlNode":
1049 classes.write(" if type(_obj).__name__ != ")
1050 classes.write("'PyCObject':\n")
1051 classes.write(" raise TypeError, ")
1052 classes.write("'%s needs a PyCObject argument'\n" % \
1053 classname)
1054 if reference_keepers.has_key(classname):
1055 rlist = reference_keepers[classname]
1056 for ref in rlist:
1057 classes.write(" self.%s = None\n" % ref[1])
1058 classes.write(" self._o = _obj\n")
1059 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1060 classes_ancestor[classname]))
1061 if classes_ancestor[classname] == "xmlCore" or \
1062 classes_ancestor[classname] == "xmlNode":
1063 classes.write(" def __repr__(self):\n")
1064 format = "<%s (%%s) object at 0x%%x>" % (classname)
1065 classes.write(" return \"%s\" %% (self.name, long(pos_id (self)))\n\n" % (
1066 format))
1067 else:
1068 txt.write("Class %s()\n" % (classname))
1069 classes.write("class %s:\n" % (classname))
1070 classes.write(" def __init__(self, _obj=None):\n")
1071 if reference_keepers.has_key(classname):
1072 list = reference_keepers[classname]
1073 for ref in list:
1074 classes.write(" self.%s = None\n" % ref[1])
1075 classes.write(" if _obj != None:self._o = _obj;return\n")
1076 classes.write(" self._o = None\n\n")
1077 destruct=None
1078 if classes_destructors.has_key(classname):
1079 classes.write(" def __del__(self):\n")
1080 classes.write(" if self._o != None:\n")
1081 classes.write(" libxml2mod.%s(self._o)\n" %
1082 classes_destructors[classname])
1083 classes.write(" self._o = None\n\n")
1084 destruct=classes_destructors[classname]
1085 flist = function_classes[classname]
1086 flist.sort(functionCompare)
1087 oldfile = ""
1088 for info in flist:
1089 (index, func, name, ret, args, file) = info
1090 #
1091 # Do not provide as method the destructors for the class
1092 # to avoid double free
1093 #
1094 if name == destruct:
1095 continue
1096 if file != oldfile:
1097 if file == "python_accessor":
1098 classes.write(" # accessors for %s\n" % (classname))
1099 txt.write(" # accessors\n")
1100 else:
1101 classes.write(" #\n")
1102 classes.write(" # %s functions from module %s\n" % (
1103 classname, file))
1104 txt.write("\n # functions from module %s\n" % file)
1105 classes.write(" #\n\n")
1106 oldfile = file
1107 classes.write(" def %s(self" % func)
1108 txt.write(" %s()\n" % func)
1109 n = 0
1110 for arg in args:
1111 if n != index:
1112 classes.write(", %s" % arg[0])
1113 n = n + 1
1114 classes.write("):\n")
1115 writeDoc(name, args, ' ', classes)
1116 n = 0
1117 for arg in args:
1118 if classes_type.has_key(arg[1]):
1119 if n != index:
1120 classes.write(" if %s is None: %s__o = None\n" %
1121 (arg[0], arg[0]))
1122 classes.write(" else: %s__o = %s%s\n" %
1123 (arg[0], arg[0], classes_type[arg[1]][0]))
1124 n = n + 1
1125 if ret[0] != "void":
1126 classes.write(" ret = ")
1127 else:
1128 classes.write(" ")
1129 classes.write("libxml2mod.%s(" % name)
1130 n = 0
1131 for arg in args:
1132 if n != 0:
1133 classes.write(", ")
1134 if n != index:
1135 classes.write("%s" % arg[0])
1136 if classes_type.has_key(arg[1]):
1137 classes.write("__o")
1138 else:
1139 classes.write("self")
1140 if classes_type.has_key(arg[1]):
1141 classes.write(classes_type[arg[1]][0])
1142 n = n + 1
1143 classes.write(")\n")
1144 if ret[0] != "void":
1145 if classes_type.has_key(ret[0]):
1146 #
1147 # Raise an exception
1148 #
1149 if functions_noexcept.has_key(name):
1150 classes.write(
1151 " if ret is None:return None\n")
1152 elif string.find(name, "URI") >= 0:
1153 classes.write(
1154 " if ret is None:raise uriError('%s() failed')\n"
1155 % (name))
1156 elif string.find(name, "XPath") >= 0:
1157 classes.write(
1158 " if ret is None:raise xpathError('%s() failed')\n"
1159 % (name))
1160 elif string.find(name, "Parse") >= 0:
1161 classes.write(
1162 " if ret is None:raise parserError('%s() failed')\n"
1163 % (name))
1164 else:
1165 classes.write(
1166 " if ret is None:raise treeError('%s() failed')\n"
1167 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001168
1169 #
Daniel Veillard39801e52008-06-03 16:08:54 +00001170 # generate the returned class wrapper for the object
1171 #
1172 classes.write(" __tmp = ")
1173 classes.write(classes_type[ret[0]][1] % ("ret"))
1174 classes.write("\n")
1175
1176 #
1177 # Sometime one need to keep references of the source
1178 # class in the returned class object.
1179 # See reference_keepers for the list
1180 #
1181 tclass = classes_type[ret[0]][2]
1182 if reference_keepers.has_key(tclass):
1183 list = reference_keepers[tclass]
1184 for pref in list:
1185 if pref[0] == classname:
1186 classes.write(" __tmp.%s = self\n" %
1187 pref[1])
1188 #
1189 # return the class
1190 #
1191 classes.write(" return __tmp\n")
1192 elif converter_type.has_key(ret[0]):
1193 #
1194 # Raise an exception
1195 #
1196 if functions_noexcept.has_key(name):
1197 classes.write(
1198 " if ret is None:return None")
1199 elif string.find(name, "URI") >= 0:
1200 classes.write(
1201 " if ret is None:raise uriError('%s() failed')\n"
1202 % (name))
1203 elif string.find(name, "XPath") >= 0:
1204 classes.write(
1205 " if ret is None:raise xpathError('%s() failed')\n"
1206 % (name))
1207 elif string.find(name, "Parse") >= 0:
1208 classes.write(
1209 " if ret is None:raise parserError('%s() failed')\n"
1210 % (name))
1211 else:
1212 classes.write(
1213 " if ret is None:raise treeError('%s() failed')\n"
1214 % (name))
1215 classes.write(" return ")
1216 classes.write(converter_type[ret[0]] % ("ret"))
1217 classes.write("\n")
1218 else:
1219 classes.write(" return ret\n")
1220 classes.write("\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001221
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001222 #
1223 # Generate enum constants
1224 #
1225 for type,enum in enums.items():
1226 classes.write("# %s\n" % type)
1227 items = enum.items()
1228 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1229 for name,value in items:
1230 classes.write("%s = %s\n" % (name,value))
Daniel Veillard39801e52008-06-03 16:08:54 +00001231 classes.write("\n")
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001232
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001233 txt.close()
1234 classes.close()
1235
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001236buildStubs()
1237buildWrappers()