📄 generator.py
字号:
skipped = 0 include = open("libxml2-py.h", "w") include.write("/* Generated */\n\n") export = open("libxml2-export.c", "w") export.write("/* Generated */\n\n") wrapper = open("libxml2-py.c", "w") wrapper.write("/* Generated */\n\n") wrapper.write("#include <Python.h>\n") wrapper.write("#include <libxml/xmlversion.h>\n") wrapper.write("#include <libxml/tree.h>\n") wrapper.write("#include <libxml/xmlschemastypes.h>\n") wrapper.write("#include \"libxml_wrap.h\"\n") wrapper.write("#include \"libxml2-py.h\"\n\n") for function in functions.keys(): ret = print_function_wrapper(function, wrapper, export, include) if ret < 0: failed = failed + 1 del functions[function] if ret == 0: skipped = skipped + 1 del functions[function] if ret == 1: nb_wrap = nb_wrap + 1 include.close() export.close() wrapper.close() print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap, failed, skipped); print "Missing type converters: " for type in unknown_types.keys(): print "%s:%d " % (type, len(unknown_types[type])), print######################################################################### This part writes part of the Python front-end classes based on# mapping rules between types and classes and also based on function# renaming to get consistent function names at the Python level########################################################################## The type automatically remapped to generated classes#classes_type = { "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"), "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"), "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"), "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"), "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"), "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"), "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"), "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"), "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"), "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"), "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"), "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"), "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"), "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"), "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"), "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"), "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"), 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"), 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"), 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"), 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"), 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"), 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"),}converter_type = { "xmlXPathObjectPtr": "xpathObjectRet(%s)",}primary_classes = ["xmlNode", "xmlDoc"]classes_ancestor = { "xmlNode" : "xmlCore", "xmlDtd" : "xmlNode", "xmlDoc" : "xmlNode", "xmlAttr" : "xmlNode", "xmlNs" : "xmlNode", "xmlEntity" : "xmlNode", "xmlElement" : "xmlNode", "xmlAttribute" : "xmlNode", "outputBuffer": "ioWriteWrapper", "inputBuffer": "ioReadWrapper", "parserCtxt": "parserCtxtCore", "xmlTextReader": "xmlTextReaderCore", "ValidCtxt": "ValidCtxtCore", "SchemaValidCtxt": "SchemaValidCtxtCore", "relaxNgValidCtxt": "relaxNgValidCtxtCore",}classes_destructors = { "parserCtxt": "xmlFreeParserCtxt", "catalog": "xmlFreeCatalog", "URI": "xmlFreeURI",# "outputBuffer": "xmlOutputBufferClose", "inputBuffer": "xmlFreeParserInputBuffer", "xmlReg": "xmlRegFreeRegexp", "xmlTextReader": "xmlFreeTextReader", "relaxNgSchema": "xmlRelaxNGFree", "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt", "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt", "Schema": "xmlSchemaFree", "SchemaParserCtxt": "xmlSchemaFreeParserCtxt", "SchemaValidCtxt": "xmlSchemaFreeValidCtxt", "ValidCtxt": "xmlFreeValidCtxt",}functions_noexcept = { "xmlHasProp": 1, "xmlHasNsProp": 1, "xmlDocSetRootElement": 1, "xmlNodeGetNs": 1, "xmlNodeGetNsDefs": 1,}reference_keepers = { "xmlTextReader": [('inputBuffer', 'input')], "relaxNgValidCtxt": [('relaxNgSchema', 'schema')], "SchemaValidCtxt": [('Schema', 'schema')],}function_classes = {}function_classes["None"] = []def nameFixup(name, classe, type, file): listname = classe + "List" ll = len(listname) l = len(classe) if name[0:l] == listname: func = name[l:] func = string.lower(func[0:1]) + func[1:] elif name[0:12] == "xmlParserGet" and file == "python_accessor": func = name[12:] func = string.lower(func[0:1]) + func[1:] elif name[0:12] == "xmlParserSet" and file == "python_accessor": func = name[12:] func = string.lower(func[0:1]) + func[1:] elif name[0:10] == "xmlNodeGet" and file == "python_accessor": func = name[10:] func = string.lower(func[0:1]) + func[1:] elif name[0:9] == "xmlURIGet" and file == "python_accessor": func = name[9:] func = string.lower(func[0:1]) + func[1:] elif name[0:9] == "xmlURISet" and file == "python_accessor": func = name[6:] func = string.lower(func[0:1]) + func[1:] elif name[0:11] == "xmlErrorGet" and file == "python_accessor": func = name[11:] func = string.lower(func[0:1]) + func[1:] elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor": func = name[17:] func = string.lower(func[0:1]) + func[1:] elif name[0:11] == "xmlXPathGet" and file == "python_accessor": func = name[11:] func = string.lower(func[0:1]) + func[1:] elif name[0:11] == "xmlXPathSet" and file == "python_accessor": func = name[8:] func = string.lower(func[0:1]) + func[1:] elif name[0:15] == "xmlOutputBuffer" and file != "python": func = name[15:] func = string.lower(func[0:1]) + func[1:] elif name[0:20] == "xmlParserInputBuffer" and file != "python": func = name[20:] func = string.lower(func[0:1]) + func[1:] elif name[0:9] == "xmlRegexp" and file == "xmlregexp": func = "regexp" + name[9:] elif name[0:6] == "xmlReg" and file == "xmlregexp": func = "regexp" + name[6:] elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader": func = name[20:] elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader": func = name[18:] elif name[0:13] == "xmlTextReader" and file == "xmlreader": func = name[13:] elif name[0:12] == "xmlReaderNew" and file == "xmlreader": func = name[9:] elif name[0:11] == "xmlACatalog": func = name[11:] func = string.lower(func[0:1]) + func[1:] elif name[0:l] == classe: func = name[l:] func = string.lower(func[0:1]) + func[1:] elif name[0:7] == "libxml_": func = name[7:] func = string.lower(func[0:1]) + func[1:] elif name[0:6] == "xmlGet": func = name[6:] func = string.lower(func[0:1]) + func[1:] elif name[0:3] == "xml": func = name[3:] func = string.lower(func[0:1]) + func[1:] else: func = name if func[0:5] == "xPath": func = "xpath" + func[5:] elif func[0:4] == "xPtr": func = "xpointer" + func[4:] elif func[0:8] == "xInclude": func = "xinclude" + func[8:] elif func[0:2] == "iD": func = "ID" + func[2:] elif func[0:3] == "uRI": func = "URI" + func[3:] elif func[0:4] == "uTF8": func = "UTF8" + func[4:] elif func[0:3] == 'sAX': func = "SAX" + func[3:] return funcdef functionCompare(info1, info2): (index1, func1, name1, ret1, args1, file1) = info1 (index2, func2, name2, ret2, args2, file2) = info2 if file1 == file2: if func1 < func2: return -1 if func1 > func2: return 1 if file1 == "python_accessor": return -1 if file2 == "python_accessor": return 1 if file1 < file2: return -1 if file1 > file2: return 1 return 0def writeDoc(name, args, indent, output): if functions[name][0] is None or functions[name][0] == "": return val = functions[name][0] val = string.replace(val, "NULL", "None"); output.write(indent) output.write('"""') while len(val) > 60: str = val[0:60] i = string.rfind(str, " "); if i < 0: i = 60 str = val[0:i] val = val[i:] output.write(str) output.write('\n '); output.write(indent) output.write(val); output.write(' """\n')def buildWrappers(): global ctypes global py_types global py_return_types global unknown_types global functions global function_classes global classes_type global classes_list global converter_type global primary_classes global converter_type global classes_ancestor global converter_type global primary_classes global classes_ancestor global classes_destructors global functions_noexcept for type in classes_type.keys(): function_classes[classes_type[type][2]] = [] # # Build the list of C types to look for ordered to start # with primary classes # ctypes = [] classes_list = [] ctypes_processed = {} classes_processed = {} for classe in primary_classes: classes_list.append(classe) classes_processed[classe] = () for type in classes_type.keys(): tinfo = classes_type[type] if tinfo[2] == classe: ctypes.append(type) ctypes_processed[type] = () for type in classes_type.keys(): if ctypes_processed.has_key(type): continue tinfo = classes_type[type] if not classes_processed.has_key(tinfo[2]): classes_list.append(tinfo[2]) classes_processed[tinfo[2]] = () ctypes.append(type) ctypes_processed[type] = ()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -