blob: 587528ae478dc6d30ed395796e00710cf067db78 [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':
344 c_call = "\n %s(%s);\n" % (name, c_call);
345 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
346 elif py_types.has_key(ret[0]):
Daniel Veillard96fe0952002-01-30 20:52:23 +0000347 (f, t, n, c) = py_types[ret[0]]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000348 c_return = " %s c_retval;\n" % (ret[0])
Daniel Veillard3ce52572002-02-03 15:08:05 +0000349 if file == "python_accessor":
350 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
351 else:
352 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
Daniel Veillard96fe0952002-01-30 20:52:23 +0000353 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
354 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillard1971ee22002-01-31 20:29:19 +0000355 elif py_return_types.has_key(ret[0]):
356 (f, t, n, c) = py_return_types[ret[0]]
357 c_return = " %s c_retval;\n" % (ret[0])
358 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
359 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
360 ret_convert = ret_convert + " return(py_retval);\n"
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000361 else:
Daniel Veillard96fe0952002-01-30 20:52:23 +0000362 if skipped_types.has_key(ret[0]):
363 return 0
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000364 if unknown_types.has_key(ret[0]):
365 lst = unknown_types[ret[0]]
366 lst.append(name)
367 else:
368 unknown_types[ret[0]] = [name]
369 return -1
370
Daniel Veillard96fe0952002-01-30 20:52:23 +0000371 include.write("PyObject * ")
372 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000373
Daniel Veillard96fe0952002-01-30 20:52:23 +0000374 export.write(" { \"%s\", libxml_%s, METH_VARARGS },\n" % (name, name))
Daniel Veillard9589d452002-02-02 10:28:17 +0000375
376 if file == "python":
377 # Those have been manually generated
378 return 1
379
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000380 output.write("PyObject *\n")
381 output.write("libxml_%s(PyObject *self, PyObject *args) {\n" % (name))
382 if ret[0] != 'void':
383 output.write(" PyObject *py_retval;\n")
384 if c_return != "":
385 output.write(c_return)
386 if c_args != "":
387 output.write(c_args)
388 if format != "":
389 output.write("\n if (!PyArg_ParseTuple(args, \"%s\"%s))\n" %
390 (format, format_args))
391 output.write(" return(NULL);\n")
Daniel Veillard96fe0952002-01-30 20:52:23 +0000392 if c_convert != "":
393 output.write(c_convert)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000394
395 output.write(c_call)
396 output.write(ret_convert)
397 output.write("}\n\n")
398 return 1
399
400try:
Daniel Veillard63276972002-01-31 23:49:05 +0000401 f = open("../doc/libxml2-api.xml")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000402 data = f.read()
403 (parser, target) = getparser()
404 parser.feed(data)
405 parser.close()
406except IOError, msg:
407 print file, ":", msg
408
Daniel Veillard9589d452002-02-02 10:28:17 +0000409n = len(functions.keys())
410print "Found %d functions in libxml2-api.xml" % (n)
411
412py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
413try:
414 f = open("libxml2-python-api.xml")
415 data = f.read()
416 (parser, target) = getparser()
417 parser.feed(data)
418 parser.close()
419except IOError, msg:
420 print file, ":", msg
421
422
423print "Found %d functions in libxml2-python-api.xml" % (
424 len(functions.keys()) - n)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000425nb_wrap = 0
426failed = 0
427skipped = 0
428
Daniel Veillard96fe0952002-01-30 20:52:23 +0000429include = open("libxml2-py.h", "w")
430include.write("/* Generated */\n\n")
431export = open("libxml2-export.c", "w")
432export.write("/* Generated */\n\n")
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000433wrapper = open("libxml2-py.c", "w")
434wrapper.write("/* Generated */\n\n")
435wrapper.write("#include <Python.h>\n")
436wrapper.write("#include <libxml/tree.h>\n")
437wrapper.write("#include \"libxml_wrap.h\"\n")
438wrapper.write("#include \"libxml2-py.h\"\n\n")
439for function in functions.keys():
Daniel Veillard96fe0952002-01-30 20:52:23 +0000440 ret = print_function_wrapper(function, wrapper, export, include)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000441 if ret < 0:
442 failed = failed + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000443 del functions[function]
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000444 if ret == 0:
445 skipped = skipped + 1
Daniel Veillard36ed5292002-01-30 23:49:06 +0000446 del functions[function]
447 if ret == 1:
448 nb_wrap = nb_wrap + 1
Daniel Veillard96fe0952002-01-30 20:52:23 +0000449include.close()
450export.close()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000451wrapper.close()
452
453print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
454 failed, skipped);
455print "Missing type converters: %s" % (unknown_types.keys())
Daniel Veillard36ed5292002-01-30 23:49:06 +0000456
Daniel Veillard1971ee22002-01-31 20:29:19 +0000457#######################################################################
458#
459# This part writes part of the Python front-end classes based on
460# mapping rules between types and classes and also based on function
461# renaming to get consistent function names at the Python level
462#
463#######################################################################
464
465#
466# The type automatically remapped to generated classes
467#
468classes_type = {
469 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
470 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
471 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
472 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
473 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
474 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
475 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
476 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
477 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
478 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
479 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
480 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
481 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
482 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
483 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
484 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
485 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
486 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
487 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
Daniel Veillard3ce52572002-02-03 15:08:05 +0000488 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
489 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
Daniel Veillard1971ee22002-01-31 20:29:19 +0000490}
491
492converter_type = {
493 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
494}
495
496primary_classes = ["xmlNode", "xmlDoc"]
497
498classes_ancestor = {
499 "xmlNode" : "xmlCore",
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000500 "xmlDtd" : "xmlNode",
501 "xmlDoc" : "xmlNode",
502 "xmlAttr" : "xmlNode",
503 "xmlNs" : "xmlNode",
504 "xmlEntity" : "xmlNode",
505 "xmlElement" : "xmlNode",
506 "xmlAttribute" : "xmlNode",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000507}
508classes_destructors = {
509 "xpathContext": "xmlXPathFreeContext",
Daniel Veillard3ce52572002-02-03 15:08:05 +0000510 "parserCtxt": "xmlFreeParserCtxt",
Daniel Veillard1971ee22002-01-31 20:29:19 +0000511}
512
Daniel Veillard36ed5292002-01-30 23:49:06 +0000513function_classes = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000514
515function_classes["None"] = []
516for type in classes_type.keys():
517 function_classes[classes_type[type][2]] = []
518
519#
520# Build the list of C types to look for ordered to start with primary classes
521#
522ctypes = []
Daniel Veillard9589d452002-02-02 10:28:17 +0000523classes_list = []
Daniel Veillard1971ee22002-01-31 20:29:19 +0000524ctypes_processed = {}
Daniel Veillard9589d452002-02-02 10:28:17 +0000525classes_processed = {}
Daniel Veillard1971ee22002-01-31 20:29:19 +0000526for classe in primary_classes:
Daniel Veillard9589d452002-02-02 10:28:17 +0000527 classes_list.append(classe)
528 classes_processed[classe] = ()
Daniel Veillard1971ee22002-01-31 20:29:19 +0000529 for type in classes_type.keys():
530 tinfo = classes_type[type]
531 if tinfo[2] == classe:
532 ctypes.append(type)
533 ctypes_processed[type] = ()
534for type in classes_type.keys():
535 if ctypes_processed.has_key(type):
536 continue
537 tinfo = classes_type[type]
Daniel Veillard9589d452002-02-02 10:28:17 +0000538 if not classes_processed.has_key(tinfo[2]):
539 classes_list.append(tinfo[2])
540 classes_processed[tinfo[2]] = ()
541
Daniel Veillard1971ee22002-01-31 20:29:19 +0000542 ctypes.append(type)
543 ctypes_processed[type] = ()
544
Daniel Veillard3ce52572002-02-03 15:08:05 +0000545def nameFixup(function, classe, type, file):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000546 listname = classe + "List"
547 ll = len(listname)
548 l = len(classe)
549 if name[0:l] == listname:
550 func = name[l:]
551 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard3ce52572002-02-03 15:08:05 +0000552 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
553 func = name[12:]
554 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000555 elif name[0:l] == classe:
556 func = name[l:]
557 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard9589d452002-02-02 10:28:17 +0000558 elif name[0:7] == "libxml_":
559 func = name[7:]
560 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard1971ee22002-01-31 20:29:19 +0000561 elif name[0:6] == "xmlGet":
562 func = name[6:]
563 func = string.lower(func[0:1]) + func[1:]
564 elif name[0:3] == "xml":
Daniel Veillard36ed5292002-01-30 23:49:06 +0000565 func = name[3:]
566 func = string.lower(func[0:1]) + func[1:]
Daniel Veillard36ed5292002-01-30 23:49:06 +0000567 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000568 func = name
569 if func[0:5] == "xPath":
570 func = "xpath" + func[5:]
571 elif func[0:4] == "xPtr":
572 func = "xpointer" + func[4:]
573 elif func[0:8] == "xInclude":
574 func = "xinclude" + func[8:]
575 elif func[0:2] == "iD":
576 func = "ID" + func[2:]
577 elif func[0:3] == "uRI":
578 func = "URI" + func[3:]
579 elif func[0:4] == "uTF8":
580 func = "UTF8" + func[4:]
581 return func
Daniel Veillard36ed5292002-01-30 23:49:06 +0000582
Daniel Veillard1971ee22002-01-31 20:29:19 +0000583for name in functions.keys():
584 found = 0;
585 (desc, ret, args, file) = functions[name]
586 for type in ctypes:
587 classe = classes_type[type][2]
588
589 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
590 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000591 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000592 info = (0, func, name, ret, args, file)
593 function_classes[classe].append(info)
594 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type:
595 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000596 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000597 info = (1, func, name, ret, args, file)
598 function_classes[classe].append(info)
599 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
600 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000601 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000602 info = (0, func, name, ret, args, file)
603 function_classes[classe].append(info)
604 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type:
605 found = 1
Daniel Veillard3ce52572002-02-03 15:08:05 +0000606 func = nameFixup(name, classe, type, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000607 info = (1, func, name, ret, args, file)
608 function_classes[classe].append(info)
609 if found == 1:
610 break
611 if found == 1:
612 continue
613 if name[0:8] == "xmlXPath":
614 continue
615 if name[0:6] == "xmlStr":
616 continue
617 if name[0:10] == "xmlCharStr":
618 continue
Daniel Veillard3ce52572002-02-03 15:08:05 +0000619 func = nameFixup(name, "None", file, file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000620 info = (0, func, name, ret, args, file)
621 function_classes['None'].append(info)
Daniel Veillard36ed5292002-01-30 23:49:06 +0000622
623classes = open("libxml2class.py", "w")
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000624txt = open("libxml2class.txt", "w")
625txt.write(" Generated Classes for libxml2-python\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000626
Daniel Veillard1971ee22002-01-31 20:29:19 +0000627def functionCompare(info1, info2):
628 (index1, func1, name1, ret1, args1, file1) = info1
629 (index2, func2, name2, ret2, args2, file2) = info2
Daniel Veillard3ce52572002-02-03 15:08:05 +0000630 if file1 == "python_accessor":
631 return -1
632 if file2 == "python_accessor":
633 return 1
Daniel Veillard1971ee22002-01-31 20:29:19 +0000634 if file1 < file2:
635 return -1
636 if file1 > file2:
637 return 1
638 if func1 < func2:
639 return -1
640 if func1 > func2:
641 return 1
642 return 0
643
644def writeDoc(name, args, indent, output):
645 if functions[name][0] == None or functions[name][0] == "":
646 return
647 val = functions[name][0]
648 val = string.replace(val, "NULL", "None");
649 output.write(indent)
650 output.write('"""')
651 while len(val) > 60:
652 str = val[0:60]
653 i = string.rfind(str, " ");
654 if i < 0:
655 i = 60
656 str = val[0:i]
657 val = val[i:]
658 output.write(str)
659 output.write('\n ');
660 output.write(indent)
661 output.write(val);
662 output.write('"""\n')
663
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000664txt.write("#\n# Global functions of the module\n#\n\n")
Daniel Veillard36ed5292002-01-30 23:49:06 +0000665if function_classes.has_key("None"):
Daniel Veillard1971ee22002-01-31 20:29:19 +0000666 flist = function_classes["None"]
667 flist.sort(functionCompare)
668 oldfile = ""
669 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000670 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000671 if file != oldfile:
672 classes.write("#\n# Functions from module %s\n#\n\n" % file)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000673 txt.write("\n# functions from module %s\n" % file)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000674 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000675 classes.write("def %s(" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000676 txt.write("%s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000677 n = 0
678 for arg in args:
679 if n != 0:
680 classes.write(", ")
681 classes.write("%s" % arg[0])
682 n = n + 1
683 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000684 writeDoc(name, args, ' ', classes);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000685 if ret[0] != "void":
686 classes.write(" ret = ");
687 else:
688 classes.write(" ");
689 classes.write("_libxml.%s(" % name)
690 n = 0
691 for arg in args:
692 if n != 0:
693 classes.write(", ");
694 classes.write("%s" % arg[0])
695 if classes_type.has_key(arg[1]):
696 classes.write(classes_type[arg[1]][0])
697 n = n + 1
698 classes.write(")\n");
699 if ret[0] != "void":
700 if classes_type.has_key(ret[0]):
701 classes.write(" if ret == None: return None\n");
702 classes.write(" return ");
703 classes.write(classes_type[ret[0]][1] % ("ret"));
704 classes.write("\n");
705 else:
706 classes.write(" return ret\n");
707 classes.write("\n");
708
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000709txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
Daniel Veillard9589d452002-02-02 10:28:17 +0000710for classname in classes_list:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000711 if classname == "None":
712 pass
713 else:
Daniel Veillard1971ee22002-01-31 20:29:19 +0000714 if classes_ancestor.has_key(classname):
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000715 txt.write("\n\nClass %s(%s)\n" % (classname,
716 classes_ancestor[classname]))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000717 classes.write("class %s(%s):\n" % (classname,
718 classes_ancestor[classname]))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000719 classes.write(" def __init__(self, _obj=None):\n")
720 classes.write(" self._o = None\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000721 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
722 classes_ancestor[classname]))
Daniel Veillard3ce52572002-02-03 15:08:05 +0000723 if classes_ancestor[classname] == "xmlCore" or \
724 classes_ancestor[classname] == "xmlNode":
725 classes.write(" def __repr__(self):\n")
726 format = "%s:%%s" % (classname)
727 classes.write(" return \"%s\" %% (self.name)\n\n" % (
728 format))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000729 else:
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000730 txt.write("Class %s()\n" % (classname))
Daniel Veillard1971ee22002-01-31 20:29:19 +0000731 classes.write("class %s:\n" % (classname))
Daniel Veillard36ed5292002-01-30 23:49:06 +0000732 classes.write(" def __init__(self, _obj=None):\n")
733 classes.write(" if _obj != None:self._o = _obj;return\n")
734 classes.write(" self._o = None\n\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000735 if classes_destructors.has_key(classname):
736 classes.write(" def __del__(self):\n")
737 classes.write(" if self._o != None:\n")
738 classes.write(" _libxml.%s(self._o)\n" %
739 classes_destructors[classname]);
740 classes.write(" self._o = None\n\n");
741 flist = function_classes[classname]
742 flist.sort(functionCompare)
743 oldfile = ""
744 for info in flist:
Daniel Veillard36ed5292002-01-30 23:49:06 +0000745 (index, func, name, ret, args, file) = info
Daniel Veillard1971ee22002-01-31 20:29:19 +0000746 if file != oldfile:
Daniel Veillard3ce52572002-02-03 15:08:05 +0000747 if file == "python_accessor":
748 classes.write(" # accessors for %s\n" % (classname))
749 txt.write(" # accessors\n")
750 else:
751 classes.write(" #\n")
752 classes.write(" # %s functions from module %s\n" % (
753 classname, file))
754 txt.write("\n # functions from module %s\n" % file)
755 classes.write(" #\n\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000756 oldfile = file
Daniel Veillard36ed5292002-01-30 23:49:06 +0000757 classes.write(" def %s(self" % func)
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000758 txt.write(" %s()\n" % func);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000759 n = 0
760 for arg in args:
761 if n != index:
762 classes.write(", %s" % arg[0])
763 n = n + 1
764 classes.write("):\n")
Daniel Veillard1971ee22002-01-31 20:29:19 +0000765 writeDoc(name, args, ' ', classes);
Daniel Veillard36ed5292002-01-30 23:49:06 +0000766 if ret[0] != "void":
767 classes.write(" ret = ");
768 else:
769 classes.write(" ");
770 classes.write("_libxml.%s(" % name)
771 n = 0
772 for arg in args:
773 if n != 0:
774 classes.write(", ");
775 if n != index:
776 classes.write("%s" % arg[0])
777 else:
778 classes.write("self");
779 if classes_type.has_key(arg[1]):
780 classes.write(classes_type[arg[1]][0])
781 n = n + 1
782 classes.write(")\n");
783 if ret[0] != "void":
784 if classes_type.has_key(ret[0]):
785 classes.write(" if ret == None: return None\n");
786 classes.write(" return ");
787 classes.write(classes_type[ret[0]][1] % ("ret"));
788 classes.write("\n");
Daniel Veillard1971ee22002-01-31 20:29:19 +0000789 elif converter_type.has_key(ret[0]):
790 classes.write(" if ret == None: return None\n");
791 classes.write(" return ");
792 classes.write(converter_type[ret[0]] % ("ret"));
793 classes.write("\n");
Daniel Veillard36ed5292002-01-30 23:49:06 +0000794 else:
795 classes.write(" return ret\n");
796 classes.write("\n");
797
Daniel Veillard253aa2c2002-02-02 09:17:16 +0000798txt.close()
Daniel Veillard36ed5292002-01-30 23:49:06 +0000799classes.close()