blob: 0b09b9a2331ffb72fccc9c607df870d4dcc619a1 [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 Veillard0fea6f42002-02-22 22:51:13 +00009import sys
Daniel Veillard36ed5292002-01-30 23:49:06 +000010import string
Daniel Veillard1971ee22002-01-31 20:29:19 +000011
William M. Brack106cad62004-12-23 15:56:12 +000012if len(sys.argv) > 1:
13 srcPref = sys.argv[1] + '/'
14else:
15 srcPref = ''
16
Daniel Veillard1971ee22002-01-31 20:29:19 +000017#######################################################################
18#
19# That part if purely the API acquisition phase from the
20# XML API description
21#
22#######################################################################
23import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000024import xmllib
25try:
26 import sgmlop
27except ImportError:
28 sgmlop = None # accelerator not available
29
30debug = 0
31
32if sgmlop:
33 class FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000034 """sgmlop based XML parser. this is typically 15x faster
35 than SlowParser..."""
Daniel Veillardd2897fd2002-01-30 16:37:32 +000036
Daniel Veillard01a6d412002-02-11 18:42:20 +000037 def __init__(self, target):
Daniel Veillardd2897fd2002-01-30 16:37:32 +000038
Daniel Veillard01a6d412002-02-11 18:42:20 +000039 # setup callbacks
40 self.finish_starttag = target.start
41 self.finish_endtag = target.end
42 self.handle_data = target.data
Daniel Veillardd2897fd2002-01-30 16:37:32 +000043
Daniel Veillard01a6d412002-02-11 18:42:20 +000044 # activate parser
45 self.parser = sgmlop.XMLParser()
46 self.parser.register(self)
47 self.feed = self.parser.feed
48 self.entity = {
49 "amp": "&", "gt": ">", "lt": "<",
50 "apos": "'", "quot": '"'
51 }
Daniel Veillardd2897fd2002-01-30 16:37:32 +000052
Daniel Veillard01a6d412002-02-11 18:42:20 +000053 def close(self):
54 try:
55 self.parser.close()
56 finally:
57 self.parser = self.feed = None # nuke circular reference
Daniel Veillardd2897fd2002-01-30 16:37:32 +000058
Daniel Veillard01a6d412002-02-11 18:42:20 +000059 def handle_entityref(self, entity):
60 # <string> entity
61 try:
62 self.handle_data(self.entity[entity])
63 except KeyError:
64 self.handle_data("&%s;" % entity)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000065
66else:
67 FastParser = None
68
69
70class SlowParser(xmllib.XMLParser):
71 """slow but safe standard parser, based on the XML parser in
72 Python's standard library."""
73
74 def __init__(self, target):
Daniel Veillard01a6d412002-02-11 18:42:20 +000075 self.unknown_starttag = target.start
76 self.handle_data = target.data
77 self.unknown_endtag = target.end
78 xmllib.XMLParser.__init__(self)
Daniel Veillardd2897fd2002-01-30 16:37:32 +000079
80def getparser(target = None):
81 # get the fastest available parser, and attach it to an
82 # unmarshalling object. return both objects.
Daniel Veillard6f46f6c2002-08-01 12:22:24 +000083 if target is None:
Daniel Veillard01a6d412002-02-11 18:42:20 +000084 target = docParser()
Daniel Veillardd2897fd2002-01-30 16:37:32 +000085 if FastParser:
Daniel Veillard01a6d412002-02-11 18:42:20 +000086 return FastParser(target), target
Daniel Veillardd2897fd2002-01-30 16:37:32 +000087 return SlowParser(target), target
88
89class docParser:
90 def __init__(self):
91 self._methodname = None
Daniel Veillard01a6d412002-02-11 18:42:20 +000092 self._data = []
93 self.in_function = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +000094
95 def close(self):
96 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +000097 print "close"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000098
99 def getmethodname(self):
100 return self._methodname
101
102 def data(self, text):
103 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000104 print "data %s" % text
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000105 self._data.append(text)
106
107 def start(self, tag, attrs):
108 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000109 print "start %s, %s" % (tag, attrs)
110 if tag == 'function':
111 self._data = []
112 self.in_function = 1
113 self.function = None
114 self.function_args = []
115 self.function_descr = None
116 self.function_return = None
117 self.function_file = None
118 if attrs.has_key('name'):
119 self.function = attrs['name']
120 if attrs.has_key('file'):
121 self.function_file = attrs['file']
122 elif tag == 'info':
123 self._data = []
124 elif tag == 'arg':
125 if self.in_function == 1:
126 self.function_arg_name = None
127 self.function_arg_type = None
128 self.function_arg_info = None
129 if attrs.has_key('name'):
130 self.function_arg_name = attrs['name']
131 if attrs.has_key('type'):
132 self.function_arg_type = attrs['type']
133 if attrs.has_key('info'):
134 self.function_arg_info = attrs['info']
135 elif tag == 'return':
136 if self.in_function == 1:
137 self.function_return_type = None
138 self.function_return_info = None
139 self.function_return_field = None
140 if attrs.has_key('type'):
141 self.function_return_type = attrs['type']
142 if attrs.has_key('info'):
143 self.function_return_info = attrs['info']
144 if attrs.has_key('field'):
145 self.function_return_field = attrs['field']
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000146 elif tag == 'enum':
147 enum(attrs['type'],attrs['name'],attrs['value'])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000148
149 def end(self, tag):
150 if debug:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000151 print "end %s" % tag
152 if tag == 'function':
153 if self.function != None:
154 function(self.function, self.function_descr,
155 self.function_return, self.function_args,
156 self.function_file)
157 self.in_function = 0
158 elif tag == 'arg':
159 if self.in_function == 1:
160 self.function_args.append([self.function_arg_name,
161 self.function_arg_type,
162 self.function_arg_info])
163 elif tag == 'return':
164 if self.in_function == 1:
165 self.function_return = [self.function_return_type,
166 self.function_return_info,
167 self.function_return_field]
168 elif tag == 'info':
169 str = ''
170 for c in self._data:
171 str = str + c
172 if self.in_function == 1:
173 self.function_descr = str
174
175
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000176def function(name, desc, ret, args, file):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000177 functions[name] = (desc, ret, args, file)
178
Daniel Veillard4f4a27f2004-01-14 23:50:34 +0000179def enum(type, name, value):
180 if not enums.has_key(type):
181 enums[type] = {}
182 enums[type][name] = value
183
Daniel Veillard1971ee22002-01-31 20:29:19 +0000184#######################################################################
185#
186# Some filtering rukes to drop functions/types which should not
187# be exposed as-is on the Python interface
188#
189#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000190
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000191skipped_modules = {
192 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000193 'DOCBparser': None,
194 'SAX': None,
195 'hash': None,
196 'list': None,
197 'threads': None,
Daniel Veillardff12c492003-01-23 16:42:55 +0000198# 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000199}
200skipped_types = {
201 'int *': "usually a return type",
202 'xmlSAXHandlerPtr': "not the proper interface for SAX",
203 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000204 'xmlRMutexPtr': "thread specific, skipped",
205 'xmlMutexPtr': "thread specific, skipped",
206 'xmlGlobalStatePtr': "thread specific, skipped",
207 'xmlListPtr': "internal representation not suitable for python",
208 'xmlBufferPtr': "internal representation not suitable for python",
209 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000210}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000211
212#######################################################################
213#
214# Table of remapping to/from the python type or class to the C
215# counterpart.
216#
217#######################################################################
218
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000219py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000220 'void': (None, None, None, None),
221 'int': ('i', None, "int", "int"),
222 'long': ('i', None, "int", "int"),
223 'double': ('d', None, "double", "double"),
224 'unsigned int': ('i', None, "int", "int"),
225 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000226 'unsigned char *': ('z', None, "charPtr", "char *"),
227 'char *': ('z', None, "charPtr", "char *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000228 'const char *': ('z', None, "charPtrConst", "const char *"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000229 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillardc575b992002-02-08 13:28:40 +0000230 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000231 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
237 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
238 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
239 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
240 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
241 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
242 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
243 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
244 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
245 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
246 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000247 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
248 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
249 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
250 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
251 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
252 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
253 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
254 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
255 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
256 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
257 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
258 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000259 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
260 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
261 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
262 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
263 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
264 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
265 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
266 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
267 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
268 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
269 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
270 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000271 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
272 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000273 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000274 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
275 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
276 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
277 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000278 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000279 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
280 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000281 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000282 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000283 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
284 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000285 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000286 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000287 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000288 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
289 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
290 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000291 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
292 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
293 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000294}
295
296py_return_types = {
297 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000298}
299
300unknown_types = {}
301
William M. Brack106cad62004-12-23 15:56:12 +0000302foreign_encoding_args = (
William M. Brackff349112004-12-24 08:39:13 +0000303 'htmlCreateMemoryParserCtxt',
304 'htmlCtxtReadMemory',
305 'htmlParseChunk',
306 'htmlReadMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000307 'xmlCreateMemoryParserCtxt',
William M. Brackff349112004-12-24 08:39:13 +0000308 'xmlCtxtReadMemory',
309 'xmlCtxtResetPush',
310 'xmlParseChunk',
311 'xmlParseMemory',
312 'xmlReadMemory',
313 'xmlRecoverMemory',
William M. Brack106cad62004-12-23 15:56:12 +0000314)
315
Daniel Veillard1971ee22002-01-31 20:29:19 +0000316#######################################################################
317#
318# This part writes the C <-> Python stubs libxml2-py.[ch] and
319# the table libxml2-export.c to add when registrering the Python module
320#
321#######################################################################
322
Daniel Veillard263ec862004-10-04 10:26:54 +0000323# Class methods which are written by hand in libxml.c but the Python-level
324# code is still automatically generated (so they are not in skip_function()).
325skip_impl = (
326 'xmlSaveFileTo',
327 'xmlSaveFormatFileTo',
328)
329
Daniel Veillard1971ee22002-01-31 20:29:19 +0000330def skip_function(name):
331 if name[0:12] == "xmlXPathWrap":
332 return 1
Daniel Veillarde6227e02003-01-14 11:42:39 +0000333 if name == "xmlFreeParserCtxt":
334 return 1
Daniel Veillardf93a8662004-07-01 12:56:30 +0000335 if name == "xmlCleanupParser":
336 return 1
Daniel Veillard26f70262003-01-16 22:45:08 +0000337 if name == "xmlFreeTextReader":
338 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000339# if name[0:11] == "xmlXPathNew":
340# return 1
Daniel Veillardc2664642003-07-29 20:44:53 +0000341 # the next function is defined in libxml.c
342 if name == "xmlRelaxNGFreeValidCtxt":
343 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000344#
345# Those are skipped because the Const version is used of the bindings
346# instead.
347#
348 if name == "xmlTextReaderBaseUri":
349 return 1
350 if name == "xmlTextReaderLocalName":
351 return 1
352 if name == "xmlTextReaderName":
353 return 1
354 if name == "xmlTextReaderNamespaceUri":
355 return 1
356 if name == "xmlTextReaderPrefix":
357 return 1
358 if name == "xmlTextReaderXmlLang":
359 return 1
360 if name == "xmlTextReaderValue":
361 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000362 if name == "xmlOutputBufferClose": # handled by by the superclass
363 return 1
364 if name == "xmlOutputBufferFlush": # handled by by the superclass
365 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000366 if name == "xmlErrMemory":
367 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000368
369 if name == "xmlValidBuildContentModel":
370 return 1
371 if name == "xmlValidateElementDecl":
372 return 1
373 if name == "xmlValidateAttributeDecl":
374 return 1
375
Daniel Veillard1971ee22002-01-31 20:29:19 +0000376 return 0
377
Daniel Veillard96fe0952002-01-30 20:52:23 +0000378def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000379 global py_types
380 global unknown_types
381 global functions
382 global skipped_modules
383
384 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000385 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000386 except:
387 print "failed to get function %s infos"
388 return
389
390 if skipped_modules.has_key(file):
391 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000392 if skip_function(name) == 1:
393 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000394 if name in skip_impl:
395 # Don't delete the function entry in the caller.
396 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000397
398 c_call = "";
399 format=""
400 format_args=""
401 c_args=""
402 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000403 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000404 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000405 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000406 # This should be correct
407 if arg[1][0:6] == "const ":
408 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000409 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000410 if py_types.has_key(arg[1]):
411 (f, t, n, c) = py_types[arg[1]]
William M. Brackff349112004-12-24 08:39:13 +0000412 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
William M. Brack106cad62004-12-23 15:56:12 +0000413 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000414 if f != None:
415 format = format + f
416 if t != None:
417 format_args = format_args + ", &pyobj_%s" % (arg[0])
418 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
419 c_convert = c_convert + \
420 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
421 arg[1], t, arg[0]);
422 else:
423 format_args = format_args + ", &%s" % (arg[0])
William M. Brack106cad62004-12-23 15:56:12 +0000424 if f == 't#':
425 format_args = format_args + ", &py_buffsize%d" % num_bufs
426 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
427 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000428 if c_call != "":
429 c_call = c_call + ", ";
430 c_call = c_call + "%s" % (arg[0])
431 else:
432 if skipped_types.has_key(arg[1]):
433 return 0
434 if unknown_types.has_key(arg[1]):
435 lst = unknown_types[arg[1]]
436 lst.append(name)
437 else:
438 unknown_types[arg[1]] = [name]
439 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000440 if format != "":
441 format = format + ":%s" % (name)
442
443 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000444 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000445 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
446 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
447 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000448 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
449 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000450 else:
451 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
452 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000453 else:
454 c_call = "\n %s(%s);\n" % (name, c_call);
455 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000456 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000457 (f, t, n, c) = py_types[ret[0]]
458 c_return = " %s c_retval;\n" % (ret[0])
459 if file == "python_accessor" and ret[2] != None:
460 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
461 else:
462 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
463 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
464 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000465 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000466 (f, t, n, c) = py_return_types[ret[0]]
467 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000468 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000469 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
470 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000471 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000472 if skipped_types.has_key(ret[0]):
473 return 0
474 if unknown_types.has_key(ret[0]):
475 lst = unknown_types[ret[0]]
476 lst.append(name)
477 else:
478 unknown_types[ret[0]] = [name]
479 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000480
Daniel Veillard42766c02002-08-22 20:52:17 +0000481 if file == "debugXML":
482 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
483 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
484 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000485 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000486 include.write("#ifdef LIBXML_HTML_ENABLED\n");
487 export.write("#ifdef LIBXML_HTML_ENABLED\n");
488 output.write("#ifdef LIBXML_HTML_ENABLED\n");
489 elif file == "c14n":
490 include.write("#ifdef LIBXML_C14N_ENABLED\n");
491 export.write("#ifdef LIBXML_C14N_ENABLED\n");
492 output.write("#ifdef LIBXML_C14N_ENABLED\n");
493 elif file == "xpathInternals" or file == "xpath":
494 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
495 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
496 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
497 elif file == "xpointer":
498 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
499 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
500 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
501 elif file == "xinclude":
502 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
503 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
504 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000505 elif file == "xmlregexp":
506 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
507 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
508 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000509 elif file == "xmlschemas" or file == "xmlschemastypes" or \
510 file == "relaxng":
511 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
512 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
513 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000514
Daniel Veillard96fe0952002-01-30 20:52:23 +0000515 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000516 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000517
Daniel Veillardd2379012002-03-15 22:24:56 +0000518 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000519 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000520
521 if file == "python":
522 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000523 if name[0:4] == "html":
524 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
525 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
526 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000527 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000528 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000529 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000530 if name[0:4] == "html":
531 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
532 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
533 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000534 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000535
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000536 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000537 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
538 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000539 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000540 output.write(" ATTRIBUTE_UNUSED")
541 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000542 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000543 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000544 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000545 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000546 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000547 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000548 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000549 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000550 (format, format_args))
551 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000552 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000553 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000554
555 output.write(c_call)
556 output.write(ret_convert)
557 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000558 if file == "debugXML":
559 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
560 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
561 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000562 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000563 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
564 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
565 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
566 elif file == "c14n":
567 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
568 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
569 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
570 elif file == "xpathInternals" or file == "xpath":
571 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
572 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
573 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
574 elif file == "xpointer":
575 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
576 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
577 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
578 elif file == "xinclude":
579 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
580 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
581 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000582 elif file == "xmlregexp":
583 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
584 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
585 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000586 elif file == "xmlschemas" or file == "xmlschemastypes" or \
587 file == "relaxng":
588 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
589 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
590 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000591 return 1
592
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000593def buildStubs():
594 global py_types
595 global py_return_types
596 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000597
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000598 try:
William M. Brack106cad62004-12-23 15:56:12 +0000599 f = open(srcPref + "libxml2-api.xml")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000600 data = f.read()
601 (parser, target) = getparser()
602 parser.feed(data)
603 parser.close()
604 except IOError, msg:
605 try:
William M. Brack106cad62004-12-23 15:56:12 +0000606 f = open(srcPref + "../doc/libxml2-api.xml")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000607 data = f.read()
608 (parser, target) = getparser()
609 parser.feed(data)
610 parser.close()
611 except IOError, msg:
612 print file, ":", msg
613 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000614
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000615 n = len(functions.keys())
616 print "Found %d functions in libxml2-api.xml" % (n)
617
618 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
619 try:
William M. Brack106cad62004-12-23 15:56:12 +0000620 f = open(srcPref + "libxml2-python-api.xml")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000621 data = f.read()
622 (parser, target) = getparser()
623 parser.feed(data)
624 parser.close()
625 except IOError, msg:
626 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000627
628
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000629 print "Found %d functions in libxml2-python-api.xml" % (
630 len(functions.keys()) - n)
631 nb_wrap = 0
632 failed = 0
633 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000634
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000635 include = open("libxml2-py.h", "w")
636 include.write("/* Generated */\n\n")
637 export = open("libxml2-export.c", "w")
638 export.write("/* Generated */\n\n")
639 wrapper = open("libxml2-py.c", "w")
640 wrapper.write("/* Generated */\n\n")
641 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000642 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000643 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000644 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000645 wrapper.write("#include \"libxml_wrap.h\"\n")
646 wrapper.write("#include \"libxml2-py.h\"\n\n")
647 for function in functions.keys():
648 ret = print_function_wrapper(function, wrapper, export, include)
649 if ret < 0:
650 failed = failed + 1
651 del functions[function]
652 if ret == 0:
653 skipped = skipped + 1
654 del functions[function]
655 if ret == 1:
656 nb_wrap = nb_wrap + 1
657 include.close()
658 export.close()
659 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000660
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000661 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
662 failed, skipped);
663 print "Missing type converters: "
664 for type in unknown_types.keys():
665 print "%s:%d " % (type, len(unknown_types[type])),
666 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000667
Daniel Veillard1971ee22002-01-31 20:29:19 +0000668#######################################################################
669#
670# This part writes part of the Python front-end classes based on
671# mapping rules between types and classes and also based on function
672# renaming to get consistent function names at the Python level
673#
674#######################################################################
675
676#
677# The type automatically remapped to generated classes
678#
679classes_type = {
680 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
681 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
682 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
683 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
684 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
685 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
686 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
687 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
688 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
689 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
690 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
691 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
692 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
693 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
694 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
695 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
696 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
697 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
698 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000699 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
700 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
701 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000702 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
703 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000704 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
705 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000706 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000707 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000708 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000709 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000710 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
711 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000712 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000713 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000714 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000715 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
716 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
717 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000718 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
719 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
720 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000721}
722
723converter_type = {
724 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
725}
726
727primary_classes = ["xmlNode", "xmlDoc"]
728
729classes_ancestor = {
730 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000731 "xmlDtd" : "xmlNode",
732 "xmlDoc" : "xmlNode",
733 "xmlAttr" : "xmlNode",
734 "xmlNs" : "xmlNode",
735 "xmlEntity" : "xmlNode",
736 "xmlElement" : "xmlNode",
737 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000738 "outputBuffer": "ioWriteWrapper",
739 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000740 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000741 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000742}
743classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000744 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000745 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000746 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000747# "outputBuffer": "xmlOutputBufferClose",
748 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000749 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000750 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000751 "relaxNgSchema": "xmlRelaxNGFree",
752 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
753 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000754 "Schema": "xmlSchemaFree",
755 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
756 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000757 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000758}
759
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000760functions_noexcept = {
761 "xmlHasProp": 1,
762 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000763 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000764 "xmlNodeGetNs": 1,
765 "xmlNodeGetNsDefs": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000766}
767
Daniel Veillarddc85f282002-12-31 11:18:37 +0000768reference_keepers = {
769 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000770 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000771 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000772}
773
Daniel Veillard36ed5292002-01-30 23:49:06 +0000774function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000775
776function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000777
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000778def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000779 listname = classe + "List"
780 ll = len(listname)
781 l = len(classe)
782 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000783 func = name[l:]
784 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000785 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
786 func = name[12:]
787 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000788 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
789 func = name[12:]
790 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000791 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
792 func = name[10:]
793 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000794 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
795 func = name[9:]
796 func = string.lower(func[0:1]) + func[1:]
797 elif name[0:9] == "xmlURISet" and file == "python_accessor":
798 func = name[6:]
799 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000800 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
801 func = name[11:]
802 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000803 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
804 func = name[17:]
805 func = string.lower(func[0:1]) + func[1:]
806 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
807 func = name[11:]
808 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000809 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
810 func = name[8:]
811 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000812 elif name[0:15] == "xmlOutputBuffer" and file != "python":
813 func = name[15:]
814 func = string.lower(func[0:1]) + func[1:]
815 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
816 func = name[20:]
817 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000818 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000819 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000820 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000821 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000822 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
823 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000824 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
825 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000826 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
827 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000828 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
829 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000830 elif name[0:11] == "xmlACatalog":
831 func = name[11:]
832 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000833 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000834 func = name[l:]
835 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000836 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000837 func = name[7:]
838 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000839 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000840 func = name[6:]
841 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000842 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000843 func = name[3:]
844 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000845 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000846 func = name
847 if func[0:5] == "xPath":
848 func = "xpath" + func[5:]
849 elif func[0:4] == "xPtr":
850 func = "xpointer" + func[4:]
851 elif func[0:8] == "xInclude":
852 func = "xinclude" + func[8:]
853 elif func[0:2] == "iD":
854 func = "ID" + func[2:]
855 elif func[0:3] == "uRI":
856 func = "URI" + func[3:]
857 elif func[0:4] == "uTF8":
858 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000859 elif func[0:3] == 'sAX':
860 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000861 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000862
Daniel Veillard36ed5292002-01-30 23:49:06 +0000863
Daniel Veillard1971ee22002-01-31 20:29:19 +0000864def functionCompare(info1, info2):
865 (index1, func1, name1, ret1, args1, file1) = info1
866 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000867 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000868 if func1 < func2:
869 return -1
870 if func1 > func2:
871 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000872 if file1 == "python_accessor":
873 return -1
874 if file2 == "python_accessor":
875 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000876 if file1 < file2:
877 return -1
878 if file1 > file2:
879 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000880 return 0
881
882def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000883 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000884 return
885 val = functions[name][0]
886 val = string.replace(val, "NULL", "None");
887 output.write(indent)
888 output.write('"""')
889 while len(val) > 60:
890 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000891 i = string.rfind(str, " ");
892 if i < 0:
893 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000894 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000895 val = val[i:]
896 output.write(str)
897 output.write('\n ');
898 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000899 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000900 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000901
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000902def buildWrappers():
903 global ctypes
904 global py_types
905 global py_return_types
906 global unknown_types
907 global functions
908 global function_classes
909 global classes_type
910 global classes_list
911 global converter_type
912 global primary_classes
913 global converter_type
914 global classes_ancestor
915 global converter_type
916 global primary_classes
917 global classes_ancestor
918 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000919 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000920
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000921 for type in classes_type.keys():
922 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000923
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000924 #
925 # Build the list of C types to look for ordered to start
926 # with primary classes
927 #
928 ctypes = []
929 classes_list = []
930 ctypes_processed = {}
931 classes_processed = {}
932 for classe in primary_classes:
933 classes_list.append(classe)
934 classes_processed[classe] = ()
935 for type in classes_type.keys():
936 tinfo = classes_type[type]
937 if tinfo[2] == classe:
938 ctypes.append(type)
939 ctypes_processed[type] = ()
940 for type in classes_type.keys():
941 if ctypes_processed.has_key(type):
942 continue
943 tinfo = classes_type[type]
944 if not classes_processed.has_key(tinfo[2]):
945 classes_list.append(tinfo[2])
946 classes_processed[tinfo[2]] = ()
947
948 ctypes.append(type)
949 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000950
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000951 for name in functions.keys():
952 found = 0;
953 (desc, ret, args, file) = functions[name]
954 for type in ctypes:
955 classe = classes_type[type][2]
956
957 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
958 found = 1
959 func = nameFixup(name, classe, type, file)
960 info = (0, func, name, ret, args, file)
961 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000962 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
963 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000964 found = 1
965 func = nameFixup(name, classe, type, file)
966 info = (1, func, name, ret, args, file)
967 function_classes[classe].append(info)
968 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
969 found = 1
970 func = nameFixup(name, classe, type, file)
971 info = (0, func, name, ret, args, file)
972 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000973 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
974 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000975 found = 1
976 func = nameFixup(name, classe, type, file)
977 info = (1, func, name, ret, args, file)
978 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000979 if found == 1:
980 continue
981 if name[0:8] == "xmlXPath":
982 continue
983 if name[0:6] == "xmlStr":
984 continue
985 if name[0:10] == "xmlCharStr":
986 continue
987 func = nameFixup(name, "None", file, file)
988 info = (0, func, name, ret, args, file)
989 function_classes['None'].append(info)
990
991 classes = open("libxml2class.py", "w")
992 txt = open("libxml2class.txt", "w")
993 txt.write(" Generated Classes for libxml2-python\n\n")
994
995 txt.write("#\n# Global functions of the module\n#\n\n")
996 if function_classes.has_key("None"):
997 flist = function_classes["None"]
998 flist.sort(functionCompare)
999 oldfile = ""
1000 for info in flist:
1001 (index, func, name, ret, args, file) = info
1002 if file != oldfile:
1003 classes.write("#\n# Functions from module %s\n#\n\n" % file)
1004 txt.write("\n# functions from module %s\n" % file)
1005 oldfile = file
1006 classes.write("def %s(" % func)
1007 txt.write("%s()\n" % func);
1008 n = 0
1009 for arg in args:
1010 if n != 0:
1011 classes.write(", ")
1012 classes.write("%s" % arg[0])
1013 n = n + 1
1014 classes.write("):\n")
1015 writeDoc(name, args, ' ', classes);
1016
1017 for arg in args:
1018 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001019 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001020 (arg[0], arg[0]))
1021 classes.write(" else: %s__o = %s%s\n" %
1022 (arg[0], arg[0], classes_type[arg[1]][0]))
1023 if ret[0] != "void":
1024 classes.write(" ret = ");
1025 else:
1026 classes.write(" ");
1027 classes.write("libxml2mod.%s(" % name)
1028 n = 0
1029 for arg in args:
1030 if n != 0:
1031 classes.write(", ");
1032 classes.write("%s" % arg[0])
1033 if classes_type.has_key(arg[1]):
1034 classes.write("__o");
1035 n = n + 1
1036 classes.write(")\n");
1037 if ret[0] != "void":
1038 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001039 #
1040 # Raise an exception
1041 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001042 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001043 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001044 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001045 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001046 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001047 % (name))
1048 elif string.find(name, "XPath") >= 0:
1049 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001050 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001051 % (name))
1052 elif string.find(name, "Parse") >= 0:
1053 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001054 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001055 % (name))
1056 else:
1057 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001058 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001059 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001060 classes.write(" return ");
1061 classes.write(classes_type[ret[0]][1] % ("ret"));
1062 classes.write("\n");
1063 else:
1064 classes.write(" return ret\n");
1065 classes.write("\n");
1066
1067 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1068 for classname in classes_list:
1069 if classname == "None":
1070 pass
1071 else:
1072 if classes_ancestor.has_key(classname):
1073 txt.write("\n\nClass %s(%s)\n" % (classname,
1074 classes_ancestor[classname]))
1075 classes.write("class %s(%s):\n" % (classname,
1076 classes_ancestor[classname]))
1077 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001078 if classes_ancestor[classname] == "xmlCore" or \
1079 classes_ancestor[classname] == "xmlNode":
1080 classes.write(" if type(_obj).__name__ != ")
1081 classes.write("'PyCObject':\n")
1082 classes.write(" raise TypeError, ")
1083 classes.write("'%s needs a PyCObject argument'\n" % \
1084 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001085 if reference_keepers.has_key(classname):
1086 rlist = reference_keepers[classname]
1087 for ref in rlist:
1088 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001089 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001090 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1091 classes_ancestor[classname]))
1092 if classes_ancestor[classname] == "xmlCore" or \
1093 classes_ancestor[classname] == "xmlNode":
1094 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001095 format = "<%s (%%s) object at 0x%%x>" % (classname)
1096 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001097 format))
1098 else:
1099 txt.write("Class %s()\n" % (classname))
1100 classes.write("class %s:\n" % (classname))
1101 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001102 if reference_keepers.has_key(classname):
1103 list = reference_keepers[classname]
1104 for ref in list:
1105 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001106 classes.write(" if _obj != None:self._o = _obj;return\n")
1107 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001108 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001109 if classes_destructors.has_key(classname):
1110 classes.write(" def __del__(self):\n")
1111 classes.write(" if self._o != None:\n")
1112 classes.write(" libxml2mod.%s(self._o)\n" %
1113 classes_destructors[classname]);
1114 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001115 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001116 flist = function_classes[classname]
1117 flist.sort(functionCompare)
1118 oldfile = ""
1119 for info in flist:
1120 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001121 #
1122 # Do not provide as method the destructors for the class
1123 # to avoid double free
1124 #
1125 if name == destruct:
1126 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001127 if file != oldfile:
1128 if file == "python_accessor":
1129 classes.write(" # accessors for %s\n" % (classname))
1130 txt.write(" # accessors\n")
1131 else:
1132 classes.write(" #\n")
1133 classes.write(" # %s functions from module %s\n" % (
1134 classname, file))
1135 txt.write("\n # functions from module %s\n" % file)
1136 classes.write(" #\n\n")
1137 oldfile = file
1138 classes.write(" def %s(self" % func)
1139 txt.write(" %s()\n" % func);
1140 n = 0
1141 for arg in args:
1142 if n != index:
1143 classes.write(", %s" % arg[0])
1144 n = n + 1
1145 classes.write("):\n")
1146 writeDoc(name, args, ' ', classes);
1147 n = 0
1148 for arg in args:
1149 if classes_type.has_key(arg[1]):
1150 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001151 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001152 (arg[0], arg[0]))
1153 classes.write(" else: %s__o = %s%s\n" %
1154 (arg[0], arg[0], classes_type[arg[1]][0]))
1155 n = n + 1
1156 if ret[0] != "void":
1157 classes.write(" ret = ");
1158 else:
1159 classes.write(" ");
1160 classes.write("libxml2mod.%s(" % name)
1161 n = 0
1162 for arg in args:
1163 if n != 0:
1164 classes.write(", ");
1165 if n != index:
1166 classes.write("%s" % arg[0])
1167 if classes_type.has_key(arg[1]):
1168 classes.write("__o");
1169 else:
1170 classes.write("self");
1171 if classes_type.has_key(arg[1]):
1172 classes.write(classes_type[arg[1]][0])
1173 n = n + 1
1174 classes.write(")\n");
1175 if ret[0] != "void":
1176 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001177 #
1178 # Raise an exception
1179 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001180 if functions_noexcept.has_key(name):
1181 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001182 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001183 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001184 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001185 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001186 % (name))
1187 elif string.find(name, "XPath") >= 0:
1188 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001189 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001190 % (name))
1191 elif string.find(name, "Parse") >= 0:
1192 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001193 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001194 % (name))
1195 else:
1196 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001197 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001198 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001199
1200 #
1201 # generate the returned class wrapper for the object
1202 #
1203 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001204 classes.write(classes_type[ret[0]][1] % ("ret"));
1205 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001206
1207 #
1208 # Sometime one need to keep references of the source
1209 # class in the returned class object.
1210 # See reference_keepers for the list
1211 #
1212 tclass = classes_type[ret[0]][2]
1213 if reference_keepers.has_key(tclass):
1214 list = reference_keepers[tclass]
1215 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001216 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001217 classes.write(" __tmp.%s = self\n" %
1218 pref[1])
1219 #
1220 # return the class
1221 #
1222 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001223 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001224 #
1225 # Raise an exception
1226 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001227 if functions_noexcept.has_key(name):
1228 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001229 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001230 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001231 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001232 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001233 % (name))
1234 elif string.find(name, "XPath") >= 0:
1235 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001236 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001237 % (name))
1238 elif string.find(name, "Parse") >= 0:
1239 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001240 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001241 % (name))
1242 else:
1243 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001244 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001245 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001246 classes.write(" return ");
1247 classes.write(converter_type[ret[0]] % ("ret"));
1248 classes.write("\n");
1249 else:
1250 classes.write(" return ret\n");
1251 classes.write("\n");
1252
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001253 #
1254 # Generate enum constants
1255 #
1256 for type,enum in enums.items():
1257 classes.write("# %s\n" % type)
1258 items = enum.items()
1259 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1260 for name,value in items:
1261 classes.write("%s = %s\n" % (name,value))
1262 classes.write("\n");
1263
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001264 txt.close()
1265 classes.close()
1266
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001267buildStubs()
1268buildWrappers()