blob: e665a870bd0ac5e81710a1e536b6aa1fe5cfa133 [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 Veillard36eea2d2002-02-04 00:17:01 +0000353 if file == "python_accessor" and ret[2] != None:
Daniel Veillard3ce52572002-02-03 15:08:05 +0000354 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
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000383 if file == "python_accessor" and ret[0] != "void" and ret[2] == None:
384 # Those have been manually generated
385 return 1
Daniel Veillard9589d452002-02-02 10:28:17 +0000386
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000387 output.write("PyObject *\n")
388 output.write("libxml_%s(PyObject *self, PyObject *args) {\n" % (name))
389 if ret[0] != 'void':
390 output.write(" PyObject *py_retval;\n")
391 if c_return != "":
392 output.write(c_return)
393 if c_args != "":
394 output.write(c_args)
395 if format != "":
396 output.write("\n if (!PyArg_ParseTuple(args, \"%s\"%s))\n" %
397 (format, format_args))
398 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000399 if c_convert != "":
400 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000401
402 output.write(c_call)
403 output.write(ret_convert)
404 output.write("}\n\n")
405 return 1
406
407try:
Daniel Veillard63276972002-01-31 23:49:05 +0000408 f = open("../doc/libxml2-api.xml")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000409 data = f.read()
410 (parser, target) = getparser()
411 parser.feed(data)
412 parser.close()
413except IOError, msg:
414 print file, ":", msg
415
Daniel Veillard9589d452002-02-02 10:28:17 +0000416n = len(functions.keys())
417print "Found %d functions in libxml2-api.xml" % (n)
418
419py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
420try:
421 f = open("libxml2-python-api.xml")
422 data = f.read()
423 (parser, target) = getparser()
424 parser.feed(data)
425 parser.close()
426except IOError, msg:
427 print file, ":", msg
428
429
430print "Found %d functions in libxml2-python-api.xml" % (
431 len(functions.keys()) - n)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000432nb_wrap = 0
433failed = 0
434skipped = 0
435
Daniel Veillard96fe0952002-01-30 20:52:23 +0000436include = open("libxml2-py.h", "w")
437include.write("/* Generated */\n\n")
438export = open("libxml2-export.c", "w")
439export.write("/* Generated */\n\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000440wrapper = open("libxml2-py.c", "w")
441wrapper.write("/* Generated */\n\n")
442wrapper.write("#include <Python.h>\n")
443wrapper.write("#include <libxml/tree.h>\n")
444wrapper.write("#include \"libxml_wrap.h\"\n")
445wrapper.write("#include \"libxml2-py.h\"\n\n")
446for function in functions.keys():
Daniel Veillard96fe0952002-01-30 20:52:23 +0000447 ret = print_function_wrapper(function, wrapper, export, include)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000448 if ret < 0:
449 failed = failed + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000450 del functions[function]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000451 if ret == 0:
452 skipped = skipped + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000453 del functions[function]
454 if ret == 1:
455 nb_wrap = nb_wrap + 1
Daniel Veillard96fe0952002-01-30 20:52:23 +0000456include.close()
457export.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000458wrapper.close()
459
460print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
461 failed, skipped);
462print "Missing type converters: %s" % (unknown_types.keys())
Daniel Veillard36ed5292002-01-30 23:49:06 +0000463
Daniel Veillard1971ee22002-01-31 20:29:19 +0000464#######################################################################
465#
466# This part writes part of the Python front-end classes based on
467# mapping rules between types and classes and also based on function
468# renaming to get consistent function names at the Python level
469#
470#######################################################################
471
472#
473# The type automatically remapped to generated classes
474#
475classes_type = {
476 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
477 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
478 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
479 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
480 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
481 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
482 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
483 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
484 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
485 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
486 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
487 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
488 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
489 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
490 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
491 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
492 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
493 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
494 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000495 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
496 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000497}
498
499converter_type = {
500 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
501}
502
503primary_classes = ["xmlNode", "xmlDoc"]
504
505classes_ancestor = {
506 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000507 "xmlDtd" : "xmlNode",
508 "xmlDoc" : "xmlNode",
509 "xmlAttr" : "xmlNode",
510 "xmlNs" : "xmlNode",
511 "xmlEntity" : "xmlNode",
512 "xmlElement" : "xmlNode",
513 "xmlAttribute" : "xmlNode",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000514}
515classes_destructors = {
516 "xpathContext": "xmlXPathFreeContext",
Daniel Veillard3ce52572002-02-03 15:08:05 +0000517 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000518}
519
Daniel Veillard36ed5292002-01-30 23:49:06 +0000520function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000521
522function_classes["None"] = []
523for type in classes_type.keys():
524 function_classes[classes_type[type][2]] = []
525
526#
527# Build the list of C types to look for ordered to start with primary classes
528#
529ctypes = []
Daniel Veillard9589d452002-02-02 10:28:17 +0000530classes_list = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000531ctypes_processed = {}
Daniel Veillard9589d452002-02-02 10:28:17 +0000532classes_processed = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000533for classe in primary_classes:
Daniel Veillard9589d452002-02-02 10:28:17 +0000534 classes_list.append(classe)
535 classes_processed[classe] = ()
Daniel Veillard1971ee22002-01-31 20:29:19 +0000536 for type in classes_type.keys():
537 tinfo = classes_type[type]
538 if tinfo[2] == classe:
539 ctypes.append(type)
540 ctypes_processed[type] = ()
541for type in classes_type.keys():
542 if ctypes_processed.has_key(type):
543 continue
544 tinfo = classes_type[type]
Daniel Veillard9589d452002-02-02 10:28:17 +0000545 if not classes_processed.has_key(tinfo[2]):
546 classes_list.append(tinfo[2])
547 classes_processed[tinfo[2]] = ()
548
Daniel Veillard1971ee22002-01-31 20:29:19 +0000549 ctypes.append(type)
550 ctypes_processed[type] = ()
551
Daniel Veillard3ce52572002-02-03 15:08:05 +0000552def nameFixup(function, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000553 listname = classe + "List"
554 ll = len(listname)
555 l = len(classe)
556 if name[0:l] == listname:
557 func = name[l:]
558 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000559 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
560 func = name[12:]
561 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000562 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
563 func = name[12:]
564 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000565 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
566 func = name[10:]
567 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000568 elif name[0:l] == classe:
569 func = name[l:]
570 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000571 elif name[0:7] == "libxml_":
572 func = name[7:]
573 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000574 elif name[0:6] == "xmlGet":
575 func = name[6:]
576 func = string.lower(func[0:1]) + func[1:]
577 elif name[0:3] == "xml":
Daniel Veillard36ed5292002-01-30 23:49:06 +0000578 func = name[3:]
579 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000580 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000581 func = name
582 if func[0:5] == "xPath":
583 func = "xpath" + func[5:]
584 elif func[0:4] == "xPtr":
585 func = "xpointer" + func[4:]
586 elif func[0:8] == "xInclude":
587 func = "xinclude" + func[8:]
588 elif func[0:2] == "iD":
589 func = "ID" + func[2:]
590 elif func[0:3] == "uRI":
591 func = "URI" + func[3:]
592 elif func[0:4] == "uTF8":
593 func = "UTF8" + func[4:]
594 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000595
Daniel Veillard1971ee22002-01-31 20:29:19 +0000596for name in functions.keys():
597 found = 0;
598 (desc, ret, args, file) = functions[name]
599 for type in ctypes:
600 classe = classes_type[type][2]
601
602 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
603 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000604 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000605 info = (0, func, name, ret, args, file)
606 function_classes[classe].append(info)
607 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type:
608 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000609 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000610 info = (1, func, name, ret, args, file)
611 function_classes[classe].append(info)
612 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
613 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000614 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000615 info = (0, func, name, ret, args, file)
616 function_classes[classe].append(info)
617 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type:
618 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000619 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000620 info = (1, func, name, ret, args, file)
621 function_classes[classe].append(info)
622 if found == 1:
623 break
624 if found == 1:
625 continue
626 if name[0:8] == "xmlXPath":
627 continue
628 if name[0:6] == "xmlStr":
629 continue
630 if name[0:10] == "xmlCharStr":
631 continue
Daniel Veillard3ce52572002-02-03 15:08:05 +0000632 func = nameFixup(name, "None", file, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000633 info = (0, func, name, ret, args, file)
634 function_classes['None'].append(info)
Daniel Veillard36ed5292002-01-30 23:49:06 +0000635
636classes = open("libxml2class.py", "w")
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000637txt = open("libxml2class.txt", "w")
638txt.write(" Generated Classes for libxml2-python\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000639
Daniel Veillard1971ee22002-01-31 20:29:19 +0000640def functionCompare(info1, info2):
641 (index1, func1, name1, ret1, args1, file1) = info1
642 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard26f1dcc2002-02-03 16:53:19 +0000643 if file1 == file2:
644 if func1 < func2:
645 return -1
646 if func1 > func2:
647 return 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000648 if file1 == "python_accessor":
649 return -1
650 if file2 == "python_accessor":
651 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000652 if file1 < file2:
653 return -1
654 if file1 > file2:
655 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000656 return 0
657
658def writeDoc(name, args, indent, output):
659 if functions[name][0] == None or functions[name][0] == "":
660 return
661 val = functions[name][0]
662 val = string.replace(val, "NULL", "None");
663 output.write(indent)
664 output.write('"""')
665 while len(val) > 60:
666 str = val[0:60]
667 i = string.rfind(str, " ");
668 if i < 0:
669 i = 60
670 str = val[0:i]
671 val = val[i:]
672 output.write(str)
673 output.write('\n ');
674 output.write(indent)
675 output.write(val);
676 output.write('"""\n')
677
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000678txt.write("#\n# Global functions of the module\n#\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000679if function_classes.has_key("None"):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000680 flist = function_classes["None"]
681 flist.sort(functionCompare)
682 oldfile = ""
683 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000684 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000685 if file != oldfile:
686 classes.write("#\n# Functions from module %s\n#\n\n" % file)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000687 txt.write("\n# functions from module %s\n" % file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000688 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000689 classes.write("def %s(" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000690 txt.write("%s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000691 n = 0
692 for arg in args:
693 if n != 0:
694 classes.write(", ")
695 classes.write("%s" % arg[0])
696 n = n + 1
697 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000698 writeDoc(name, args, ' ', classes);
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000699
700 for arg in args:
701 if classes_type.has_key(arg[1]):
702 classes.write(" if %s == None: %s__o = None\n" %
703 (arg[0], arg[0]))
704 classes.write(" else: %s__o = %s%s\n" %
705 (arg[0], arg[0], classes_type[arg[1]][0]))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000706 if ret[0] != "void":
707 classes.write(" ret = ");
708 else:
709 classes.write(" ");
710 classes.write("_libxml.%s(" % name)
711 n = 0
712 for arg in args:
713 if n != 0:
714 classes.write(", ");
715 classes.write("%s" % arg[0])
716 if classes_type.has_key(arg[1]):
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000717 classes.write("__o");
Daniel Veillard36ed5292002-01-30 23:49:06 +0000718 n = n + 1
719 classes.write(")\n");
720 if ret[0] != "void":
721 if classes_type.has_key(ret[0]):
722 classes.write(" if ret == None: return None\n");
723 classes.write(" return ");
724 classes.write(classes_type[ret[0]][1] % ("ret"));
725 classes.write("\n");
726 else:
727 classes.write(" return ret\n");
728 classes.write("\n");
729
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000730txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
Daniel Veillard9589d452002-02-02 10:28:17 +0000731for classname in classes_list:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000732 if classname == "None":
733 pass
734 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000735 if classes_ancestor.has_key(classname):
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000736 txt.write("\n\nClass %s(%s)\n" % (classname,
737 classes_ancestor[classname]))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000738 classes.write("class %s(%s):\n" % (classname,
739 classes_ancestor[classname]))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000740 classes.write(" def __init__(self, _obj=None):\n")
741 classes.write(" self._o = None\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000742 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
743 classes_ancestor[classname]))
Daniel Veillard3ce52572002-02-03 15:08:05 +0000744 if classes_ancestor[classname] == "xmlCore" or \
745 classes_ancestor[classname] == "xmlNode":
746 classes.write(" def __repr__(self):\n")
747 format = "%s:%%s" % (classname)
748 classes.write(" return \"%s\" %% (self.name)\n\n" % (
749 format))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000750 else:
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000751 txt.write("Class %s()\n" % (classname))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000752 classes.write("class %s:\n" % (classname))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000753 classes.write(" def __init__(self, _obj=None):\n")
754 classes.write(" if _obj != None:self._o = _obj;return\n")
755 classes.write(" self._o = None\n\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000756 if classes_destructors.has_key(classname):
757 classes.write(" def __del__(self):\n")
758 classes.write(" if self._o != None:\n")
759 classes.write(" _libxml.%s(self._o)\n" %
760 classes_destructors[classname]);
761 classes.write(" self._o = None\n\n");
762 flist = function_classes[classname]
763 flist.sort(functionCompare)
764 oldfile = ""
765 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000766 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000767 if file != oldfile:
Daniel Veillard3ce52572002-02-03 15:08:05 +0000768 if file == "python_accessor":
769 classes.write(" # accessors for %s\n" % (classname))
770 txt.write(" # accessors\n")
771 else:
772 classes.write(" #\n")
773 classes.write(" # %s functions from module %s\n" % (
774 classname, file))
775 txt.write("\n # functions from module %s\n" % file)
776 classes.write(" #\n\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000777 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000778 classes.write(" def %s(self" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000779 txt.write(" %s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000780 n = 0
781 for arg in args:
782 if n != index:
783 classes.write(", %s" % arg[0])
784 n = n + 1
785 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000786 writeDoc(name, args, ' ', classes);
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000787 n = 0
788 for arg in args:
789 if classes_type.has_key(arg[1]):
790 if n != index:
791 classes.write(" if %s == None: %s__o = None\n" %
792 (arg[0], arg[0]))
793 classes.write(" else: %s__o = %s%s\n" %
794 (arg[0], arg[0], classes_type[arg[1]][0]))
795 n = n + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000796 if ret[0] != "void":
797 classes.write(" ret = ");
798 else:
799 classes.write(" ");
800 classes.write("_libxml.%s(" % name)
801 n = 0
802 for arg in args:
803 if n != 0:
804 classes.write(", ");
805 if n != index:
806 classes.write("%s" % arg[0])
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000807 if classes_type.has_key(arg[1]):
808 classes.write("__o");
Daniel Veillard36ed5292002-01-30 23:49:06 +0000809 else:
810 classes.write("self");
Daniel Veillard36eea2d2002-02-04 00:17:01 +0000811 if classes_type.has_key(arg[1]):
812 classes.write(classes_type[arg[1]][0])
Daniel Veillard36ed5292002-01-30 23:49:06 +0000813 n = n + 1
814 classes.write(")\n");
815 if ret[0] != "void":
816 if classes_type.has_key(ret[0]):
817 classes.write(" if ret == None: return None\n");
818 classes.write(" return ");
819 classes.write(classes_type[ret[0]][1] % ("ret"));
820 classes.write("\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000821 elif converter_type.has_key(ret[0]):
822 classes.write(" if ret == None: return None\n");
823 classes.write(" return ");
824 classes.write(converter_type[ret[0]] % ("ret"));
825 classes.write("\n");
Daniel Veillard36ed5292002-01-30 23:49:06 +0000826 else:
827 classes.write(" return ret\n");
828 classes.write("\n");
829
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000830txt.close()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000831classes.close()