# Boa file upgrade helper. # paul sorenson Feb 2005 # WARNING - changes files, take care. # requires: pyparsing # usage: python upgrade myFrame.py > myNewFrame.py # Current status: concept demonstrator # $Id$ # # This file currently munges the EVT_xxx macros from those used with # wxPython 2.4 to the 2.5 style. At the moment only EVT_'s found in my # code are catered for, adding new ones is simple. # The code could be made more general - that was not my initial design # goal. from pyparsing import * import string class Upgrade: def __init__(self): COMMA = Literal(',').suppress() LPAREN = Literal('(').suppress() RPAREN = Literal(')').suppress() ident = Word(alphas, alphanums+"_") qident = Word(alphas, alphanums+"_.") # 2 Parameter evt macros. P2_Evt = Literal("RIGHT_DOWN") ^ Literal("GRID_CELL_CHANGE") ^\ Literal("CHAR") ^ Literal("KEY_DOWN") ^\ Literal("GRID_CELL_RIGHT_CLICK") evt_P2 = Literal("EVT_") + P2_Evt + LPAREN +\ qident + COMMA +\ qident +\ RPAREN evt_P2.setParseAction(self.evt_P2Action) # 3 Parameter evt macros. P3_Evt = Literal("MENU") ^ Literal("TOOL") ^\ Literal("NOTEBOOK_PAGE_CHANGED") ^ Literal("TREE_ITEM_ACTIVATED") ^\ Literal("TEXT_ENTER") ^ Literal("RADIOBOX") evt_P3 = Literal("EVT_") + P3_Evt + LPAREN +\ qident + COMMA +\ qident + COMMA +\ qident +\ RPAREN evt_P3.setParseAction(self.evt_P3Action) self.grammar = evt_P2 ^ evt_P3 def evt_P2Action(self, s, l, t): ev, evname, win, fn = t module = 'wx' if evname.find("GRID") != -1: module += ".grid" return '%s.Bind(%s.%s%s, %s)' % (win, module, ev, evname, fn) def evt_P3Action(self, s, l, t): ev, evname, win, id, fn = t return '%s.Bind(wx.%s%s, %s, id=%s)' % (win, ev, evname, fn, id) def replace(self, text): self.data = text for t, s, e in self.grammar.scanString(self.data): print s, e, t[0] def scanner(self, text): ''' Scan text, replacing grammar as we go. ''' pos = 0 for t, s, e in self.grammar.scanString(text): yield text[pos:s], t[0] pos = e if pos < len(text): yield text[pos:], '' if __name__ == "__main__": import sys u = Upgrade() if len(sys.argv) < 2: print 'usage: python update.py ' sys.exit(1) filename = sys.argv[1] fin = file(filename, 'r') try: frag = [] for non, rep in u.scanner(fin.read()): frag.append(non); frag.append(rep); newtext = string.join(frag, '') print newtext finally: fin.close()