]> git.saurik.com Git - wxWidgets.git/blame - wxPython/distrib/build_renamers.py
wxGTK compilation fixes.
[wxWidgets.git] / wxPython / distrib / build_renamers.py
CommitLineData
d14a1e28
RD
1#!/usr/bin/env python
2#---------------------------------------------------------------------------
3"""
8994c3c2 4Usage: build_renamers.py destdir modulename filename.xml
d14a1e28 5
8994c3c2
RD
6Scans the XML file produced by SWIG (see setup.py) and generate the
7%rename directives needed to implement the new wx namespace. The
8rename directives are output in a file named _modulename_rename.i in
9the destdir given.
d14a1e28
RD
10
11Also output a reverse 'renamer' Python module located in
12wxPython/filename.py (relative the the current dir) to make a
13backwards compatibility interface for the old wxPython packages.
14"""
15
16import sys, os, tempfile, pprint
8994c3c2 17import xml.sax
d14a1e28
RD
18from distutils.spawn import spawn
19
8994c3c2
RD
20try:
21 import libxml2
22 USE_LIBXML2 = True
23except ImportError:
24 USE_LIBXML2 = False
d14a1e28
RD
25
26wxPythonDir = "wxPython"
d14a1e28 27
8994c3c2 28#---------------------------------------------------------------------------
d14a1e28
RD
29
30
31renamerTemplateStart = """\
32// A bunch of %%rename directives generated by %s
33// in order to remove the wx prefix from all global scope names.
34
8994c3c2 35#ifndef BUILDING_RENAMERS
d14a1e28
RD
36
37"""
38
39renamerTemplateEnd = """
40#endif
41"""
42
43wxPythonTemplateStart = """\
44## This file reverse renames symbols in the wx package to give
45## them their wx prefix again, for backwards compatibility.
46##
47## Generated by %s
48
49# This silly stuff here is so the wxPython.wx module doesn't conflict
50# with the wx package. We need to import modules from the wx package
51# here, then we'll put the wxPython.wx entry back in sys.modules.
52import sys
53_wx = None
54if sys.modules.has_key('wxPython.wx'):
55 _wx = sys.modules['wxPython.wx']
56 del sys.modules['wxPython.wx']
57
58import wx.%s
59
60sys.modules['wxPython.wx'] = _wx
61del sys, _wx
62
63
64# Now assign all the reverse-renamed names:
65"""
66
67wxPythonTemplateEnd = """
68
69"""
70
71
72
73#---------------------------------------------------------------------------
74
75def main(args):
76 # check args
8994c3c2 77 if len(args) < 3:
d14a1e28
RD
78 print __doc__
79 sys.exit(1)
80
81 # check location (there should be a wxPython subdir)
82 if not os.path.exists(wxPythonDir) or not os.path.isdir(wxPythonDir):
83 print __doc__
84 print "You should only run this script from the main wxPython source dir.\n"
85 sys.exit(1)
86
87
8994c3c2
RD
88 destdir = args[0]
89 modname = args[1]
90 xmlfile = args[2]
91
92 swigDest = os.path.join(destdir, "_"+modname+"_rename.i")
93 pyDest = os.path.join(wxPythonDir, modname + '.py')
d14a1e28
RD
94
95 swigDestTemp = tempfile.mktemp('.tmp')
96 swigFile = open(swigDestTemp, "w")
97 swigFile.write(renamerTemplateStart % sys.argv[0])
98
8994c3c2
RD
99 pyDestTemp = tempfile.mktemp('.tmp')
100 pyFile = open(pyDestTemp, "w")
101 pyFile.write(wxPythonTemplateStart % (sys.argv[0], modname))
102
d14a1e28 103 try:
8994c3c2
RD
104 print "Parsing and building renamers",
105 if USE_LIBXML2:
106 processXML(xmlfile, modname, swigFile, pyFile)
107 else:
108 print "using xml.sax..."
109 xml.sax.parse(xmlfile, ContentHandler(modname, swigFile, pyFile))
d14a1e28
RD
110
111 finally:
8994c3c2
RD
112
113 checkOtherNames(pyFile, modname,
114 os.path.join(destdir, '_'+modname+'_reverse.txt'))
d14a1e28
RD
115 pyFile.write(wxPythonTemplateEnd)
116 pyFile.close()
117
118 swigFile.write(renamerTemplateEnd)
119 swigFile.close()
120
8994c3c2 121 # Compare the files just created with the existing one and
d14a1e28 122 # blow away the old one if they are different.
8994c3c2
RD
123 for dest, temp in [(swigDest, swigDestTemp),
124 (pyDest, pyDestTemp)]:
6c72a026
RD
125 if not os.path.exists(dest):
126 os.rename(temp, dest)
127 elif open(dest).read() != open(temp).read():
8994c3c2
RD
128 os.unlink(dest)
129 os.rename(temp, dest)
130 else:
131 print dest + " not changed."
132 os.unlink(temp)
133
134#---------------------------------------------------------------------------
135
136
137def GetAttr(node, name):
138 path = "./attributelist/attribute[@name='%s']/@value" % name
3798e298 139 n = node.xpathEval2(path)
8994c3c2
RD
140 if len(n):
141 return n[0].content
142 else:
143 return None
144
145
146def processXML(xmlfile, modname, swigFile, pyFile):
147 import libxml2
148 print "using libxml2..."
149
150 topnode = libxml2.parseFile(xmlfile).children
151
152 # remove any import nodes as we don't need to do renamers for symbols found therein
3798e298 153 imports = topnode.xpathEval2("*/import")
8994c3c2
RD
154 for n in imports:
155 n.unlinkNode()
156 n.freeNode()
157
158 # do a depth first iteration over what's left
159 for node in topnode:
160 doRename = False
161 doPtr = False
162 addWX = False
163 revOnly = False
d14a1e28 164
d14a1e28 165
8994c3c2
RD
166 if node.name == "class":
167 lastClassName = name = GetAttr(node, "name")
168 lastClassSymName = sym_name = GetAttr(node, "sym_name")
169 doRename = True
170 doPtr = True
171 if sym_name != name:
172 name = sym_name
173 addWX = True
174
175 # renamed constructors
176 elif node.name == "constructor":
177 name = GetAttr(node, "name")
178 sym_name = GetAttr(node, "sym_name")
179 if sym_name != name:
180 name = sym_name
181 addWX = True
182 doRename = True
183
184 # only enumitems at the top level
185 elif node.name == "enumitem" and node.parent.parent.name == "include":
186 name = GetAttr(node, "name")
187 sym_name = GetAttr(node, "sym_name")
188 doRename = True
189
190
191 elif node.name in ["cdecl", "constant"]:
192 name = GetAttr(node, "name")
193 sym_name = GetAttr(node, "sym_name")
194 toplevel = node.parent.name == "include"
195
196 # top-level functions
197 if toplevel and GetAttr(node, "view") == "globalfunctionHandler":
198 doRename = True
199
200 # top-level global vars
201 elif toplevel and GetAttr(node, "feature_immutable") == "1":
202 doRename = True
203
204 # static methods
205 elif GetAttr(node, "view") == "staticmemberfunctionHandler":
206 name = lastClassName + '_' + name
207 sym_name = lastClassSymName + '_' + sym_name
208 # only output the reverse renamer in this case
209 doRename = revOnly = True
210
211 if doRename and name != sym_name:
212 name = sym_name
213 addWX = True
214
215
089142a5 216 if doRename and name:
8994c3c2
RD
217 old = new = name
218 if old.startswith('wx') and not old.startswith('wxEVT_'):
219 # remove all wx prefixes except wxEVT_ and write a %rename directive for it
220 new = old[2:]
221 if not revOnly:
222 swigFile.write("%%rename(%s) %35s;\n" % (new, old))
223
224 # Write assignments to import into the old wxPython namespace
225 if addWX and not old.startswith('wx'):
226 old = 'wx'+old
227 pyFile.write("%s = wx.%s.%s\n" % (old, modname, new))
228 if doPtr:
229 pyFile.write("%sPtr = wx.%s.%sPtr\n" % (old, modname, new))
230
d14a1e28
RD
231
232#---------------------------------------------------------------------------
233
234def checkOtherNames(pyFile, moduleName, filename):
235 if os.path.exists(filename):
236 prefixes = []
237 for line in file(filename):
238 if line.endswith('\n'):
239 line = line[:-1]
240 if line and not line.startswith('#'):
241 if line.endswith('*'):
242 prefixes.append(line[:-1])
243 elif line.find('=') != -1:
244 pyFile.write("%s\n" % line)
245 else:
246 wxname = 'wx' + line
247 if line.startswith('wx') or line.startswith('WX') or line.startswith('EVT'):
248 wxname = line
249 pyFile.write("%s = wx.%s.%s\n" % (wxname, moduleName, line))
250
251 if prefixes:
252 pyFile.write(
253 "\n\nd = globals()\nfor k, v in wx.%s.__dict__.iteritems():"
254 % moduleName)
255 first = True
256 for p in prefixes:
257 if first:
258 pyFile.write("\n if ")
259 first = False
260 else:
261 pyFile.write("\n elif ")
262 pyFile.write("k.startswith('%s'):\n d[k] = v" % p)
263 pyFile.write("\ndel d, k, v\n\n")
264
265
266#---------------------------------------------------------------------------
267
268interestingTypes = [ 'class', 'cdecl', 'enumitem', 'constructor', 'constant' ]
269interestingAttrs = [ 'name', 'sym_name', 'decl', 'feature_immutable', 'module',
270 'storage', 'type' ]
271
272
273class Element:
274 def __init__(self, tagtype):
275 self.tagtype = tagtype
276 self.level = -1
277 self.name = None
278 self.sym_name = None
279 self.decl = None
280 self.immutable = None
281 self.klass = None
282 self.module = None
283 self.storage = None
284 self.type = None
285 self.startLine = -1
286
287
288 def write(self, moduleName, swigFile, pyFile):
289 doRename = False
423af76e 290 doPtr = False
d14a1e28
RD
291 addWX = False
292 revOnly = False
293
294 #if self.name.find('DefaultPosition') != -1:
295 # pprint.pprint(self.__dict__)
296
297 if self.tagtype in ['cdecl', 'constant']:
298 if self.storage == 'typedef':
299 pass
300
301 # top level functions
302 elif self.level == 0 and self.decl != "":
303 doRename = True
304
305 # top level global vars
306 elif self.level == 0 and self.immutable == '1':
307 doRename = True
308
309 # static methods
310 elif self.storage == 'static':
311 if not self.klass:
312 pprint.pprint(self.__dict__)
313 else:
314 self.name = self.klass + '_' + self.name
315 self.sym_name = self.sym_klass + '_' + self.sym_name
316 # only output the reverse renamer in this case
317 doRename = revOnly = True
318
319
320
321 if doRename and self.name != self.sym_name:
322 #print "%-25s %-25s" % (self.name, self.sym_name)
323 self.name = self.sym_name
324 addWX = True
325
326
327 elif self.tagtype == 'class' and self.module == moduleName:
328 doRename = True
423af76e 329 doPtr = True
d14a1e28
RD
330 if self.sym_name != self.klass:
331 #print self.sym_name
332 self.name = self.sym_name
333 addWX = True
334
335 elif self.tagtype == 'constructor':
336 #print "%-25s %-25s" % (self.name, self.sym_name)
337 if self.sym_name != self.klass:
338 #print self.sym_name
339 self.name = self.sym_name
340 addWX = True
341 doRename = True
342
343 elif self.tagtype == 'enumitem' and self.level == 0:
344 doRename = True
345
346
347 if doRename:
348 #print "%-25s %-25s" % (self.name, self.sym_name)
349 old = new = self.name
350 if old.startswith('wx') and not old.startswith('wxEVT_'):
351 # remove all wx prefixes except wxEVT_ and write a %rename directive for it
352 new = old[2:]
353 if not revOnly:
354 swigFile.write("%%rename(%s) %35s;\n" % (new, old))
355
356 # Write assignments to import into the old wxPython namespace
357 if addWX and not old.startswith('wx'):
358 old = 'wx'+old
359 pyFile.write("%s = wx.%s.%s\n" % (old, moduleName, new))
423af76e
RD
360 if doPtr:
361 pyFile.write("%sPtr = wx.%s.%sPtr\n" % (old, moduleName, new))
362
d14a1e28
RD
363
364
365 #else:
366 # text = "%07d %d %10s %-35s %s\n" % (
367 # self.startLine, self.level, self.tagtype, self.name, self.decl)
368 # #rejects.write(text)
369 # print text,
370
371
372#---------------------------------------------------------------------------
373
374class ContentHandler(xml.sax.ContentHandler):
8994c3c2 375 def __init__(self, modname, swigFile, pyFile):
d14a1e28 376 xml.sax.ContentHandler.__init__(self)
8994c3c2 377 self.modname = modname
d14a1e28
RD
378 self.swigFile = swigFile
379 self.pyFile = pyFile
380 self.elements = []
381 self.imports = 0
382 self.klass = None
383 self.sym_klass = None
384
385
386 def setDocumentLocator(self, locator):
387 self.locator = locator
388
389
390
391 def startElement(self, name, attrs):
392 if name in interestingTypes:
393 # start of a new element that we are interested in
394 ce = Element(name)
395 ce.startLine = self.locator.getLineNumber()
396 ce.level = len(self.elements)
397 if name == 'constructor':
398 ce.klass = self.elements[0].name
399 else:
400 ce.klass = self.klass
401 ce.sym_klass = self.sym_klass
402 self.elements.insert(0, ce)
403
404
405 elif len(self.elements) and name == 'attribute' and attrs['name'] in interestingAttrs:
406 attrName = attrs['name']
407 attrVal = attrs['value']
408 if attrName.startswith('feature_'):
409 attrName = attrName.replace('feature_', '')
410 ce = self.elements[0]
411 if getattr(ce, attrName) is None:
412 setattr(ce, attrName, attrVal)
413 if ce.tagtype == 'class' and attrName == 'name' and self.klass is None:
414 self.klass = attrVal
415 if ce.tagtype == 'class' and attrName == 'sym_name' and self.sym_klass is None:
416 self.sym_klass = attrVal
417
418
419## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'name':
420## # save the elements name
421## ce = self.elements[0]
422## if ce.name is None:
423## ce.name = attrs['value']
424## ce.nameLine = self.locator.getLineNumber()
425
426## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'sym_name':
427## # save the elements name
428## ce = self.elements[0]
429## if ce.sym_name is None:
430## ce.sym_name = attrs['value']
431
432## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'decl':
433## # save the elements decl
434## ce = self.elements[0]
435## ce.decl = attrs['value']
436
437## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'feature_immutable':
438## # save the elements decl
439## ce = self.elements[0]
440## ce.immutable = int(attrs['value'])
441
442## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'module':
443## # save the elements decl
444## ce = self.elements[0]
445## ce.module = attrs['value']
446
447 elif name == 'import':
448 self.imports += 1
449
450## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'storage':
451## # save the elements decl
452## ce = self.elements[0]
453## ce.storage = attrs['value']
454
455## elif len(self.elements) and name == 'attribute' and attrs['name'] == 'type':
456## # save the elements decl
457## ce = self.elements[0]
458## ce.type = attrs['value']
459
460
461 def endElement(self, name):
462 if name in interestingTypes:
463 # end of an element that we are interested in
464 ce = self.elements.pop(0)
465
466 if self.imports == 0:
467 # only write for items that are in this file, not imported
8994c3c2 468 ce.write(self.modname, self.swigFile, self.pyFile)
d14a1e28
RD
469
470 if name == 'import':
471 self.imports -= 1
472
473 if name == 'class':
474 self.klass = None
475 self.sym_klass = None
476
477
478#---------------------------------------------------------------------------
479
480if __name__ == "__main__":
481 main(sys.argv[1:])
482