3 # generate python wrappers from the XML API description
11 #######################################################################
13 # That part if purely the API acquisition phase from the
16 #######################################################################
22 sgmlop = None # accelerator not available
28 """sgmlop based XML parser. this is typically 15x faster
31 def __init__(self, target):
34 self.finish_starttag = target.start
35 self.finish_endtag = target.end
36 self.handle_data = target.data
39 self.parser = sgmlop.XMLParser()
40 self.parser.register(self)
41 self.feed = self.parser.feed
43 "amp": "&", "gt": ">", "lt": "<",
44 "apos": "'", "quot": '"'
51 self.parser = self.feed = None # nuke circular reference
53 def handle_entityref(self, entity):
56 self.handle_data(self.entity[entity])
58 self.handle_data("&%s;" % entity)
64 class SlowParser(xmllib.XMLParser):
65 """slow but safe standard parser, based on the XML parser in
66 Python's standard library."""
68 def __init__(self, target):
69 self.unknown_starttag = target.start
70 self.handle_data = target.data
71 self.unknown_endtag = target.end
72 xmllib.XMLParser.__init__(self)
74 def getparser(target = None):
75 # get the fastest available parser, and attach it to an
76 # unmarshalling object. return both objects.
80 return FastParser(target), target
81 return SlowParser(target), target
85 self._methodname = None
93 def getmethodname(self):
94 return self._methodname
98 print "data %s" % text
99 self._data.append(text)
101 def start(self, tag, attrs):
103 print "start %s, %s" % (tag, attrs)
104 if tag == 'function':
108 self.function_args = []
109 self.function_descr = None
110 self.function_return = None
111 self.function_file = None
112 if attrs.has_key('name'):
113 self.function = attrs['name']
114 if attrs.has_key('file'):
115 self.function_file = attrs['file']
119 if self.in_function == 1:
120 self.function_arg_name = None
121 self.function_arg_type = None
122 self.function_arg_info = None
123 if attrs.has_key('name'):
124 self.function_arg_name = attrs['name']
125 if attrs.has_key('type'):
126 self.function_arg_type = attrs['type']
127 if attrs.has_key('info'):
128 self.function_arg_info = attrs['info']
129 elif tag == 'return':
130 if self.in_function == 1:
131 self.function_return_type = None
132 self.function_return_info = None
133 self.function_return_field = None
134 if attrs.has_key('type'):
135 self.function_return_type = attrs['type']
136 if attrs.has_key('info'):
137 self.function_return_info = attrs['info']
138 if attrs.has_key('field'):
139 self.function_return_field = attrs['field']
145 if tag == 'function':
146 if self.function != None:
147 function(self.function, self.function_descr,
148 self.function_return, self.function_args,
152 if self.in_function == 1:
153 self.function_args.append([self.function_arg_name,
154 self.function_arg_type,
155 self.function_arg_info])
156 elif tag == 'return':
157 if self.in_function == 1:
158 self.function_return = [self.function_return_type,
159 self.function_return_info,
160 self.function_return_field]
165 if self.in_function == 1:
166 self.function_descr = str
169 def function(name, desc, ret, args, file):
172 functions[name] = (desc, ret, args, file)
174 #######################################################################
176 # Some filtering rukes to drop functions/types which should not
177 # be exposed as-is on the Python interface
179 #######################################################################
191 'int *': "usually a return type",
192 'xmlSAXHandlerPtr': "not the proper interface for SAX",
193 'htmlSAXHandlerPtr': "not the proper interface for SAX",
194 'xmlRMutexPtr': "thread specific, skipped",
195 'xmlMutexPtr': "thread specific, skipped",
196 'xmlGlobalStatePtr': "thread specific, skipped",
197 'xmlListPtr': "internal representation not suitable for python",
198 'xmlBufferPtr': "internal representation not suitable for python",
202 #######################################################################
204 # Table of remapping to/from the python type or class to the C
207 #######################################################################
210 'void': (None, None, None, None),
211 'int': ('i', None, "int", "int"),
212 'long': ('i', None, "int", "int"),
213 'double': ('d', None, "double", "double"),
214 'unsigned int': ('i', None, "int", "int"),
215 'xmlChar': ('c', None, "int", "int"),
216 'unsigned char *': ('z', None, "charPtr", "char *"),
217 'char *': ('z', None, "charPtr", "char *"),
218 'const char *': ('z', None, "charPtrConst", "const char *"),
219 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
220 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
221 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
222 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
223 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
224 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
225 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
226 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
227 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
228 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
229 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
234 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
235 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
236 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
237 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
238 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
239 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
240 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
241 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
242 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
243 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
244 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
245 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
246 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
247 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
248 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
249 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
250 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
251 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
252 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
253 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
254 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
255 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
256 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
257 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
258 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
259 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
260 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
261 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
262 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
263 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
264 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
265 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
266 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
267 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
268 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
269 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
270 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
271 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
272 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
273 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
274 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
275 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
276 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
277 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
278 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
282 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
287 #######################################################################
289 # This part writes the C <-> Python stubs libxml2-py.[ch] and
290 # the table libxml2-export.c to add when registrering the Python module
292 #######################################################################
294 def skip_function(name):
295 if name[0:12] == "xmlXPathWrap":
297 if name == "xmlFreeParserCtxt":
299 if name == "xmlFreeTextReader":
301 # if name[0:11] == "xmlXPathNew":
305 def print_function_wrapper(name, output, export, include):
309 global skipped_modules
312 (desc, ret, args, file) = functions[name]
314 print "failed to get function %s infos"
317 if skipped_modules.has_key(file):
319 if skip_function(name) == 1:
329 # This should be correct
330 if arg[1][0:6] == "const ":
332 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
333 if py_types.has_key(arg[1]):
334 (f, t, n, c) = py_types[arg[1]]
338 format_args = format_args + ", &pyobj_%s" % (arg[0])
339 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
340 c_convert = c_convert + \
341 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
344 format_args = format_args + ", &%s" % (arg[0])
346 c_call = c_call + ", ";
347 c_call = c_call + "%s" % (arg[0])
349 if skipped_types.has_key(arg[1]):
351 if unknown_types.has_key(arg[1]):
352 lst = unknown_types[arg[1]]
355 unknown_types[arg[1]] = [name]
358 format = format + ":%s" % (name)
361 if file == "python_accessor":
362 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
363 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
364 args[0][0], args[1][0], args[0][0], args[1][0])
365 c_call = c_call + " %s->%s = xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
366 args[1][0], args[1][0])
368 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
371 c_call = "\n %s(%s);\n" % (name, c_call);
372 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
373 elif py_types.has_key(ret[0]):
374 (f, t, n, c) = py_types[ret[0]]
375 c_return = " %s c_retval;\n" % (ret[0])
376 if file == "python_accessor" and ret[2] != None:
377 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
379 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
380 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
381 ret_convert = ret_convert + " return(py_retval);\n"
382 elif py_return_types.has_key(ret[0]):
383 (f, t, n, c) = py_return_types[ret[0]]
384 c_return = " %s c_retval;\n" % (ret[0])
385 c_call = "\n c_retval = %s(%s);\n" % (name, c_call);
386 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
387 ret_convert = ret_convert + " return(py_retval);\n"
389 if skipped_types.has_key(ret[0]):
391 if unknown_types.has_key(ret[0]):
392 lst = unknown_types[ret[0]]
395 unknown_types[ret[0]] = [name]
398 if file == "debugXML":
399 include.write("#ifdef LIBXML_DEBUG_ENABLED\n");
400 export.write("#ifdef LIBXML_DEBUG_ENABLED\n");
401 output.write("#ifdef LIBXML_DEBUG_ENABLED\n");
402 elif file == "HTMLtree" or file == "HTMLparser":
403 include.write("#ifdef LIBXML_HTML_ENABLED\n");
404 export.write("#ifdef LIBXML_HTML_ENABLED\n");
405 output.write("#ifdef LIBXML_HTML_ENABLED\n");
407 include.write("#ifdef LIBXML_C14N_ENABLED\n");
408 export.write("#ifdef LIBXML_C14N_ENABLED\n");
409 output.write("#ifdef LIBXML_C14N_ENABLED\n");
410 elif file == "xpathInternals" or file == "xpath":
411 include.write("#ifdef LIBXML_XPATH_ENABLED\n");
412 export.write("#ifdef LIBXML_XPATH_ENABLED\n");
413 output.write("#ifdef LIBXML_XPATH_ENABLED\n");
414 elif file == "xpointer":
415 include.write("#ifdef LIBXML_XPTR_ENABLED\n");
416 export.write("#ifdef LIBXML_XPTR_ENABLED\n");
417 output.write("#ifdef LIBXML_XPTR_ENABLED\n");
418 elif file == "xinclude":
419 include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
420 export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
421 output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n");
422 elif file == "xmlregexp":
423 include.write("#ifdef LIBXML_REGEXP_ENABLED\n");
424 export.write("#ifdef LIBXML_REGEXP_ENABLED\n");
425 output.write("#ifdef LIBXML_REGEXP_ENABLED\n");
426 elif file == "xmlschemas" or file == "xmlschemastypes" or \
428 include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
429 export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
430 output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n");
432 include.write("PyObject * ")
433 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));
435 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
439 # Those have been manually generated
441 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
442 # Those have been manually generated
445 output.write("PyObject *\n")
446 output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name))
448 output.write("ATTRIBUTE_UNUSED ")
449 output.write(" PyObject *args) {\n")
451 output.write(" PyObject *py_retval;\n")
453 output.write(c_return)
457 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
458 (format, format_args))
459 output.write(" return(NULL);\n")
461 output.write(c_convert)
464 output.write(ret_convert)
465 output.write("}\n\n")
466 if file == "debugXML":
467 include.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
468 export.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
469 output.write("#endif /* LIBXML_DEBUG_ENABLED */\n");
470 elif file == "HTMLtree" or file == "HTMLparser":
471 include.write("#endif /* LIBXML_HTML_ENABLED */\n");
472 export.write("#endif /* LIBXML_HTML_ENABLED */\n");
473 output.write("#endif /* LIBXML_HTML_ENABLED */\n");
475 include.write("#endif /* LIBXML_C14N_ENABLED */\n");
476 export.write("#endif /* LIBXML_C14N_ENABLED */\n");
477 output.write("#endif /* LIBXML_C14N_ENABLED */\n");
478 elif file == "xpathInternals" or file == "xpath":
479 include.write("#endif /* LIBXML_XPATH_ENABLED */\n");
480 export.write("#endif /* LIBXML_XPATH_ENABLED */\n");
481 output.write("#endif /* LIBXML_XPATH_ENABLED */\n");
482 elif file == "xpointer":
483 include.write("#endif /* LIBXML_XPTR_ENABLED */\n");
484 export.write("#endif /* LIBXML_XPTR_ENABLED */\n");
485 output.write("#endif /* LIBXML_XPTR_ENABLED */\n");
486 elif file == "xinclude":
487 include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
488 export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
489 output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n");
490 elif file == "xmlregexp":
491 include.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
492 export.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
493 output.write("#endif /* LIBXML_REGEXP_ENABLED */\n");
494 elif file == "xmlschemas" or file == "xmlschemastypes" or \
496 include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
497 export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
498 output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n");
503 global py_return_types
507 f = open("libxml2-api.xml")
509 (parser, target) = getparser()
514 f = open("../doc/libxml2-api.xml")
516 (parser, target) = getparser()
523 n = len(functions.keys())
524 print "Found %d functions in libxml2-api.xml" % (n)
526 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
528 f = open("libxml2-python-api.xml")
530 (parser, target) = getparser()
537 print "Found %d functions in libxml2-python-api.xml" % (
538 len(functions.keys()) - n)
543 include = open("libxml2-py.h", "w")
544 include.write("/* Generated */\n\n")
545 export = open("libxml2-export.c", "w")
546 export.write("/* Generated */\n\n")
547 wrapper = open("libxml2-py.c", "w")
548 wrapper.write("/* Generated */\n\n")
549 wrapper.write("#include <Python.h>\n")
550 # wrapper.write("#include \"config.h\"\n")
551 wrapper.write("#include <libxml/xmlversion.h>\n")
552 wrapper.write("#include <libxml/tree.h>\n")
553 wrapper.write("#include \"libxml_wrap.h\"\n")
554 wrapper.write("#include \"libxml2-py.h\"\n\n")
555 for function in functions.keys():
556 ret = print_function_wrapper(function, wrapper, export, include)
559 del functions[function]
561 skipped = skipped + 1
562 del functions[function]
564 nb_wrap = nb_wrap + 1
569 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
571 print "Missing type converters: "
572 for type in unknown_types.keys():
573 print "%s:%d " % (type, len(unknown_types[type])),
576 #######################################################################
578 # This part writes part of the Python front-end classes based on
579 # mapping rules between types and classes and also based on function
580 # renaming to get consistent function names at the Python level
582 #######################################################################
585 # The type automatically remapped to generated classes
588 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
589 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
590 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
591 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
592 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
593 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
594 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
595 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
596 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
597 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
598 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
599 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
600 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
601 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
602 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
603 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
604 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
605 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
606 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
607 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
608 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
609 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
610 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
611 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
612 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
613 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
614 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
615 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
616 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
617 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
618 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
619 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
620 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
621 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
622 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
623 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
627 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
630 primary_classes = ["xmlNode", "xmlDoc"]
633 "xmlNode" : "xmlCore",
634 "xmlDtd" : "xmlNode",
635 "xmlDoc" : "xmlNode",
636 "xmlAttr" : "xmlNode",
638 "xmlEntity" : "xmlNode",
639 "xmlElement" : "xmlNode",
640 "xmlAttribute" : "xmlNode",
641 "outputBuffer": "ioWriteWrapper",
642 "inputBuffer": "ioReadWrapper",
643 "parserCtxt": "parserCtxtCore",
644 "xmlTextReader": "xmlTextReaderCore",
646 classes_destructors = {
647 "parserCtxt": "xmlFreeParserCtxt",
648 "catalog": "xmlFreeCatalog",
650 # "outputBuffer": "xmlOutputBufferClose",
651 "inputBuffer": "xmlFreeParserInputBuffer",
652 "xmlReg": "xmlRegFreeRegexp",
653 "xmlTextReader": "xmlFreeTextReader",
654 "relaxNgSchema": "xmlRelaxNGFree",
655 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
656 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
659 functions_noexcept = {
662 "xmlDocSetRootElement": 1,
665 reference_keepers = {
666 "xmlTextReader": [('inputBuffer', 'input')],
667 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
670 function_classes = {}
672 function_classes["None"] = []
674 def nameFixup(name, classe, type, file):
675 listname = classe + "List"
678 if name[0:l] == listname:
680 func = string.lower(func[0:1]) + func[1:]
681 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
683 func = string.lower(func[0:1]) + func[1:]
684 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
686 func = string.lower(func[0:1]) + func[1:]
687 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
689 func = string.lower(func[0:1]) + func[1:]
690 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
692 func = string.lower(func[0:1]) + func[1:]
693 elif name[0:9] == "xmlURISet" and file == "python_accessor":
695 func = string.lower(func[0:1]) + func[1:]
696 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
698 func = string.lower(func[0:1]) + func[1:]
699 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
701 func = string.lower(func[0:1]) + func[1:]
702 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
704 func = string.lower(func[0:1]) + func[1:]
705 elif name[0:15] == "xmlOutputBuffer" and file != "python":
707 func = string.lower(func[0:1]) + func[1:]
708 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
710 func = string.lower(func[0:1]) + func[1:]
711 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
712 func = "regexp" + name[9:]
713 elif name[0:6] == "xmlReg" and file == "xmlregexp":
714 func = "regexp" + name[6:]
715 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
717 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
719 elif name[0:11] == "xmlACatalog":
721 func = string.lower(func[0:1]) + func[1:]
722 elif name[0:l] == classe:
724 func = string.lower(func[0:1]) + func[1:]
725 elif name[0:7] == "libxml_":
727 func = string.lower(func[0:1]) + func[1:]
728 elif name[0:6] == "xmlGet":
730 func = string.lower(func[0:1]) + func[1:]
731 elif name[0:3] == "xml":
733 func = string.lower(func[0:1]) + func[1:]
736 if func[0:5] == "xPath":
737 func = "xpath" + func[5:]
738 elif func[0:4] == "xPtr":
739 func = "xpointer" + func[4:]
740 elif func[0:8] == "xInclude":
741 func = "xinclude" + func[8:]
742 elif func[0:2] == "iD":
743 func = "ID" + func[2:]
744 elif func[0:3] == "uRI":
745 func = "URI" + func[3:]
746 elif func[0:4] == "uTF8":
747 func = "UTF8" + func[4:]
748 elif func[0:3] == 'sAX':
749 func = "SAX" + func[3:]
753 def functionCompare(info1, info2):
754 (index1, func1, name1, ret1, args1, file1) = info1
755 (index2, func2, name2, ret2, args2, file2) = info2
761 if file1 == "python_accessor":
763 if file2 == "python_accessor":
771 def writeDoc(name, args, indent, output):
772 if functions[name][0] is None or functions[name][0] == "":
774 val = functions[name][0]
775 val = string.replace(val, "NULL", "None");
780 i = string.rfind(str, " ");
789 output.write(' """\n')
794 global py_return_types
797 global function_classes
800 global converter_type
801 global primary_classes
802 global converter_type
803 global classes_ancestor
804 global converter_type
805 global primary_classes
806 global classes_ancestor
807 global classes_destructors
808 global functions_noexcept
810 for type in classes_type.keys():
811 function_classes[classes_type[type][2]] = []
814 # Build the list of C types to look for ordered to start
815 # with primary classes
819 ctypes_processed = {}
820 classes_processed = {}
821 for classe in primary_classes:
822 classes_list.append(classe)
823 classes_processed[classe] = ()
824 for type in classes_type.keys():
825 tinfo = classes_type[type]
826 if tinfo[2] == classe:
828 ctypes_processed[type] = ()
829 for type in classes_type.keys():
830 if ctypes_processed.has_key(type):
832 tinfo = classes_type[type]
833 if not classes_processed.has_key(tinfo[2]):
834 classes_list.append(tinfo[2])
835 classes_processed[tinfo[2]] = ()
838 ctypes_processed[type] = ()
840 for name in functions.keys():
842 (desc, ret, args, file) = functions[name]
844 classe = classes_type[type][2]
846 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
848 func = nameFixup(name, classe, type, file)
849 info = (0, func, name, ret, args, file)
850 function_classes[classe].append(info)
851 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
852 and file != "python_accessor":
854 func = nameFixup(name, classe, type, file)
855 info = (1, func, name, ret, args, file)
856 function_classes[classe].append(info)
857 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
859 func = nameFixup(name, classe, type, file)
860 info = (0, func, name, ret, args, file)
861 function_classes[classe].append(info)
862 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
863 and file != "python_accessor":
865 func = nameFixup(name, classe, type, file)
866 info = (1, func, name, ret, args, file)
867 function_classes[classe].append(info)
872 if name[0:8] == "xmlXPath":
874 if name[0:6] == "xmlStr":
876 if name[0:10] == "xmlCharStr":
878 func = nameFixup(name, "None", file, file)
879 info = (0, func, name, ret, args, file)
880 function_classes['None'].append(info)
882 classes = open("libxml2class.py", "w")
883 txt = open("libxml2class.txt", "w")
884 txt.write(" Generated Classes for libxml2-python\n\n")
886 txt.write("#\n# Global functions of the module\n#\n\n")
887 if function_classes.has_key("None"):
888 flist = function_classes["None"]
889 flist.sort(functionCompare)
892 (index, func, name, ret, args, file) = info
894 classes.write("#\n# Functions from module %s\n#\n\n" % file)
895 txt.write("\n# functions from module %s\n" % file)
897 classes.write("def %s(" % func)
898 txt.write("%s()\n" % func);
903 classes.write("%s" % arg[0])
905 classes.write("):\n")
906 writeDoc(name, args, ' ', classes);
909 if classes_type.has_key(arg[1]):
910 classes.write(" if %s is None: %s__o = None\n" %
912 classes.write(" else: %s__o = %s%s\n" %
913 (arg[0], arg[0], classes_type[arg[1]][0]))
915 classes.write(" ret = ");
918 classes.write("libxml2mod.%s(" % name)
923 classes.write("%s" % arg[0])
924 if classes_type.has_key(arg[1]):
925 classes.write("__o");
927 classes.write(")\n");
929 if classes_type.has_key(ret[0]):
933 if functions_noexcept.has_key(name):
934 classes.write(" if ret is None:return None\n");
935 elif string.find(name, "URI") >= 0:
937 " if ret is None:raise uriError('%s() failed')\n"
939 elif string.find(name, "XPath") >= 0:
941 " if ret is None:raise xpathError('%s() failed')\n"
943 elif string.find(name, "Parse") >= 0:
945 " if ret is None:raise parserError('%s() failed')\n"
949 " if ret is None:raise treeError('%s() failed')\n"
951 classes.write(" return ");
952 classes.write(classes_type[ret[0]][1] % ("ret"));
955 classes.write(" return ret\n");
958 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
959 for classname in classes_list:
960 if classname == "None":
963 if classes_ancestor.has_key(classname):
964 txt.write("\n\nClass %s(%s)\n" % (classname,
965 classes_ancestor[classname]))
966 classes.write("class %s(%s):\n" % (classname,
967 classes_ancestor[classname]))
968 classes.write(" def __init__(self, _obj=None):\n")
969 if reference_keepers.has_key(classname):
970 rlist = reference_keepers[classname]
972 classes.write(" self.%s = None\n" % ref[1])
973 classes.write(" self._o = None\n")
974 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
975 classes_ancestor[classname]))
976 if classes_ancestor[classname] == "xmlCore" or \
977 classes_ancestor[classname] == "xmlNode":
978 classes.write(" def __repr__(self):\n")
979 format = "<%s (%%s) object at 0x%%x>" % (classname)
980 classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % (
983 txt.write("Class %s()\n" % (classname))
984 classes.write("class %s:\n" % (classname))
985 classes.write(" def __init__(self, _obj=None):\n")
986 if reference_keepers.has_key(classname):
987 list = reference_keepers[classname]
989 classes.write(" self.%s = None\n" % ref[1])
990 classes.write(" if _obj != None:self._o = _obj;return\n")
991 classes.write(" self._o = None\n\n");
992 if classes_destructors.has_key(classname):
993 classes.write(" def __del__(self):\n")
994 classes.write(" if self._o != None:\n")
995 classes.write(" libxml2mod.%s(self._o)\n" %
996 classes_destructors[classname]);
997 classes.write(" self._o = None\n\n");
998 flist = function_classes[classname]
999 flist.sort(functionCompare)
1002 (index, func, name, ret, args, file) = info
1004 if file == "python_accessor":
1005 classes.write(" # accessors for %s\n" % (classname))
1006 txt.write(" # accessors\n")
1008 classes.write(" #\n")
1009 classes.write(" # %s functions from module %s\n" % (
1011 txt.write("\n # functions from module %s\n" % file)
1012 classes.write(" #\n\n")
1014 classes.write(" def %s(self" % func)
1015 txt.write(" %s()\n" % func);
1019 classes.write(", %s" % arg[0])
1021 classes.write("):\n")
1022 writeDoc(name, args, ' ', classes);
1025 if classes_type.has_key(arg[1]):
1027 classes.write(" if %s is None: %s__o = None\n" %
1029 classes.write(" else: %s__o = %s%s\n" %
1030 (arg[0], arg[0], classes_type[arg[1]][0]))
1032 if ret[0] != "void":
1033 classes.write(" ret = ");
1036 classes.write("libxml2mod.%s(" % name)
1040 classes.write(", ");
1042 classes.write("%s" % arg[0])
1043 if classes_type.has_key(arg[1]):
1044 classes.write("__o");
1046 classes.write("self");
1047 if classes_type.has_key(arg[1]):
1048 classes.write(classes_type[arg[1]][0])
1050 classes.write(")\n");
1051 if ret[0] != "void":
1052 if classes_type.has_key(ret[0]):
1054 # Raise an exception
1056 if functions_noexcept.has_key(name):
1058 " if ret is None:return None\n");
1059 elif string.find(name, "URI") >= 0:
1061 " if ret is None:raise uriError('%s() failed')\n"
1063 elif string.find(name, "XPath") >= 0:
1065 " if ret is None:raise xpathError('%s() failed')\n"
1067 elif string.find(name, "Parse") >= 0:
1069 " if ret is None:raise parserError('%s() failed')\n"
1073 " if ret is None:raise treeError('%s() failed')\n"
1077 # generate the returned class wrapper for the object
1079 classes.write(" __tmp = ");
1080 classes.write(classes_type[ret[0]][1] % ("ret"));
1081 classes.write("\n");
1084 # Sometime one need to keep references of the source
1085 # class in the returned class object.
1086 # See reference_keepers for the list
1088 tclass = classes_type[ret[0]][2]
1089 if reference_keepers.has_key(tclass):
1090 list = reference_keepers[tclass]
1092 if pref[0] == classname:
1093 classes.write(" __tmp.%s = self\n" %
1098 classes.write(" return __tmp\n");
1099 elif converter_type.has_key(ret[0]):
1101 # Raise an exception
1103 if functions_noexcept.has_key(name):
1105 " if ret is None:return None");
1106 elif string.find(name, "URI") >= 0:
1108 " if ret is None:raise uriError('%s() failed')\n"
1110 elif string.find(name, "XPath") >= 0:
1112 " if ret is None:raise xpathError('%s() failed')\n"
1114 elif string.find(name, "Parse") >= 0:
1116 " if ret is None:raise parserError('%s() failed')\n"
1120 " if ret is None:raise treeError('%s() failed')\n"
1122 classes.write(" return ");
1123 classes.write(converter_type[ret[0]] % ("ret"));
1124 classes.write("\n");
1126 classes.write(" return ret\n");
1127 classes.write("\n");