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