⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generator.py

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 PY
📖 第 1 页 / 共 3 页
字号:
	# Don't delete the function entry in the caller.	return 1    c_call = "";    format=""    format_args=""    c_args=""    c_return=""    c_convert=""    num_bufs=0    for arg in args:        # This should be correct        if arg[1][0:6] == "const ":            arg[1] = arg[1][6:]        c_args = c_args + "    %s %s;\n" % (arg[1], arg[0])        if py_types.has_key(arg[1]):            (f, t, n, c) = py_types[arg[1]]	    if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):	        f = 't#'            if f != None:                format = format + f            if t != None:                format_args = format_args + ", &pyobj_%s" % (arg[0])                c_args = c_args + "    PyObject *pyobj_%s;\n" % (arg[0])                c_convert = c_convert + \                   "    %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0],                   arg[1], t, arg[0]);            else:                format_args = format_args + ", &%s" % (arg[0])	    if f == 't#':	        format_args = format_args + ", &py_buffsize%d" % num_bufs	        c_args = c_args + "    int py_buffsize%d;\n" % num_bufs		num_bufs = num_bufs + 1            if c_call != "":                c_call = c_call + ", ";            c_call = c_call + "%s" % (arg[0])        else:            if skipped_types.has_key(arg[1]):                return 0            if unknown_types.has_key(arg[1]):                lst = unknown_types[arg[1]]                lst.append(name)            else:                unknown_types[arg[1]] = [name]            return -1    if format != "":        format = format + ":%s" % (name)    if ret[0] == 'void':        if file == "python_accessor":	    if args[1][1] == "char *" or args[1][1] == "xmlChar *":		c_call = "\n    if (%s->%s != NULL) xmlFree(%s->%s);\n" % (		                 args[0][0], args[1][0], args[0][0], args[1][0])		c_call = c_call + "    %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0],		                 args[1][0], args[1][1], args[1][0])	    else:		c_call = "\n    %s->%s = %s;\n" % (args[0][0], args[1][0],						   args[1][0])        else:            c_call = "\n    %s(%s);\n" % (name, c_call);        ret_convert = "    Py_INCREF(Py_None);\n    return(Py_None);\n"    elif py_types.has_key(ret[0]):        (f, t, n, c) = py_types[ret[0]]        c_return = "    %s c_retval;\n" % (ret[0])        if file == "python_accessor" and ret[2] != None:            c_call = "\n    c_retval = %s->%s;\n" % (args[0][0], ret[2])        else:            c_call = "\n    c_retval = %s(%s);\n" % (name, c_call);        ret_convert = "    py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)        ret_convert = ret_convert + "    return(py_retval);\n"    elif py_return_types.has_key(ret[0]):        (f, t, n, c) = py_return_types[ret[0]]        c_return = "    %s c_retval;\n" % (ret[0])        c_call = "\n    c_retval = %s(%s);\n" % (name, c_call);        ret_convert = "    py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)        ret_convert = ret_convert + "    return(py_retval);\n"    else:        if skipped_types.has_key(ret[0]):            return 0        if unknown_types.has_key(ret[0]):            lst = unknown_types[ret[0]]            lst.append(name)        else:            unknown_types[ret[0]] = [name]        return -1    if cond != None and cond != "":        include.write("#if %s\n" % cond)        export.write("#if %s\n" % cond)        output.write("#if %s\n" % cond)    include.write("PyObject * ")    include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name));    export.write("    { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" %                 (name, name))    if file == "python":        # Those have been manually generated	if cond != None and cond != "":	    include.write("#endif\n");	    export.write("#endif\n");	    output.write("#endif\n");        return 1    if file == "python_accessor" and ret[0] != "void" and ret[2] is None:        # Those have been manually generated	if cond != None and cond != "":	    include.write("#endif\n");	    export.write("#endif\n");	    output.write("#endif\n");        return 1    output.write("PyObject *\n")    output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))    output.write(" PyObject *args")    if format == "":	output.write(" ATTRIBUTE_UNUSED")    output.write(") {\n")    if ret[0] != 'void':        output.write("    PyObject *py_retval;\n")    if c_return != "":        output.write(c_return)    if c_args != "":        output.write(c_args)    if format != "":        output.write("\n    if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %                     (format, format_args))        output.write("        return(NULL);\n")    if c_convert != "":        output.write(c_convert)                                                                  output.write(c_call)    output.write(ret_convert)    output.write("}\n\n")    if cond != None and cond != "":        include.write("#endif /* %s */\n" % cond)        export.write("#endif /* %s */\n" % cond)        output.write("#endif /* %s */\n" % cond)    return 1def buildStubs():    global py_types    global py_return_types    global unknown_types    try:	f = open(os.path.join(srcPref,"libxml2-api.xml"))	data = f.read()	(parser, target)  = getparser()	parser.feed(data)	parser.close()    except IOError, msg:	try:	    f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml"))	    data = f.read()	    (parser, target)  = getparser()	    parser.feed(data)	    parser.close()	except IOError, msg:	    print file, ":", msg	    sys.exit(1)    n = len(functions.keys())    print "Found %d functions in libxml2-api.xml" % (n)    py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")    try:	f = open(os.path.join(srcPref,"libxml2-python-api.xml"))	data = f.read()	(parser, target)  = getparser()	parser.feed(data)	parser.close()    except IOError, msg:	print file, ":", msg    print "Found %d functions in libxml2-python-api.xml" % (	  len(functions.keys()) - n)    nb_wrap = 0    failed = 0    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:]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -