blob: f2c793921a0b22de7ca0823dcb74ed6f64182d81 [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
117 self.function_args = []
118 self.function_descr = None
119 self.function_return = None
120 self.function_file = None
121 if attrs.has_key('name'):
122 self.function = attrs['name']
123 if attrs.has_key('file'):
124 self.function_file = attrs['file']
125 elif tag == 'info':
126 self._data = []
127 elif tag == 'arg':
128 if self.in_function == 1:
129 self.function_arg_name = None
130 self.function_arg_type = None
131 self.function_arg_info = None
132 if attrs.has_key('name'):
133 self.function_arg_name = attrs['name']
134 if attrs.has_key('type'):
135 self.function_arg_type = attrs['type']
136 if attrs.has_key('info'):
137 self.function_arg_info = attrs['info']
138 elif tag == 'return':
139 if self.in_function == 1:
140 self.function_return_type = None
141 self.function_return_info = None
142 self.function_return_field = None
143 if attrs.has_key('type'):
144 self.function_return_type = attrs['type']
145 if attrs.has_key('info'):
146 self.function_return_info = attrs['info']
147 if attrs.has_key('field'):
148 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000149 elif tag == 'enum':
150 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000151
152 def end(self, tag):
153 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000154 print "end %s" % tag
155 if tag == 'function':
156 if self.function != None:
157 function(self.function, self.function_descr,
158 self.function_return, self.function_args,
159 self.function_file)
160 self.in_function = 0
161 elif tag == 'arg':
162 if self.in_function == 1:
163 self.function_args.append([self.function_arg_name,
164 self.function_arg_type,
165 self.function_arg_info])
166 elif tag == 'return':
167 if self.in_function == 1:
168 self.function_return = [self.function_return_type,
169 self.function_return_info,
170 self.function_return_field]
171 elif tag == 'info':
172 str = ''
173 for c in self._data:
174 str = str + c
175 if self.in_function == 1:
176 self.function_descr = str
177
178
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000179def function(name, desc, ret, args, file):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000180 functions[name] = (desc, ret, args, file)
181
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000182def enum(type, name, value):
183 if not enums.has_key(type):
184 enums[type] = {}
185 enums[type][name] = value
186
Daniel Veillard1971ee22002-01-31 20:29:19 +0000187#######################################################################
188#
189# Some filtering rukes to drop functions/types which should not
190# be exposed as-is on the Python interface
191#
192#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000193
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000194skipped_modules = {
195 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000196 'DOCBparser': None,
197 'SAX': None,
198 'hash': None,
199 'list': None,
200 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000201# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000202}
203skipped_types = {
204 'int *': "usually a return type",
205 'xmlSAXHandlerPtr': "not the proper interface for SAX",
206 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000207 'xmlRMutexPtr': "thread specific, skipped",
208 'xmlMutexPtr': "thread specific, skipped",
209 'xmlGlobalStatePtr': "thread specific, skipped",
210 'xmlListPtr': "internal representation not suitable for python",
211 'xmlBufferPtr': "internal representation not suitable for python",
212 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000213}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000214
215#######################################################################
216#
217# Table of remapping to/from the python type or class to the C
218# counterpart.
219#
220#######################################################################
221
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000222py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000223 'void': (None, None, None, None),
224 'int': ('i', None, "int", "int"),
225 'long': ('i', None, "int", "int"),
226 'double': ('d', None, "double", "double"),
227 'unsigned int': ('i', None, "int", "int"),
228 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000229 'unsigned char *': ('z', None, "charPtr", "char *"),
230 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000231 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000232 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000233 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000234 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
237 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
238 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
239 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
240 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
241 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
242 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
243 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
244 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
245 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
246 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
247 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
248 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
249 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000250 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
251 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
252 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
253 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
254 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
255 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
256 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
257 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
258 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
259 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
260 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
261 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000262 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
263 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
264 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
265 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
266 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
267 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
268 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
269 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
270 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
271 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
272 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
273 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000274 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
275 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000276 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000277 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
278 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
279 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
280 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000281 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000282 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
283 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000284 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000285 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000286 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
287 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000288 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000289 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000290 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000291 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
292 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
293 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000294 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
295 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
296 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000297}
298
299py_return_types = {
300 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000301}
302
303unknown_types = {}
304
William M. Brack106cad62004-12-23 15:56:12 +0000305foreign_encoding_args = (
William M. Brackff349112004-12-24 08:39:13 +0000306 'htmlCreateMemoryParserCtxt',
307 'htmlCtxtReadMemory',
308 'htmlParseChunk',
309 'htmlReadMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000310 'xmlCreateMemoryParserCtxt',
William M. Brackff349112004-12-24 08:39:13 +0000311 'xmlCtxtReadMemory',
312 'xmlCtxtResetPush',
313 'xmlParseChunk',
314 'xmlParseMemory',
315 'xmlReadMemory',
316 'xmlRecoverMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000317)
318
Daniel Veillard1971ee22002-01-31 20:29:19 +0000319#######################################################################
320#
321# This part writes the C <-> Python stubs libxml2-py.[ch] and
322# the table libxml2-export.c to add when registrering the Python module
323#
324#######################################################################
325
Daniel Veillard263ec862004-10-04 10:26:54 +0000326# Class methods which are written by hand in libxml.c but the Python-level
327# code is still automatically generated (so they are not in skip_function()).
328skip_impl = (
329 'xmlSaveFileTo',
330 'xmlSaveFormatFileTo',
331)
332
Daniel Veillard1971ee22002-01-31 20:29:19 +0000333def skip_function(name):
334 if name[0:12] == "xmlXPathWrap":
335 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000336 if name == "xmlFreeParserCtxt":
337 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000338 if name == "xmlCleanupParser":
339 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000340 if name == "xmlFreeTextReader":
341 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000342# if name[0:11] == "xmlXPathNew":
343# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000344 # the next function is defined in libxml.c
345 if name == "xmlRelaxNGFreeValidCtxt":
346 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000347#
348# Those are skipped because the Const version is used of the bindings
349# instead.
350#
351 if name == "xmlTextReaderBaseUri":
352 return 1
353 if name == "xmlTextReaderLocalName":
354 return 1
355 if name == "xmlTextReaderName":
356 return 1
357 if name == "xmlTextReaderNamespaceUri":
358 return 1
359 if name == "xmlTextReaderPrefix":
360 return 1
361 if name == "xmlTextReaderXmlLang":
362 return 1
363 if name == "xmlTextReaderValue":
364 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000365 if name == "xmlOutputBufferClose": # handled by by the superclass
366 return 1
367 if name == "xmlOutputBufferFlush": # handled by by the superclass
368 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000369 if name == "xmlErrMemory":
370 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000371
372 if name == "xmlValidBuildContentModel":
373 return 1
374 if name == "xmlValidateElementDecl":
375 return 1
376 if name == "xmlValidateAttributeDecl":
377 return 1
378
Daniel Veillard1971ee22002-01-31 20:29:19 +0000379 return 0
380
Daniel Veillard96fe0952002-01-30 20:52:23 +0000381def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000382 global py_types
383 global unknown_types
384 global functions
385 global skipped_modules
386
387 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000388 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000389 except:
390 print "failed to get function %s infos"
391 return
392
393 if skipped_modules.has_key(file):
394 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000395 if skip_function(name) == 1:
396 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000397 if name in skip_impl:
398 # Don't delete the function entry in the caller.
399 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000400
401 c_call = "";
402 format=""
403 format_args=""
404 c_args=""
405 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000406 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000407 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000408 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000409 # This should be correct
410 if arg[1][0:6] == "const ":
411 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000412 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000413 if py_types.has_key(arg[1]):
414 (f, t, n, c) = py_types[arg[1]]
William M. Brackff349112004-12-24 08:39:13 +0000415 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
William M. Brack106cad62004-12-23 15:56:12 +0000416 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000417 if f != None:
418 format = format + f
419 if t != None:
420 format_args = format_args + ", &pyobj_%s" % (arg[0])
421 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
422 c_convert = c_convert + \
423 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
424 arg[1], t, arg[0]);
425 else:
426 format_args = format_args + ", &%s" % (arg[0])
William M. Brack106cad62004-12-23 15:56:12 +0000427 if f == 't#':
428 format_args = format_args + ", &py_buffsize%d" % num_bufs
429 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
430 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000431 if c_call != "":
432 c_call = c_call + ", ";
433 c_call = c_call + "%s" % (arg[0])
434 else:
435 if skipped_types.has_key(arg[1]):
436 return 0
437 if unknown_types.has_key(arg[1]):
438 lst = unknown_types[arg[1]]
439 lst.append(name)
440 else:
441 unknown_types[arg[1]] = [name]
442 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000443 if format != "":
444 format = format + ":%s" % (name)
445
446 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000447 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000448 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
449 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
450 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000451 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
452 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000453 else:
454 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
455 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000456 else:
457 c_call = "\n %s(%s);\n" % (name, c_call);
458 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000459 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000460 (f, t, n, c) = py_types[ret[0]]
461 c_return = " %s c_retval;\n" % (ret[0])
462 if file == "python_accessor" and ret[2] != None:
463 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
464 else:
465 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
466 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
467 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000468 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000469 (f, t, n, c) = py_return_types[ret[0]]
470 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000471 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000472 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
473 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000474 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000475 if skipped_types.has_key(ret[0]):
476 return 0
477 if unknown_types.has_key(ret[0]):
478 lst = unknown_types[ret[0]]
479 lst.append(name)
480 else:
481 unknown_types[ret[0]] = [name]
482 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000483
Daniel Veillard42766c02002-08-22 20:52:17 +0000484 if file == "debugXML":
485 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
486 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
487 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000488 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000489 include.write("#ifdef LIBXML_HTML_ENABLED\n");
490 export.write("#ifdef LIBXML_HTML_ENABLED\n");
491 output.write("#ifdef LIBXML_HTML_ENABLED\n");
492 elif file == "c14n":
493 include.write("#ifdef LIBXML_C14N_ENABLED\n");
494 export.write("#ifdef LIBXML_C14N_ENABLED\n");
495 output.write("#ifdef LIBXML_C14N_ENABLED\n");
496 elif file == "xpathInternals" or file == "xpath":
497 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
498 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
499 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
500 elif file == "xpointer":
501 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
502 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
503 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
504 elif file == "xinclude":
505 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
506 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
507 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000508 elif file == "xmlregexp":
509 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
510 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
511 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000512 elif file == "xmlschemas" or file == "xmlschemastypes" or \
513 file == "relaxng":
514 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
515 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
516 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000517
Daniel Veillard96fe0952002-01-30 20:52:23 +0000518 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000519 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000520
Daniel Veillardd2379012002-03-15 22:24:56 +0000521 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000522 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000523
524 if file == "python":
525 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000526 if name[0:4] == "html":
527 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
528 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
529 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000530 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000531 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000532 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000533 if name[0:4] == "html":
534 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
535 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
536 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000537 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000538
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000539 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000540 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
541 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000542 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000543 output.write(" ATTRIBUTE_UNUSED")
544 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000545 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000546 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000547 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000548 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000549 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000550 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000551 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000552 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000553 (format, format_args))
554 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000555 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000556 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000557
558 output.write(c_call)
559 output.write(ret_convert)
560 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000561 if file == "debugXML":
562 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
563 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
564 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000565 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000566 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
567 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
568 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
569 elif file == "c14n":
570 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
571 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
572 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
573 elif file == "xpathInternals" or file == "xpath":
574 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
575 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
576 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
577 elif file == "xpointer":
578 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
579 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
580 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
581 elif file == "xinclude":
582 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
583 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
584 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000585 elif file == "xmlregexp":
586 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
587 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
588 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000589 elif file == "xmlschemas" or file == "xmlschemastypes" or \
590 file == "relaxng":
591 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
592 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
593 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000594 return 1
595
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000596def buildStubs():
597 global py_types
598 global py_return_types
599 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000600
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000601 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000602 f = open(os.path.join(srcPref,"libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000603 data = f.read()
604 (parser, target) = getparser()
605 parser.feed(data)
606 parser.close()
607 except IOError, msg:
608 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000609 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000610 data = f.read()
611 (parser, target) = getparser()
612 parser.feed(data)
613 parser.close()
614 except IOError, msg:
615 print file, ":", msg
616 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000617
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000618 n = len(functions.keys())
619 print "Found %d functions in libxml2-api.xml" % (n)
620
621 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
622 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000623 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000624 data = f.read()
625 (parser, target) = getparser()
626 parser.feed(data)
627 parser.close()
628 except IOError, msg:
629 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000630
631
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000632 print "Found %d functions in libxml2-python-api.xml" % (
633 len(functions.keys()) - n)
634 nb_wrap = 0
635 failed = 0
636 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000637
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000638 include = open("libxml2-py.h", "w")
639 include.write("/* Generated */\n\n")
640 export = open("libxml2-export.c", "w")
641 export.write("/* Generated */\n\n")
642 wrapper = open("libxml2-py.c", "w")
643 wrapper.write("/* Generated */\n\n")
644 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000645 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000646 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000647 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000648 wrapper.write("#include \"libxml_wrap.h\"\n")
649 wrapper.write("#include \"libxml2-py.h\"\n\n")
650 for function in functions.keys():
651 ret = print_function_wrapper(function, wrapper, export, include)
652 if ret < 0:
653 failed = failed + 1
654 del functions[function]
655 if ret == 0:
656 skipped = skipped + 1
657 del functions[function]
658 if ret == 1:
659 nb_wrap = nb_wrap + 1
660 include.close()
661 export.close()
662 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000663
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000664 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
665 failed, skipped);
666 print "Missing type converters: "
667 for type in unknown_types.keys():
668 print "%s:%d " % (type, len(unknown_types[type])),
669 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000670
Daniel Veillard1971ee22002-01-31 20:29:19 +0000671#######################################################################
672#
673# This part writes part of the Python front-end classes based on
674# mapping rules between types and classes and also based on function
675# renaming to get consistent function names at the Python level
676#
677#######################################################################
678
679#
680# The type automatically remapped to generated classes
681#
682classes_type = {
683 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
684 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
685 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
686 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
687 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
688 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
689 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
690 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
691 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
692 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
693 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
694 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
695 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
696 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
697 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
698 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
699 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
700 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
701 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000702 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
703 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
704 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000705 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
706 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000707 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
708 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000709 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000710 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000711 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000712 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000713 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
714 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000715 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000716 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000717 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000718 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
719 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
720 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000721 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
722 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
723 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000724}
725
726converter_type = {
727 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
728}
729
730primary_classes = ["xmlNode", "xmlDoc"]
731
732classes_ancestor = {
733 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000734 "xmlDtd" : "xmlNode",
735 "xmlDoc" : "xmlNode",
736 "xmlAttr" : "xmlNode",
737 "xmlNs" : "xmlNode",
738 "xmlEntity" : "xmlNode",
739 "xmlElement" : "xmlNode",
740 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000741 "outputBuffer": "ioWriteWrapper",
742 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000743 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000744 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000745}
746classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000747 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000748 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000749 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000750# "outputBuffer": "xmlOutputBufferClose",
751 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000752 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000753 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000754 "relaxNgSchema": "xmlRelaxNGFree",
755 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
756 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000757 "Schema": "xmlSchemaFree",
758 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
759 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000760 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000761}
762
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000763functions_noexcept = {
764 "xmlHasProp": 1,
765 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000766 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000767 "xmlNodeGetNs": 1,
768 "xmlNodeGetNsDefs": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000769}
770
Daniel Veillarddc85f282002-12-31 11:18:37 +0000771reference_keepers = {
772 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000773 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000774 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000775}
776
Daniel Veillard36ed5292002-01-30 23:49:06 +0000777function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000778
779function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000780
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000781def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000782 listname = classe + "List"
783 ll = len(listname)
784 l = len(classe)
785 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000786 func = name[l:]
787 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000788 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
789 func = name[12:]
790 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000791 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
792 func = name[12:]
793 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000794 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
795 func = name[10:]
796 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000797 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
798 func = name[9:]
799 func = string.lower(func[0:1]) + func[1:]
800 elif name[0:9] == "xmlURISet" and file == "python_accessor":
801 func = name[6:]
802 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000803 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
804 func = name[11:]
805 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000806 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
807 func = name[17:]
808 func = string.lower(func[0:1]) + func[1:]
809 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
810 func = name[11:]
811 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000812 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
813 func = name[8:]
814 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000815 elif name[0:15] == "xmlOutputBuffer" and file != "python":
816 func = name[15:]
817 func = string.lower(func[0:1]) + func[1:]
818 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
819 func = name[20:]
820 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000821 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000822 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000823 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000824 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000825 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
826 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000827 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
828 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000829 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
830 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000831 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
832 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000833 elif name[0:11] == "xmlACatalog":
834 func = name[11:]
835 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000836 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000837 func = name[l:]
838 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000839 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000840 func = name[7:]
841 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000842 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000843 func = name[6:]
844 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000845 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000846 func = name[3:]
847 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000848 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000849 func = name
850 if func[0:5] == "xPath":
851 func = "xpath" + func[5:]
852 elif func[0:4] == "xPtr":
853 func = "xpointer" + func[4:]
854 elif func[0:8] == "xInclude":
855 func = "xinclude" + func[8:]
856 elif func[0:2] == "iD":
857 func = "ID" + func[2:]
858 elif func[0:3] == "uRI":
859 func = "URI" + func[3:]
860 elif func[0:4] == "uTF8":
861 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000862 elif func[0:3] == 'sAX':
863 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000864 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000865
Daniel Veillard36ed5292002-01-30 23:49:06 +0000866
Daniel Veillard1971ee22002-01-31 20:29:19 +0000867def functionCompare(info1, info2):
868 (index1, func1, name1, ret1, args1, file1) = info1
869 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000870 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000871 if func1 < func2:
872 return -1
873 if func1 > func2:
874 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000875 if file1 == "python_accessor":
876 return -1
877 if file2 == "python_accessor":
878 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000879 if file1 < file2:
880 return -1
881 if file1 > file2:
882 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000883 return 0
884
885def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000886 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000887 return
888 val = functions[name][0]
889 val = string.replace(val, "NULL", "None");
890 output.write(indent)
891 output.write('"""')
892 while len(val) > 60:
893 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000894 i = string.rfind(str, " ");
895 if i < 0:
896 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000897 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000898 val = val[i:]
899 output.write(str)
900 output.write('\n ');
901 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000902 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000903 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000904
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000905def buildWrappers():
906 global ctypes
907 global py_types
908 global py_return_types
909 global unknown_types
910 global functions
911 global function_classes
912 global classes_type
913 global classes_list
914 global converter_type
915 global primary_classes
916 global converter_type
917 global classes_ancestor
918 global converter_type
919 global primary_classes
920 global classes_ancestor
921 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000922 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000923
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000924 for type in classes_type.keys():
925 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000926
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000927 #
928 # Build the list of C types to look for ordered to start
929 # with primary classes
930 #
931 ctypes = []
932 classes_list = []
933 ctypes_processed = {}
934 classes_processed = {}
935 for classe in primary_classes:
936 classes_list.append(classe)
937 classes_processed[classe] = ()
938 for type in classes_type.keys():
939 tinfo = classes_type[type]
940 if tinfo[2] == classe:
941 ctypes.append(type)
942 ctypes_processed[type] = ()
943 for type in classes_type.keys():
944 if ctypes_processed.has_key(type):
945 continue
946 tinfo = classes_type[type]
947 if not classes_processed.has_key(tinfo[2]):
948 classes_list.append(tinfo[2])
949 classes_processed[tinfo[2]] = ()
950
951 ctypes.append(type)
952 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000953
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000954 for name in functions.keys():
955 found = 0;
956 (desc, ret, args, file) = functions[name]
957 for type in ctypes:
958 classe = classes_type[type][2]
959
960 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
961 found = 1
962 func = nameFixup(name, classe, type, file)
963 info = (0, func, name, ret, args, file)
964 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000965 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
966 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000967 found = 1
968 func = nameFixup(name, classe, type, file)
969 info = (1, func, name, ret, args, file)
970 function_classes[classe].append(info)
971 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
972 found = 1
973 func = nameFixup(name, classe, type, file)
974 info = (0, func, name, ret, args, file)
975 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000976 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
977 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000978 found = 1
979 func = nameFixup(name, classe, type, file)
980 info = (1, func, name, ret, args, file)
981 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000982 if found == 1:
983 continue
984 if name[0:8] == "xmlXPath":
985 continue
986 if name[0:6] == "xmlStr":
987 continue
988 if name[0:10] == "xmlCharStr":
989 continue
990 func = nameFixup(name, "None", file, file)
991 info = (0, func, name, ret, args, file)
992 function_classes['None'].append(info)
993
994 classes = open("libxml2class.py", "w")
995 txt = open("libxml2class.txt", "w")
996 txt.write(" Generated Classes for libxml2-python\n\n")
997
998 txt.write("#\n# Global functions of the module\n#\n\n")
999 if function_classes.has_key("None"):
1000 flist = function_classes["None"]
1001 flist.sort(functionCompare)
1002 oldfile = ""
1003 for info in flist:
1004 (index, func, name, ret, args, file) = info
1005 if file != oldfile:
1006 classes.write("#\n# Functions from module %s\n#\n\n" % file)
1007 txt.write("\n# functions from module %s\n" % file)
1008 oldfile = file
1009 classes.write("def %s(" % func)
1010 txt.write("%s()\n" % func);
1011 n = 0
1012 for arg in args:
1013 if n != 0:
1014 classes.write(", ")
1015 classes.write("%s" % arg[0])
1016 n = n + 1
1017 classes.write("):\n")
1018 writeDoc(name, args, ' ', classes);
1019
1020 for arg in args:
1021 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001022 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001023 (arg[0], arg[0]))
1024 classes.write(" else: %s__o = %s%s\n" %
1025 (arg[0], arg[0], classes_type[arg[1]][0]))
1026 if ret[0] != "void":
1027 classes.write(" ret = ");
1028 else:
1029 classes.write(" ");
1030 classes.write("libxml2mod.%s(" % name)
1031 n = 0
1032 for arg in args:
1033 if n != 0:
1034 classes.write(", ");
1035 classes.write("%s" % arg[0])
1036 if classes_type.has_key(arg[1]):
1037 classes.write("__o");
1038 n = n + 1
1039 classes.write(")\n");
1040 if ret[0] != "void":
1041 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001042 #
1043 # Raise an exception
1044 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001045 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001046 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001047 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001048 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001049 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001050 % (name))
1051 elif string.find(name, "XPath") >= 0:
1052 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001053 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001054 % (name))
1055 elif string.find(name, "Parse") >= 0:
1056 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001057 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001058 % (name))
1059 else:
1060 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001061 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001062 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001063 classes.write(" return ");
1064 classes.write(classes_type[ret[0]][1] % ("ret"));
1065 classes.write("\n");
1066 else:
1067 classes.write(" return ret\n");
1068 classes.write("\n");
1069
1070 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1071 for classname in classes_list:
1072 if classname == "None":
1073 pass
1074 else:
1075 if classes_ancestor.has_key(classname):
1076 txt.write("\n\nClass %s(%s)\n" % (classname,
1077 classes_ancestor[classname]))
1078 classes.write("class %s(%s):\n" % (classname,
1079 classes_ancestor[classname]))
1080 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001081 if classes_ancestor[classname] == "xmlCore" or \
1082 classes_ancestor[classname] == "xmlNode":
1083 classes.write(" if type(_obj).__name__ != ")
1084 classes.write("'PyCObject':\n")
1085 classes.write(" raise TypeError, ")
1086 classes.write("'%s needs a PyCObject argument'\n" % \
1087 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001088 if reference_keepers.has_key(classname):
1089 rlist = reference_keepers[classname]
1090 for ref in rlist:
1091 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001092 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001093 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1094 classes_ancestor[classname]))
1095 if classes_ancestor[classname] == "xmlCore" or \
1096 classes_ancestor[classname] == "xmlNode":
1097 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001098 format = "<%s (%%s) object at 0x%%x>" % (classname)
1099 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001100 format))
1101 else:
1102 txt.write("Class %s()\n" % (classname))
1103 classes.write("class %s:\n" % (classname))
1104 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001105 if reference_keepers.has_key(classname):
1106 list = reference_keepers[classname]
1107 for ref in list:
1108 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001109 classes.write(" if _obj != None:self._o = _obj;return\n")
1110 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001111 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001112 if classes_destructors.has_key(classname):
1113 classes.write(" def __del__(self):\n")
1114 classes.write(" if self._o != None:\n")
1115 classes.write(" libxml2mod.%s(self._o)\n" %
1116 classes_destructors[classname]);
1117 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001118 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001119 flist = function_classes[classname]
1120 flist.sort(functionCompare)
1121 oldfile = ""
1122 for info in flist:
1123 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001124 #
1125 # Do not provide as method the destructors for the class
1126 # to avoid double free
1127 #
1128 if name == destruct:
1129 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001130 if file != oldfile:
1131 if file == "python_accessor":
1132 classes.write(" # accessors for %s\n" % (classname))
1133 txt.write(" # accessors\n")
1134 else:
1135 classes.write(" #\n")
1136 classes.write(" # %s functions from module %s\n" % (
1137 classname, file))
1138 txt.write("\n # functions from module %s\n" % file)
1139 classes.write(" #\n\n")
1140 oldfile = file
1141 classes.write(" def %s(self" % func)
1142 txt.write(" %s()\n" % func);
1143 n = 0
1144 for arg in args:
1145 if n != index:
1146 classes.write(", %s" % arg[0])
1147 n = n + 1
1148 classes.write("):\n")
1149 writeDoc(name, args, ' ', classes);
1150 n = 0
1151 for arg in args:
1152 if classes_type.has_key(arg[1]):
1153 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001154 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001155 (arg[0], arg[0]))
1156 classes.write(" else: %s__o = %s%s\n" %
1157 (arg[0], arg[0], classes_type[arg[1]][0]))
1158 n = n + 1
1159 if ret[0] != "void":
1160 classes.write(" ret = ");
1161 else:
1162 classes.write(" ");
1163 classes.write("libxml2mod.%s(" % name)
1164 n = 0
1165 for arg in args:
1166 if n != 0:
1167 classes.write(", ");
1168 if n != index:
1169 classes.write("%s" % arg[0])
1170 if classes_type.has_key(arg[1]):
1171 classes.write("__o");
1172 else:
1173 classes.write("self");
1174 if classes_type.has_key(arg[1]):
1175 classes.write(classes_type[arg[1]][0])
1176 n = n + 1
1177 classes.write(")\n");
1178 if ret[0] != "void":
1179 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001180 #
1181 # Raise an exception
1182 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001183 if functions_noexcept.has_key(name):
1184 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001185 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001186 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001187 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001188 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001189 % (name))
1190 elif string.find(name, "XPath") >= 0:
1191 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001192 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001193 % (name))
1194 elif string.find(name, "Parse") >= 0:
1195 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001196 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001197 % (name))
1198 else:
1199 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001200 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001201 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001202
1203 #
1204 # generate the returned class wrapper for the object
1205 #
1206 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001207 classes.write(classes_type[ret[0]][1] % ("ret"));
1208 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001209
1210 #
1211 # Sometime one need to keep references of the source
1212 # class in the returned class object.
1213 # See reference_keepers for the list
1214 #
1215 tclass = classes_type[ret[0]][2]
1216 if reference_keepers.has_key(tclass):
1217 list = reference_keepers[tclass]
1218 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001219 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001220 classes.write(" __tmp.%s = self\n" %
1221 pref[1])
1222 #
1223 # return the class
1224 #
1225 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001226 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001227 #
1228 # Raise an exception
1229 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001230 if functions_noexcept.has_key(name):
1231 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001232 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001233 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001234 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001235 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001236 % (name))
1237 elif string.find(name, "XPath") >= 0:
1238 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001239 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001240 % (name))
1241 elif string.find(name, "Parse") >= 0:
1242 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001243 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001244 % (name))
1245 else:
1246 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001247 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001248 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001249 classes.write(" return ");
1250 classes.write(converter_type[ret[0]] % ("ret"));
1251 classes.write("\n");
1252 else:
1253 classes.write(" return ret\n");
1254 classes.write("\n");
1255
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001256 #
1257 # Generate enum constants
1258 #
1259 for type,enum in enums.items():
1260 classes.write("# %s\n" % type)
1261 items = enum.items()
1262 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1263 for name,value in items:
1264 classes.write("%s = %s\n" % (name,value))
1265 classes.write("\n");
1266
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001267 txt.close()
1268 classes.close()
1269
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001270buildStubs()
1271buildWrappers()