📄 generator.py
字号:
#!/usr/bin/python -u## generate python wrappers from the XML API description#functions = {}enums = {} # { enumType: { enumConstant: enumValue } }import osimport sysimport stringif __name__ == "__main__": # launched as a script srcPref = os.path.dirname(sys.argv[0])else: # imported srcPref = os.path.dirname(__file__)######################################################################### That part if purely the API acquisition phase from the# XML API description########################################################################import osimport xmllibtry: import sgmlopexcept ImportError: sgmlop = None # accelerator not availabledebug = 0if sgmlop: class FastParser: """sgmlop based XML parser. this is typically 15x faster than SlowParser...""" def __init__(self, target): # setup callbacks self.finish_starttag = target.start self.finish_endtag = target.end self.handle_data = target.data # activate parser self.parser = sgmlop.XMLParser() self.parser.register(self) self.feed = self.parser.feed self.entity = { "amp": "&", "gt": ">", "lt": "<", "apos": "'", "quot": '"' } def close(self): try: self.parser.close() finally: self.parser = self.feed = None # nuke circular reference def handle_entityref(self, entity): # <string> entity try: self.handle_data(self.entity[entity]) except KeyError: self.handle_data("&%s;" % entity)else: FastParser = Noneclass SlowParser(xmllib.XMLParser): """slow but safe standard parser, based on the XML parser in Python's standard library.""" def __init__(self, target): self.unknown_starttag = target.start self.handle_data = target.data self.unknown_endtag = target.end xmllib.XMLParser.__init__(self)def getparser(target = None): # get the fastest available parser, and attach it to an # unmarshalling object. return both objects. if target is None: target = docParser() if FastParser: return FastParser(target), target return SlowParser(target), targetclass docParser: def __init__(self): self._methodname = None self._data = [] self.in_function = 0 def close(self): if debug: print "close" def getmethodname(self): return self._methodname def data(self, text): if debug: print "data %s" % text self._data.append(text) def start(self, tag, attrs): if debug: print "start %s, %s" % (tag, attrs) if tag == 'function': self._data = [] self.in_function = 1 self.function = None self.function_args = [] self.function_descr = None self.function_return = None self.function_file = None if attrs.has_key('name'): self.function = attrs['name'] if attrs.has_key('file'): self.function_file = attrs['file'] elif tag == 'info': self._data = [] elif tag == 'arg': if self.in_function == 1: self.function_arg_name = None self.function_arg_type = None self.function_arg_info = None if attrs.has_key('name'): self.function_arg_name = attrs['name'] if attrs.has_key('type'): self.function_arg_type = attrs['type'] if attrs.has_key('info'): self.function_arg_info = attrs['info'] elif tag == 'return': if self.in_function == 1: self.function_return_type = None self.function_return_info = None self.function_return_field = None if attrs.has_key('type'): self.function_return_type = attrs['type'] if attrs.has_key('info'): self.function_return_info = attrs['info'] if attrs.has_key('field'): self.function_return_field = attrs['field'] elif tag == 'enum': enum(attrs['type'],attrs['name'],attrs['value']) def end(self, tag): if debug: print "end %s" % tag if tag == 'function': if self.function != None: function(self.function, self.function_descr, self.function_return, self.function_args, self.function_file) self.in_function = 0 elif tag == 'arg': if self.in_function == 1: self.function_args.append([self.function_arg_name, self.function_arg_type, self.function_arg_info]) elif tag == 'return': if self.in_function == 1: self.function_return = [self.function_return_type, self.function_return_info, self.function_return_field] elif tag == 'info': str = '' for c in self._data: str = str + c if self.in_function == 1: self.function_descr = str def function(name, desc, ret, args, file): functions[name] = (desc, ret, args, file)def enum(type, name, value): if not enums.has_key(type): enums[type] = {} enums[type][name] = value######################################################################### Some filtering rukes to drop functions/types which should not# be exposed as-is on the Python interface########################################################################skipped_modules = { 'xmlmemory': None, 'DOCBparser': None, 'SAX': None, 'hash': None, 'list': None, 'threads': None,# 'xpointer': None,}skipped_types = { 'int *': "usually a return type", 'xmlSAXHandlerPtr': "not the proper interface for SAX", 'htmlSAXHandlerPtr': "not the proper interface for SAX", 'xmlRMutexPtr': "thread specific, skipped", 'xmlMutexPtr': "thread specific, skipped", 'xmlGlobalStatePtr': "thread specific, skipped", 'xmlListPtr': "internal representation not suitable for python", 'xmlBufferPtr': "internal representation not suitable for python", 'FILE *': None,}######################################################################### Table of remapping to/from the python type or class to the C# counterpart.########################################################################py_types = { 'void': (None, None, None, None), 'int': ('i', None, "int", "int"), 'long': ('i', None, "int", "int"), 'double': ('d', None, "double", "double"), 'unsigned int': ('i', None, "int", "int"), 'xmlChar': ('c', None, "int", "int"), 'unsigned char *': ('z', None, "charPtr", "char *"), 'char *': ('z', None, "charPtr", "char *"), 'const char *': ('z', None, "charPtrConst", "const char *"), 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"), 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"), 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"), 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"), 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"), 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"), 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"), 'FILE *': ('O', "File", "FILEPtr", "FILE *"), 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"), 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"), 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"), 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"), 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"), 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"), 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"), 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"), 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"), 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"), 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"), 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"), 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"),}py_return_types = { 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"),}unknown_types = {}foreign_encoding_args = ( 'htmlCreateMemoryParserCtxt', 'htmlCtxtReadMemory', 'htmlParseChunk', 'htmlReadMemory', 'xmlCreateMemoryParserCtxt', 'xmlCtxtReadMemory', 'xmlCtxtResetPush', 'xmlParseChunk', 'xmlParseMemory', 'xmlReadMemory', 'xmlRecoverMemory',)########################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -