Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | # |
| 3 | # generate python wrappers from the XML API description |
| 4 | # |
| 5 | |
| 6 | functions = {} |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 7 | enums = {} # { enumType: { enumConstant: enumValue } } |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 8 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 9 | import os |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 10 | import sys |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 11 | import string |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 12 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 13 | if __name__ == "__main__": |
| 14 | # launched as a script |
| 15 | srcPref = os.path.dirname(sys.argv[0]) |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 16 | else: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 17 | # imported |
| 18 | srcPref = os.path.dirname(__file__) |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 19 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 20 | ####################################################################### |
| 21 | # |
| 22 | # That part if purely the API acquisition phase from the |
| 23 | # XML API description |
| 24 | # |
| 25 | ####################################################################### |
| 26 | import os |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 27 | import xmllib |
| 28 | try: |
| 29 | import sgmlop |
| 30 | except ImportError: |
| 31 | sgmlop = None # accelerator not available |
| 32 | |
| 33 | debug = 0 |
| 34 | |
| 35 | if sgmlop: |
| 36 | class FastParser: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 37 | """sgmlop based XML parser. this is typically 15x faster |
| 38 | than SlowParser...""" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 40 | def __init__(self, target): |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 41 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 42 | # setup callbacks |
| 43 | self.finish_starttag = target.start |
| 44 | self.finish_endtag = target.end |
| 45 | self.handle_data = target.data |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 46 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 47 | # activate parser |
| 48 | self.parser = sgmlop.XMLParser() |
| 49 | self.parser.register(self) |
| 50 | self.feed = self.parser.feed |
| 51 | self.entity = { |
| 52 | "amp": "&", "gt": ">", "lt": "<", |
| 53 | "apos": "'", "quot": '"' |
| 54 | } |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 56 | def close(self): |
| 57 | try: |
| 58 | self.parser.close() |
| 59 | finally: |
| 60 | self.parser = self.feed = None # nuke circular reference |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 62 | def handle_entityref(self, entity): |
| 63 | # <string> entity |
| 64 | try: |
| 65 | self.handle_data(self.entity[entity]) |
| 66 | except KeyError: |
| 67 | self.handle_data("&%s;" % entity) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 68 | |
| 69 | else: |
| 70 | FastParser = None |
| 71 | |
| 72 | |
| 73 | class SlowParser(xmllib.XMLParser): |
| 74 | """slow but safe standard parser, based on the XML parser in |
| 75 | Python's standard library.""" |
| 76 | |
| 77 | def __init__(self, target): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 78 | self.unknown_starttag = target.start |
| 79 | self.handle_data = target.data |
| 80 | self.unknown_endtag = target.end |
| 81 | xmllib.XMLParser.__init__(self) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 82 | |
| 83 | def getparser(target = None): |
| 84 | # get the fastest available parser, and attach it to an |
| 85 | # unmarshalling object. return both objects. |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 86 | if target is None: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 87 | target = docParser() |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 88 | if FastParser: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 89 | return FastParser(target), target |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 90 | return SlowParser(target), target |
| 91 | |
| 92 | class docParser: |
| 93 | def __init__(self): |
| 94 | self._methodname = None |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 95 | self._data = [] |
| 96 | self.in_function = 0 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 97 | |
| 98 | def close(self): |
| 99 | if debug: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 100 | print "close" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 101 | |
| 102 | def getmethodname(self): |
| 103 | return self._methodname |
| 104 | |
| 105 | def data(self, text): |
| 106 | if debug: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 107 | print "data %s" % text |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 108 | self._data.append(text) |
| 109 | |
| 110 | def start(self, tag, attrs): |
| 111 | if debug: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 112 | print "start %s, %s" % (tag, attrs) |
| 113 | if tag == 'function': |
| 114 | self._data = [] |
| 115 | self.in_function = 1 |
| 116 | self.function = None |
| 117 | self.function_args = [] |
| 118 | self.function_descr = None |
| 119 | self.function_return = None |
| 120 | self.function_file = None |
| 121 | if attrs.has_key('name'): |
| 122 | self.function = attrs['name'] |
| 123 | if attrs.has_key('file'): |
| 124 | self.function_file = attrs['file'] |
| 125 | elif tag == 'info': |
| 126 | self._data = [] |
| 127 | elif tag == 'arg': |
| 128 | if self.in_function == 1: |
| 129 | self.function_arg_name = None |
| 130 | self.function_arg_type = None |
| 131 | self.function_arg_info = None |
| 132 | if attrs.has_key('name'): |
| 133 | self.function_arg_name = attrs['name'] |
| 134 | if attrs.has_key('type'): |
| 135 | self.function_arg_type = attrs['type'] |
| 136 | if attrs.has_key('info'): |
| 137 | self.function_arg_info = attrs['info'] |
| 138 | elif tag == 'return': |
| 139 | if self.in_function == 1: |
| 140 | self.function_return_type = None |
| 141 | self.function_return_info = None |
| 142 | self.function_return_field = None |
| 143 | if attrs.has_key('type'): |
| 144 | self.function_return_type = attrs['type'] |
| 145 | if attrs.has_key('info'): |
| 146 | self.function_return_info = attrs['info'] |
| 147 | if attrs.has_key('field'): |
| 148 | self.function_return_field = attrs['field'] |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 149 | elif tag == 'enum': |
| 150 | enum(attrs['type'],attrs['name'],attrs['value']) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 151 | |
| 152 | def end(self, tag): |
| 153 | if debug: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 154 | print "end %s" % tag |
| 155 | if tag == 'function': |
| 156 | if self.function != None: |
| 157 | function(self.function, self.function_descr, |
| 158 | self.function_return, self.function_args, |
| 159 | self.function_file) |
| 160 | self.in_function = 0 |
| 161 | elif tag == 'arg': |
| 162 | if self.in_function == 1: |
| 163 | self.function_args.append([self.function_arg_name, |
| 164 | self.function_arg_type, |
| 165 | self.function_arg_info]) |
| 166 | elif tag == 'return': |
| 167 | if self.in_function == 1: |
| 168 | self.function_return = [self.function_return_type, |
| 169 | self.function_return_info, |
| 170 | self.function_return_field] |
| 171 | elif tag == 'info': |
| 172 | str = '' |
| 173 | for c in self._data: |
| 174 | str = str + c |
| 175 | if self.in_function == 1: |
| 176 | self.function_descr = str |
| 177 | |
| 178 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 179 | def function(name, desc, ret, args, file): |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 180 | functions[name] = (desc, ret, args, file) |
| 181 | |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 182 | def enum(type, name, value): |
| 183 | if not enums.has_key(type): |
| 184 | enums[type] = {} |
| 185 | enums[type][name] = value |
| 186 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 187 | ####################################################################### |
| 188 | # |
| 189 | # Some filtering rukes to drop functions/types which should not |
| 190 | # be exposed as-is on the Python interface |
| 191 | # |
| 192 | ####################################################################### |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 193 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 194 | skipped_modules = { |
| 195 | 'xmlmemory': None, |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 196 | 'DOCBparser': None, |
| 197 | 'SAX': None, |
| 198 | 'hash': None, |
| 199 | 'list': None, |
| 200 | 'threads': None, |
Daniel Veillard | ff12c49 | 2003-01-23 16:42:55 +0000 | [diff] [blame] | 201 | # 'xpointer': None, |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 202 | } |
| 203 | skipped_types = { |
| 204 | 'int *': "usually a return type", |
| 205 | 'xmlSAXHandlerPtr': "not the proper interface for SAX", |
| 206 | 'htmlSAXHandlerPtr': "not the proper interface for SAX", |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 207 | 'xmlRMutexPtr': "thread specific, skipped", |
| 208 | 'xmlMutexPtr': "thread specific, skipped", |
| 209 | 'xmlGlobalStatePtr': "thread specific, skipped", |
| 210 | 'xmlListPtr': "internal representation not suitable for python", |
| 211 | 'xmlBufferPtr': "internal representation not suitable for python", |
| 212 | 'FILE *': None, |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 213 | } |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 214 | |
| 215 | ####################################################################### |
| 216 | # |
| 217 | # Table of remapping to/from the python type or class to the C |
| 218 | # counterpart. |
| 219 | # |
| 220 | ####################################################################### |
| 221 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 222 | py_types = { |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 223 | 'void': (None, None, None, None), |
| 224 | 'int': ('i', None, "int", "int"), |
| 225 | 'long': ('i', None, "int", "int"), |
| 226 | 'double': ('d', None, "double", "double"), |
| 227 | 'unsigned int': ('i', None, "int", "int"), |
| 228 | 'xmlChar': ('c', None, "int", "int"), |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 229 | 'unsigned char *': ('z', None, "charPtr", "char *"), |
| 230 | 'char *': ('z', None, "charPtr", "char *"), |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 231 | 'const char *': ('z', None, "charPtrConst", "const char *"), |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 232 | 'xmlChar *': ('z', None, "xmlCharPtr", "xmlChar *"), |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 233 | 'const xmlChar *': ('z', None, "xmlCharPtrConst", "const xmlChar *"), |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 234 | 'xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 235 | 'const xmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 236 | 'xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 237 | 'const xmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 238 | 'xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 239 | 'const xmlDtdPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 240 | 'xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 241 | 'const xmlDtd *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 242 | 'xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 243 | 'const xmlAttrPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 244 | 'xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 245 | 'const xmlAttr *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 246 | 'xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 247 | 'const xmlEntityPtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 248 | 'xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 249 | 'const xmlEntity *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 250 | 'xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), |
| 251 | 'const xmlElementPtr': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), |
| 252 | 'xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), |
| 253 | 'const xmlElement *': ('O', "xmlElement", "xmlElementPtr", "xmlElementPtr"), |
| 254 | 'xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), |
| 255 | 'const xmlAttributePtr': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), |
| 256 | 'xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), |
| 257 | 'const xmlAttribute *': ('O', "xmlAttribute", "xmlAttributePtr", "xmlAttributePtr"), |
| 258 | 'xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), |
| 259 | 'const xmlNsPtr': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), |
| 260 | 'xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), |
| 261 | 'const xmlNs *': ('O', "xmlNode", "xmlNsPtr", "xmlNsPtr"), |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 262 | 'xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 263 | 'const xmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 264 | 'xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 265 | 'const xmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 266 | 'htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 267 | 'const htmlDocPtr': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 268 | 'htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 269 | 'const htmlDoc *': ('O', "xmlNode", "xmlDocPtr", "xmlDocPtr"), |
| 270 | 'htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 271 | 'const htmlNodePtr': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 272 | 'htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
| 273 | 'const htmlNode *': ('O', "xmlNode", "xmlNodePtr", "xmlNodePtr"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 274 | 'xmlXPathContextPtr': ('O', "xmlXPathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"), |
| 275 | 'xmlXPathContext *': ('O', "xpathContext", "xmlXPathContextPtr", "xmlXPathContextPtr"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 276 | 'xmlXPathParserContextPtr': ('O', "xmlXPathParserContext", "xmlXPathParserContextPtr", "xmlXPathParserContextPtr"), |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 277 | 'xmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), |
| 278 | 'xmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), |
| 279 | 'htmlParserCtxtPtr': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), |
| 280 | 'htmlParserCtxt *': ('O', "parserCtxt", "xmlParserCtxtPtr", "xmlParserCtxtPtr"), |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 281 | 'xmlValidCtxtPtr': ('O', "ValidCtxt", "xmlValidCtxtPtr", "xmlValidCtxtPtr"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 282 | 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"), |
| 283 | 'FILE *': ('O', "File", "FILEPtr", "FILE *"), |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 284 | 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"), |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 285 | 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"), |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 286 | 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"), |
| 287 | 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"), |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 288 | 'xmlRegexpPtr': ('O', "xmlReg", "xmlRegexpPtr", "xmlRegexpPtr"), |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 289 | 'xmlTextReaderLocatorPtr': ('O', "xmlTextReaderLocator", "xmlTextReaderLocatorPtr", "xmlTextReaderLocatorPtr"), |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 290 | 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"), |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 291 | 'xmlRelaxNGPtr': ('O', "relaxNgSchema", "xmlRelaxNGPtr", "xmlRelaxNGPtr"), |
| 292 | 'xmlRelaxNGParserCtxtPtr': ('O', "relaxNgParserCtxt", "xmlRelaxNGParserCtxtPtr", "xmlRelaxNGParserCtxtPtr"), |
| 293 | 'xmlRelaxNGValidCtxtPtr': ('O', "relaxNgValidCtxt", "xmlRelaxNGValidCtxtPtr", "xmlRelaxNGValidCtxtPtr"), |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 294 | 'xmlSchemaPtr': ('O', "Schema", "xmlSchemaPtr", "xmlSchemaPtr"), |
| 295 | 'xmlSchemaParserCtxtPtr': ('O', "SchemaParserCtxt", "xmlSchemaParserCtxtPtr", "xmlSchemaParserCtxtPtr"), |
| 296 | 'xmlSchemaValidCtxtPtr': ('O', "SchemaValidCtxt", "xmlSchemaValidCtxtPtr", "xmlSchemaValidCtxtPtr"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | py_return_types = { |
| 300 | 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"), |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | unknown_types = {} |
| 304 | |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 305 | foreign_encoding_args = ( |
William M. Brack | ff34911 | 2004-12-24 08:39:13 +0000 | [diff] [blame] | 306 | 'htmlCreateMemoryParserCtxt', |
| 307 | 'htmlCtxtReadMemory', |
| 308 | 'htmlParseChunk', |
| 309 | 'htmlReadMemory', |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 310 | 'xmlCreateMemoryParserCtxt', |
William M. Brack | ff34911 | 2004-12-24 08:39:13 +0000 | [diff] [blame] | 311 | 'xmlCtxtReadMemory', |
| 312 | 'xmlCtxtResetPush', |
| 313 | 'xmlParseChunk', |
| 314 | 'xmlParseMemory', |
| 315 | 'xmlReadMemory', |
| 316 | 'xmlRecoverMemory', |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 317 | ) |
| 318 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 319 | ####################################################################### |
| 320 | # |
| 321 | # This part writes the C <-> Python stubs libxml2-py.[ch] and |
| 322 | # the table libxml2-export.c to add when registrering the Python module |
| 323 | # |
| 324 | ####################################################################### |
| 325 | |
Daniel Veillard | 263ec86 | 2004-10-04 10:26:54 +0000 | [diff] [blame] | 326 | # Class methods which are written by hand in libxml.c but the Python-level |
| 327 | # code is still automatically generated (so they are not in skip_function()). |
| 328 | skip_impl = ( |
| 329 | 'xmlSaveFileTo', |
| 330 | 'xmlSaveFormatFileTo', |
| 331 | ) |
| 332 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 333 | def skip_function(name): |
| 334 | if name[0:12] == "xmlXPathWrap": |
| 335 | return 1 |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 336 | if name == "xmlFreeParserCtxt": |
| 337 | return 1 |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 338 | if name == "xmlCleanupParser": |
| 339 | return 1 |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 340 | if name == "xmlFreeTextReader": |
| 341 | return 1 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 342 | # if name[0:11] == "xmlXPathNew": |
| 343 | # return 1 |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 344 | # the next function is defined in libxml.c |
| 345 | if name == "xmlRelaxNGFreeValidCtxt": |
| 346 | return 1 |
Daniel Veillard | 25c90c5 | 2005-03-02 10:47:41 +0000 | [diff] [blame] | 347 | if name == "xmlFreeValidCtxt": |
| 348 | return 1 |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 349 | # |
| 350 | # Those are skipped because the Const version is used of the bindings |
| 351 | # instead. |
| 352 | # |
| 353 | if name == "xmlTextReaderBaseUri": |
| 354 | return 1 |
| 355 | if name == "xmlTextReaderLocalName": |
| 356 | return 1 |
| 357 | if name == "xmlTextReaderName": |
| 358 | return 1 |
| 359 | if name == "xmlTextReaderNamespaceUri": |
| 360 | return 1 |
| 361 | if name == "xmlTextReaderPrefix": |
| 362 | return 1 |
| 363 | if name == "xmlTextReaderXmlLang": |
| 364 | return 1 |
| 365 | if name == "xmlTextReaderValue": |
| 366 | return 1 |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 367 | if name == "xmlOutputBufferClose": # handled by by the superclass |
| 368 | return 1 |
| 369 | if name == "xmlOutputBufferFlush": # handled by by the superclass |
| 370 | return 1 |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 371 | if name == "xmlErrMemory": |
| 372 | return 1 |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 373 | |
| 374 | if name == "xmlValidBuildContentModel": |
| 375 | return 1 |
| 376 | if name == "xmlValidateElementDecl": |
| 377 | return 1 |
| 378 | if name == "xmlValidateAttributeDecl": |
| 379 | return 1 |
| 380 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 381 | return 0 |
| 382 | |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 383 | def print_function_wrapper(name, output, export, include): |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 384 | global py_types |
| 385 | global unknown_types |
| 386 | global functions |
| 387 | global skipped_modules |
| 388 | |
| 389 | try: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 390 | (desc, ret, args, file) = functions[name] |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 391 | except: |
| 392 | print "failed to get function %s infos" |
| 393 | return |
| 394 | |
| 395 | if skipped_modules.has_key(file): |
| 396 | return 0 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 397 | if skip_function(name) == 1: |
| 398 | return 0 |
Daniel Veillard | 263ec86 | 2004-10-04 10:26:54 +0000 | [diff] [blame] | 399 | if name in skip_impl: |
| 400 | # Don't delete the function entry in the caller. |
| 401 | return 1 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 402 | |
| 403 | c_call = ""; |
| 404 | format="" |
| 405 | format_args="" |
| 406 | c_args="" |
| 407 | c_return="" |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 408 | c_convert="" |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 409 | num_bufs=0 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 410 | for arg in args: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 411 | # This should be correct |
| 412 | if arg[1][0:6] == "const ": |
| 413 | arg[1] = arg[1][6:] |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 414 | c_args = c_args + " %s %s;\n" % (arg[1], arg[0]) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 415 | if py_types.has_key(arg[1]): |
| 416 | (f, t, n, c) = py_types[arg[1]] |
William M. Brack | ff34911 | 2004-12-24 08:39:13 +0000 | [diff] [blame] | 417 | if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0): |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 418 | f = 't#' |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 419 | if f != None: |
| 420 | format = format + f |
| 421 | if t != None: |
| 422 | format_args = format_args + ", &pyobj_%s" % (arg[0]) |
| 423 | c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0]) |
| 424 | c_convert = c_convert + \ |
| 425 | " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0], |
| 426 | arg[1], t, arg[0]); |
| 427 | else: |
| 428 | format_args = format_args + ", &%s" % (arg[0]) |
William M. Brack | 106cad6 | 2004-12-23 15:56:12 +0000 | [diff] [blame] | 429 | if f == 't#': |
| 430 | format_args = format_args + ", &py_buffsize%d" % num_bufs |
| 431 | c_args = c_args + " int py_buffsize%d;\n" % num_bufs |
| 432 | num_bufs = num_bufs + 1 |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 433 | if c_call != "": |
| 434 | c_call = c_call + ", "; |
| 435 | c_call = c_call + "%s" % (arg[0]) |
| 436 | else: |
| 437 | if skipped_types.has_key(arg[1]): |
| 438 | return 0 |
| 439 | if unknown_types.has_key(arg[1]): |
| 440 | lst = unknown_types[arg[1]] |
| 441 | lst.append(name) |
| 442 | else: |
| 443 | unknown_types[arg[1]] = [name] |
| 444 | return -1 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 445 | if format != "": |
| 446 | format = format + ":%s" % (name) |
| 447 | |
| 448 | if ret[0] == 'void': |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 449 | if file == "python_accessor": |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 450 | if args[1][1] == "char *" or args[1][1] == "xmlChar *": |
| 451 | c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % ( |
| 452 | args[0][0], args[1][0], args[0][0], args[1][0]) |
William M. Brack | a71a8ef | 2003-08-06 04:43:55 +0000 | [diff] [blame] | 453 | c_call = c_call + " %s->%s = (%s)xmlStrdup((const xmlChar *)%s);\n" % (args[0][0], |
| 454 | args[1][0], args[1][1], args[1][0]) |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 455 | else: |
| 456 | c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0], |
| 457 | args[1][0]) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 458 | else: |
| 459 | c_call = "\n %s(%s);\n" % (name, c_call); |
| 460 | ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 461 | elif py_types.has_key(ret[0]): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 462 | (f, t, n, c) = py_types[ret[0]] |
| 463 | c_return = " %s c_retval;\n" % (ret[0]) |
| 464 | if file == "python_accessor" and ret[2] != None: |
| 465 | c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2]) |
| 466 | else: |
| 467 | c_call = "\n c_retval = %s(%s);\n" % (name, c_call); |
| 468 | ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c) |
| 469 | ret_convert = ret_convert + " return(py_retval);\n" |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 470 | elif py_return_types.has_key(ret[0]): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 471 | (f, t, n, c) = py_return_types[ret[0]] |
| 472 | c_return = " %s c_retval;\n" % (ret[0]) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 473 | c_call = "\n c_retval = %s(%s);\n" % (name, c_call); |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 474 | ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c) |
| 475 | ret_convert = ret_convert + " return(py_retval);\n" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 476 | else: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 477 | if skipped_types.has_key(ret[0]): |
| 478 | return 0 |
| 479 | if unknown_types.has_key(ret[0]): |
| 480 | lst = unknown_types[ret[0]] |
| 481 | lst.append(name) |
| 482 | else: |
| 483 | unknown_types[ret[0]] = [name] |
| 484 | return -1 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 485 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 486 | if file == "debugXML": |
| 487 | include.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
| 488 | export.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
| 489 | output.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 490 | elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html": |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 491 | include.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 492 | export.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 493 | output.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 494 | elif file == "c14n": |
| 495 | include.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 496 | export.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 497 | output.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 498 | elif file == "xpathInternals" or file == "xpath": |
| 499 | include.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 500 | export.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 501 | output.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 502 | elif file == "xpointer": |
| 503 | include.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 504 | export.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 505 | output.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 506 | elif file == "xinclude": |
| 507 | include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
| 508 | export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
| 509 | output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 510 | elif file == "xmlregexp": |
| 511 | include.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
| 512 | export.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
| 513 | output.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 514 | elif file == "xmlschemas" or file == "xmlschemastypes" or \ |
| 515 | file == "relaxng": |
| 516 | include.write("#ifdef LIBXML_SCHEMAS_ENABLED\n"); |
| 517 | export.write("#ifdef LIBXML_SCHEMAS_ENABLED\n"); |
| 518 | output.write("#ifdef LIBXML_SCHEMAS_ENABLED\n"); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 519 | |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 520 | include.write("PyObject * ") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 521 | include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name)); |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 522 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 523 | export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" % |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 524 | (name, name)) |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 525 | |
| 526 | if file == "python": |
| 527 | # Those have been manually generated |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 528 | if name[0:4] == "html": |
| 529 | include.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 530 | export.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 531 | output.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 532 | return 1 |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 533 | if file == "python_accessor" and ret[0] != "void" and ret[2] is None: |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 534 | # Those have been manually generated |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 535 | if name[0:4] == "html": |
| 536 | include.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 537 | export.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 538 | output.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 539 | return 1 |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 540 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 541 | output.write("PyObject *\n") |
William M. Brack | 6bf4d6f | 2003-11-04 23:29:16 +0000 | [diff] [blame] | 542 | output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name)) |
| 543 | output.write(" PyObject *args") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 544 | if format == "": |
William M. Brack | 6bf4d6f | 2003-11-04 23:29:16 +0000 | [diff] [blame] | 545 | output.write(" ATTRIBUTE_UNUSED") |
| 546 | output.write(") {\n") |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 547 | if ret[0] != 'void': |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 548 | output.write(" PyObject *py_retval;\n") |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 549 | if c_return != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 550 | output.write(c_return) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 551 | if c_args != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 552 | output.write(c_args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 553 | if format != "": |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 554 | output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" % |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 555 | (format, format_args)) |
| 556 | output.write(" return(NULL);\n") |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 557 | if c_convert != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 558 | output.write(c_convert) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 559 | |
| 560 | output.write(c_call) |
| 561 | output.write(ret_convert) |
| 562 | output.write("}\n\n") |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 563 | if file == "debugXML": |
| 564 | include.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
| 565 | export.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
| 566 | output.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 567 | elif file == "HTMLtree" or file == "HTMLparser" or name[0:4] == "html": |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 568 | include.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 569 | export.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 570 | output.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 571 | elif file == "c14n": |
| 572 | include.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 573 | export.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 574 | output.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 575 | elif file == "xpathInternals" or file == "xpath": |
| 576 | include.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 577 | export.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 578 | output.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 579 | elif file == "xpointer": |
| 580 | include.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 581 | export.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 582 | output.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 583 | elif file == "xinclude": |
| 584 | include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
| 585 | export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
| 586 | output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 587 | elif file == "xmlregexp": |
| 588 | include.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
| 589 | export.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
| 590 | output.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 591 | elif file == "xmlschemas" or file == "xmlschemastypes" or \ |
| 592 | file == "relaxng": |
| 593 | include.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n"); |
| 594 | export.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n"); |
| 595 | output.write("#endif /* LIBXML_SCHEMAS_ENABLED */\n"); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 596 | return 1 |
| 597 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 598 | def buildStubs(): |
| 599 | global py_types |
| 600 | global py_return_types |
| 601 | global unknown_types |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 602 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 603 | try: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 604 | f = open(os.path.join(srcPref,"libxml2-api.xml")) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 605 | data = f.read() |
| 606 | (parser, target) = getparser() |
| 607 | parser.feed(data) |
| 608 | parser.close() |
| 609 | except IOError, msg: |
| 610 | try: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 611 | f = open(os.path.join(srcPref,"..","doc","libxml2-api.xml")) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 612 | data = f.read() |
| 613 | (parser, target) = getparser() |
| 614 | parser.feed(data) |
| 615 | parser.close() |
| 616 | except IOError, msg: |
| 617 | print file, ":", msg |
| 618 | sys.exit(1) |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 619 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 620 | n = len(functions.keys()) |
| 621 | print "Found %d functions in libxml2-api.xml" % (n) |
| 622 | |
| 623 | py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") |
| 624 | try: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 625 | f = open(os.path.join(srcPref,"libxml2-python-api.xml")) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 626 | data = f.read() |
| 627 | (parser, target) = getparser() |
| 628 | parser.feed(data) |
| 629 | parser.close() |
| 630 | except IOError, msg: |
| 631 | print file, ":", msg |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 632 | |
| 633 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 634 | print "Found %d functions in libxml2-python-api.xml" % ( |
| 635 | len(functions.keys()) - n) |
| 636 | nb_wrap = 0 |
| 637 | failed = 0 |
| 638 | skipped = 0 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 639 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 640 | include = open("libxml2-py.h", "w") |
| 641 | include.write("/* Generated */\n\n") |
| 642 | export = open("libxml2-export.c", "w") |
| 643 | export.write("/* Generated */\n\n") |
| 644 | wrapper = open("libxml2-py.c", "w") |
| 645 | wrapper.write("/* Generated */\n\n") |
| 646 | wrapper.write("#include <Python.h>\n") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 647 | wrapper.write("#include <libxml/xmlversion.h>\n") |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 648 | wrapper.write("#include <libxml/tree.h>\n") |
William M. Brack | a71a8ef | 2003-08-06 04:43:55 +0000 | [diff] [blame] | 649 | wrapper.write("#include <libxml/xmlschemastypes.h>\n") |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 650 | wrapper.write("#include \"libxml_wrap.h\"\n") |
| 651 | wrapper.write("#include \"libxml2-py.h\"\n\n") |
| 652 | for function in functions.keys(): |
| 653 | ret = print_function_wrapper(function, wrapper, export, include) |
| 654 | if ret < 0: |
| 655 | failed = failed + 1 |
| 656 | del functions[function] |
| 657 | if ret == 0: |
| 658 | skipped = skipped + 1 |
| 659 | del functions[function] |
| 660 | if ret == 1: |
| 661 | nb_wrap = nb_wrap + 1 |
| 662 | include.close() |
| 663 | export.close() |
| 664 | wrapper.close() |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 665 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 666 | print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap, |
| 667 | failed, skipped); |
| 668 | print "Missing type converters: " |
| 669 | for type in unknown_types.keys(): |
| 670 | print "%s:%d " % (type, len(unknown_types[type])), |
| 671 | print |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 672 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 673 | ####################################################################### |
| 674 | # |
| 675 | # This part writes part of the Python front-end classes based on |
| 676 | # mapping rules between types and classes and also based on function |
| 677 | # renaming to get consistent function names at the Python level |
| 678 | # |
| 679 | ####################################################################### |
| 680 | |
| 681 | # |
| 682 | # The type automatically remapped to generated classes |
| 683 | # |
| 684 | classes_type = { |
| 685 | "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"), |
| 686 | "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"), |
| 687 | "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 688 | "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 689 | "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 690 | "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 691 | "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), |
| 692 | "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), |
| 693 | "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"), |
| 694 | "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"), |
| 695 | "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), |
| 696 | "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), |
| 697 | "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), |
| 698 | "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), |
| 699 | "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"), |
| 700 | "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"), |
| 701 | "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), |
| 702 | "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), |
| 703 | "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 704 | "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"), |
| 705 | "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), |
| 706 | "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 707 | "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
| 708 | "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 709 | "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
| 710 | "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 711 | "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 712 | "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"), |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 713 | "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"), |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 714 | "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"), |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 715 | "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"), |
| 716 | "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"), |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 717 | "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"), |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 718 | "xmlTextReaderLocatorPtr": ("._o", "xmlTextReaderLocator(_obj=%s)", "xmlTextReaderLocator"), |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 719 | "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"), |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 720 | 'xmlRelaxNGPtr': ('._o', "relaxNgSchema(_obj=%s)", "relaxNgSchema"), |
| 721 | 'xmlRelaxNGParserCtxtPtr': ('._o', "relaxNgParserCtxt(_obj=%s)", "relaxNgParserCtxt"), |
| 722 | 'xmlRelaxNGValidCtxtPtr': ('._o', "relaxNgValidCtxt(_obj=%s)", "relaxNgValidCtxt"), |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 723 | 'xmlSchemaPtr': ("._o", "Schema(_obj=%s)", "Schema"), |
| 724 | 'xmlSchemaParserCtxtPtr': ("._o", "SchemaParserCtxt(_obj=%s)", "SchemaParserCtxt"), |
| 725 | 'xmlSchemaValidCtxtPtr': ("._o", "SchemaValidCtxt(_obj=%s)", "SchemaValidCtxt"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | converter_type = { |
| 729 | "xmlXPathObjectPtr": "xpathObjectRet(%s)", |
| 730 | } |
| 731 | |
| 732 | primary_classes = ["xmlNode", "xmlDoc"] |
| 733 | |
| 734 | classes_ancestor = { |
| 735 | "xmlNode" : "xmlCore", |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 736 | "xmlDtd" : "xmlNode", |
| 737 | "xmlDoc" : "xmlNode", |
| 738 | "xmlAttr" : "xmlNode", |
| 739 | "xmlNs" : "xmlNode", |
| 740 | "xmlEntity" : "xmlNode", |
| 741 | "xmlElement" : "xmlNode", |
| 742 | "xmlAttribute" : "xmlNode", |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 743 | "outputBuffer": "ioWriteWrapper", |
| 744 | "inputBuffer": "ioReadWrapper", |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 745 | "parserCtxt": "parserCtxtCore", |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 746 | "xmlTextReader": "xmlTextReaderCore", |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 747 | } |
| 748 | classes_destructors = { |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 749 | "parserCtxt": "xmlFreeParserCtxt", |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 750 | "catalog": "xmlFreeCatalog", |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 751 | "URI": "xmlFreeURI", |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 752 | # "outputBuffer": "xmlOutputBufferClose", |
| 753 | "inputBuffer": "xmlFreeParserInputBuffer", |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 754 | "xmlReg": "xmlRegFreeRegexp", |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 755 | "xmlTextReader": "xmlFreeTextReader", |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 756 | "relaxNgSchema": "xmlRelaxNGFree", |
| 757 | "relaxNgParserCtxt": "xmlRelaxNGFreeParserCtxt", |
| 758 | "relaxNgValidCtxt": "xmlRelaxNGFreeValidCtxt", |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 759 | "Schema": "xmlSchemaFree", |
| 760 | "SchemaParserCtxt": "xmlSchemaFreeParserCtxt", |
| 761 | "SchemaValidCtxt": "xmlSchemaFreeValidCtxt", |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 762 | "ValidCtxt": "xmlFreeValidCtxt", |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 765 | functions_noexcept = { |
| 766 | "xmlHasProp": 1, |
| 767 | "xmlHasNsProp": 1, |
Daniel Veillard | 3b87b6b | 2003-01-10 15:21:50 +0000 | [diff] [blame] | 768 | "xmlDocSetRootElement": 1, |
William M. Brack | dbbcf8e | 2004-12-17 22:50:53 +0000 | [diff] [blame] | 769 | "xmlNodeGetNs": 1, |
| 770 | "xmlNodeGetNsDefs": 1, |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 773 | reference_keepers = { |
| 774 | "xmlTextReader": [('inputBuffer', 'input')], |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 775 | "relaxNgValidCtxt": [('relaxNgSchema', 'schema')], |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 776 | "SchemaValidCtxt": [('Schema', 'schema')], |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 779 | function_classes = {} |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 780 | |
| 781 | function_classes["None"] = [] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 782 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 783 | def nameFixup(name, classe, type, file): |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 784 | listname = classe + "List" |
| 785 | ll = len(listname) |
| 786 | l = len(classe) |
| 787 | if name[0:l] == listname: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 788 | func = name[l:] |
| 789 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 790 | elif name[0:12] == "xmlParserGet" and file == "python_accessor": |
| 791 | func = name[12:] |
| 792 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 26f1dcc | 2002-02-03 16:53:19 +0000 | [diff] [blame] | 793 | elif name[0:12] == "xmlParserSet" and file == "python_accessor": |
| 794 | func = name[12:] |
| 795 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 796 | elif name[0:10] == "xmlNodeGet" and file == "python_accessor": |
| 797 | func = name[10:] |
| 798 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 799 | elif name[0:9] == "xmlURIGet" and file == "python_accessor": |
| 800 | func = name[9:] |
| 801 | func = string.lower(func[0:1]) + func[1:] |
| 802 | elif name[0:9] == "xmlURISet" and file == "python_accessor": |
| 803 | func = name[6:] |
| 804 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 805 | elif name[0:11] == "xmlErrorGet" and file == "python_accessor": |
| 806 | func = name[11:] |
| 807 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 808 | elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor": |
| 809 | func = name[17:] |
| 810 | func = string.lower(func[0:1]) + func[1:] |
| 811 | elif name[0:11] == "xmlXPathGet" and file == "python_accessor": |
| 812 | func = name[11:] |
| 813 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 814 | elif name[0:11] == "xmlXPathSet" and file == "python_accessor": |
| 815 | func = name[8:] |
| 816 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 817 | elif name[0:15] == "xmlOutputBuffer" and file != "python": |
| 818 | func = name[15:] |
| 819 | func = string.lower(func[0:1]) + func[1:] |
| 820 | elif name[0:20] == "xmlParserInputBuffer" and file != "python": |
| 821 | func = name[20:] |
| 822 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | d4cb1e8 | 2002-09-26 09:34:23 +0000 | [diff] [blame] | 823 | elif name[0:9] == "xmlRegexp" and file == "xmlregexp": |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 824 | func = "regexp" + name[9:] |
Daniel Veillard | d4cb1e8 | 2002-09-26 09:34:23 +0000 | [diff] [blame] | 825 | elif name[0:6] == "xmlReg" and file == "xmlregexp": |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 826 | func = "regexp" + name[6:] |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 827 | elif name[0:20] == "xmlTextReaderLocator" and file == "xmlreader": |
| 828 | func = name[20:] |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 829 | elif name[0:18] == "xmlTextReaderConst" and file == "xmlreader": |
| 830 | func = name[18:] |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 831 | elif name[0:13] == "xmlTextReader" and file == "xmlreader": |
| 832 | func = name[13:] |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 833 | elif name[0:12] == "xmlReaderNew" and file == "xmlreader": |
| 834 | func = name[9:] |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 835 | elif name[0:11] == "xmlACatalog": |
| 836 | func = name[11:] |
| 837 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 838 | elif name[0:l] == classe: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 839 | func = name[l:] |
| 840 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 841 | elif name[0:7] == "libxml_": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 842 | func = name[7:] |
| 843 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 844 | elif name[0:6] == "xmlGet": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 845 | func = name[6:] |
| 846 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 847 | elif name[0:3] == "xml": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 848 | func = name[3:] |
| 849 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 850 | else: |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 851 | func = name |
| 852 | if func[0:5] == "xPath": |
| 853 | func = "xpath" + func[5:] |
| 854 | elif func[0:4] == "xPtr": |
| 855 | func = "xpointer" + func[4:] |
| 856 | elif func[0:8] == "xInclude": |
| 857 | func = "xinclude" + func[8:] |
| 858 | elif func[0:2] == "iD": |
| 859 | func = "ID" + func[2:] |
| 860 | elif func[0:3] == "uRI": |
| 861 | func = "URI" + func[3:] |
| 862 | elif func[0:4] == "uTF8": |
| 863 | func = "UTF8" + func[4:] |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 864 | elif func[0:3] == 'sAX': |
| 865 | func = "SAX" + func[3:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 866 | return func |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 867 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 868 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 869 | def functionCompare(info1, info2): |
| 870 | (index1, func1, name1, ret1, args1, file1) = info1 |
| 871 | (index2, func2, name2, ret2, args2, file2) = info2 |
Daniel Veillard | 26f1dcc | 2002-02-03 16:53:19 +0000 | [diff] [blame] | 872 | if file1 == file2: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 873 | if func1 < func2: |
| 874 | return -1 |
| 875 | if func1 > func2: |
| 876 | return 1 |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 877 | if file1 == "python_accessor": |
| 878 | return -1 |
| 879 | if file2 == "python_accessor": |
| 880 | return 1 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 881 | if file1 < file2: |
| 882 | return -1 |
| 883 | if file1 > file2: |
| 884 | return 1 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 885 | return 0 |
| 886 | |
| 887 | def writeDoc(name, args, indent, output): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 888 | if functions[name][0] is None or functions[name][0] == "": |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 889 | return |
| 890 | val = functions[name][0] |
| 891 | val = string.replace(val, "NULL", "None"); |
| 892 | output.write(indent) |
| 893 | output.write('"""') |
| 894 | while len(val) > 60: |
| 895 | str = val[0:60] |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 896 | i = string.rfind(str, " "); |
| 897 | if i < 0: |
| 898 | i = 60 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 899 | str = val[0:i] |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 900 | val = val[i:] |
| 901 | output.write(str) |
| 902 | output.write('\n '); |
| 903 | output.write(indent) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 904 | output.write(val); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 905 | output.write(' """\n') |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 906 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 907 | def buildWrappers(): |
| 908 | global ctypes |
| 909 | global py_types |
| 910 | global py_return_types |
| 911 | global unknown_types |
| 912 | global functions |
| 913 | global function_classes |
| 914 | global classes_type |
| 915 | global classes_list |
| 916 | global converter_type |
| 917 | global primary_classes |
| 918 | global converter_type |
| 919 | global classes_ancestor |
| 920 | global converter_type |
| 921 | global primary_classes |
| 922 | global classes_ancestor |
| 923 | global classes_destructors |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 924 | global functions_noexcept |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 925 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 926 | for type in classes_type.keys(): |
| 927 | function_classes[classes_type[type][2]] = [] |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 928 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 929 | # |
| 930 | # Build the list of C types to look for ordered to start |
| 931 | # with primary classes |
| 932 | # |
| 933 | ctypes = [] |
| 934 | classes_list = [] |
| 935 | ctypes_processed = {} |
| 936 | classes_processed = {} |
| 937 | for classe in primary_classes: |
| 938 | classes_list.append(classe) |
| 939 | classes_processed[classe] = () |
| 940 | for type in classes_type.keys(): |
| 941 | tinfo = classes_type[type] |
| 942 | if tinfo[2] == classe: |
| 943 | ctypes.append(type) |
| 944 | ctypes_processed[type] = () |
| 945 | for type in classes_type.keys(): |
| 946 | if ctypes_processed.has_key(type): |
| 947 | continue |
| 948 | tinfo = classes_type[type] |
| 949 | if not classes_processed.has_key(tinfo[2]): |
| 950 | classes_list.append(tinfo[2]) |
| 951 | classes_processed[tinfo[2]] = () |
| 952 | |
| 953 | ctypes.append(type) |
| 954 | ctypes_processed[type] = () |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 955 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 956 | for name in functions.keys(): |
| 957 | found = 0; |
| 958 | (desc, ret, args, file) = functions[name] |
| 959 | for type in ctypes: |
| 960 | classe = classes_type[type][2] |
| 961 | |
| 962 | if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type: |
| 963 | found = 1 |
| 964 | func = nameFixup(name, classe, type, file) |
| 965 | info = (0, func, name, ret, args, file) |
| 966 | function_classes[classe].append(info) |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 967 | elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \ |
| 968 | and file != "python_accessor": |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 969 | found = 1 |
| 970 | func = nameFixup(name, classe, type, file) |
| 971 | info = (1, func, name, ret, args, file) |
| 972 | function_classes[classe].append(info) |
| 973 | elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type: |
| 974 | found = 1 |
| 975 | func = nameFixup(name, classe, type, file) |
| 976 | info = (0, func, name, ret, args, file) |
| 977 | function_classes[classe].append(info) |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 978 | elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \ |
| 979 | and file != "python_accessor": |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 980 | found = 1 |
| 981 | func = nameFixup(name, classe, type, file) |
| 982 | info = (1, func, name, ret, args, file) |
| 983 | function_classes[classe].append(info) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 984 | if found == 1: |
| 985 | continue |
| 986 | if name[0:8] == "xmlXPath": |
| 987 | continue |
| 988 | if name[0:6] == "xmlStr": |
| 989 | continue |
| 990 | if name[0:10] == "xmlCharStr": |
| 991 | continue |
| 992 | func = nameFixup(name, "None", file, file) |
| 993 | info = (0, func, name, ret, args, file) |
| 994 | function_classes['None'].append(info) |
| 995 | |
| 996 | classes = open("libxml2class.py", "w") |
| 997 | txt = open("libxml2class.txt", "w") |
| 998 | txt.write(" Generated Classes for libxml2-python\n\n") |
| 999 | |
| 1000 | txt.write("#\n# Global functions of the module\n#\n\n") |
| 1001 | if function_classes.has_key("None"): |
| 1002 | flist = function_classes["None"] |
| 1003 | flist.sort(functionCompare) |
| 1004 | oldfile = "" |
| 1005 | for info in flist: |
| 1006 | (index, func, name, ret, args, file) = info |
| 1007 | if file != oldfile: |
| 1008 | classes.write("#\n# Functions from module %s\n#\n\n" % file) |
| 1009 | txt.write("\n# functions from module %s\n" % file) |
| 1010 | oldfile = file |
| 1011 | classes.write("def %s(" % func) |
| 1012 | txt.write("%s()\n" % func); |
| 1013 | n = 0 |
| 1014 | for arg in args: |
| 1015 | if n != 0: |
| 1016 | classes.write(", ") |
| 1017 | classes.write("%s" % arg[0]) |
| 1018 | n = n + 1 |
| 1019 | classes.write("):\n") |
| 1020 | writeDoc(name, args, ' ', classes); |
| 1021 | |
| 1022 | for arg in args: |
| 1023 | if classes_type.has_key(arg[1]): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1024 | classes.write(" if %s is None: %s__o = None\n" % |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1025 | (arg[0], arg[0])) |
| 1026 | classes.write(" else: %s__o = %s%s\n" % |
| 1027 | (arg[0], arg[0], classes_type[arg[1]][0])) |
| 1028 | if ret[0] != "void": |
| 1029 | classes.write(" ret = "); |
| 1030 | else: |
| 1031 | classes.write(" "); |
| 1032 | classes.write("libxml2mod.%s(" % name) |
| 1033 | n = 0 |
| 1034 | for arg in args: |
| 1035 | if n != 0: |
| 1036 | classes.write(", "); |
| 1037 | classes.write("%s" % arg[0]) |
| 1038 | if classes_type.has_key(arg[1]): |
| 1039 | classes.write("__o"); |
| 1040 | n = n + 1 |
| 1041 | classes.write(")\n"); |
| 1042 | if ret[0] != "void": |
| 1043 | if classes_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1044 | # |
| 1045 | # Raise an exception |
| 1046 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1047 | if functions_noexcept.has_key(name): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1048 | classes.write(" if ret is None:return None\n"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1049 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1050 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1051 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1052 | % (name)) |
| 1053 | elif string.find(name, "XPath") >= 0: |
| 1054 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1055 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1056 | % (name)) |
| 1057 | elif string.find(name, "Parse") >= 0: |
| 1058 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1059 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1060 | % (name)) |
| 1061 | else: |
| 1062 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1063 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1064 | % (name)) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1065 | classes.write(" return "); |
| 1066 | classes.write(classes_type[ret[0]][1] % ("ret")); |
| 1067 | classes.write("\n"); |
| 1068 | else: |
| 1069 | classes.write(" return ret\n"); |
| 1070 | classes.write("\n"); |
| 1071 | |
| 1072 | txt.write("\n\n#\n# Set of classes of the module\n#\n\n") |
| 1073 | for classname in classes_list: |
| 1074 | if classname == "None": |
| 1075 | pass |
| 1076 | else: |
| 1077 | if classes_ancestor.has_key(classname): |
| 1078 | txt.write("\n\nClass %s(%s)\n" % (classname, |
| 1079 | classes_ancestor[classname])) |
| 1080 | classes.write("class %s(%s):\n" % (classname, |
| 1081 | classes_ancestor[classname])) |
| 1082 | classes.write(" def __init__(self, _obj=None):\n") |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 1083 | if classes_ancestor[classname] == "xmlCore" or \ |
| 1084 | classes_ancestor[classname] == "xmlNode": |
| 1085 | classes.write(" if type(_obj).__name__ != ") |
| 1086 | classes.write("'PyCObject':\n") |
| 1087 | classes.write(" raise TypeError, ") |
| 1088 | classes.write("'%s needs a PyCObject argument'\n" % \ |
| 1089 | classname) |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 1090 | if reference_keepers.has_key(classname): |
| 1091 | rlist = reference_keepers[classname] |
| 1092 | for ref in rlist: |
| 1093 | classes.write(" self.%s = None\n" % ref[1]) |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 1094 | classes.write(" self._o = _obj\n") |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1095 | classes.write(" %s.__init__(self, _obj=_obj)\n\n" % ( |
| 1096 | classes_ancestor[classname])) |
| 1097 | if classes_ancestor[classname] == "xmlCore" or \ |
| 1098 | classes_ancestor[classname] == "xmlNode": |
| 1099 | classes.write(" def __repr__(self):\n") |
Daniel Veillard | ba5e18a | 2002-03-05 09:36:43 +0000 | [diff] [blame] | 1100 | format = "<%s (%%s) object at 0x%%x>" % (classname) |
| 1101 | classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % ( |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1102 | format)) |
| 1103 | else: |
| 1104 | txt.write("Class %s()\n" % (classname)) |
| 1105 | classes.write("class %s:\n" % (classname)) |
| 1106 | classes.write(" def __init__(self, _obj=None):\n") |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 1107 | if reference_keepers.has_key(classname): |
| 1108 | list = reference_keepers[classname] |
| 1109 | for ref in list: |
| 1110 | classes.write(" self.%s = None\n" % ref[1]) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1111 | classes.write(" if _obj != None:self._o = _obj;return\n") |
| 1112 | classes.write(" self._o = None\n\n"); |
Daniel Veillard | d69cc81 | 2004-07-01 09:36:26 +0000 | [diff] [blame] | 1113 | destruct=None |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1114 | if classes_destructors.has_key(classname): |
| 1115 | classes.write(" def __del__(self):\n") |
| 1116 | classes.write(" if self._o != None:\n") |
| 1117 | classes.write(" libxml2mod.%s(self._o)\n" % |
| 1118 | classes_destructors[classname]); |
| 1119 | classes.write(" self._o = None\n\n"); |
Daniel Veillard | d69cc81 | 2004-07-01 09:36:26 +0000 | [diff] [blame] | 1120 | destruct=classes_destructors[classname] |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1121 | flist = function_classes[classname] |
| 1122 | flist.sort(functionCompare) |
| 1123 | oldfile = "" |
| 1124 | for info in flist: |
| 1125 | (index, func, name, ret, args, file) = info |
Daniel Veillard | d69cc81 | 2004-07-01 09:36:26 +0000 | [diff] [blame] | 1126 | # |
| 1127 | # Do not provide as method the destructors for the class |
| 1128 | # to avoid double free |
| 1129 | # |
| 1130 | if name == destruct: |
| 1131 | continue; |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1132 | if file != oldfile: |
| 1133 | if file == "python_accessor": |
| 1134 | classes.write(" # accessors for %s\n" % (classname)) |
| 1135 | txt.write(" # accessors\n") |
| 1136 | else: |
| 1137 | classes.write(" #\n") |
| 1138 | classes.write(" # %s functions from module %s\n" % ( |
| 1139 | classname, file)) |
| 1140 | txt.write("\n # functions from module %s\n" % file) |
| 1141 | classes.write(" #\n\n") |
| 1142 | oldfile = file |
| 1143 | classes.write(" def %s(self" % func) |
| 1144 | txt.write(" %s()\n" % func); |
| 1145 | n = 0 |
| 1146 | for arg in args: |
| 1147 | if n != index: |
| 1148 | classes.write(", %s" % arg[0]) |
| 1149 | n = n + 1 |
| 1150 | classes.write("):\n") |
| 1151 | writeDoc(name, args, ' ', classes); |
| 1152 | n = 0 |
| 1153 | for arg in args: |
| 1154 | if classes_type.has_key(arg[1]): |
| 1155 | if n != index: |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1156 | classes.write(" if %s is None: %s__o = None\n" % |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1157 | (arg[0], arg[0])) |
| 1158 | classes.write(" else: %s__o = %s%s\n" % |
| 1159 | (arg[0], arg[0], classes_type[arg[1]][0])) |
| 1160 | n = n + 1 |
| 1161 | if ret[0] != "void": |
| 1162 | classes.write(" ret = "); |
| 1163 | else: |
| 1164 | classes.write(" "); |
| 1165 | classes.write("libxml2mod.%s(" % name) |
| 1166 | n = 0 |
| 1167 | for arg in args: |
| 1168 | if n != 0: |
| 1169 | classes.write(", "); |
| 1170 | if n != index: |
| 1171 | classes.write("%s" % arg[0]) |
| 1172 | if classes_type.has_key(arg[1]): |
| 1173 | classes.write("__o"); |
| 1174 | else: |
| 1175 | classes.write("self"); |
| 1176 | if classes_type.has_key(arg[1]): |
| 1177 | classes.write(classes_type[arg[1]][0]) |
| 1178 | n = n + 1 |
| 1179 | classes.write(")\n"); |
| 1180 | if ret[0] != "void": |
| 1181 | if classes_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1182 | # |
| 1183 | # Raise an exception |
| 1184 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1185 | if functions_noexcept.has_key(name): |
| 1186 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1187 | " if ret is None:return None\n"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1188 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1189 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1190 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1191 | % (name)) |
| 1192 | elif string.find(name, "XPath") >= 0: |
| 1193 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1194 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1195 | % (name)) |
| 1196 | elif string.find(name, "Parse") >= 0: |
| 1197 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1198 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1199 | % (name)) |
| 1200 | else: |
| 1201 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1202 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1203 | % (name)) |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 1204 | |
| 1205 | # |
| 1206 | # generate the returned class wrapper for the object |
| 1207 | # |
| 1208 | classes.write(" __tmp = "); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1209 | classes.write(classes_type[ret[0]][1] % ("ret")); |
| 1210 | classes.write("\n"); |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 1211 | |
| 1212 | # |
| 1213 | # Sometime one need to keep references of the source |
| 1214 | # class in the returned class object. |
| 1215 | # See reference_keepers for the list |
| 1216 | # |
| 1217 | tclass = classes_type[ret[0]][2] |
| 1218 | if reference_keepers.has_key(tclass): |
| 1219 | list = reference_keepers[tclass] |
| 1220 | for pref in list: |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 1221 | if pref[0] == classname: |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame] | 1222 | classes.write(" __tmp.%s = self\n" % |
| 1223 | pref[1]) |
| 1224 | # |
| 1225 | # return the class |
| 1226 | # |
| 1227 | classes.write(" return __tmp\n"); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1228 | elif converter_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1229 | # |
| 1230 | # Raise an exception |
| 1231 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1232 | if functions_noexcept.has_key(name): |
| 1233 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1234 | " if ret is None:return None"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1235 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1236 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1237 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1238 | % (name)) |
| 1239 | elif string.find(name, "XPath") >= 0: |
| 1240 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1241 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1242 | % (name)) |
| 1243 | elif string.find(name, "Parse") >= 0: |
| 1244 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1245 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1246 | % (name)) |
| 1247 | else: |
| 1248 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1249 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1250 | % (name)) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1251 | classes.write(" return "); |
| 1252 | classes.write(converter_type[ret[0]] % ("ret")); |
| 1253 | classes.write("\n"); |
| 1254 | else: |
| 1255 | classes.write(" return ret\n"); |
| 1256 | classes.write("\n"); |
| 1257 | |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 1258 | # |
| 1259 | # Generate enum constants |
| 1260 | # |
| 1261 | for type,enum in enums.items(): |
| 1262 | classes.write("# %s\n" % type) |
| 1263 | items = enum.items() |
| 1264 | items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) |
| 1265 | for name,value in items: |
| 1266 | classes.write("%s = %s\n" % (name,value)) |
| 1267 | classes.write("\n"); |
| 1268 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1269 | txt.close() |
| 1270 | classes.close() |
| 1271 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1272 | buildStubs() |
| 1273 | buildWrappers() |