blob: 8add0a93d18bdb1b79da20e40224434dbe14a0c7 [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 Veillard25c90c52005-03-02 10:47:41 +0000347 if name == "xmlFreeValidCtxt":
348 return 1
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000349#
350# Those are skipped because the Const version is used of the bindings
351# instead.
352#
353 if name == "xmlTextReaderBaseUri":
354 return 1
355 if name == "xmlTextReaderLocalName":
356 return 1
357 if name == "xmlTextReaderName":
358 return 1
359 if name == "xmlTextReaderNamespaceUri":
360 return 1
361 if name == "xmlTextReaderPrefix":
362 return 1
363 if name == "xmlTextReaderXmlLang":
364 return 1
365 if name == "xmlTextReaderValue":
366 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000367 if name == "xmlOutputBufferClose": # handled by by the superclass
368 return 1
369 if name == "xmlOutputBufferFlush": # handled by by the superclass
370 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000371 if name == "xmlErrMemory":
372 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000373
374 if name == "xmlValidBuildContentModel":
375 return 1
376 if name == "xmlValidateElementDecl":
377 return 1
378 if name == "xmlValidateAttributeDecl":
379 return 1
380
Daniel Veillard1971ee22002-01-31 20:29:19 +0000381 return 0
382
Daniel Veillard96fe0952002-01-30 20:52:23 +0000383def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000384 global py_types
385 global unknown_types
386 global functions
387 global skipped_modules
388
389 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000390 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000391 except:
392 print "failed to get function %s infos"
393 return
394
395 if skipped_modules.has_key(file):
396 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000397 if skip_function(name) == 1:
398 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000399 if name in skip_impl:
400 # Don't delete the function entry in the caller.
401 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000402
403 c_call = "";
404 format=""
405 format_args=""
406 c_args=""
407 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000408 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000409 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000410 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000411 # This should be correct
412 if arg[1][0:6] == "const ":
413 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000414 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000415 if py_types.has_key(arg[1]):
416 (f, t, n, c) = py_types[arg[1]]
William M. Brackff349112004-12-24 08:39:13 +0000417 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
William M. Brack106cad62004-12-23 15:56:12 +0000418 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000419 if f != None:
420 format = format + f
421 if t != None:
422 format_args = format_args + ", &pyobj_%s" % (arg[0])
423 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
424 c_convert = c_convert + \
425 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
426 arg[1], t, arg[0]);
427 else:
428 format_args = format_args + ", &%s" % (arg[0])
William M. Brack106cad62004-12-23 15:56:12 +0000429 if f == 't#':
430 format_args = format_args + ", &py_buffsize%d" % num_bufs
431 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
432 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000433 if c_call != "":
434 c_call = c_call + ", ";
435 c_call = c_call + "%s" % (arg[0])
436 else:
437 if skipped_types.has_key(arg[1]):
438 return 0
439 if unknown_types.has_key(arg[1]):
440 lst = unknown_types[arg[1]]
441 lst.append(name)
442 else:
443 unknown_types[arg[1]] = [name]
444 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000445 if format != "":
446 format = format + ":%s" % (name)
447
448 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000449 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000450 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
451 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
452 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000453 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
454 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000455 else:
456 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
457 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000458 else:
459 c_call = "\n %s(%s);\n" % (name, c_call);
460 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000461 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000462 (f, t, n, c) = py_types[ret[0]]
463 c_return = " %s c_retval;\n" % (ret[0])
464 if file == "python_accessor" and ret[2] != None:
465 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
466 else:
467 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
468 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
469 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000470 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000471 (f, t, n, c) = py_return_types[ret[0]]
472 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000473 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000474 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
475 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000476 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000477 if skipped_types.has_key(ret[0]):
478 return 0
479 if unknown_types.has_key(ret[0]):
480 lst = unknown_types[ret[0]]
481 lst.append(name)
482 else:
483 unknown_types[ret[0]] = [name]
484 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000485
Daniel Veillard42766c02002-08-22 20:52:17 +0000486 if file == "debugXML":
487 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
488 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
489 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000490 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000491 include.write("#ifdef LIBXML_HTML_ENABLED\n");
492 export.write("#ifdef LIBXML_HTML_ENABLED\n");
493 output.write("#ifdef LIBXML_HTML_ENABLED\n");
494 elif file == "c14n":
495 include.write("#ifdef LIBXML_C14N_ENABLED\n");
496 export.write("#ifdef LIBXML_C14N_ENABLED\n");
497 output.write("#ifdef LIBXML_C14N_ENABLED\n");
498 elif file == "xpathInternals" or file == "xpath":
499 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
500 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
501 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
502 elif file == "xpointer":
503 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
504 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
505 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
506 elif file == "xinclude":
507 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
508 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
509 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000510 elif file == "xmlregexp":
511 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
512 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
513 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000514 elif file == "xmlschemas" or file == "xmlschemastypes" or \
515 file == "relaxng":
516 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
517 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
518 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000519
Daniel Veillard96fe0952002-01-30 20:52:23 +0000520 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000521 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000522
Daniel Veillardd2379012002-03-15 22:24:56 +0000523 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000524 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000525
526 if file == "python":
527 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000528 if name[0:4] == "html":
529 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
530 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
531 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000532 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000533 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000534 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000535 if name[0:4] == "html":
536 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
537 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
538 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000539 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000540
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000541 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000542 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
543 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000544 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000545 output.write(" ATTRIBUTE_UNUSED")
546 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000547 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000548 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000549 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000550 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000551 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000552 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000553 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000554 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000555 (format, format_args))
556 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000557 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000558 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000559
560 output.write(c_call)
561 output.write(ret_convert)
562 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000563 if file == "debugXML":
564 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
565 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
566 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000567 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000568 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
569 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
570 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
571 elif file == "c14n":
572 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
573 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
574 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
575 elif file == "xpathInternals" or file == "xpath":
576 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
577 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
578 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
579 elif file == "xpointer":
580 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
581 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
582 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
583 elif file == "xinclude":
584 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
585 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
586 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000587 elif file == "xmlregexp":
588 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
589 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
590 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000591 elif file == "xmlschemas" or file == "xmlschemastypes" or \
592 file == "relaxng":
593 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
594 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
595 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000596 return 1
597
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000598def buildStubs():
599 global py_types
600 global py_return_types
601 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000602
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000603 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000604 f = open(os.path.join(srcPref,"libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000605 data = f.read()
606 (parser, target) = getparser()
607 parser.feed(data)
608 parser.close()
609 except IOError, msg:
610 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000611 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000612 data = f.read()
613 (parser, target) = getparser()
614 parser.feed(data)
615 parser.close()
616 except IOError, msg:
617 print file, ":", msg
618 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000619
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000620 n = len(functions.keys())
621 print "Found %d functions in libxml2-api.xml" % (n)
622
623 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
624 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000625 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000626 data = f.read()
627 (parser, target) = getparser()
628 parser.feed(data)
629 parser.close()
630 except IOError, msg:
631 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000632
633
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000634 print "Found %d functions in libxml2-python-api.xml" % (
635 len(functions.keys()) - n)
636 nb_wrap = 0
637 failed = 0
638 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000639
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000640 include = open("libxml2-py.h", "w")
641 include.write("/* Generated */\n\n")
642 export = open("libxml2-export.c", "w")
643 export.write("/* Generated */\n\n")
644 wrapper = open("libxml2-py.c", "w")
645 wrapper.write("/* Generated */\n\n")
646 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000647 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000648 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000649 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000650 wrapper.write("#include \"libxml_wrap.h\"\n")
651 wrapper.write("#include \"libxml2-py.h\"\n\n")
652 for function in functions.keys():
653 ret = print_function_wrapper(function, wrapper, export, include)
654 if ret < 0:
655 failed = failed + 1
656 del functions[function]
657 if ret == 0:
658 skipped = skipped + 1
659 del functions[function]
660 if ret == 1:
661 nb_wrap = nb_wrap + 1
662 include.close()
663 export.close()
664 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000665
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000666 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
667 failed, skipped);
668 print "Missing type converters: "
669 for type in unknown_types.keys():
670 print "%s:%d " % (type, len(unknown_types[type])),
671 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000672
Daniel Veillard1971ee22002-01-31 20:29:19 +0000673#######################################################################
674#
675# This part writes part of the Python front-end classes based on
676# mapping rules between types and classes and also based on function
677# renaming to get consistent function names at the Python level
678#
679#######################################################################
680
681#
682# The type automatically remapped to generated classes
683#
684classes_type = {
685 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
686 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
687 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
688 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
689 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
690 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
691 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
692 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
693 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
694 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
695 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
696 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
697 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
698 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
699 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
700 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
701 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
702 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
703 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000704 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
705 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
706 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000707 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
708 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000709 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
710 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000711 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000712 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000713 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000714 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000715 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
716 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000717 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000718 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000719 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000720 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
721 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
722 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000723 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
724 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
725 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000726}
727
728converter_type = {
729 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
730}
731
732primary_classes = ["xmlNode", "xmlDoc"]
733
734classes_ancestor = {
735 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000736 "xmlDtd" : "xmlNode",
737 "xmlDoc" : "xmlNode",
738 "xmlAttr" : "xmlNode",
739 "xmlNs" : "xmlNode",
740 "xmlEntity" : "xmlNode",
741 "xmlElement" : "xmlNode",
742 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000743 "outputBuffer": "ioWriteWrapper",
744 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000745 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000746 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000747}
748classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000749 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000750 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000751 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000752# "outputBuffer": "xmlOutputBufferClose",
753 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000754 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000755 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000756 "relaxNgSchema": "xmlRelaxNGFree",
757 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
758 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000759 "Schema": "xmlSchemaFree",
760 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
761 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000762 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000763}
764
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000765functions_noexcept = {
766 "xmlHasProp": 1,
767 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000768 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000769 "xmlNodeGetNs": 1,
770 "xmlNodeGetNsDefs": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000771}
772
Daniel Veillarddc85f282002-12-31 11:18:37 +0000773reference_keepers = {
774 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000775 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000776 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000777}
778
Daniel Veillard36ed5292002-01-30 23:49:06 +0000779function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000780
781function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000782
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000783def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000784 listname = classe + "List"
785 ll = len(listname)
786 l = len(classe)
787 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000788 func = name[l:]
789 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000790 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
791 func = name[12:]
792 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000793 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
794 func = name[12:]
795 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000796 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
797 func = name[10:]
798 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000799 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
800 func = name[9:]
801 func = string.lower(func[0:1]) + func[1:]
802 elif name[0:9] == "xmlURISet" and file == "python_accessor":
803 func = name[6:]
804 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000805 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
806 func = name[11:]
807 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000808 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
809 func = name[17:]
810 func = string.lower(func[0:1]) + func[1:]
811 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
812 func = name[11:]
813 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000814 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
815 func = name[8:]
816 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000817 elif name[0:15] == "xmlOutputBuffer" and file != "python":
818 func = name[15:]
819 func = string.lower(func[0:1]) + func[1:]
820 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
821 func = name[20:]
822 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000823 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000824 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000825 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000826 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000827 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
828 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000829 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
830 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000831 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
832 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000833 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
834 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000835 elif name[0:11] == "xmlACatalog":
836 func = name[11:]
837 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000838 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000839 func = name[l:]
840 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000841 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000842 func = name[7:]
843 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000844 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000845 func = name[6:]
846 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000847 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000848 func = name[3:]
849 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000850 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000851 func = name
852 if func[0:5] == "xPath":
853 func = "xpath" + func[5:]
854 elif func[0:4] == "xPtr":
855 func = "xpointer" + func[4:]
856 elif func[0:8] == "xInclude":
857 func = "xinclude" + func[8:]
858 elif func[0:2] == "iD":
859 func = "ID" + func[2:]
860 elif func[0:3] == "uRI":
861 func = "URI" + func[3:]
862 elif func[0:4] == "uTF8":
863 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000864 elif func[0:3] == 'sAX':
865 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000866 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000867
Daniel Veillard36ed5292002-01-30 23:49:06 +0000868
Daniel Veillard1971ee22002-01-31 20:29:19 +0000869def functionCompare(info1, info2):
870 (index1, func1, name1, ret1, args1, file1) = info1
871 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000872 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000873 if func1 < func2:
874 return -1
875 if func1 > func2:
876 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000877 if file1 == "python_accessor":
878 return -1
879 if file2 == "python_accessor":
880 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000881 if file1 < file2:
882 return -1
883 if file1 > file2:
884 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000885 return 0
886
887def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000888 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000889 return
890 val = functions[name][0]
891 val = string.replace(val, "NULL", "None");
892 output.write(indent)
893 output.write('"""')
894 while len(val) > 60:
895 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000896 i = string.rfind(str, " ");
897 if i < 0:
898 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000899 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000900 val = val[i:]
901 output.write(str)
902 output.write('\n ');
903 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000904 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000905 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000906
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000907def buildWrappers():
908 global ctypes
909 global py_types
910 global py_return_types
911 global unknown_types
912 global functions
913 global function_classes
914 global classes_type
915 global classes_list
916 global converter_type
917 global primary_classes
918 global converter_type
919 global classes_ancestor
920 global converter_type
921 global primary_classes
922 global classes_ancestor
923 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000924 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000925
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000926 for type in classes_type.keys():
927 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000928
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000929 #
930 # Build the list of C types to look for ordered to start
931 # with primary classes
932 #
933 ctypes = []
934 classes_list = []
935 ctypes_processed = {}
936 classes_processed = {}
937 for classe in primary_classes:
938 classes_list.append(classe)
939 classes_processed[classe] = ()
940 for type in classes_type.keys():
941 tinfo = classes_type[type]
942 if tinfo[2] == classe:
943 ctypes.append(type)
944 ctypes_processed[type] = ()
945 for type in classes_type.keys():
946 if ctypes_processed.has_key(type):
947 continue
948 tinfo = classes_type[type]
949 if not classes_processed.has_key(tinfo[2]):
950 classes_list.append(tinfo[2])
951 classes_processed[tinfo[2]] = ()
952
953 ctypes.append(type)
954 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000955
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000956 for name in functions.keys():
957 found = 0;
958 (desc, ret, args, file) = functions[name]
959 for type in ctypes:
960 classe = classes_type[type][2]
961
962 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
963 found = 1
964 func = nameFixup(name, classe, type, file)
965 info = (0, func, name, ret, args, file)
966 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000967 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
968 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000969 found = 1
970 func = nameFixup(name, classe, type, file)
971 info = (1, func, name, ret, args, file)
972 function_classes[classe].append(info)
973 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
974 found = 1
975 func = nameFixup(name, classe, type, file)
976 info = (0, func, name, ret, args, file)
977 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000978 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
979 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000980 found = 1
981 func = nameFixup(name, classe, type, file)
982 info = (1, func, name, ret, args, file)
983 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000984 if found == 1:
985 continue
986 if name[0:8] == "xmlXPath":
987 continue
988 if name[0:6] == "xmlStr":
989 continue
990 if name[0:10] == "xmlCharStr":
991 continue
992 func = nameFixup(name, "None", file, file)
993 info = (0, func, name, ret, args, file)
994 function_classes['None'].append(info)
995
996 classes = open("libxml2class.py", "w")
997 txt = open("libxml2class.txt", "w")
998 txt.write(" Generated Classes for libxml2-python\n\n")
999
1000 txt.write("#\n# Global functions of the module\n#\n\n")
1001 if function_classes.has_key("None"):
1002 flist = function_classes["None"]
1003 flist.sort(functionCompare)
1004 oldfile = ""
1005 for info in flist:
1006 (index, func, name, ret, args, file) = info
1007 if file != oldfile:
1008 classes.write("#\n# Functions from module %s\n#\n\n" % file)
1009 txt.write("\n# functions from module %s\n" % file)
1010 oldfile = file
1011 classes.write("def %s(" % func)
1012 txt.write("%s()\n" % func);
1013 n = 0
1014 for arg in args:
1015 if n != 0:
1016 classes.write(", ")
1017 classes.write("%s" % arg[0])
1018 n = n + 1
1019 classes.write("):\n")
1020 writeDoc(name, args, ' ', classes);
1021
1022 for arg in args:
1023 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001024 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001025 (arg[0], arg[0]))
1026 classes.write(" else: %s__o = %s%s\n" %
1027 (arg[0], arg[0], classes_type[arg[1]][0]))
1028 if ret[0] != "void":
1029 classes.write(" ret = ");
1030 else:
1031 classes.write(" ");
1032 classes.write("libxml2mod.%s(" % name)
1033 n = 0
1034 for arg in args:
1035 if n != 0:
1036 classes.write(", ");
1037 classes.write("%s" % arg[0])
1038 if classes_type.has_key(arg[1]):
1039 classes.write("__o");
1040 n = n + 1
1041 classes.write(")\n");
1042 if ret[0] != "void":
1043 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001044 #
1045 # Raise an exception
1046 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001047 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001048 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001049 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001050 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001051 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001052 % (name))
1053 elif string.find(name, "XPath") >= 0:
1054 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001055 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001056 % (name))
1057 elif string.find(name, "Parse") >= 0:
1058 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001059 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001060 % (name))
1061 else:
1062 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001063 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001064 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001065 classes.write(" return ");
1066 classes.write(classes_type[ret[0]][1] % ("ret"));
1067 classes.write("\n");
1068 else:
1069 classes.write(" return ret\n");
1070 classes.write("\n");
1071
1072 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1073 for classname in classes_list:
1074 if classname == "None":
1075 pass
1076 else:
1077 if classes_ancestor.has_key(classname):
1078 txt.write("\n\nClass %s(%s)\n" % (classname,
1079 classes_ancestor[classname]))
1080 classes.write("class %s(%s):\n" % (classname,
1081 classes_ancestor[classname]))
1082 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001083 if classes_ancestor[classname] == "xmlCore" or \
1084 classes_ancestor[classname] == "xmlNode":
1085 classes.write(" if type(_obj).__name__ != ")
1086 classes.write("'PyCObject':\n")
1087 classes.write(" raise TypeError, ")
1088 classes.write("'%s needs a PyCObject argument'\n" % \
1089 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001090 if reference_keepers.has_key(classname):
1091 rlist = reference_keepers[classname]
1092 for ref in rlist:
1093 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001094 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001095 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1096 classes_ancestor[classname]))
1097 if classes_ancestor[classname] == "xmlCore" or \
1098 classes_ancestor[classname] == "xmlNode":
1099 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001100 format = "<%s (%%s) object at 0x%%x>" % (classname)
1101 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001102 format))
1103 else:
1104 txt.write("Class %s()\n" % (classname))
1105 classes.write("class %s:\n" % (classname))
1106 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001107 if reference_keepers.has_key(classname):
1108 list = reference_keepers[classname]
1109 for ref in list:
1110 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001111 classes.write(" if _obj != None:self._o = _obj;return\n")
1112 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001113 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001114 if classes_destructors.has_key(classname):
1115 classes.write(" def __del__(self):\n")
1116 classes.write(" if self._o != None:\n")
1117 classes.write(" libxml2mod.%s(self._o)\n" %
1118 classes_destructors[classname]);
1119 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001120 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001121 flist = function_classes[classname]
1122 flist.sort(functionCompare)
1123 oldfile = ""
1124 for info in flist:
1125 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001126 #
1127 # Do not provide as method the destructors for the class
1128 # to avoid double free
1129 #
1130 if name == destruct:
1131 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001132 if file != oldfile:
1133 if file == "python_accessor":
1134 classes.write(" # accessors for %s\n" % (classname))
1135 txt.write(" # accessors\n")
1136 else:
1137 classes.write(" #\n")
1138 classes.write(" # %s functions from module %s\n" % (
1139 classname, file))
1140 txt.write("\n # functions from module %s\n" % file)
1141 classes.write(" #\n\n")
1142 oldfile = file
1143 classes.write(" def %s(self" % func)
1144 txt.write(" %s()\n" % func);
1145 n = 0
1146 for arg in args:
1147 if n != index:
1148 classes.write(", %s" % arg[0])
1149 n = n + 1
1150 classes.write("):\n")
1151 writeDoc(name, args, ' ', classes);
1152 n = 0
1153 for arg in args:
1154 if classes_type.has_key(arg[1]):
1155 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001156 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001157 (arg[0], arg[0]))
1158 classes.write(" else: %s__o = %s%s\n" %
1159 (arg[0], arg[0], classes_type[arg[1]][0]))
1160 n = n + 1
1161 if ret[0] != "void":
1162 classes.write(" ret = ");
1163 else:
1164 classes.write(" ");
1165 classes.write("libxml2mod.%s(" % name)
1166 n = 0
1167 for arg in args:
1168 if n != 0:
1169 classes.write(", ");
1170 if n != index:
1171 classes.write("%s" % arg[0])
1172 if classes_type.has_key(arg[1]):
1173 classes.write("__o");
1174 else:
1175 classes.write("self");
1176 if classes_type.has_key(arg[1]):
1177 classes.write(classes_type[arg[1]][0])
1178 n = n + 1
1179 classes.write(")\n");
1180 if ret[0] != "void":
1181 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001182 #
1183 # Raise an exception
1184 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001185 if functions_noexcept.has_key(name):
1186 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001187 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001188 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001189 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001190 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001191 % (name))
1192 elif string.find(name, "XPath") >= 0:
1193 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001194 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001195 % (name))
1196 elif string.find(name, "Parse") >= 0:
1197 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001198 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001199 % (name))
1200 else:
1201 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001202 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001203 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001204
1205 #
1206 # generate the returned class wrapper for the object
1207 #
1208 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001209 classes.write(classes_type[ret[0]][1] % ("ret"));
1210 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001211
1212 #
1213 # Sometime one need to keep references of the source
1214 # class in the returned class object.
1215 # See reference_keepers for the list
1216 #
1217 tclass = classes_type[ret[0]][2]
1218 if reference_keepers.has_key(tclass):
1219 list = reference_keepers[tclass]
1220 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001221 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001222 classes.write(" __tmp.%s = self\n" %
1223 pref[1])
1224 #
1225 # return the class
1226 #
1227 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001228 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001229 #
1230 # Raise an exception
1231 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001232 if functions_noexcept.has_key(name):
1233 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001234 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001235 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001236 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001237 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001238 % (name))
1239 elif string.find(name, "XPath") >= 0:
1240 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001241 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001242 % (name))
1243 elif string.find(name, "Parse") >= 0:
1244 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001245 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001246 % (name))
1247 else:
1248 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001249 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001250 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001251 classes.write(" return ");
1252 classes.write(converter_type[ret[0]] % ("ret"));
1253 classes.write("\n");
1254 else:
1255 classes.write(" return ret\n");
1256 classes.write("\n");
1257
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001258 #
1259 # Generate enum constants
1260 #
1261 for type,enum in enums.items():
1262 classes.write("# %s\n" % type)
1263 items = enum.items()
1264 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1265 for name,value in items:
1266 classes.write("%s = %s\n" % (name,value))
1267 classes.write("\n");
1268
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001269 txt.close()
1270 classes.close()
1271
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001272buildStubs()
1273buildWrappers()