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 | 1971ee2 | 2002-01-31 20:29:19 +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 | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 274 | 'xmlTextReaderPtr': ('O', "xmlTextReader", "xmlTextReaderPtr", "xmlTextReaderPtr"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | py_return_types = { |
| 278 | 'xmlXPathObjectPtr': ('O', "foo", "xmlXPathObjectPtr", "xmlXPathObjectPtr"), |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | unknown_types = {} |
| 282 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 283 | ####################################################################### |
| 284 | # |
| 285 | # This part writes the C <-> Python stubs libxml2-py.[ch] and |
| 286 | # the table libxml2-export.c to add when registrering the Python module |
| 287 | # |
| 288 | ####################################################################### |
| 289 | |
| 290 | def skip_function(name): |
| 291 | if name[0:12] == "xmlXPathWrap": |
| 292 | return 1 |
| 293 | # if name[0:11] == "xmlXPathNew": |
| 294 | # return 1 |
| 295 | return 0 |
| 296 | |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 297 | def print_function_wrapper(name, output, export, include): |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 298 | global py_types |
| 299 | global unknown_types |
| 300 | global functions |
| 301 | global skipped_modules |
| 302 | |
| 303 | try: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 304 | (desc, ret, args, file) = functions[name] |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 305 | except: |
| 306 | print "failed to get function %s infos" |
| 307 | return |
| 308 | |
| 309 | if skipped_modules.has_key(file): |
| 310 | return 0 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 311 | if skip_function(name) == 1: |
| 312 | return 0 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 313 | |
| 314 | c_call = ""; |
| 315 | format="" |
| 316 | format_args="" |
| 317 | c_args="" |
| 318 | c_return="" |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 319 | c_convert="" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 320 | for arg in args: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 321 | # This should be correct |
| 322 | if arg[1][0:6] == "const ": |
| 323 | arg[1] = arg[1][6:] |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 324 | c_args = c_args + " %s %s;\n" % (arg[1], arg[0]) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 325 | if py_types.has_key(arg[1]): |
| 326 | (f, t, n, c) = py_types[arg[1]] |
| 327 | if f != None: |
| 328 | format = format + f |
| 329 | if t != None: |
| 330 | format_args = format_args + ", &pyobj_%s" % (arg[0]) |
| 331 | c_args = c_args + " PyObject *pyobj_%s;\n" % (arg[0]) |
| 332 | c_convert = c_convert + \ |
| 333 | " %s = (%s) Py%s_Get(pyobj_%s);\n" % (arg[0], |
| 334 | arg[1], t, arg[0]); |
| 335 | else: |
| 336 | format_args = format_args + ", &%s" % (arg[0]) |
| 337 | if c_call != "": |
| 338 | c_call = c_call + ", "; |
| 339 | c_call = c_call + "%s" % (arg[0]) |
| 340 | else: |
| 341 | if skipped_types.has_key(arg[1]): |
| 342 | return 0 |
| 343 | if unknown_types.has_key(arg[1]): |
| 344 | lst = unknown_types[arg[1]] |
| 345 | lst.append(name) |
| 346 | else: |
| 347 | unknown_types[arg[1]] = [name] |
| 348 | return -1 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 349 | if format != "": |
| 350 | format = format + ":%s" % (name) |
| 351 | |
| 352 | if ret[0] == 'void': |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 353 | if file == "python_accessor": |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 354 | if args[1][1] == "char *" or args[1][1] == "xmlChar *": |
| 355 | c_call = "\n if (%s->%s != NULL) xmlFree(%s->%s);\n" % ( |
| 356 | args[0][0], args[1][0], args[0][0], args[1][0]) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 357 | 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] | 358 | args[1][0], args[1][0]) |
| 359 | else: |
| 360 | c_call = "\n %s->%s = %s;\n" % (args[0][0], args[1][0], |
| 361 | args[1][0]) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 362 | else: |
| 363 | c_call = "\n %s(%s);\n" % (name, c_call); |
| 364 | ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 365 | elif py_types.has_key(ret[0]): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 366 | (f, t, n, c) = py_types[ret[0]] |
| 367 | c_return = " %s c_retval;\n" % (ret[0]) |
| 368 | if file == "python_accessor" and ret[2] != None: |
| 369 | c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2]) |
| 370 | else: |
| 371 | c_call = "\n c_retval = %s(%s);\n" % (name, c_call); |
| 372 | ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c) |
| 373 | ret_convert = ret_convert + " return(py_retval);\n" |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 374 | elif py_return_types.has_key(ret[0]): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 375 | (f, t, n, c) = py_return_types[ret[0]] |
| 376 | c_return = " %s c_retval;\n" % (ret[0]) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 377 | c_call = "\n c_retval = %s(%s);\n" % (name, c_call); |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 378 | ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c) |
| 379 | ret_convert = ret_convert + " return(py_retval);\n" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 380 | else: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 381 | if skipped_types.has_key(ret[0]): |
| 382 | return 0 |
| 383 | if unknown_types.has_key(ret[0]): |
| 384 | lst = unknown_types[ret[0]] |
| 385 | lst.append(name) |
| 386 | else: |
| 387 | unknown_types[ret[0]] = [name] |
| 388 | return -1 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 389 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 390 | if file == "debugXML": |
| 391 | include.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
| 392 | export.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
| 393 | output.write("#ifdef LIBXML_DEBUG_ENABLED\n"); |
| 394 | elif file == "HTMLtree" or file == "HTMLparser": |
| 395 | include.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 396 | export.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 397 | output.write("#ifdef LIBXML_HTML_ENABLED\n"); |
| 398 | elif file == "c14n": |
| 399 | include.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 400 | export.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 401 | output.write("#ifdef LIBXML_C14N_ENABLED\n"); |
| 402 | elif file == "xpathInternals" or file == "xpath": |
| 403 | include.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 404 | export.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 405 | output.write("#ifdef LIBXML_XPATH_ENABLED\n"); |
| 406 | elif file == "xpointer": |
| 407 | include.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 408 | export.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 409 | output.write("#ifdef LIBXML_XPTR_ENABLED\n"); |
| 410 | elif file == "xinclude": |
| 411 | include.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
| 412 | export.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
| 413 | output.write("#ifdef LIBXML_XINCLUDE_ENABLED\n"); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 414 | elif file == "xmlregexp": |
| 415 | include.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
| 416 | export.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
| 417 | output.write("#ifdef LIBXML_REGEXP_ENABLED\n"); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 418 | |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 419 | include.write("PyObject * ") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 420 | include.write("libxml_%s(PyObject *self, PyObject *args);\n" % (name)); |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 421 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 422 | export.write(" { (char *)\"%s\", libxml_%s, METH_VARARGS, NULL },\n" % |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 423 | (name, name)) |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 424 | |
| 425 | if file == "python": |
| 426 | # Those have been manually generated |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 427 | return 1 |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 428 | 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] | 429 | # Those have been manually generated |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 430 | return 1 |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 431 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 432 | output.write("PyObject *\n") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 433 | output.write("libxml_%s(ATTRIBUTE_UNUSED PyObject *self," % (name)) |
| 434 | if format == "": |
| 435 | output.write("ATTRIBUTE_UNUSED ") |
| 436 | output.write(" PyObject *args) {\n") |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 437 | if ret[0] != 'void': |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 438 | output.write(" PyObject *py_retval;\n") |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 439 | if c_return != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 440 | output.write(c_return) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 441 | if c_args != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 442 | output.write(c_args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 443 | if format != "": |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 444 | output.write("\n if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" % |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 445 | (format, format_args)) |
| 446 | output.write(" return(NULL);\n") |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 447 | if c_convert != "": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 448 | output.write(c_convert) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 449 | |
| 450 | output.write(c_call) |
| 451 | output.write(ret_convert) |
| 452 | output.write("}\n\n") |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 453 | if file == "debugXML": |
| 454 | include.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
| 455 | export.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
| 456 | output.write("#endif /* LIBXML_DEBUG_ENABLED */\n"); |
| 457 | elif file == "HTMLtree" or file == "HTMLparser": |
| 458 | include.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 459 | export.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 460 | output.write("#endif /* LIBXML_HTML_ENABLED */\n"); |
| 461 | elif file == "c14n": |
| 462 | include.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 463 | export.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 464 | output.write("#endif /* LIBXML_C14N_ENABLED */\n"); |
| 465 | elif file == "xpathInternals" or file == "xpath": |
| 466 | include.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 467 | export.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 468 | output.write("#endif /* LIBXML_XPATH_ENABLED */\n"); |
| 469 | elif file == "xpointer": |
| 470 | include.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 471 | export.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 472 | output.write("#endif /* LIBXML_XPTR_ENABLED */\n"); |
| 473 | elif file == "xinclude": |
| 474 | include.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
| 475 | export.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
| 476 | output.write("#endif /* LIBXML_XINCLUDE_ENABLED */\n"); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 477 | elif file == "xmlregexp": |
| 478 | include.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
| 479 | export.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
| 480 | output.write("#endif /* LIBXML_REGEXP_ENABLED */\n"); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 481 | return 1 |
| 482 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 483 | def buildStubs(): |
| 484 | global py_types |
| 485 | global py_return_types |
| 486 | global unknown_types |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 487 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 488 | try: |
| 489 | f = open("libxml2-api.xml") |
| 490 | data = f.read() |
| 491 | (parser, target) = getparser() |
| 492 | parser.feed(data) |
| 493 | parser.close() |
| 494 | except IOError, msg: |
| 495 | try: |
| 496 | f = open("../doc/libxml2-api.xml") |
| 497 | data = f.read() |
| 498 | (parser, target) = getparser() |
| 499 | parser.feed(data) |
| 500 | parser.close() |
| 501 | except IOError, msg: |
| 502 | print file, ":", msg |
| 503 | sys.exit(1) |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 504 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 505 | n = len(functions.keys()) |
| 506 | print "Found %d functions in libxml2-api.xml" % (n) |
| 507 | |
| 508 | py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") |
| 509 | try: |
| 510 | f = open("libxml2-python-api.xml") |
| 511 | data = f.read() |
| 512 | (parser, target) = getparser() |
| 513 | parser.feed(data) |
| 514 | parser.close() |
| 515 | except IOError, msg: |
| 516 | print file, ":", msg |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 517 | |
| 518 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 519 | print "Found %d functions in libxml2-python-api.xml" % ( |
| 520 | len(functions.keys()) - n) |
| 521 | nb_wrap = 0 |
| 522 | failed = 0 |
| 523 | skipped = 0 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 524 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 525 | include = open("libxml2-py.h", "w") |
| 526 | include.write("/* Generated */\n\n") |
| 527 | export = open("libxml2-export.c", "w") |
| 528 | export.write("/* Generated */\n\n") |
| 529 | wrapper = open("libxml2-py.c", "w") |
| 530 | wrapper.write("/* Generated */\n\n") |
| 531 | wrapper.write("#include <Python.h>\n") |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 532 | # wrapper.write("#include \"config.h\"\n") |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 533 | wrapper.write("#include <libxml/xmlversion.h>\n") |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 534 | wrapper.write("#include <libxml/tree.h>\n") |
| 535 | wrapper.write("#include \"libxml_wrap.h\"\n") |
| 536 | wrapper.write("#include \"libxml2-py.h\"\n\n") |
| 537 | for function in functions.keys(): |
| 538 | ret = print_function_wrapper(function, wrapper, export, include) |
| 539 | if ret < 0: |
| 540 | failed = failed + 1 |
| 541 | del functions[function] |
| 542 | if ret == 0: |
| 543 | skipped = skipped + 1 |
| 544 | del functions[function] |
| 545 | if ret == 1: |
| 546 | nb_wrap = nb_wrap + 1 |
| 547 | include.close() |
| 548 | export.close() |
| 549 | wrapper.close() |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 550 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 551 | print "Generated %d wrapper functions, %d failed, %d skipped\n" % (nb_wrap, |
| 552 | failed, skipped); |
| 553 | print "Missing type converters: " |
| 554 | for type in unknown_types.keys(): |
| 555 | print "%s:%d " % (type, len(unknown_types[type])), |
| 556 | print |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 557 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 558 | ####################################################################### |
| 559 | # |
| 560 | # This part writes part of the Python front-end classes based on |
| 561 | # mapping rules between types and classes and also based on function |
| 562 | # renaming to get consistent function names at the Python level |
| 563 | # |
| 564 | ####################################################################### |
| 565 | |
| 566 | # |
| 567 | # The type automatically remapped to generated classes |
| 568 | # |
| 569 | classes_type = { |
| 570 | "xmlNodePtr": ("._o", "xmlNode(_obj=%s)", "xmlNode"), |
| 571 | "xmlNode *": ("._o", "xmlNode(_obj=%s)", "xmlNode"), |
| 572 | "xmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 573 | "xmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 574 | "htmlDocPtr": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 575 | "htmlxmlDocPtr *": ("._o", "xmlDoc(_obj=%s)", "xmlDoc"), |
| 576 | "xmlAttrPtr": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), |
| 577 | "xmlAttr *": ("._o", "xmlAttr(_obj=%s)", "xmlAttr"), |
| 578 | "xmlNsPtr": ("._o", "xmlNs(_obj=%s)", "xmlNs"), |
| 579 | "xmlNs *": ("._o", "xmlNs(_obj=%s)", "xmlNs"), |
| 580 | "xmlDtdPtr": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), |
| 581 | "xmlDtd *": ("._o", "xmlDtd(_obj=%s)", "xmlDtd"), |
| 582 | "xmlEntityPtr": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), |
| 583 | "xmlEntity *": ("._o", "xmlEntity(_obj=%s)", "xmlEntity"), |
| 584 | "xmlElementPtr": ("._o", "xmlElement(_obj=%s)", "xmlElement"), |
| 585 | "xmlElement *": ("._o", "xmlElement(_obj=%s)", "xmlElement"), |
| 586 | "xmlAttributePtr": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), |
| 587 | "xmlAttribute *": ("._o", "xmlAttribute(_obj=%s)", "xmlAttribute"), |
| 588 | "xmlXPathContextPtr": ("._o", "xpathContext(_obj=%s)", "xpathContext"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 589 | "xmlXPathContext *": ("._o", "xpathContext(_obj=%s)", "xpathContext"), |
| 590 | "xmlXPathParserContext *": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), |
| 591 | "xmlXPathParserContextPtr": ("._o", "xpathParserContext(_obj=%s)", "xpathParserContext"), |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 592 | "xmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
| 593 | "xmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 594 | "htmlParserCtxtPtr": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
| 595 | "htmlParserCtxt *": ("._o", "parserCtxt(_obj=%s)", "parserCtxt"), |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 596 | "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"), |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 597 | "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"), |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 598 | "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"), |
| 599 | "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"), |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 600 | "xmlRegexpPtr": ("._o", "xmlReg(_obj=%s)", "xmlReg"), |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 601 | "xmlTextReaderPtr": ("._o", "xmlTextReader(_obj=%s)", "xmlTextReader"), |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | converter_type = { |
| 605 | "xmlXPathObjectPtr": "xpathObjectRet(%s)", |
| 606 | } |
| 607 | |
| 608 | primary_classes = ["xmlNode", "xmlDoc"] |
| 609 | |
| 610 | classes_ancestor = { |
| 611 | "xmlNode" : "xmlCore", |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 612 | "xmlDtd" : "xmlNode", |
| 613 | "xmlDoc" : "xmlNode", |
| 614 | "xmlAttr" : "xmlNode", |
| 615 | "xmlNs" : "xmlNode", |
| 616 | "xmlEntity" : "xmlNode", |
| 617 | "xmlElement" : "xmlNode", |
| 618 | "xmlAttribute" : "xmlNode", |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 619 | "outputBuffer": "ioWriteWrapper", |
| 620 | "inputBuffer": "ioReadWrapper", |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 621 | } |
| 622 | classes_destructors = { |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 623 | "parserCtxt": "xmlFreeParserCtxt", |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 624 | "catalog": "xmlFreeCatalog", |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 625 | "URI": "xmlFreeURI", |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 626 | # "outputBuffer": "xmlOutputBufferClose", |
| 627 | "inputBuffer": "xmlFreeParserInputBuffer", |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 628 | "xmlReg": "xmlRegFreeRegexp", |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 629 | "xmlTextReader": "xmlFreeTextReader", |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 632 | functions_noexcept = { |
| 633 | "xmlHasProp": 1, |
| 634 | "xmlHasNsProp": 1, |
| 635 | } |
| 636 | |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame^] | 637 | reference_keepers = { |
| 638 | "xmlTextReader": [('inputBuffer', 'input')], |
| 639 | } |
| 640 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 641 | function_classes = {} |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 642 | |
| 643 | function_classes["None"] = [] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 644 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 645 | def nameFixup(name, classe, type, file): |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 646 | listname = classe + "List" |
| 647 | ll = len(listname) |
| 648 | l = len(classe) |
| 649 | if name[0:l] == listname: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 650 | func = name[l:] |
| 651 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 652 | elif name[0:12] == "xmlParserGet" and file == "python_accessor": |
| 653 | func = name[12:] |
| 654 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 26f1dcc | 2002-02-03 16:53:19 +0000 | [diff] [blame] | 655 | elif name[0:12] == "xmlParserSet" and file == "python_accessor": |
| 656 | func = name[12:] |
| 657 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 658 | elif name[0:10] == "xmlNodeGet" and file == "python_accessor": |
| 659 | func = name[10:] |
| 660 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 661 | elif name[0:9] == "xmlURIGet" and file == "python_accessor": |
| 662 | func = name[9:] |
| 663 | func = string.lower(func[0:1]) + func[1:] |
| 664 | elif name[0:9] == "xmlURISet" and file == "python_accessor": |
| 665 | func = name[6:] |
| 666 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 667 | elif name[0:17] == "xmlXPathParserGet" and file == "python_accessor": |
| 668 | func = name[17:] |
| 669 | func = string.lower(func[0:1]) + func[1:] |
| 670 | elif name[0:11] == "xmlXPathGet" and file == "python_accessor": |
| 671 | func = name[11:] |
| 672 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 673 | elif name[0:11] == "xmlXPathSet" and file == "python_accessor": |
| 674 | func = name[8:] |
| 675 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 676 | elif name[0:15] == "xmlOutputBuffer" and file != "python": |
| 677 | func = name[15:] |
| 678 | func = string.lower(func[0:1]) + func[1:] |
| 679 | elif name[0:20] == "xmlParserInputBuffer" and file != "python": |
| 680 | func = name[20:] |
| 681 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | d4cb1e8 | 2002-09-26 09:34:23 +0000 | [diff] [blame] | 682 | elif name[0:9] == "xmlRegexp" and file == "xmlregexp": |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 683 | func = "regexp" + name[9:] |
Daniel Veillard | d4cb1e8 | 2002-09-26 09:34:23 +0000 | [diff] [blame] | 684 | elif name[0:6] == "xmlReg" and file == "xmlregexp": |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 685 | func = "regexp" + name[6:] |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 686 | elif name[0:13] == "xmlTextReader" and file == "xmlreader": |
| 687 | func = name[13:] |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 688 | elif name[0:11] == "xmlACatalog": |
| 689 | func = name[11:] |
| 690 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 691 | elif name[0:l] == classe: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 692 | func = name[l:] |
| 693 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 9589d45 | 2002-02-02 10:28:17 +0000 | [diff] [blame] | 694 | elif name[0:7] == "libxml_": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 695 | func = name[7:] |
| 696 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 697 | elif name[0:6] == "xmlGet": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 698 | func = name[6:] |
| 699 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 700 | elif name[0:3] == "xml": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 701 | func = name[3:] |
| 702 | func = string.lower(func[0:1]) + func[1:] |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 703 | else: |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 704 | func = name |
| 705 | if func[0:5] == "xPath": |
| 706 | func = "xpath" + func[5:] |
| 707 | elif func[0:4] == "xPtr": |
| 708 | func = "xpointer" + func[4:] |
| 709 | elif func[0:8] == "xInclude": |
| 710 | func = "xinclude" + func[8:] |
| 711 | elif func[0:2] == "iD": |
| 712 | func = "ID" + func[2:] |
| 713 | elif func[0:3] == "uRI": |
| 714 | func = "URI" + func[3:] |
| 715 | elif func[0:4] == "uTF8": |
| 716 | func = "UTF8" + func[4:] |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 717 | elif func[0:3] == 'sAX': |
| 718 | func = "SAX" + func[3:] |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 719 | return func |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 720 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 721 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 722 | def functionCompare(info1, info2): |
| 723 | (index1, func1, name1, ret1, args1, file1) = info1 |
| 724 | (index2, func2, name2, ret2, args2, file2) = info2 |
Daniel Veillard | 26f1dcc | 2002-02-03 16:53:19 +0000 | [diff] [blame] | 725 | if file1 == file2: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 726 | if func1 < func2: |
| 727 | return -1 |
| 728 | if func1 > func2: |
| 729 | return 1 |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 730 | if file1 == "python_accessor": |
| 731 | return -1 |
| 732 | if file2 == "python_accessor": |
| 733 | return 1 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 734 | if file1 < file2: |
| 735 | return -1 |
| 736 | if file1 > file2: |
| 737 | return 1 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 738 | return 0 |
| 739 | |
| 740 | def writeDoc(name, args, indent, output): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 741 | if functions[name][0] is None or functions[name][0] == "": |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 742 | return |
| 743 | val = functions[name][0] |
| 744 | val = string.replace(val, "NULL", "None"); |
| 745 | output.write(indent) |
| 746 | output.write('"""') |
| 747 | while len(val) > 60: |
| 748 | str = val[0:60] |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 749 | i = string.rfind(str, " "); |
| 750 | if i < 0: |
| 751 | i = 60 |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 752 | str = val[0:i] |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 753 | val = val[i:] |
| 754 | output.write(str) |
| 755 | output.write('\n '); |
| 756 | output.write(indent) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 757 | output.write(val); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 758 | output.write(' """\n') |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 759 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 760 | def buildWrappers(): |
| 761 | global ctypes |
| 762 | global py_types |
| 763 | global py_return_types |
| 764 | global unknown_types |
| 765 | global functions |
| 766 | global function_classes |
| 767 | global classes_type |
| 768 | global classes_list |
| 769 | global converter_type |
| 770 | global primary_classes |
| 771 | global converter_type |
| 772 | global classes_ancestor |
| 773 | global converter_type |
| 774 | global primary_classes |
| 775 | global classes_ancestor |
| 776 | global classes_destructors |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 777 | global functions_noexcept |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 778 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 779 | for type in classes_type.keys(): |
| 780 | function_classes[classes_type[type][2]] = [] |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 781 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 782 | # |
| 783 | # Build the list of C types to look for ordered to start |
| 784 | # with primary classes |
| 785 | # |
| 786 | ctypes = [] |
| 787 | classes_list = [] |
| 788 | ctypes_processed = {} |
| 789 | classes_processed = {} |
| 790 | for classe in primary_classes: |
| 791 | classes_list.append(classe) |
| 792 | classes_processed[classe] = () |
| 793 | for type in classes_type.keys(): |
| 794 | tinfo = classes_type[type] |
| 795 | if tinfo[2] == classe: |
| 796 | ctypes.append(type) |
| 797 | ctypes_processed[type] = () |
| 798 | for type in classes_type.keys(): |
| 799 | if ctypes_processed.has_key(type): |
| 800 | continue |
| 801 | tinfo = classes_type[type] |
| 802 | if not classes_processed.has_key(tinfo[2]): |
| 803 | classes_list.append(tinfo[2]) |
| 804 | classes_processed[tinfo[2]] = () |
| 805 | |
| 806 | ctypes.append(type) |
| 807 | ctypes_processed[type] = () |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 808 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 809 | for name in functions.keys(): |
| 810 | found = 0; |
| 811 | (desc, ret, args, file) = functions[name] |
| 812 | for type in ctypes: |
| 813 | classe = classes_type[type][2] |
| 814 | |
| 815 | if name[0:3] == "xml" and len(args) >= 1 and args[0][1] == type: |
| 816 | found = 1 |
| 817 | func = nameFixup(name, classe, type, file) |
| 818 | info = (0, func, name, ret, args, file) |
| 819 | function_classes[classe].append(info) |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 820 | elif name[0:3] == "xml" and len(args) >= 2 and args[1][1] == type \ |
| 821 | and file != "python_accessor": |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 822 | found = 1 |
| 823 | func = nameFixup(name, classe, type, file) |
| 824 | info = (1, func, name, ret, args, file) |
| 825 | function_classes[classe].append(info) |
| 826 | elif name[0:4] == "html" and len(args) >= 1 and args[0][1] == type: |
| 827 | found = 1 |
| 828 | func = nameFixup(name, classe, type, file) |
| 829 | info = (0, func, name, ret, args, file) |
| 830 | function_classes[classe].append(info) |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 831 | elif name[0:4] == "html" and len(args) >= 2 and args[1][1] == type \ |
| 832 | and file != "python_accessor": |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 833 | found = 1 |
| 834 | func = nameFixup(name, classe, type, file) |
| 835 | info = (1, func, name, ret, args, file) |
| 836 | function_classes[classe].append(info) |
| 837 | if found == 1: |
| 838 | break |
| 839 | if found == 1: |
| 840 | continue |
| 841 | if name[0:8] == "xmlXPath": |
| 842 | continue |
| 843 | if name[0:6] == "xmlStr": |
| 844 | continue |
| 845 | if name[0:10] == "xmlCharStr": |
| 846 | continue |
| 847 | func = nameFixup(name, "None", file, file) |
| 848 | info = (0, func, name, ret, args, file) |
| 849 | function_classes['None'].append(info) |
| 850 | |
| 851 | classes = open("libxml2class.py", "w") |
| 852 | txt = open("libxml2class.txt", "w") |
| 853 | txt.write(" Generated Classes for libxml2-python\n\n") |
| 854 | |
| 855 | txt.write("#\n# Global functions of the module\n#\n\n") |
| 856 | if function_classes.has_key("None"): |
| 857 | flist = function_classes["None"] |
| 858 | flist.sort(functionCompare) |
| 859 | oldfile = "" |
| 860 | for info in flist: |
| 861 | (index, func, name, ret, args, file) = info |
| 862 | if file != oldfile: |
| 863 | classes.write("#\n# Functions from module %s\n#\n\n" % file) |
| 864 | txt.write("\n# functions from module %s\n" % file) |
| 865 | oldfile = file |
| 866 | classes.write("def %s(" % func) |
| 867 | txt.write("%s()\n" % func); |
| 868 | n = 0 |
| 869 | for arg in args: |
| 870 | if n != 0: |
| 871 | classes.write(", ") |
| 872 | classes.write("%s" % arg[0]) |
| 873 | n = n + 1 |
| 874 | classes.write("):\n") |
| 875 | writeDoc(name, args, ' ', classes); |
| 876 | |
| 877 | for arg in args: |
| 878 | if classes_type.has_key(arg[1]): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 879 | classes.write(" if %s is None: %s__o = None\n" % |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 880 | (arg[0], arg[0])) |
| 881 | classes.write(" else: %s__o = %s%s\n" % |
| 882 | (arg[0], arg[0], classes_type[arg[1]][0])) |
| 883 | if ret[0] != "void": |
| 884 | classes.write(" ret = "); |
| 885 | else: |
| 886 | classes.write(" "); |
| 887 | classes.write("libxml2mod.%s(" % name) |
| 888 | n = 0 |
| 889 | for arg in args: |
| 890 | if n != 0: |
| 891 | classes.write(", "); |
| 892 | classes.write("%s" % arg[0]) |
| 893 | if classes_type.has_key(arg[1]): |
| 894 | classes.write("__o"); |
| 895 | n = n + 1 |
| 896 | classes.write(")\n"); |
| 897 | if ret[0] != "void": |
| 898 | if classes_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 899 | # |
| 900 | # Raise an exception |
| 901 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 902 | if functions_noexcept.has_key(name): |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 903 | classes.write(" if ret is None:return None\n"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 904 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 905 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 906 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 907 | % (name)) |
| 908 | elif string.find(name, "XPath") >= 0: |
| 909 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 910 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 911 | % (name)) |
| 912 | elif string.find(name, "Parse") >= 0: |
| 913 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 914 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 915 | % (name)) |
| 916 | else: |
| 917 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 918 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 919 | % (name)) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 920 | classes.write(" return "); |
| 921 | classes.write(classes_type[ret[0]][1] % ("ret")); |
| 922 | classes.write("\n"); |
| 923 | else: |
| 924 | classes.write(" return ret\n"); |
| 925 | classes.write("\n"); |
| 926 | |
| 927 | txt.write("\n\n#\n# Set of classes of the module\n#\n\n") |
| 928 | for classname in classes_list: |
| 929 | if classname == "None": |
| 930 | pass |
| 931 | else: |
| 932 | if classes_ancestor.has_key(classname): |
| 933 | txt.write("\n\nClass %s(%s)\n" % (classname, |
| 934 | classes_ancestor[classname])) |
| 935 | classes.write("class %s(%s):\n" % (classname, |
| 936 | classes_ancestor[classname])) |
| 937 | classes.write(" def __init__(self, _obj=None):\n") |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame^] | 938 | if reference_keepers.has_key(classname): |
| 939 | rlist = reference_keepers[classname] |
| 940 | for ref in rlist: |
| 941 | classes.write(" self.%s = None\n" % ref[1]) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 942 | classes.write(" self._o = None\n") |
| 943 | classes.write(" %s.__init__(self, _obj=_obj)\n\n" % ( |
| 944 | classes_ancestor[classname])) |
| 945 | if classes_ancestor[classname] == "xmlCore" or \ |
| 946 | classes_ancestor[classname] == "xmlNode": |
| 947 | classes.write(" def __repr__(self):\n") |
Daniel Veillard | ba5e18a | 2002-03-05 09:36:43 +0000 | [diff] [blame] | 948 | format = "<%s (%%s) object at 0x%%x>" % (classname) |
| 949 | classes.write(" return \"%s\" %% (self.name, id (self))\n\n" % ( |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 950 | format)) |
| 951 | else: |
| 952 | txt.write("Class %s()\n" % (classname)) |
| 953 | classes.write("class %s:\n" % (classname)) |
| 954 | classes.write(" def __init__(self, _obj=None):\n") |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame^] | 955 | if reference_keepers.has_key(classname): |
| 956 | list = reference_keepers[classname] |
| 957 | for ref in list: |
| 958 | classes.write(" self.%s = None\n" % ref[1]) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 959 | classes.write(" if _obj != None:self._o = _obj;return\n") |
| 960 | classes.write(" self._o = None\n\n"); |
| 961 | if classes_destructors.has_key(classname): |
| 962 | classes.write(" def __del__(self):\n") |
| 963 | classes.write(" if self._o != None:\n") |
| 964 | classes.write(" libxml2mod.%s(self._o)\n" % |
| 965 | classes_destructors[classname]); |
| 966 | classes.write(" self._o = None\n\n"); |
| 967 | flist = function_classes[classname] |
| 968 | flist.sort(functionCompare) |
| 969 | oldfile = "" |
| 970 | for info in flist: |
| 971 | (index, func, name, ret, args, file) = info |
| 972 | if file != oldfile: |
| 973 | if file == "python_accessor": |
| 974 | classes.write(" # accessors for %s\n" % (classname)) |
| 975 | txt.write(" # accessors\n") |
| 976 | else: |
| 977 | classes.write(" #\n") |
| 978 | classes.write(" # %s functions from module %s\n" % ( |
| 979 | classname, file)) |
| 980 | txt.write("\n # functions from module %s\n" % file) |
| 981 | classes.write(" #\n\n") |
| 982 | oldfile = file |
| 983 | classes.write(" def %s(self" % func) |
| 984 | txt.write(" %s()\n" % func); |
| 985 | n = 0 |
| 986 | for arg in args: |
| 987 | if n != index: |
| 988 | classes.write(", %s" % arg[0]) |
| 989 | n = n + 1 |
| 990 | classes.write("):\n") |
| 991 | writeDoc(name, args, ' ', classes); |
| 992 | n = 0 |
| 993 | for arg in args: |
| 994 | if classes_type.has_key(arg[1]): |
| 995 | if n != index: |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 996 | classes.write(" if %s is None: %s__o = None\n" % |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 997 | (arg[0], arg[0])) |
| 998 | classes.write(" else: %s__o = %s%s\n" % |
| 999 | (arg[0], arg[0], classes_type[arg[1]][0])) |
| 1000 | n = n + 1 |
| 1001 | if ret[0] != "void": |
| 1002 | classes.write(" ret = "); |
| 1003 | else: |
| 1004 | classes.write(" "); |
| 1005 | classes.write("libxml2mod.%s(" % name) |
| 1006 | n = 0 |
| 1007 | for arg in args: |
| 1008 | if n != 0: |
| 1009 | classes.write(", "); |
| 1010 | if n != index: |
| 1011 | classes.write("%s" % arg[0]) |
| 1012 | if classes_type.has_key(arg[1]): |
| 1013 | classes.write("__o"); |
| 1014 | else: |
| 1015 | classes.write("self"); |
| 1016 | if classes_type.has_key(arg[1]): |
| 1017 | classes.write(classes_type[arg[1]][0]) |
| 1018 | n = n + 1 |
| 1019 | classes.write(")\n"); |
| 1020 | if ret[0] != "void": |
| 1021 | if classes_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1022 | # |
| 1023 | # Raise an exception |
| 1024 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1025 | if functions_noexcept.has_key(name): |
| 1026 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1027 | " if ret is None:return None\n"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1028 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1029 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1030 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1031 | % (name)) |
| 1032 | elif string.find(name, "XPath") >= 0: |
| 1033 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1034 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1035 | % (name)) |
| 1036 | elif string.find(name, "Parse") >= 0: |
| 1037 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1038 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1039 | % (name)) |
| 1040 | else: |
| 1041 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1042 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1043 | % (name)) |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame^] | 1044 | |
| 1045 | # |
| 1046 | # generate the returned class wrapper for the object |
| 1047 | # |
| 1048 | classes.write(" __tmp = "); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1049 | classes.write(classes_type[ret[0]][1] % ("ret")); |
| 1050 | classes.write("\n"); |
Daniel Veillard | dc85f28 | 2002-12-31 11:18:37 +0000 | [diff] [blame^] | 1051 | |
| 1052 | # |
| 1053 | # Sometime one need to keep references of the source |
| 1054 | # class in the returned class object. |
| 1055 | # See reference_keepers for the list |
| 1056 | # |
| 1057 | tclass = classes_type[ret[0]][2] |
| 1058 | if reference_keepers.has_key(tclass): |
| 1059 | list = reference_keepers[tclass] |
| 1060 | for pref in list: |
| 1061 | if pref[0] == ref[0]: |
| 1062 | classes.write(" __tmp.%s = self\n" % |
| 1063 | pref[1]) |
| 1064 | # |
| 1065 | # return the class |
| 1066 | # |
| 1067 | classes.write(" return __tmp\n"); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1068 | elif converter_type.has_key(ret[0]): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1069 | # |
| 1070 | # Raise an exception |
| 1071 | # |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1072 | if functions_noexcept.has_key(name): |
| 1073 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1074 | " if ret is None:return None"); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 1075 | elif string.find(name, "URI") >= 0: |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1076 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1077 | " if ret is None:raise uriError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1078 | % (name)) |
| 1079 | elif string.find(name, "XPath") >= 0: |
| 1080 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1081 | " if ret is None:raise xpathError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1082 | % (name)) |
| 1083 | elif string.find(name, "Parse") >= 0: |
| 1084 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1085 | " if ret is None:raise parserError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1086 | % (name)) |
| 1087 | else: |
| 1088 | classes.write( |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 1089 | " if ret is None:raise treeError('%s() failed')\n" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1090 | % (name)) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 1091 | classes.write(" return "); |
| 1092 | classes.write(converter_type[ret[0]] % ("ret")); |
| 1093 | classes.write("\n"); |
| 1094 | else: |
| 1095 | classes.write(" return ret\n"); |
| 1096 | classes.write("\n"); |
| 1097 | |
| 1098 | txt.close() |
| 1099 | classes.close() |
| 1100 | |
| 1101 | |
| 1102 | buildStubs() |
| 1103 | buildWrappers() |