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