blob: 3d28200422c6f82e820c01c9e64aaf95e6c50063 [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 = {}
7
Daniel Veillard36ed5292002-01-30 23:49:06 +00008import string
Daniel Veillard1971ee22002-01-31 20:29:19 +00009
10#######################################################################
11#
12# That part if purely the API acquisition phase from the
13# XML API description
14#
15#######################################################################
16import os
Daniel Veillardd2897fd2002-01-30 16:37:32 +000017import xmllib
18try:
19 import sgmlop
20except ImportError:
21 sgmlop = None # accelerator not available
22
23debug = 0
24
25if sgmlop:
26 class FastParser:
27 """sgmlop based XML parser. this is typically 15x faster
28 than SlowParser..."""
29
30 def __init__(self, target):
31
32 # setup callbacks
33 self.finish_starttag = target.start
34 self.finish_endtag = target.end
35 self.handle_data = target.data
36
37 # activate parser
38 self.parser = sgmlop.XMLParser()
39 self.parser.register(self)
40 self.feed = self.parser.feed
41 self.entity = {
42 "amp": "&", "gt": ">", "lt": "<",
43 "apos": "'", "quot": '"'
44 }
45
46 def close(self):
47 try:
48 self.parser.close()
49 finally:
50 self.parser = self.feed = None # nuke circular reference
51
52 def handle_entityref(self, entity):
53 # <string> entity
54 try:
55 self.handle_data(self.entity[entity])
56 except KeyError:
57 self.handle_data("&%s;" % entity)
58
59else:
60 FastParser = None
61
62
63class SlowParser(xmllib.XMLParser):
64 """slow but safe standard parser, based on the XML parser in
65 Python's standard library."""
66
67 def __init__(self, target):
68 self.unknown_starttag = target.start
69 self.handle_data = target.data
70 self.unknown_endtag = target.end
71 xmllib.XMLParser.__init__(self)
72
73def getparser(target = None):
74 # get the fastest available parser, and attach it to an
75 # unmarshalling object. return both objects.
76 if target == None:
77 target = docParser()
78 if FastParser:
79 return FastParser(target), target
80 return SlowParser(target), target
81
82class docParser:
83 def __init__(self):
84 self._methodname = None
85 self._data = []
86 self.in_function = 0
87
88 def close(self):
89 if debug:
90 print "close"
91
92 def getmethodname(self):
93 return self._methodname
94
95 def data(self, text):
96 if debug:
97 print "data %s" % text
98 self._data.append(text)
99
100 def start(self, tag, attrs):
101 if debug:
102 print "start %s, %s" % (tag, attrs)
103 if tag == 'function':
104 self._data = []
105 self.in_function = 1
106 self.function = None
107 self.function_args = []
108 self.function_descr = None
109 self.function_return = None
110 self.function_file = None
111 if attrs.has_key('name'):
112 self.function = attrs['name']
113 if attrs.has_key('file'):
114 self.function_file = attrs['file']
115 elif tag == 'info':
116 self._data = []
117 elif tag == 'arg':
118 if self.in_function == 1:
119 self.function_arg_name = None
120 self.function_arg_type = None
121 self.function_arg_info = None
122 if attrs.has_key('name'):
123 self.function_arg_name = attrs['name']
124 if attrs.has_key('type'):
125 self.function_arg_type = attrs['type']
126 if attrs.has_key('info'):
127 self.function_arg_info = attrs['info']
128 elif tag == 'return':
129 if self.in_function == 1:
130 self.function_return_type = None
131 self.function_return_info = None
Daniel Veillard3ce52572002-02-03 15:08:05 +0000132 self.function_return_field = None
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000133 if attrs.has_key('type'):
134 self.function_return_type = attrs['type']
135 if attrs.has_key('info'):
136 self.function_return_info = attrs['info']
Daniel Veillard3ce52572002-02-03 15:08:05 +0000137 if attrs.has_key('field'):
138 self.function_return_field = attrs['field']
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000139
140
141 def end(self, tag):
142 if debug:
143 print "end %s" % tag
144 if tag == 'function':
145 if self.function != None:
146 function(self.function, self.function_descr,
147 self.function_return, self.function_args,
148 self.function_file)
149 self.in_function = 0
150 elif tag == 'arg':
151 if self.in_function == 1:
152 self.function_args.append([self.function_arg_name,
153 self.function_arg_type,
154 self.function_arg_info])
155 elif tag == 'return':
156 if self.in_function == 1:
157 self.function_return = [self.function_return_type,
Daniel Veillard3ce52572002-02-03 15:08:05 +0000158 self.function_return_info,
159 self.function_return_field]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000160 elif tag == 'info':
161 str = ''
162 for c in self._data:
163 str = str + c
164 if self.in_function == 1:
165 self.function_descr = str
166
167
168def function(name, desc, ret, args, file):
169 global functions
170
171 functions[name] = (desc, ret, args, file)
172
Daniel Veillard1971ee22002-01-31 20:29:19 +0000173#######################################################################
174#
175# Some filtering rukes to drop functions/types which should not
176# be exposed as-is on the Python interface
177#
178#######################################################################
Daniel Veillard36ed5292002-01-30 23:49:06 +0000179
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000180skipped_modules = {
181 'xmlmemory': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000182 'DOCBparser': None,
183 'SAX': None,
184 'hash': None,
185 'list': None,
186 'threads': None,
Daniel Veillard1971ee22002-01-31 20:29:19 +0000187 'xpointer': None,
Daniel Veillard96fe0952002-01-30 20:52:23 +0000188}
189skipped_types = {
190 'int *': "usually a return type",
191 'xmlSAXHandlerPtr': "not the proper interface for SAX",
192 'htmlSAXHandlerPtr': "not the proper interface for SAX",
Daniel Veillard96fe0952002-01-30 20:52:23 +0000193 'xmlRMutexPtr': "thread specific, skipped",
194 'xmlMutexPtr': "thread specific, skipped",
195 'xmlGlobalStatePtr': "thread specific, skipped",
196 'xmlListPtr': "internal representation not suitable for python",
197 'xmlBufferPtr': "internal representation not suitable for python",
198 'FILE *': None,
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000199}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000200
201#######################################################################
202#
203# Table of remapping to/from the python type or class to the C
204# counterpart.
205#
206#######################################################################
207
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000208py_types = {
Daniel Veillard96fe0952002-01-30 20:52:23 +0000209 'void': (None, None, None, None),
210 'int': ('i', None, "int", "int"),
211 'long': ('i', None, "int", "int"),
212 'double': ('d', None, "double", "double"),
213 'unsigned int': ('i', None, "int", "int"),
214 'xmlChar': ('c', None, "int", "int"),
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000215 'unsigned char *': ('z', None, "charPtr", "char *"),
216 'char *': ('z', None, "charPtr", "char *"),
217 'const char *': ('z', None, "charPtr", "char *"),
218 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
219 'const xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000220 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
221 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
222 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
223 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
224 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
225 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
226 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000236 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
237 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
238 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
239 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
240 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
241 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
242 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
243 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
244 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
245 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
246 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
247 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
Daniel Veillard96fe0952002-01-30 20:52:23 +0000248 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
249 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
250 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
251 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
252 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
253 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
254 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
257 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
258 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
259 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000260 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
261 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000262 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
263 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
264 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
265 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000266}
267
268py_return_types = {
269 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000270}
271
272unknown_types = {}
273
Daniel Veillard1971ee22002-01-31 20:29:19 +0000274#######################################################################
275#
276# This part writes the C <-> Python stubs libxml2-py.[ch] and
277# the table libxml2-export.c to add when registrering the Python module
278#
279#######################################################################
280
281def skip_function(name):
282 if name[0:12] == "xmlXPathWrap":
283 return 1
284# if name[0:11] == "xmlXPathNew":
285# return 1
286 return 0
287
Daniel Veillard96fe0952002-01-30 20:52:23 +0000288def print_function_wrapper(name, output, export, include):
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000289 global py_types
290 global unknown_types
291 global functions
292 global skipped_modules
293
294 try:
295 (desc, ret, args, file) = functions[name]
296 except:
297 print "failed to get function %s infos"
298 return
299
300 if skipped_modules.has_key(file):
301 return 0
Daniel Veillard1971ee22002-01-31 20:29:19 +0000302 if skip_function(name) == 1:
303 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000304
305 c_call = "";
306 format=""
307 format_args=""
308 c_args=""
309 c_return=""
Daniel Veillard96fe0952002-01-30 20:52:23 +0000310 c_convert=""
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000311 for arg in args:
Daniel Veillard96fe0952002-01-30 20:52:23 +0000312 # This should be correct
313 if arg[1][0:6] == "const ":
314 arg[1] = arg[1][6:]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000315 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
316 if py_types.has_key(arg[1]):
Daniel Veillard96fe0952002-01-30 20:52:23 +0000317 (f, t, n, c) = py_types[arg[1]]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000318 if f != None:
319 format = format + f
320 if t != None:
Daniel Veillard96fe0952002-01-30 20:52:23 +0000321 format_args = format_args + ", &pyobj_%s" % (arg[0])
322 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
323 c_convert = c_convert + \
324 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
325 arg[1], t, arg[0]);
326 else:
327 format_args = format_args + ", &%s" % (arg[0])
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000328 if c_call != "":
329 c_call = c_call + ", ";
330 c_call = c_call + "%s" % (arg[0])
331 else:
Daniel Veillard96fe0952002-01-30 20:52:23 +0000332 if skipped_types.has_key(arg[1]):
333 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000334 if unknown_types.has_key(arg[1]):
335 lst = unknown_types[arg[1]]
336 lst.append(name)
337 else:
338 unknown_types[arg[1]] = [name]
339 return -1
340 if format != "":
341 format = format + ":%s" % (name)
342
343 if ret[0] == 'void':
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000344 if file == "python_accessor":
345 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
346 args[1][0])
347 else:
348 c_call = "\n %s(%s);\n" % (name, c_call);
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000349 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
350 elif py_types.has_key(ret[0]):
Daniel Veillard96fe0952002-01-30 20:52:23 +0000351 (f, t, n, c) = py_types[ret[0]]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000352 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard3ce52572002-02-03 15:08:05 +0000353 if file == "python_accessor":
354 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
355 else:
356 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard96fe0952002-01-30 20:52:23 +0000357 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
358 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000359 elif py_return_types.has_key(ret[0]):
360 (f, t, n, c) = py_return_types[ret[0]]
361 c_return = " %s c_retval;\n" % (ret[0])
362 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
363 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
364 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000365 else:
Daniel Veillard96fe0952002-01-30 20:52:23 +0000366 if skipped_types.has_key(ret[0]):
367 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000368 if unknown_types.has_key(ret[0]):
369 lst = unknown_types[ret[0]]
370 lst.append(name)
371 else:
372 unknown_types[ret[0]] = [name]
373 return -1
374
Daniel Veillard96fe0952002-01-30 20:52:23 +0000375 include.write("PyObject * ")
376 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000377
Daniel Veillard96fe0952002-01-30 20:52:23 +0000378 export.write(" { \"%s\", libxml_%s, METH_VARARGS },\n" % (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000379
380 if file == "python":
381 # Those have been manually generated
382 return 1
383
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000384 output.write("PyObject *\n")
385 output.write("libxml_%s(PyObject *self, PyObject *args) {\n" % (name))
386 if ret[0] != 'void':
387 output.write(" PyObject *py_retval;\n")
388 if c_return != "":
389 output.write(c_return)
390 if c_args != "":
391 output.write(c_args)
392 if format != "":
393 output.write("\n if (!PyArg_ParseTuple(args, \"%s\"%s))\n" %
394 (format, format_args))
395 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000396 if c_convert != "":
397 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000398
399 output.write(c_call)
400 output.write(ret_convert)
401 output.write("}\n\n")
402 return 1
403
404try:
Daniel Veillard63276972002-01-31 23:49:05 +0000405 f = open("../doc/libxml2-api.xml")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000406 data = f.read()
407 (parser, target) = getparser()
408 parser.feed(data)
409 parser.close()
410except IOError, msg:
411 print file, ":", msg
412
Daniel Veillard9589d452002-02-02 10:28:17 +0000413n = len(functions.keys())
414print "Found %d functions in libxml2-api.xml" % (n)
415
416py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
417try:
418 f = open("libxml2-python-api.xml")
419 data = f.read()
420 (parser, target) = getparser()
421 parser.feed(data)
422 parser.close()
423except IOError, msg:
424 print file, ":", msg
425
426
427print "Found %d functions in libxml2-python-api.xml" % (
428 len(functions.keys()) - n)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000429nb_wrap = 0
430failed = 0
431skipped = 0
432
Daniel Veillard96fe0952002-01-30 20:52:23 +0000433include = open("libxml2-py.h", "w")
434include.write("/* Generated */\n\n")
435export = open("libxml2-export.c", "w")
436export.write("/* Generated */\n\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000437wrapper = open("libxml2-py.c", "w")
438wrapper.write("/* Generated */\n\n")
439wrapper.write("#include <Python.h>\n")
440wrapper.write("#include <libxml/tree.h>\n")
441wrapper.write("#include \"libxml_wrap.h\"\n")
442wrapper.write("#include \"libxml2-py.h\"\n\n")
443for function in functions.keys():
Daniel Veillard96fe0952002-01-30 20:52:23 +0000444 ret = print_function_wrapper(function, wrapper, export, include)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000445 if ret < 0:
446 failed = failed + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000447 del functions[function]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000448 if ret == 0:
449 skipped = skipped + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000450 del functions[function]
451 if ret == 1:
452 nb_wrap = nb_wrap + 1
Daniel Veillard96fe0952002-01-30 20:52:23 +0000453include.close()
454export.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000455wrapper.close()
456
457print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
458 failed, skipped);
459print "Missing type converters: %s" % (unknown_types.keys())
Daniel Veillard36ed5292002-01-30 23:49:06 +0000460
Daniel Veillard1971ee22002-01-31 20:29:19 +0000461#######################################################################
462#
463# This part writes part of the Python front-end classes based on
464# mapping rules between types and classes and also based on function
465# renaming to get consistent function names at the Python level
466#
467#######################################################################
468
469#
470# The type automatically remapped to generated classes
471#
472classes_type = {
473 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
474 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
475 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
476 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
477 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
478 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
479 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
480 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
481 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
482 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
483 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
484 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
485 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
486 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
487 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
488 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
489 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
490 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
491 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000492 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
493 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000494}
495
496converter_type = {
497 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
498}
499
500primary_classes = ["xmlNode", "xmlDoc"]
501
502classes_ancestor = {
503 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000504 "xmlDtd" : "xmlNode",
505 "xmlDoc" : "xmlNode",
506 "xmlAttr" : "xmlNode",
507 "xmlNs" : "xmlNode",
508 "xmlEntity" : "xmlNode",
509 "xmlElement" : "xmlNode",
510 "xmlAttribute" : "xmlNode",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000511}
512classes_destructors = {
513 "xpathContext": "xmlXPathFreeContext",
Daniel Veillard3ce52572002-02-03 15:08:05 +0000514 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000515}
516
Daniel Veillard36ed5292002-01-30 23:49:06 +0000517function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000518
519function_classes["None"] = []
520for type in classes_type.keys():
521 function_classes[classes_type[type][2]] = []
522
523#
524# Build the list of C types to look for ordered to start with primary classes
525#
526ctypes = []
Daniel Veillard9589d452002-02-02 10:28:17 +0000527classes_list = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000528ctypes_processed = {}
Daniel Veillard9589d452002-02-02 10:28:17 +0000529classes_processed = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000530for classe in primary_classes:
Daniel Veillard9589d452002-02-02 10:28:17 +0000531 classes_list.append(classe)
532 classes_processed[classe] = ()
Daniel Veillard1971ee22002-01-31 20:29:19 +0000533 for type in classes_type.keys():
534 tinfo = classes_type[type]
535 if tinfo[2] == classe:
536 ctypes.append(type)
537 ctypes_processed[type] = ()
538for type in classes_type.keys():
539 if ctypes_processed.has_key(type):
540 continue
541 tinfo = classes_type[type]
Daniel Veillard9589d452002-02-02 10:28:17 +0000542 if not classes_processed.has_key(tinfo[2]):
543 classes_list.append(tinfo[2])
544 classes_processed[tinfo[2]] = ()
545
Daniel Veillard1971ee22002-01-31 20:29:19 +0000546 ctypes.append(type)
547 ctypes_processed[type] = ()
548
Daniel Veillard3ce52572002-02-03 15:08:05 +0000549def nameFixup(function, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000550 listname = classe + "List"
551 ll = len(listname)
552 l = len(classe)
553 if name[0:l] == listname:
554 func = name[l:]
555 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000556 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
557 func = name[12:]
558 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000559 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
560 func = name[12:]
561 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000562 elif name[0:l] == classe:
563 func = name[l:]
564 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000565 elif name[0:7] == "libxml_":
566 func = name[7:]
567 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000568 elif name[0:6] == "xmlGet":
569 func = name[6:]
570 func = string.lower(func[0:1]) + func[1:]
571 elif name[0:3] == "xml":
Daniel Veillard36ed5292002-01-30 23:49:06 +0000572 func = name[3:]
573 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000574 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000575 func = name
576 if func[0:5] == "xPath":
577 func = "xpath" + func[5:]
578 elif func[0:4] == "xPtr":
579 func = "xpointer" + func[4:]
580 elif func[0:8] == "xInclude":
581 func = "xinclude" + func[8:]
582 elif func[0:2] == "iD":
583 func = "ID" + func[2:]
584 elif func[0:3] == "uRI":
585 func = "URI" + func[3:]
586 elif func[0:4] == "uTF8":
587 func = "UTF8" + func[4:]
588 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000589
Daniel Veillard1971ee22002-01-31 20:29:19 +0000590for name in functions.keys():
591 found = 0;
592 (desc, ret, args, file) = functions[name]
593 for type in ctypes:
594 classe = classes_type[type][2]
595
596 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
597 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000598 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000599 info = (0, func, name, ret, args, file)
600 function_classes[classe].append(info)
601 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type:
602 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000603 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000604 info = (1, func, name, ret, args, file)
605 function_classes[classe].append(info)
606 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
607 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000608 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000609 info = (0, func, name, ret, args, file)
610 function_classes[classe].append(info)
611 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type:
612 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000613 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000614 info = (1, func, name, ret, args, file)
615 function_classes[classe].append(info)
616 if found == 1:
617 break
618 if found == 1:
619 continue
620 if name[0:8] == "xmlXPath":
621 continue
622 if name[0:6] == "xmlStr":
623 continue
624 if name[0:10] == "xmlCharStr":
625 continue
Daniel Veillard3ce52572002-02-03 15:08:05 +0000626 func = nameFixup(name, "None", file, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000627 info = (0, func, name, ret, args, file)
628 function_classes['None'].append(info)
Daniel Veillard36ed5292002-01-30 23:49:06 +0000629
630classes = open("libxml2class.py", "w")
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000631txt = open("libxml2class.txt", "w")
632txt.write(" Generated Classes for libxml2-python\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000633
Daniel Veillard1971ee22002-01-31 20:29:19 +0000634def functionCompare(info1, info2):
635 (index1, func1, name1, ret1, args1, file1) = info1
636 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000637 if file1 == file2:
638 if func1 < func2:
639 return -1
640 if func1 > func2:
641 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000642 if file1 == "python_accessor":
643 return -1
644 if file2 == "python_accessor":
645 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000646 if file1 < file2:
647 return -1
648 if file1 > file2:
649 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000650 return 0
651
652def writeDoc(name, args, indent, output):
653 if functions[name][0] == None or functions[name][0] == "":
654 return
655 val = functions[name][0]
656 val = string.replace(val, "NULL", "None");
657 output.write(indent)
658 output.write('"""')
659 while len(val) > 60:
660 str = val[0:60]
661 i = string.rfind(str, " ");
662 if i < 0:
663 i = 60
664 str = val[0:i]
665 val = val[i:]
666 output.write(str)
667 output.write('\n ');
668 output.write(indent)
669 output.write(val);
670 output.write('"""\n')
671
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000672txt.write("#\n# Global functions of the module\n#\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000673if function_classes.has_key("None"):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000674 flist = function_classes["None"]
675 flist.sort(functionCompare)
676 oldfile = ""
677 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000678 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000679 if file != oldfile:
680 classes.write("#\n# Functions from module %s\n#\n\n" % file)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000681 txt.write("\n# functions from module %s\n" % file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000682 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000683 classes.write("def %s(" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000684 txt.write("%s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000685 n = 0
686 for arg in args:
687 if n != 0:
688 classes.write(", ")
689 classes.write("%s" % arg[0])
690 n = n + 1
691 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000692 writeDoc(name, args, ' ', classes);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000693 if ret[0] != "void":
694 classes.write(" ret = ");
695 else:
696 classes.write(" ");
697 classes.write("_libxml.%s(" % name)
698 n = 0
699 for arg in args:
700 if n != 0:
701 classes.write(", ");
702 classes.write("%s" % arg[0])
703 if classes_type.has_key(arg[1]):
704 classes.write(classes_type[arg[1]][0])
705 n = n + 1
706 classes.write(")\n");
707 if ret[0] != "void":
708 if classes_type.has_key(ret[0]):
709 classes.write(" if ret == None: return None\n");
710 classes.write(" return ");
711 classes.write(classes_type[ret[0]][1] % ("ret"));
712 classes.write("\n");
713 else:
714 classes.write(" return ret\n");
715 classes.write("\n");
716
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000717txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
Daniel Veillard9589d452002-02-02 10:28:17 +0000718for classname in classes_list:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000719 if classname == "None":
720 pass
721 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000722 if classes_ancestor.has_key(classname):
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000723 txt.write("\n\nClass %s(%s)\n" % (classname,
724 classes_ancestor[classname]))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000725 classes.write("class %s(%s):\n" % (classname,
726 classes_ancestor[classname]))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000727 classes.write(" def __init__(self, _obj=None):\n")
728 classes.write(" self._o = None\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000729 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
730 classes_ancestor[classname]))
Daniel Veillard3ce52572002-02-03 15:08:05 +0000731 if classes_ancestor[classname] == "xmlCore" or \
732 classes_ancestor[classname] == "xmlNode":
733 classes.write(" def __repr__(self):\n")
734 format = "%s:%%s" % (classname)
735 classes.write(" return \"%s\" %% (self.name)\n\n" % (
736 format))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000737 else:
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000738 txt.write("Class %s()\n" % (classname))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000739 classes.write("class %s:\n" % (classname))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000740 classes.write(" def __init__(self, _obj=None):\n")
741 classes.write(" if _obj != None:self._o = _obj;return\n")
742 classes.write(" self._o = None\n\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000743 if classes_destructors.has_key(classname):
744 classes.write(" def __del__(self):\n")
745 classes.write(" if self._o != None:\n")
746 classes.write(" _libxml.%s(self._o)\n" %
747 classes_destructors[classname]);
748 classes.write(" self._o = None\n\n");
749 flist = function_classes[classname]
750 flist.sort(functionCompare)
751 oldfile = ""
752 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000753 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000754 if file != oldfile:
Daniel Veillard3ce52572002-02-03 15:08:05 +0000755 if file == "python_accessor":
756 classes.write(" # accessors for %s\n" % (classname))
757 txt.write(" # accessors\n")
758 else:
759 classes.write(" #\n")
760 classes.write(" # %s functions from module %s\n" % (
761 classname, file))
762 txt.write("\n # functions from module %s\n" % file)
763 classes.write(" #\n\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000764 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000765 classes.write(" def %s(self" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000766 txt.write(" %s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000767 n = 0
768 for arg in args:
769 if n != index:
770 classes.write(", %s" % arg[0])
771 n = n + 1
772 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000773 writeDoc(name, args, ' ', classes);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000774 if ret[0] != "void":
775 classes.write(" ret = ");
776 else:
777 classes.write(" ");
778 classes.write("_libxml.%s(" % name)
779 n = 0
780 for arg in args:
781 if n != 0:
782 classes.write(", ");
783 if n != index:
784 classes.write("%s" % arg[0])
785 else:
786 classes.write("self");
787 if classes_type.has_key(arg[1]):
788 classes.write(classes_type[arg[1]][0])
789 n = n + 1
790 classes.write(")\n");
791 if ret[0] != "void":
792 if classes_type.has_key(ret[0]):
793 classes.write(" if ret == None: return None\n");
794 classes.write(" return ");
795 classes.write(classes_type[ret[0]][1] % ("ret"));
796 classes.write("\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000797 elif converter_type.has_key(ret[0]):
798 classes.write(" if ret == None: return None\n");
799 classes.write(" return ");
800 classes.write(converter_type[ret[0]] % ("ret"));
801 classes.write("\n");
Daniel Veillard36ed5292002-01-30 23:49:06 +0000802 else:
803 classes.write(" return ret\n");
804 classes.write("\n");
805
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000806txt.close()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000807classes.close()