blob: 0856d76d77378d00352f434d298afbf980db015a [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 Veillardbb8502c2005-03-30 07:40:35 +0000349 if name == "xmlSchemaFreeValidCtxt":
350 return 1
351
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000352#
353# Those are skipped because the Const version is used of the bindings
354# instead.
355#
356 if name == "xmlTextReaderBaseUri":
357 return 1
358 if name == "xmlTextReaderLocalName":
359 return 1
360 if name == "xmlTextReaderName":
361 return 1
362 if name == "xmlTextReaderNamespaceUri":
363 return 1
364 if name == "xmlTextReaderPrefix":
365 return 1
366 if name == "xmlTextReaderXmlLang":
367 return 1
368 if name == "xmlTextReaderValue":
369 return 1
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000370 if name == "xmlOutputBufferClose": # handled by by the superclass
371 return 1
372 if name == "xmlOutputBufferFlush": # handled by by the superclass
373 return 1
William M. Brackf7eb7942003-12-31 07:59:17 +0000374 if name == "xmlErrMemory":
375 return 1
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000376
377 if name == "xmlValidBuildContentModel":
378 return 1
379 if name == "xmlValidateElementDecl":
380 return 1
381 if name == "xmlValidateAttributeDecl":
382 return 1
383
Daniel Veillard1971ee22002-01-31 20:29:19 +0000384 return 0
385
Daniel Veillard96fe0952002-01-30 20:52:23 +0000386def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000387 global py_types
388 global unknown_types
389 global functions
390 global skipped_modules
391
392 try:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000393 (desc, ret, args, file) = functions[name]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000394 except:
395 print "failed to get function %s infos"
396 return
397
398 if skipped_modules.has_key(file):
399 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000400 if skip_function(name) == 1:
401 return 0
Daniel Veillard263ec862004-10-04 10:26:54 +0000402 if name in skip_impl:
403 # Don't delete the function entry in the caller.
404 return 1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000405
406 c_call = "";
407 format=""
408 format_args=""
409 c_args=""
410 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000411 c_convert=""
William M. Brack106cad62004-12-23 15:56:12 +0000412 num_bufs=0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000413 for arg in args:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000414 # This should be correct
415 if arg[1][0:6] == "const ":
416 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000417 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000418 if py_types.has_key(arg[1]):
419 (f, t, n, c) = py_types[arg[1]]
William M. Brackff349112004-12-24 08:39:13 +0000420 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
William M. Brack106cad62004-12-23 15:56:12 +0000421 f = 't#'
Daniel Veillard01a6d412002-02-11 18:42:20 +0000422 if f != None:
423 format = format + f
424 if t != None:
425 format_args = format_args + ", &pyobj_%s" % (arg[0])
426 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
427 c_convert = c_convert + \
428 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
429 arg[1], t, arg[0]);
430 else:
431 format_args = format_args + ", &%s" % (arg[0])
William M. Brack106cad62004-12-23 15:56:12 +0000432 if f == 't#':
433 format_args = format_args + ", &py_buffsize%d" % num_bufs
434 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
435 num_bufs = num_bufs + 1
Daniel Veillard01a6d412002-02-11 18:42:20 +0000436 if c_call != "":
437 c_call = c_call + ", ";
438 c_call = c_call + "%s" % (arg[0])
439 else:
440 if skipped_types.has_key(arg[1]):
441 return 0
442 if unknown_types.has_key(arg[1]):
443 lst = unknown_types[arg[1]]
444 lst.append(name)
445 else:
446 unknown_types[arg[1]] = [name]
447 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000448 if format != "":
449 format = format + ":%s" % (name)
450
451 if ret[0] == 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000452 if file == "python_accessor":
Daniel Veillard6361da02002-02-23 10:10:33 +0000453 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
454 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
455 args[0][0], args[1][0], args[0][0], args[1][0])
William M. Bracka71a8ef2003-08-06 04:43:55 +0000456 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
457 args[1][0], args[1][1], args[1][0])
Daniel Veillard6361da02002-02-23 10:10:33 +0000458 else:
459 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
460 args[1][0])
Daniel Veillard01a6d412002-02-11 18:42:20 +0000461 else:
462 c_call = "\n %s(%s);\n" % (name, c_call);
463 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000464 elif py_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000465 (f, t, n, c) = py_types[ret[0]]
466 c_return = " %s c_retval;\n" % (ret[0])
467 if file == "python_accessor" and ret[2] != None:
468 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
469 else:
470 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
471 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
472 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000473 elif py_return_types.has_key(ret[0]):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000474 (f, t, n, c) = py_return_types[ret[0]]
475 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard1971ee22002-01-31 20:29:19 +0000476 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard01a6d412002-02-11 18:42:20 +0000477 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
478 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000479 else:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000480 if skipped_types.has_key(ret[0]):
481 return 0
482 if unknown_types.has_key(ret[0]):
483 lst = unknown_types[ret[0]]
484 lst.append(name)
485 else:
486 unknown_types[ret[0]] = [name]
487 return -1
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000488
Daniel Veillard42766c02002-08-22 20:52:17 +0000489 if file == "debugXML":
490 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
491 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
492 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000493 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000494 include.write("#ifdef LIBXML_HTML_ENABLED\n");
495 export.write("#ifdef LIBXML_HTML_ENABLED\n");
496 output.write("#ifdef LIBXML_HTML_ENABLED\n");
497 elif file == "c14n":
498 include.write("#ifdef LIBXML_C14N_ENABLED\n");
499 export.write("#ifdef LIBXML_C14N_ENABLED\n");
500 output.write("#ifdef LIBXML_C14N_ENABLED\n");
501 elif file == "xpathInternals" or file == "xpath":
502 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
503 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
504 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
505 elif file == "xpointer":
506 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
507 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
508 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
509 elif file == "xinclude":
510 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
511 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
512 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000513 elif file == "xmlregexp":
514 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
515 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
516 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000517 elif file == "xmlschemas" or file == "xmlschemastypes" or \
518 file == "relaxng":
519 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
520 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
521 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000522
Daniel Veillard96fe0952002-01-30 20:52:23 +0000523 include.write("PyObject * ")
Daniel Veillardd2379012002-03-15 22:24:56 +0000524 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
Daniel Veillard9589d452002-02-02 10:28:17 +0000525
Daniel Veillardd2379012002-03-15 22:24:56 +0000526 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000527 (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000528
529 if file == "python":
530 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000531 if name[0:4] == "html":
532 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
533 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
534 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000535 return 1
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000536 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000537 # Those have been manually generated
Daniel Veillard656ce942004-04-30 23:11:45 +0000538 if name[0:4] == "html":
539 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
540 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
541 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
Daniel Veillard01a6d412002-02-11 18:42:20 +0000542 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000543
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000544 output.write("PyObject *\n")
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000545 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
546 output.write(" PyObject *args")
Daniel Veillardd2379012002-03-15 22:24:56 +0000547 if format == "":
William M. Brack6bf4d6f2003-11-04 23:29:16 +0000548 output.write(" ATTRIBUTE_UNUSED")
549 output.write(") {\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000550 if ret[0] != 'void':
Daniel Veillard01a6d412002-02-11 18:42:20 +0000551 output.write(" PyObject *py_retval;\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000552 if c_return != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000553 output.write(c_return)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000554 if c_args != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000555 output.write(c_args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000556 if format != "":
Daniel Veillardd2379012002-03-15 22:24:56 +0000557 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
Daniel Veillard01a6d412002-02-11 18:42:20 +0000558 (format, format_args))
559 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000560 if c_convert != "":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000561 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000562
563 output.write(c_call)
564 output.write(ret_convert)
565 output.write("}\n\n")
Daniel Veillard42766c02002-08-22 20:52:17 +0000566 if file == "debugXML":
567 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
568 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
569 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
Daniel Veillard656ce942004-04-30 23:11:45 +0000570 elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html":
Daniel Veillard42766c02002-08-22 20:52:17 +0000571 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
572 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
573 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
574 elif file == "c14n":
575 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
576 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
577 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
578 elif file == "xpathInternals" or file == "xpath":
579 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
580 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
581 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
582 elif file == "xpointer":
583 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
584 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
585 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
586 elif file == "xinclude":
587 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
588 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
589 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000590 elif file == "xmlregexp":
591 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
592 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
593 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
Daniel Veillard71531f32003-02-05 13:19:53 +0000594 elif file == "xmlschemas" or file == "xmlschemastypes" or \
595 file == "relaxng":
596 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
597 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
598 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000599 return 1
600
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000601def buildStubs():
602 global py_types
603 global py_return_types
604 global unknown_types
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000605
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000606 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000607 f = open(os.path.join(srcPref,"libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000608 data = f.read()
609 (parser, target) = getparser()
610 parser.feed(data)
611 parser.close()
612 except IOError, msg:
613 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000614 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000615 data = f.read()
616 (parser, target) = getparser()
617 parser.feed(data)
618 parser.close()
619 except IOError, msg:
620 print file, ":", msg
621 sys.exit(1)
Daniel Veillard9589d452002-02-02 10:28:17 +0000622
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000623 n = len(functions.keys())
624 print "Found %d functions in libxml2-api.xml" % (n)
625
626 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
627 try:
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000628 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000629 data = f.read()
630 (parser, target) = getparser()
631 parser.feed(data)
632 parser.close()
633 except IOError, msg:
634 print file, ":", msg
Daniel Veillard9589d452002-02-02 10:28:17 +0000635
636
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000637 print "Found %d functions in libxml2-python-api.xml" % (
638 len(functions.keys()) - n)
639 nb_wrap = 0
640 failed = 0
641 skipped = 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000642
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000643 include = open("libxml2-py.h", "w")
644 include.write("/* Generated */\n\n")
645 export = open("libxml2-export.c", "w")
646 export.write("/* Generated */\n\n")
647 wrapper = open("libxml2-py.c", "w")
648 wrapper.write("/* Generated */\n\n")
649 wrapper.write("#include <Python.h>\n")
Daniel Veillardd2379012002-03-15 22:24:56 +0000650 wrapper.write("#include <libxml/xmlversion.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000651 wrapper.write("#include <libxml/tree.h>\n")
William M. Bracka71a8ef2003-08-06 04:43:55 +0000652 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000653 wrapper.write("#include \"libxml_wrap.h\"\n")
654 wrapper.write("#include \"libxml2-py.h\"\n\n")
655 for function in functions.keys():
656 ret = print_function_wrapper(function, wrapper, export, include)
657 if ret < 0:
658 failed = failed + 1
659 del functions[function]
660 if ret == 0:
661 skipped = skipped + 1
662 del functions[function]
663 if ret == 1:
664 nb_wrap = nb_wrap + 1
665 include.close()
666 export.close()
667 wrapper.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000668
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000669 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
670 failed, skipped);
671 print "Missing type converters: "
672 for type in unknown_types.keys():
673 print "%s:%d " % (type, len(unknown_types[type])),
674 print
Daniel Veillard36ed5292002-01-30 23:49:06 +0000675
Daniel Veillard1971ee22002-01-31 20:29:19 +0000676#######################################################################
677#
678# This part writes part of the Python front-end classes based on
679# mapping rules between types and classes and also based on function
680# renaming to get consistent function names at the Python level
681#
682#######################################################################
683
684#
685# The type automatically remapped to generated classes
686#
687classes_type = {
688 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
689 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
690 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
691 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
692 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
693 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
694 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
695 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
696 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
697 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
698 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
699 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
700 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
701 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
702 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
703 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
704 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
705 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
706 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000707 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
708 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
709 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000710 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
711 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard3cd72402002-05-13 10:33:30 +0000712 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
713 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000714 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
Daniel Veillard7db38712002-02-07 16:39:11 +0000715 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
Daniel Veillard6361da02002-02-23 10:10:33 +0000716 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
Daniel Veillard46da4642004-01-06 22:54:57 +0000717 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000718 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
719 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000720 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
Daniel Veillard417be3a2003-01-20 21:26:34 +0000721 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000722 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
Daniel Veillard591b4be2003-02-09 23:33:36 +0000723 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
724 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
725 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
Daniel Veillard259f0df2004-08-18 09:13:18 +0000726 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
727 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
728 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000729}
730
731converter_type = {
732 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
733}
734
735primary_classes = ["xmlNode", "xmlDoc"]
736
737classes_ancestor = {
738 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000739 "xmlDtd" : "xmlNode",
740 "xmlDoc" : "xmlNode",
741 "xmlAttr" : "xmlNode",
742 "xmlNs" : "xmlNode",
743 "xmlEntity" : "xmlNode",
744 "xmlElement" : "xmlNode",
745 "xmlAttribute" : "xmlNode",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000746 "outputBuffer": "ioWriteWrapper",
747 "inputBuffer": "ioReadWrapper",
Daniel Veillarde6227e02003-01-14 11:42:39 +0000748 "parserCtxt": "parserCtxtCore",
Daniel Veillard26f70262003-01-16 22:45:08 +0000749 "xmlTextReader": "xmlTextReaderCore",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000750}
751classes_destructors = {
Daniel Veillard3ce52572002-02-03 15:08:05 +0000752 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard7db38712002-02-07 16:39:11 +0000753 "catalog": "xmlFreeCatalog",
Daniel Veillard6361da02002-02-23 10:10:33 +0000754 "URI": "xmlFreeURI",
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000755# "outputBuffer": "xmlOutputBufferClose",
756 "inputBuffer": "xmlFreeParserInputBuffer",
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000757 "xmlReg": "xmlRegFreeRegexp",
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000758 "xmlTextReader": "xmlFreeTextReader",
Daniel Veillard591b4be2003-02-09 23:33:36 +0000759 "relaxNgSchema": "xmlRelaxNGFree",
760 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
761 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
Daniel Veillard259f0df2004-08-18 09:13:18 +0000762 "Schema": "xmlSchemaFree",
763 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
764 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
Daniel Veillard850ce9b2004-11-10 11:55:47 +0000765 "ValidCtxt": "xmlFreeValidCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000766}
767
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000768functions_noexcept = {
769 "xmlHasProp": 1,
770 "xmlHasNsProp": 1,
Daniel Veillard3b87b6b2003-01-10 15:21:50 +0000771 "xmlDocSetRootElement": 1,
William M. Brackdbbcf8e2004-12-17 22:50:53 +0000772 "xmlNodeGetNs": 1,
773 "xmlNodeGetNsDefs": 1,
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000774}
775
Daniel Veillarddc85f282002-12-31 11:18:37 +0000776reference_keepers = {
777 "xmlTextReader": [('inputBuffer', 'input')],
Daniel Veillard591b4be2003-02-09 23:33:36 +0000778 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
Daniel Veillard259f0df2004-08-18 09:13:18 +0000779 "SchemaValidCtxt": [('Schema', 'schema')],
Daniel Veillarddc85f282002-12-31 11:18:37 +0000780}
781
Daniel Veillard36ed5292002-01-30 23:49:06 +0000782function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000783
784function_classes["None"] = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000785
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000786def nameFixup(name, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000787 listname = classe + "List"
788 ll = len(listname)
789 l = len(classe)
790 if name[0:l] == listname:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000791 func = name[l:]
792 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000793 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
794 func = name[12:]
795 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000796 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
797 func = name[12:]
798 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000799 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
800 func = name[10:]
801 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard6361da02002-02-23 10:10:33 +0000802 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
803 func = name[9:]
804 func = string.lower(func[0:1]) + func[1:]
805 elif name[0:9] == "xmlURISet" and file == "python_accessor":
806 func = name[6:]
807 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard46da4642004-01-06 22:54:57 +0000808 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
809 func = name[11:]
810 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc575b992002-02-08 13:28:40 +0000811 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
812 func = name[17:]
813 func = string.lower(func[0:1]) + func[1:]
814 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
815 func = name[11:]
816 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000817 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
818 func = name[8:]
819 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000820 elif name[0:15] == "xmlOutputBuffer" and file != "python":
821 func = name[15:]
822 func = string.lower(func[0:1]) + func[1:]
823 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
824 func = name[20:]
825 func = string.lower(func[0:1]) + func[1:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000826 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000827 func = "regexp" + name[9:]
Daniel Veillardd4cb1e82002-09-26 09:34:23 +0000828 elif name[0:6] == "xmlReg" and file == "xmlregexp":
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000829 func = "regexp" + name[6:]
Daniel Veillard417be3a2003-01-20 21:26:34 +0000830 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
831 func = name[20:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000832 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
833 func = name[18:]
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000834 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
835 func = name[13:]
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000836 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
837 func = name[9:]
Daniel Veillard7db38712002-02-07 16:39:11 +0000838 elif name[0:11] == "xmlACatalog":
839 func = name[11:]
840 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000841 elif name[0:l] == classe:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000842 func = name[l:]
843 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000844 elif name[0:7] == "libxml_":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000845 func = name[7:]
846 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000847 elif name[0:6] == "xmlGet":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000848 func = name[6:]
849 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000850 elif name[0:3] == "xml":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000851 func = name[3:]
852 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000853 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000854 func = name
855 if func[0:5] == "xPath":
856 func = "xpath" + func[5:]
857 elif func[0:4] == "xPtr":
858 func = "xpointer" + func[4:]
859 elif func[0:8] == "xInclude":
860 func = "xinclude" + func[8:]
861 elif func[0:2] == "iD":
862 func = "ID" + func[2:]
863 elif func[0:3] == "uRI":
864 func = "URI" + func[3:]
865 elif func[0:4] == "uTF8":
866 func = "UTF8" + func[4:]
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000867 elif func[0:3] == 'sAX':
868 func = "SAX" + func[3:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000869 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000870
Daniel Veillard36ed5292002-01-30 23:49:06 +0000871
Daniel Veillard1971ee22002-01-31 20:29:19 +0000872def functionCompare(info1, info2):
873 (index1, func1, name1, ret1, args1, file1) = info1
874 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000875 if file1 == file2:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000876 if func1 < func2:
877 return -1
878 if func1 > func2:
879 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000880 if file1 == "python_accessor":
881 return -1
882 if file2 == "python_accessor":
883 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000884 if file1 < file2:
885 return -1
886 if file1 > file2:
887 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000888 return 0
889
890def writeDoc(name, args, indent, output):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000891 if functions[name][0] is None or functions[name][0] == "":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000892 return
893 val = functions[name][0]
894 val = string.replace(val, "NULL", "None");
895 output.write(indent)
896 output.write('"""')
897 while len(val) > 60:
898 str = val[0:60]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000899 i = string.rfind(str, " ");
900 if i < 0:
901 i = 60
Daniel Veillard1971ee22002-01-31 20:29:19 +0000902 str = val[0:i]
Daniel Veillard01a6d412002-02-11 18:42:20 +0000903 val = val[i:]
904 output.write(str)
905 output.write('\n ');
906 output.write(indent)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000907 output.write(val);
Daniel Veillardd076a202002-11-20 13:28:31 +0000908 output.write(' """\n')
Daniel Veillard1971ee22002-01-31 20:29:19 +0000909
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000910def buildWrappers():
911 global ctypes
912 global py_types
913 global py_return_types
914 global unknown_types
915 global functions
916 global function_classes
917 global classes_type
918 global classes_list
919 global converter_type
920 global primary_classes
921 global converter_type
922 global classes_ancestor
923 global converter_type
924 global primary_classes
925 global classes_ancestor
926 global classes_destructors
Daniel Veillardef6c46f2002-03-07 22:21:56 +0000927 global functions_noexcept
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000928
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000929 for type in classes_type.keys():
930 function_classes[classes_type[type][2]] = []
Daniel Veillard36ed5292002-01-30 23:49:06 +0000931
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000932 #
933 # Build the list of C types to look for ordered to start
934 # with primary classes
935 #
936 ctypes = []
937 classes_list = []
938 ctypes_processed = {}
939 classes_processed = {}
940 for classe in primary_classes:
941 classes_list.append(classe)
942 classes_processed[classe] = ()
943 for type in classes_type.keys():
944 tinfo = classes_type[type]
945 if tinfo[2] == classe:
946 ctypes.append(type)
947 ctypes_processed[type] = ()
948 for type in classes_type.keys():
949 if ctypes_processed.has_key(type):
950 continue
951 tinfo = classes_type[type]
952 if not classes_processed.has_key(tinfo[2]):
953 classes_list.append(tinfo[2])
954 classes_processed[tinfo[2]] = ()
955
956 ctypes.append(type)
957 ctypes_processed[type] = ()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000958
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000959 for name in functions.keys():
960 found = 0;
961 (desc, ret, args, file) = functions[name]
962 for type in ctypes:
963 classe = classes_type[type][2]
964
965 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
966 found = 1
967 func = nameFixup(name, classe, type, file)
968 info = (0, func, name, ret, args, file)
969 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000970 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
971 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000972 found = 1
973 func = nameFixup(name, classe, type, file)
974 info = (1, func, name, ret, args, file)
975 function_classes[classe].append(info)
976 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
977 found = 1
978 func = nameFixup(name, classe, type, file)
979 info = (0, func, name, ret, args, file)
980 function_classes[classe].append(info)
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000981 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
982 and file != "python_accessor":
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000983 found = 1
984 func = nameFixup(name, classe, type, file)
985 info = (1, func, name, ret, args, file)
986 function_classes[classe].append(info)
Daniel Veillard0fea6f42002-02-22 22:51:13 +0000987 if found == 1:
988 continue
989 if name[0:8] == "xmlXPath":
990 continue
991 if name[0:6] == "xmlStr":
992 continue
993 if name[0:10] == "xmlCharStr":
994 continue
995 func = nameFixup(name, "None", file, file)
996 info = (0, func, name, ret, args, file)
997 function_classes['None'].append(info)
998
999 classes = open("libxml2class.py", "w")
1000 txt = open("libxml2class.txt", "w")
1001 txt.write(" Generated Classes for libxml2-python\n\n")
1002
1003 txt.write("#\n# Global functions of the module\n#\n\n")
1004 if function_classes.has_key("None"):
1005 flist = function_classes["None"]
1006 flist.sort(functionCompare)
1007 oldfile = ""
1008 for info in flist:
1009 (index, func, name, ret, args, file) = info
1010 if file != oldfile:
1011 classes.write("#\n# Functions from module %s\n#\n\n" % file)
1012 txt.write("\n# functions from module %s\n" % file)
1013 oldfile = file
1014 classes.write("def %s(" % func)
1015 txt.write("%s()\n" % func);
1016 n = 0
1017 for arg in args:
1018 if n != 0:
1019 classes.write(", ")
1020 classes.write("%s" % arg[0])
1021 n = n + 1
1022 classes.write("):\n")
1023 writeDoc(name, args, ' ', classes);
1024
1025 for arg in args:
1026 if classes_type.has_key(arg[1]):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001027 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001028 (arg[0], arg[0]))
1029 classes.write(" else: %s__o = %s%s\n" %
1030 (arg[0], arg[0], classes_type[arg[1]][0]))
1031 if ret[0] != "void":
1032 classes.write(" ret = ");
1033 else:
1034 classes.write(" ");
1035 classes.write("libxml2mod.%s(" % name)
1036 n = 0
1037 for arg in args:
1038 if n != 0:
1039 classes.write(", ");
1040 classes.write("%s" % arg[0])
1041 if classes_type.has_key(arg[1]):
1042 classes.write("__o");
1043 n = n + 1
1044 classes.write(")\n");
1045 if ret[0] != "void":
1046 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001047 #
1048 # Raise an exception
1049 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001050 if functions_noexcept.has_key(name):
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001051 classes.write(" if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001052 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001053 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001054 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001055 % (name))
1056 elif string.find(name, "XPath") >= 0:
1057 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001058 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001059 % (name))
1060 elif string.find(name, "Parse") >= 0:
1061 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001062 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001063 % (name))
1064 else:
1065 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001066 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001067 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001068 classes.write(" return ");
1069 classes.write(classes_type[ret[0]][1] % ("ret"));
1070 classes.write("\n");
1071 else:
1072 classes.write(" return ret\n");
1073 classes.write("\n");
1074
1075 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
1076 for classname in classes_list:
1077 if classname == "None":
1078 pass
1079 else:
1080 if classes_ancestor.has_key(classname):
1081 txt.write("\n\nClass %s(%s)\n" % (classname,
1082 classes_ancestor[classname]))
1083 classes.write("class %s(%s):\n" % (classname,
1084 classes_ancestor[classname]))
1085 classes.write(" def __init__(self, _obj=None):\n")
William M. Brackc68d78d2004-07-16 10:39:30 +00001086 if classes_ancestor[classname] == "xmlCore" or \
1087 classes_ancestor[classname] == "xmlNode":
1088 classes.write(" if type(_obj).__name__ != ")
1089 classes.write("'PyCObject':\n")
1090 classes.write(" raise TypeError, ")
1091 classes.write("'%s needs a PyCObject argument'\n" % \
1092 classname)
Daniel Veillarddc85f282002-12-31 11:18:37 +00001093 if reference_keepers.has_key(classname):
1094 rlist = reference_keepers[classname]
1095 for ref in rlist:
1096 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00001097 classes.write(" self._o = _obj\n")
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001098 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1099 classes_ancestor[classname]))
1100 if classes_ancestor[classname] == "xmlCore" or \
1101 classes_ancestor[classname] == "xmlNode":
1102 classes.write(" def __repr__(self):\n")
Daniel Veillardba5e18a2002-03-05 09:36:43 +00001103 format = "<%s (%%s) object at 0x%%x>" % (classname)
1104 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001105 format))
1106 else:
1107 txt.write("Class %s()\n" % (classname))
1108 classes.write("class %s:\n" % (classname))
1109 classes.write(" def __init__(self, _obj=None):\n")
Daniel Veillarddc85f282002-12-31 11:18:37 +00001110 if reference_keepers.has_key(classname):
1111 list = reference_keepers[classname]
1112 for ref in list:
1113 classes.write(" self.%s = None\n" % ref[1])
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001114 classes.write(" if _obj != None:self._o = _obj;return\n")
1115 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001116 destruct=None
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001117 if classes_destructors.has_key(classname):
1118 classes.write(" def __del__(self):\n")
1119 classes.write(" if self._o != None:\n")
1120 classes.write(" libxml2mod.%s(self._o)\n" %
1121 classes_destructors[classname]);
1122 classes.write(" self._o = None\n\n");
Daniel Veillardd69cc812004-07-01 09:36:26 +00001123 destruct=classes_destructors[classname]
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001124 flist = function_classes[classname]
1125 flist.sort(functionCompare)
1126 oldfile = ""
1127 for info in flist:
1128 (index, func, name, ret, args, file) = info
Daniel Veillardd69cc812004-07-01 09:36:26 +00001129 #
1130 # Do not provide as method the destructors for the class
1131 # to avoid double free
1132 #
1133 if name == destruct:
1134 continue;
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001135 if file != oldfile:
1136 if file == "python_accessor":
1137 classes.write(" # accessors for %s\n" % (classname))
1138 txt.write(" # accessors\n")
1139 else:
1140 classes.write(" #\n")
1141 classes.write(" # %s functions from module %s\n" % (
1142 classname, file))
1143 txt.write("\n # functions from module %s\n" % file)
1144 classes.write(" #\n\n")
1145 oldfile = file
1146 classes.write(" def %s(self" % func)
1147 txt.write(" %s()\n" % func);
1148 n = 0
1149 for arg in args:
1150 if n != index:
1151 classes.write(", %s" % arg[0])
1152 n = n + 1
1153 classes.write("):\n")
1154 writeDoc(name, args, ' ', classes);
1155 n = 0
1156 for arg in args:
1157 if classes_type.has_key(arg[1]):
1158 if n != index:
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001159 classes.write(" if %s is None: %s__o = None\n" %
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001160 (arg[0], arg[0]))
1161 classes.write(" else: %s__o = %s%s\n" %
1162 (arg[0], arg[0], classes_type[arg[1]][0]))
1163 n = n + 1
1164 if ret[0] != "void":
1165 classes.write(" ret = ");
1166 else:
1167 classes.write(" ");
1168 classes.write("libxml2mod.%s(" % name)
1169 n = 0
1170 for arg in args:
1171 if n != 0:
1172 classes.write(", ");
1173 if n != index:
1174 classes.write("%s" % arg[0])
1175 if classes_type.has_key(arg[1]):
1176 classes.write("__o");
1177 else:
1178 classes.write("self");
1179 if classes_type.has_key(arg[1]):
1180 classes.write(classes_type[arg[1]][0])
1181 n = n + 1
1182 classes.write(")\n");
1183 if ret[0] != "void":
1184 if classes_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001185 #
1186 # Raise an exception
1187 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001188 if functions_noexcept.has_key(name):
1189 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001190 " if ret is None:return None\n");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001191 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001192 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001193 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001194 % (name))
1195 elif string.find(name, "XPath") >= 0:
1196 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001197 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001198 % (name))
1199 elif string.find(name, "Parse") >= 0:
1200 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001201 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001202 % (name))
1203 else:
1204 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001205 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001206 % (name))
Daniel Veillarddc85f282002-12-31 11:18:37 +00001207
1208 #
1209 # generate the returned class wrapper for the object
1210 #
1211 classes.write(" __tmp = ");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001212 classes.write(classes_type[ret[0]][1] % ("ret"));
1213 classes.write("\n");
Daniel Veillarddc85f282002-12-31 11:18:37 +00001214
1215 #
1216 # Sometime one need to keep references of the source
1217 # class in the returned class object.
1218 # See reference_keepers for the list
1219 #
1220 tclass = classes_type[ret[0]][2]
1221 if reference_keepers.has_key(tclass):
1222 list = reference_keepers[tclass]
1223 for pref in list:
Daniel Veillardfebcca42003-02-16 15:44:18 +00001224 if pref[0] == classname:
Daniel Veillarddc85f282002-12-31 11:18:37 +00001225 classes.write(" __tmp.%s = self\n" %
1226 pref[1])
1227 #
1228 # return the class
1229 #
1230 classes.write(" return __tmp\n");
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001231 elif converter_type.has_key(ret[0]):
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001232 #
1233 # Raise an exception
1234 #
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001235 if functions_noexcept.has_key(name):
1236 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001237 " if ret is None:return None");
Daniel Veillardef6c46f2002-03-07 22:21:56 +00001238 elif string.find(name, "URI") >= 0:
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001239 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001240 " if ret is None:raise uriError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001241 % (name))
1242 elif string.find(name, "XPath") >= 0:
1243 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001244 " if ret is None:raise xpathError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001245 % (name))
1246 elif string.find(name, "Parse") >= 0:
1247 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001248 " if ret is None:raise parserError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001249 % (name))
1250 else:
1251 classes.write(
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00001252 " if ret is None:raise treeError('%s() failed')\n"
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001253 % (name))
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001254 classes.write(" return ");
1255 classes.write(converter_type[ret[0]] % ("ret"));
1256 classes.write("\n");
1257 else:
1258 classes.write(" return ret\n");
1259 classes.write("\n");
1260
Daniel Veillard4f4a27f2004-01-14 23:50:34 +00001261 #
1262 # Generate enum constants
1263 #
1264 for type,enum in enums.items():
1265 classes.write("# %s\n" % type)
1266 items = enum.items()
1267 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1268 for name,value in items:
1269 classes.write("%s = %s\n" % (name,value))
1270 classes.write("\n");
1271
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001272 txt.close()
1273 classes.close()
1274
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001275buildStubs()
1276buildWrappers()