3 # generate python wrappers from the XML API description
7 enums = {} # { enumType: { enumConstant: enumValue } }
13 if __name__ == "__main__":
14 # launched as a script
15 srcPref = os.path.dirname(sys.argv[0])
18 srcPref = os.path.dirname(__file__)
20 #######################################################################
22 # That part if purely the API acquisition phase from the
25 #######################################################################
32 # Attach parser to an unmarshalling object. return both objects.
34 parser = xml.sax.make_parser()
35 parser.setContentHandler(target)
38 class docParser(xml.sax.handler.ContentHandler):
40 self._methodname = None
44 self.startElement = self.start
45 self.endElement = self.end
46 self.characters = self.data
52 def getmethodname(self):
53 return self._methodname
57 print "data %s" % text
58 self._data.append(text)
60 def start(self, tag, attrs):
62 print "start %s, %s" % (tag, attrs)
67 self.function_cond = None
68 self.function_args = []
69 self.function_descr = None
70 self.function_return = None
71 self.function_file = None
72 if attrs.has_key('name'):
73 self.function = attrs['name']
74 if attrs.has_key('file'):
75 self.function_file = attrs['file']
81 if self.in_function == 1:
82 self.function_arg_name = None
83 self.function_arg_type = None
84 self.function_arg_info = None
85 if attrs.has_key('name'):
86 self.function_arg_name = attrs['name']
87 if attrs.has_key('type'):
88 self.function_arg_type = attrs['type']
89 if attrs.has_key('info'):
90 self.function_arg_info = attrs['info']
92 if self.in_function == 1:
93 self.function_return_type = None
94 self.function_return_info = None
95 self.function_return_field = None
96 if attrs.has_key('type'):
97 self.function_return_type = attrs['type']
98 if attrs.has_key('info'):
99 self.function_return_info = attrs['info']
100 if attrs.has_key('field'):
101 self.function_return_field = attrs['field']
103 enum(attrs['type'],attrs['name'],attrs['value'])
108 if tag == 'function':
109 if self.function != None:
110 function(self.function, self.function_descr,
111 self.function_return, self.function_args,
112 self.function_file, self.function_cond)
115 if self.in_function == 1:
116 self.function_args.append([self.function_arg_name,
117 self.function_arg_type,
118 self.function_arg_info])
119 elif tag == 'return':
120 if self.in_function == 1:
121 self.function_return = [self.function_return_type,
122 self.function_return_info,
123 self.function_return_field]
128 if self.in_function == 1:
129 self.function_descr = str
134 if self.in_function == 1:
135 self.function_cond = str
138 def function(name, desc, ret, args, file, cond):
139 functions[name] = (desc, ret, args, file, cond)
141 def enum(type, name, value):
142 if not enums.has_key(type):
144 enums[type][name] = value
146 #######################################################################
148 # Some filtering rukes to drop functions/types which should not
149 # be exposed as-is on the Python interface
151 #######################################################################
163 'int *': "usually a return type",
164 'xmlSAXHandlerPtr': "not the proper interface for SAX",
165 'htmlSAXHandlerPtr': "not the proper interface for SAX",
166 'xmlRMutexPtr': "thread specific, skipped",
167 'xmlMutexPtr': "thread specific, skipped",
168 'xmlGlobalStatePtr': "thread specific, skipped",
169 'xmlListPtr': "internal representation not suitable for python",
170 'xmlBufferPtr': "internal representation not suitable for python",
174 #######################################################################
176 # Table of remapping to/from the python type or class to the C
179 #######################################################################
182 'void': (None, None, None, None),
183 'int': ('i', None, "int", "int"),
184 'long': ('l', None, "long", "long"),
185 'double': ('d', None, "double", "double"),
186 'unsigned int': ('i', None, "int", "int"),
187 'xmlChar': ('c', None, "int", "int"),
188 'unsigned char *': ('z', None, "charPtr", "char *"),
189 'char *': ('z', None, "charPtr", "char *"),
190 'const char *': ('z', None, "charPtrConst", "const char *"),
191 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"),
192 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"),
193 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
194 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
195 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
196 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
197 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
198 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
199 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
200 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
201 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
202 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
203 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
204 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
205 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
206 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
207 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
208 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
209 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
210 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
211 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
212 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"),
213 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
214 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
215 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
216 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"),
217 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
218 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
219 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
220 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"),
221 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
222 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
223 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
224 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
225 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
226 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
227 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
228 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"),
229 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
230 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
231 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
232 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"),
233 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
234 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"),
235 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"),
236 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
237 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
238 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
239 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"),
240 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"),
241 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
242 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
243 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
244 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
245 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
246 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
247 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"),
248 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"),
249 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"),
250 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"),
251 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"),
252 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"),
253 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"),
254 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"),
255 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),
259 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),
264 foreign_encoding_args = (
265 'htmlCreateMemoryParserCtxt',
266 'htmlCtxtReadMemory',
269 'xmlCreateMemoryParserCtxt',
278 #######################################################################
280 # This part writes the C <-> Python stubs libxml2-py.[ch] and
281 # the table libxml2-export.c to add when registrering the Python module
283 #######################################################################
285 # Class methods which are written by hand in libxml.c but the Python-level
286 # code is still automatically generated (so they are not in skip_function()).
289 'xmlSaveFormatFileTo',
292 def skip_function(name):
293 if name[0:12] == "xmlXPathWrap":
295 if name == "xmlFreeParserCtxt":
297 if name == "xmlCleanupParser":
299 if name == "xmlFreeTextReader":
301 # if name[0:11] == "xmlXPathNew":
303 # the next function is defined in libxml.c
304 if name == "xmlRelaxNGFreeValidCtxt":
306 if name == "xmlFreeValidCtxt":
308 if name == "xmlSchemaFreeValidCtxt":
312 # Those are skipped because the Const version is used of the bindings
315 if name == "xmlTextReaderBaseUri":
317 if name == "xmlTextReaderLocalName":
319 if name == "xmlTextReaderName":
321 if name == "xmlTextReaderNamespaceUri":
323 if name == "xmlTextReaderPrefix":
325 if name == "xmlTextReaderXmlLang":
327 if name == "xmlTextReaderValue":
329 if name == "xmlOutputBufferClose": # handled by by the superclass
331 if name == "xmlOutputBufferFlush": # handled by by the superclass
333 if name == "xmlErrMemory":
336 if name == "xmlValidBuildContentModel":
338 if name == "xmlValidateElementDecl":
340 if name == "xmlValidateAttributeDecl":
345 def print_function_wrapper(name, output, export, include):
349 global skipped_modules
352 (desc, ret, args, file, cond) = functions[name]
354 print "failed to get function %s infos"
357 if skipped_modules.has_key(file):
359 if skip_function(name) == 1:
361 if name in skip_impl:
362 # Don't delete the function entry in the caller.
373 # This should be correct
374 if arg[1][0:6] == "const ":
376 c_args = c_args + " %s %s;\n" % (arg[1], arg[0])
377 if py_types.has_key(arg[1]):
378 (f, t, n, c) = py_types[arg[1]]
379 if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
384 format_args = format_args + ", &pyobj_%s" % (arg[0])
385 c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0])
386 c_convert = c_convert + \
387 " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],
390 format_args = format_args + ", &%s" % (arg[0])
392 format_args = format_args + ", &py_buffsize%d" % num_bufs
393 c_args = c_args + " int py_buffsize%d;\n" % num_bufs
394 num_bufs = num_bufs + 1
396 c_call = c_call + ", "
397 c_call = c_call + "%s" % (arg[0])
399 if skipped_types.has_key(arg[1]):
401 if unknown_types.has_key(arg[1]):
402 lst = unknown_types[arg[1]]
405 unknown_types[arg[1]] = [name]
408 format = format + ":%s" % (name)
411 if file == "python_accessor":
412 if args[1][1] == "char *" or args[1][1] == "xmlChar *":
413 c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % (
414 args[0][0], args[1][0], args[0][0], args[1][0])
415 c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],
416 args[1][0], args[1][1], args[1][0])
418 c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0],
421 c_call = "\n %s(%s);\n" % (name, c_call)
422 ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
423 elif py_types.has_key(ret[0]):
424 (f, t, n, c) = py_types[ret[0]]
425 c_return = " %s c_retval;\n" % (ret[0])
426 if file == "python_accessor" and ret[2] != None:
427 c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
429 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
430 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
431 ret_convert = ret_convert + " return(py_retval);\n"
432 elif py_return_types.has_key(ret[0]):
433 (f, t, n, c) = py_return_types[ret[0]]
434 c_return = " %s c_retval;\n" % (ret[0])
435 c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
436 ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
437 ret_convert = ret_convert + " return(py_retval);\n"
439 if skipped_types.has_key(ret[0]):
441 if unknown_types.has_key(ret[0]):
442 lst = unknown_types[ret[0]]
445 unknown_types[ret[0]] = [name]
448 if cond != None and cond != "":
449 include.write("#if %s\n" % cond)
450 export.write("#if %s\n" % cond)
451 output.write("#if %s\n" % cond)
453 include.write("PyObject * ")
454 include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name))
456 export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %
460 # Those have been manually generated
461 if cond != None and cond != "":
462 include.write("#endif\n")
463 export.write("#endif\n")
464 output.write("#endif\n")
466 if file == "python_accessor" and ret[0] != "void" and ret[2] is None:
467 # Those have been manually generated
468 if cond != None and cond != "":
469 include.write("#endif\n")
470 export.write("#endif\n")
471 output.write("#endif\n")
474 output.write("PyObject *\n")
475 output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
476 output.write(" PyObject *args")
478 output.write(" ATTRIBUTE_UNUSED")
479 output.write(") {\n")
481 output.write(" PyObject *py_retval;\n")
483 output.write(c_return)
487 output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
488 (format, format_args))
489 output.write(" return(NULL);\n")
491 output.write(c_convert)
494 output.write(ret_convert)
495 output.write("}\n\n")
496 if cond != None and cond != "":
497 include.write("#endif /* %s */\n" % cond)
498 export.write("#endif /* %s */\n" % cond)
499 output.write("#endif /* %s */\n" % cond)
504 global py_return_types
508 f = open(os.path.join(srcPref,"libxml2-api.xml"))
510 (parser, target) = getparser()
515 f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))
517 (parser, target) = getparser()
524 n = len(functions.keys())
525 print "Found %d functions in libxml2-api.xml" % (n)
527 py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
529 f = open(os.path.join(srcPref,"libxml2-python-api.xml"))
531 (parser, target) = getparser()
538 print "Found %d functions in libxml2-python-api.xml" % (
539 len(functions.keys()) - n)
544 include = open("libxml2-py.h", "w")
545 include.write("/* Generated */\n\n")
546 export = open("libxml2-export.c", "w")
547 export.write("/* Generated */\n\n")
548 wrapper = open("libxml2-py.c", "w")
549 wrapper.write("/* Generated */\n\n")
550 wrapper.write("#include <Python.h>\n")
551 wrapper.write("#include <libxml/xmlversion.h>\n")
552 wrapper.write("#include <libxml/tree.h>\n")
553 wrapper.write("#include <libxml/xmlschemastypes.h>\n")
554 wrapper.write("#include \"libxml_wrap.h\"\n")
555 wrapper.write("#include \"libxml2-py.h\"\n\n")
556 for function in sorted(functions.keys()):
557 ret = print_function_wrapper(function, wrapper, export, include)
560 del functions[function]
562 skipped = skipped + 1
563 del functions[function]
565 nb_wrap = nb_wrap + 1
570 print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap,
572 print "Missing type converters: "
573 for type in unknown_types.keys():
574 print "%s:%d " % (type, len(unknown_types[type])),
577 #######################################################################
579 # This part writes part of the Python front-end classes based on
580 # mapping rules between types and classes and also based on function
581 # renaming to get consistent function names at the Python level
583 #######################################################################
586 # The type automatically remapped to generated classes
589 "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
590 "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"),
591 "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
592 "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
593 "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
594 "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"),
595 "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
596 "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"),
597 "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
598 "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"),
599 "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
600 "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"),
601 "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
602 "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"),
603 "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
604 "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"),
605 "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
606 "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"),
607 "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
608 "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"),
609 "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
610 "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"),
611 "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
612 "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
613 "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
614 "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"),
615 "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
616 "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
617 "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
618 "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
619 "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
620 "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),
621 "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"),
622 "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"),
623 "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"),
624 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"),
625 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"),
626 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"),
627 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"),
628 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"),
629 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),
633 "xmlXPathObjectPtr": "xpathObjectRet(%s)",
636 primary_classes = ["xmlNode", "xmlDoc"]
639 "xmlNode" : "xmlCore",
640 "xmlDtd" : "xmlNode",
641 "xmlDoc" : "xmlNode",
642 "xmlAttr" : "xmlNode",
644 "xmlEntity" : "xmlNode",
645 "xmlElement" : "xmlNode",
646 "xmlAttribute" : "xmlNode",
647 "outputBuffer": "ioWriteWrapper",
648 "inputBuffer": "ioReadWrapper",
649 "parserCtxt": "parserCtxtCore",
650 "xmlTextReader": "xmlTextReaderCore",
651 "ValidCtxt": "ValidCtxtCore",
652 "SchemaValidCtxt": "SchemaValidCtxtCore",
653 "relaxNgValidCtxt": "relaxNgValidCtxtCore",
655 classes_destructors = {
656 "parserCtxt": "xmlFreeParserCtxt",
657 "catalog": "xmlFreeCatalog",
659 # "outputBuffer": "xmlOutputBufferClose",
660 "inputBuffer": "xmlFreeParserInputBuffer",
661 "xmlReg": "xmlRegFreeRegexp",
662 "xmlTextReader": "xmlFreeTextReader",
663 "relaxNgSchema": "xmlRelaxNGFree",
664 "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt",
665 "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt",
666 "Schema": "xmlSchemaFree",
667 "SchemaParserCtxt": "xmlSchemaFreeParserCtxt",
668 "SchemaValidCtxt": "xmlSchemaFreeValidCtxt",
669 "ValidCtxt": "xmlFreeValidCtxt",
672 functions_noexcept = {
675 "xmlDocSetRootElement": 1,
677 "xmlNodeGetNsDefs": 1,
678 "xmlNextElementSibling": 1,
679 "xmlPreviousElementSibling": 1,
680 "xmlFirstElementChild": 1,
681 "xmlLastElementChild": 1,
684 reference_keepers = {
685 "xmlTextReader": [('inputBuffer', 'input')],
686 "relaxNgValidCtxt": [('relaxNgSchema', 'schema')],
687 "SchemaValidCtxt": [('Schema', 'schema')],
690 function_classes = {}
692 function_classes["None"] = []
694 def nameFixup(name, classe, type, file):
695 listname = classe + "List"
698 if name[0:l] == listname:
700 func = string.lower(func[0:1]) + func[1:]
701 elif name[0:12] == "xmlParserGet" and file == "python_accessor":
703 func = string.lower(func[0:1]) + func[1:]
704 elif name[0:12] == "xmlParserSet" and file == "python_accessor":
706 func = string.lower(func[0:1]) + func[1:]
707 elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
709 func = string.lower(func[0:1]) + func[1:]
710 elif name[0:9] == "xmlURIGet" and file == "python_accessor":
712 func = string.lower(func[0:1]) + func[1:]
713 elif name[0:9] == "xmlURISet" and file == "python_accessor":
715 func = string.lower(func[0:1]) + func[1:]
716 elif name[0:11] == "xmlErrorGet" and file == "python_accessor":
718 func = string.lower(func[0:1]) + func[1:]
719 elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor":
721 func = string.lower(func[0:1]) + func[1:]
722 elif name[0:11] == "xmlXPathGet" and file == "python_accessor":
724 func = string.lower(func[0:1]) + func[1:]
725 elif name[0:11] == "xmlXPathSet" and file == "python_accessor":
727 func = string.lower(func[0:1]) + func[1:]
728 elif name[0:15] == "xmlOutputBuffer" and file != "python":
730 func = string.lower(func[0:1]) + func[1:]
731 elif name[0:20] == "xmlParserInputBuffer" and file != "python":
733 func = string.lower(func[0:1]) + func[1:]
734 elif name[0:9] == "xmlRegexp" and file == "xmlregexp":
735 func = "regexp" + name[9:]
736 elif name[0:6] == "xmlReg" and file == "xmlregexp":
737 func = "regexp" + name[6:]
738 elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader":
740 elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader":
742 elif name[0:13] == "xmlTextReader" and file == "xmlreader":
744 elif name[0:12] == "xmlReaderNew" and file == "xmlreader":
746 elif name[0:11] == "xmlACatalog":
748 func = string.lower(func[0:1]) + func[1:]
749 elif name[0:l] == classe:
751 func = string.lower(func[0:1]) + func[1:]
752 elif name[0:7] == "libxml_":
754 func = string.lower(func[0:1]) + func[1:]
755 elif name[0:6] == "xmlGet":
757 func = string.lower(func[0:1]) + func[1:]
758 elif name[0:3] == "xml":
760 func = string.lower(func[0:1]) + func[1:]
763 if func[0:5] == "xPath":
764 func = "xpath" + func[5:]
765 elif func[0:4] == "xPtr":
766 func = "xpointer" + func[4:]
767 elif func[0:8] == "xInclude":
768 func = "xinclude" + func[8:]
769 elif func[0:2] == "iD":
770 func = "ID" + func[2:]
771 elif func[0:3] == "uRI":
772 func = "URI" + func[3:]
773 elif func[0:4] == "uTF8":
774 func = "UTF8" + func[4:]
775 elif func[0:3] == 'sAX':
776 func = "SAX" + func[3:]
780 def functionCompare(info1, info2):
781 (index1, func1, name1, ret1, args1, file1) = info1
782 (index2, func2, name2, ret2, args2, file2) = info2
788 if file1 == "python_accessor":
790 if file2 == "python_accessor":
798 def writeDoc(name, args, indent, output):
799 if functions[name][0] is None or functions[name][0] == "":
801 val = functions[name][0]
802 val = string.replace(val, "NULL", "None")
810 i = string.rfind(str, " ")
819 output.write(' """\n')
824 global py_return_types
827 global function_classes
830 global converter_type
831 global primary_classes
832 global converter_type
833 global classes_ancestor
834 global converter_type
835 global primary_classes
836 global classes_ancestor
837 global classes_destructors
838 global functions_noexcept
840 for type in classes_type.keys():
841 function_classes[classes_type[type][2]] = []
844 # Build the list of C types to look for ordered to start
845 # with primary classes
849 ctypes_processed = {}
850 classes_processed = {}
851 for classe in primary_classes:
852 classes_list.append(classe)
853 classes_processed[classe] = ()
854 for type in classes_type.keys():
855 tinfo = classes_type[type]
856 if tinfo[2] == classe:
858 ctypes_processed[type] = ()
859 for type in sorted(classes_type.keys()):
860 if ctypes_processed.has_key(type):
862 tinfo = classes_type[type]
863 if not classes_processed.has_key(tinfo[2]):
864 classes_list.append(tinfo[2])
865 classes_processed[tinfo[2]] = ()
868 ctypes_processed[type] = ()
870 for name in functions.keys():
872 (desc, ret, args, file, cond) = functions[name]
874 classe = classes_type[type][2]
876 if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type:
878 func = nameFixup(name, classe, type, file)
879 info = (0, func, name, ret, args, file)
880 function_classes[classe].append(info)
881 elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \
882 and file != "python_accessor":
884 func = nameFixup(name, classe, type, file)
885 info = (1, func, name, ret, args, file)
886 function_classes[classe].append(info)
887 elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type:
889 func = nameFixup(name, classe, type, file)
890 info = (0, func, name, ret, args, file)
891 function_classes[classe].append(info)
892 elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \
893 and file != "python_accessor":
895 func = nameFixup(name, classe, type, file)
896 info = (1, func, name, ret, args, file)
897 function_classes[classe].append(info)
900 if name[0:8] == "xmlXPath":
902 if name[0:6] == "xmlStr":
904 if name[0:10] == "xmlCharStr":
906 func = nameFixup(name, "None", file, file)
907 info = (0, func, name, ret, args, file)
908 function_classes['None'].append(info)
910 classes = open("libxml2class.py", "w")
911 txt = open("libxml2class.txt", "w")
912 txt.write(" Generated Classes for libxml2-python\n\n")
914 txt.write("#\n# Global functions of the module\n#\n\n")
915 if function_classes.has_key("None"):
916 flist = function_classes["None"]
917 flist.sort(functionCompare)
920 (index, func, name, ret, args, file) = info
922 classes.write("#\n# Functions from module %s\n#\n\n" % file)
923 txt.write("\n# functions from module %s\n" % file)
925 classes.write("def %s(" % func)
926 txt.write("%s()\n" % func)
931 classes.write("%s" % arg[0])
933 classes.write("):\n")
934 writeDoc(name, args, ' ', classes)
937 if classes_type.has_key(arg[1]):
938 classes.write(" if %s is None: %s__o = None\n" %
940 classes.write(" else: %s__o = %s%s\n" %
941 (arg[0], arg[0], classes_type[arg[1]][0]))
943 classes.write(" ret = ")
946 classes.write("libxml2mod.%s(" % name)
951 classes.write("%s" % arg[0])
952 if classes_type.has_key(arg[1]):
957 if classes_type.has_key(ret[0]):
961 if functions_noexcept.has_key(name):
962 classes.write(" if ret is None:return None\n")
963 elif string.find(name, "URI") >= 0:
965 " if ret is None:raise uriError('%s() failed')\n"
967 elif string.find(name, "XPath") >= 0:
969 " if ret is None:raise xpathError('%s() failed')\n"
971 elif string.find(name, "Parse") >= 0:
973 " if ret is None:raise parserError('%s() failed')\n"
977 " if ret is None:raise treeError('%s() failed')\n"
979 classes.write(" return ")
980 classes.write(classes_type[ret[0]][1] % ("ret"))
983 classes.write(" return ret\n")
986 txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
987 for classname in classes_list:
988 if classname == "None":
991 if classes_ancestor.has_key(classname):
992 txt.write("\n\nClass %s(%s)\n" % (classname,
993 classes_ancestor[classname]))
994 classes.write("class %s(%s):\n" % (classname,
995 classes_ancestor[classname]))
996 classes.write(" def __init__(self, _obj=None):\n")
997 if classes_ancestor[classname] == "xmlCore" or \
998 classes_ancestor[classname] == "xmlNode":
999 classes.write(" if type(_obj).__name__ != ")
1000 classes.write("'PyCObject':\n")
1001 classes.write(" raise TypeError, ")
1002 classes.write("'%s needs a PyCObject argument'\n" % \
1004 if reference_keepers.has_key(classname):
1005 rlist = reference_keepers[classname]
1007 classes.write(" self.%s = None\n" % ref[1])
1008 classes.write(" self._o = _obj\n")
1009 classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
1010 classes_ancestor[classname]))
1011 if classes_ancestor[classname] == "xmlCore" or \
1012 classes_ancestor[classname] == "xmlNode":
1013 classes.write(" def __repr__(self):\n")
1014 format = "<%s (%%s) object at 0x%%x>" % (classname)
1015 classes.write(" return \"%s\" %% (self.name, long(pos_id (self)))\n\n" % (
1018 txt.write("Class %s()\n" % (classname))
1019 classes.write("class %s:\n" % (classname))
1020 classes.write(" def __init__(self, _obj=None):\n")
1021 if reference_keepers.has_key(classname):
1022 list = reference_keepers[classname]
1024 classes.write(" self.%s = None\n" % ref[1])
1025 classes.write(" if _obj != None:self._o = _obj;return\n")
1026 classes.write(" self._o = None\n\n")
1028 if classes_destructors.has_key(classname):
1029 classes.write(" def __del__(self):\n")
1030 classes.write(" if self._o != None:\n")
1031 classes.write(" libxml2mod.%s(self._o)\n" %
1032 classes_destructors[classname])
1033 classes.write(" self._o = None\n\n")
1034 destruct=classes_destructors[classname]
1035 flist = function_classes[classname]
1036 flist.sort(functionCompare)
1039 (index, func, name, ret, args, file) = info
1041 # Do not provide as method the destructors for the class
1042 # to avoid double free
1044 if name == destruct:
1047 if file == "python_accessor":
1048 classes.write(" # accessors for %s\n" % (classname))
1049 txt.write(" # accessors\n")
1051 classes.write(" #\n")
1052 classes.write(" # %s functions from module %s\n" % (
1054 txt.write("\n # functions from module %s\n" % file)
1055 classes.write(" #\n\n")
1057 classes.write(" def %s(self" % func)
1058 txt.write(" %s()\n" % func)
1062 classes.write(", %s" % arg[0])
1064 classes.write("):\n")
1065 writeDoc(name, args, ' ', classes)
1068 if classes_type.has_key(arg[1]):
1070 classes.write(" if %s is None: %s__o = None\n" %
1072 classes.write(" else: %s__o = %s%s\n" %
1073 (arg[0], arg[0], classes_type[arg[1]][0]))
1075 if ret[0] != "void":
1076 classes.write(" ret = ")
1079 classes.write("libxml2mod.%s(" % name)
1085 classes.write("%s" % arg[0])
1086 if classes_type.has_key(arg[1]):
1087 classes.write("__o")
1089 classes.write("self")
1090 if classes_type.has_key(arg[1]):
1091 classes.write(classes_type[arg[1]][0])
1093 classes.write(")\n")
1094 if ret[0] != "void":
1095 if classes_type.has_key(ret[0]):
1097 # Raise an exception
1099 if functions_noexcept.has_key(name):
1101 " if ret is None:return None\n")
1102 elif string.find(name, "URI") >= 0:
1104 " if ret is None:raise uriError('%s() failed')\n"
1106 elif string.find(name, "XPath") >= 0:
1108 " if ret is None:raise xpathError('%s() failed')\n"
1110 elif string.find(name, "Parse") >= 0:
1112 " if ret is None:raise parserError('%s() failed')\n"
1116 " if ret is None:raise treeError('%s() failed')\n"
1120 # generate the returned class wrapper for the object
1122 classes.write(" __tmp = ")
1123 classes.write(classes_type[ret[0]][1] % ("ret"))
1127 # Sometime one need to keep references of the source
1128 # class in the returned class object.
1129 # See reference_keepers for the list
1131 tclass = classes_type[ret[0]][2]
1132 if reference_keepers.has_key(tclass):
1133 list = reference_keepers[tclass]
1135 if pref[0] == classname:
1136 classes.write(" __tmp.%s = self\n" %
1141 classes.write(" return __tmp\n")
1142 elif converter_type.has_key(ret[0]):
1144 # Raise an exception
1146 if functions_noexcept.has_key(name):
1148 " if ret is None:return None")
1149 elif string.find(name, "URI") >= 0:
1151 " if ret is None:raise uriError('%s() failed')\n"
1153 elif string.find(name, "XPath") >= 0:
1155 " if ret is None:raise xpathError('%s() failed')\n"
1157 elif string.find(name, "Parse") >= 0:
1159 " if ret is None:raise parserError('%s() failed')\n"
1163 " if ret is None:raise treeError('%s() failed')\n"
1165 classes.write(" return ")
1166 classes.write(converter_type[ret[0]] % ("ret"))
1169 classes.write(" return ret\n")
1173 # Generate enum constants
1175 for type,enum in enums.items():
1176 classes.write("# %s\n" % type)
1177 items = enum.items()
1178 items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1])))
1179 for name,value in items:
1180 classes.write("%s = %s\n" % (name,value))