~ubuntu-branches/ubuntu/trusty/boa-constructor/trusty

« back to all changes in this revision

Viewing changes to Plug-ins/wx25upgrade.plug-in.py

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2007-01-23 21:32:29 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070123213229-1d9lxp9c4dutjwv5
Add a .desktop file (Closes: #349081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-----------------------------------------------------------------------------
 
2
# Name:        wx25upgradeDialog.py
 
3
# Purpose:     Dialog to select a folder to upgrade
 
4
#
 
5
#              Inspired by paul sorenson's upgrade.py, which is required
 
6
#
 
7
# Author:      Werner F. Bruhin
 
8
#
 
9
# Created:     2005/07/03
 
10
# RCS-ID:      $Id: wx25upgrade.plug-in.py,v 1.3 2005/05/13 21:20:24 riaan Exp $
 
11
# Licence:     very simple, any one may use and/or change it.
 
12
#-----------------------------------------------------------------------------
 
13
#Boa:Dialog:Wx25CodeUpgradeDlg
 
14
 
 
15
import wx
 
16
import os
 
17
import sys
 
18
import string
 
19
 
 
20
from ExternalLib import wx25upgrade, reindent
 
21
import Utils
 
22
 
 
23
[wxID_WX25CODEUPGRADEDLG, wxID_WX25CODEUPGRADEDLGSETFILE, 
 
24
 wxID_WX25CODEUPGRADEDLGSETSOURCE, wxID_WX25CODEUPGRADEDLGSETTARGET, 
 
25
 wxID_WX25CODEUPGRADEDLGSOURCEFILE, wxID_WX25CODEUPGRADEDLGSOURCEFOLDER, 
 
26
 wxID_WX25CODEUPGRADEDLGSTARTFILE, wxID_WX25CODEUPGRADEDLGSTARTFOLDER, 
 
27
 wxID_WX25CODEUPGRADEDLGSTATICTEXT1, wxID_WX25CODEUPGRADEDLGSTATICTEXT2, 
 
28
 wxID_WX25CODEUPGRADEDLGSTFILE, wxID_WX25CODEUPGRADEDLGSTSOURCEFOLDER, 
 
29
 wxID_WX25CODEUPGRADEDLGSTTARGETFOLDER, wxID_WX25CODEUPGRADEDLGTARGETFOLDER, 
 
30
 wxID_WX25CODEUPGRADEDLGUPGRADEGUIDE, 
 
31
] = [wx.NewId() for _init_ctrls in range(15)]
 
32
 
 
33
class Wx25CodeUpgradeDlg(wx.Dialog):
 
34
    def _init_coll_flexGridSizer1_Items(self, parent):
 
35
        # generated method, don't edit
 
36
 
 
37
        parent.AddWindow(self.stSourceFolder, 1, border=2,
 
38
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)
 
39
        parent.AddWindow(self.sourceFolder, 1, border=2, flag=wx.ALL)
 
40
        parent.AddWindow(self.stTargetFolder, 1, border=2,
 
41
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)
 
42
        parent.AddWindow(self.targetFolder, 1, border=2, flag=wx.ALL)
 
43
        parent.AddWindow(self.stFile, 1, border=2,
 
44
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)
 
45
        parent.AddWindow(self.sourceFile, 1, border=2, flag=wx.ALL)
 
46
 
 
47
    def _init_coll_bsDialog_Items(self, parent):
 
48
        # generated method, don't edit
 
49
 
 
50
        parent.AddSizer(self.flexGridSizer1, 1, border=2, flag=wx.ALL)
 
51
        parent.AddWindow(self.staticText1, 0, border=10,
 
52
              flag=wx.ALIGN_LEFT | wx.ALL)
 
53
        parent.AddSizer(self.fgsButtons, 0, border=2, flag=wx.ALL)
 
54
        parent.AddSpacer(wx.Size(8, 8), border=0, flag=0)
 
55
        parent.AddWindow(self.StaticText2, 0, border=2, flag=wx.ALL)
 
56
 
 
57
    def _init_coll_fgsButtons_Items(self, parent):
 
58
        # generated method, don't edit
 
59
 
 
60
        parent.AddWindow(self.setSource, 1, border=2, flag=wx.ALL)
 
61
        parent.AddWindow(self.setTarget, 1, border=2, flag=wx.ALL)
 
62
        parent.AddWindow(self.startFolder, 1, border=2, flag=wx.ALL)
 
63
        parent.AddSpacer(wx.Size(8, 8), border=0, flag=0)
 
64
        parent.AddWindow(self.setFile, 1, border=2, flag=wx.ALL)
 
65
        parent.AddWindow(self.startFile, 1, border=2, flag=wx.ALL)
 
66
        parent.AddSpacer(wx.Size(8, 8), border=0, flag=0)
 
67
        parent.AddWindow(self.upgradeGuide, 1, border=2, flag=wx.ALL)
 
68
 
 
69
    def _init_sizers(self):
 
70
        # generated method, don't edit
 
71
        self.flexGridSizer1 = wx.FlexGridSizer(cols=2, hgap=0, rows=0, vgap=0)
 
72
 
 
73
        self.fgsButtons = wx.FlexGridSizer(cols=4, hgap=0, rows=0, vgap=0)
 
74
 
 
75
        self.bsDialog = wx.BoxSizer(orient=wx.VERTICAL)
 
76
 
 
77
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
 
78
        self._init_coll_fgsButtons_Items(self.fgsButtons)
 
79
        self._init_coll_bsDialog_Items(self.bsDialog)
 
80
 
 
81
        self.SetSizer(self.bsDialog)
 
82
 
 
83
    def _init_ctrls(self, prnt):
 
84
        # generated method, don't edit
 
85
        wx.Dialog.__init__(self, id=wxID_WX25CODEUPGRADEDLG,
 
86
              name='Wx25CodeUpgradeDlg', parent=prnt, pos=wx.Point(527, 423),
 
87
              size=wx.Size(573, 220), style=wx.DEFAULT_DIALOG_STYLE,
 
88
              title='Upgrade Boa code to 0.4 style')
 
89
        self.SetClientSize(wx.Size(565, 193))
 
90
        self.Center(wx.BOTH)
 
91
 
 
92
        self.stSourceFolder = wx.StaticText(id=wxID_WX25CODEUPGRADEDLGSTSOURCEFOLDER,
 
93
              label='Source Folder:', name='stSourceFolder', parent=self,
 
94
              pos=wx.Point(4, 8), size=wx.Size(69, 13), style=0)
 
95
 
 
96
        self.sourceFolder = wx.TextCtrl(id=wxID_WX25CODEUPGRADEDLGSOURCEFOLDER,
 
97
              name='sourceFolder', parent=self, pos=wx.Point(108, 4),
 
98
              size=wx.Size(435, 21), style=0, value='')
 
99
 
 
100
        self.targetFolder = wx.TextCtrl(id=wxID_WX25CODEUPGRADEDLGTARGETFOLDER,
 
101
              name='targetFolder', parent=self, pos=wx.Point(108, 29),
 
102
              size=wx.Size(435, 21), style=0, value='')
 
103
 
 
104
        self.stTargetFolder = wx.StaticText(id=wxID_WX25CODEUPGRADEDLGSTTARGETFOLDER,
 
105
              label='Target Folder', name='stTargetFolder', parent=self,
 
106
              pos=wx.Point(4, 33), size=wx.Size(63, 13), style=0)
 
107
 
 
108
        self.stFile = wx.StaticText(id=wxID_WX25CODEUPGRADEDLGSTFILE,
 
109
              label='File (individual):', name='stFile', parent=self,
 
110
              pos=wx.Point(4, 57), size=wx.Size(100, 13), style=0)
 
111
 
 
112
        self.sourceFile = wx.TextCtrl(id=wxID_WX25CODEUPGRADEDLGSOURCEFILE,
 
113
              name='sourceFile', parent=self, pos=wx.Point(108, 54),
 
114
              size=wx.Size(435, 21), style=0, value='')
 
115
 
 
116
        self.staticText1 = wx.StaticText(id=wxID_WX25CODEUPGRADEDLGSTATICTEXT1,
 
117
              label='It is strongly recommended NOT to set the "Target Folder" to the same name as "Source Folder"!',
 
118
              name='staticText1', parent=self, pos=wx.Point(10, 87),
 
119
              size=wx.Size(457, 13), style=0)
 
120
        self.staticText1.SetForegroundColour(wx.Colour(255, 0, 0))
 
121
 
 
122
        self.setSource = wx.Button(id=wxID_WX25CODEUPGRADEDLGSETSOURCE,
 
123
              label='Set Source Folder', name='setSource', parent=self,
 
124
              pos=wx.Point(4, 114), size=wx.Size(100, 23), style=0)
 
125
        self.setSource.Bind(wx.EVT_BUTTON, self.OnSetSourceButton,
 
126
              id=wxID_WX25CODEUPGRADEDLGSETSOURCE)
 
127
 
 
128
        self.setTarget = wx.Button(id=wxID_WX25CODEUPGRADEDLGSETTARGET,
 
129
              label='Set Target Folder', name='setTarget', parent=self,
 
130
              pos=wx.Point(108, 114), size=wx.Size(97, 23), style=0)
 
131
        self.setTarget.Bind(wx.EVT_BUTTON, self.OnSetTargetButton,
 
132
              id=wxID_WX25CODEUPGRADEDLGSETTARGET)
 
133
 
 
134
        self.startFolder = wx.Button(id=wxID_WX25CODEUPGRADEDLGSTARTFOLDER,
 
135
              label='Convert Folder', name='start', parent=self,
 
136
              pos=wx.Point(209, 114), size=wx.Size(92, 23), style=0)
 
137
        self.startFolder.Bind(wx.EVT_BUTTON, self.OnStartFolderButton,
 
138
              id=wxID_WX25CODEUPGRADEDLGSTARTFOLDER)
 
139
 
 
140
        self.setFile = wx.Button(id=wxID_WX25CODEUPGRADEDLGSETFILE,
 
141
              label='Set File name', name='setFile', parent=self,
 
142
              pos=wx.Point(4, 141), size=wx.Size(97, 23), style=0)
 
143
        self.setFile.Bind(wx.EVT_BUTTON, self.OnSetFileButton,
 
144
              id=wxID_WX25CODEUPGRADEDLGSETFILE)
 
145
 
 
146
        self.startFile = wx.Button(id=wxID_WX25CODEUPGRADEDLGSTARTFILE,
 
147
              label='Convert File', name='startFile', parent=self,
 
148
              pos=wx.Point(108, 141), size=wx.Size(92, 23), style=0)
 
149
        self.startFile.Bind(wx.EVT_BUTTON, self.OnStartFileButton,
 
150
              id=wxID_WX25CODEUPGRADEDLGSTARTFILE)
 
151
 
 
152
        self.upgradeGuide = wx.Button(id=wxID_WX25CODEUPGRADEDLGUPGRADEGUIDE,
 
153
              label='Upgrade Guide', name='upgradeGuide', parent=self,
 
154
              pos=wx.Point(305, 141), size=wx.Size(103, 23), style=0)
 
155
        self.upgradeGuide.Bind(wx.EVT_BUTTON, self.OnUpgradeGuideButton,
 
156
              id=wxID_WX25CODEUPGRADEDLGUPGRADEGUIDE)
 
157
 
 
158
        self.StaticText2 = wx.StaticText(id=wxID_WX25CODEUPGRADEDLGSTATICTEXT2,
 
159
              label='The "Upgrade Guide" provides information you should read before using this tool.',
 
160
              name='StaticText2', parent=self, pos=wx.Point(2, 178),
 
161
              size=wx.Size(438, 13), style=0)
 
162
 
 
163
        self._init_sizers()
 
164
 
 
165
    def __init__(self, parent, defaultDir):
 
166
        self._init_ctrls(parent)
 
167
        if '://' in defaultDir:
 
168
            if defaultDir.startswith('file://'):
 
169
                defaultDir = defaultDir[7:]
 
170
            else:
 
171
                defaultDir = '.'
 
172
        self.defaultDir = self.sourceFolderName = os.path.abspath(defaultDir)
 
173
        self.targetFolderName = self.sourceFolderName + 'Upgraded'
 
174
        self.sourceFolder.SetValue(self.sourceFolderName)
 
175
        self.targetFolder.SetValue(self.targetFolderName)
 
176
 
 
177
        self.u = wx25upgrade.Upgrade()
 
178
        
 
179
    def OnSetSourceButton(self, event):
 
180
        dlg = wx.DirDialog(self, defaultPath=self.sourceFolderName,
 
181
              style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON)
 
182
        try:
 
183
            if dlg.ShowModal() == wx.ID_OK:
 
184
                dir = dlg.GetPath()
 
185
                self.sourceFolder.SetValue(dir)
 
186
                self.targetFolder.SetValue(self.sourceFolder.GetValue()+'Upgraded')
 
187
        finally:
 
188
            dlg.Destroy()
 
189
 
 
190
    def OnSetTargetButton(self, event):
 
191
        dlg = wx.DirDialog(self, defaultPath=self.targetFolderName, 
 
192
              style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON )
 
193
        try:
 
194
            if dlg.ShowModal() == wx.ID_OK:
 
195
                dir = dlg.GetPath()
 
196
                self.targetFolder.SetValue(dir)
 
197
        finally:
 
198
            dlg.Destroy()
 
199
 
 
200
    def OnSetFileButton(self, event):
 
201
        dlg = wx.FileDialog(self, "Choose a file to convert", self.defaultDir, "", "*.py", wx.OPEN)
 
202
        try:
 
203
            if dlg.ShowModal() == wx.ID_OK:
 
204
                filename = dlg.GetPath()
 
205
                self.sourceFile.SetValue(filename)
 
206
        finally:
 
207
            dlg.Destroy()
 
208
 
 
209
    def OnStartFolderButton(self, event):
 
210
        targetFolder = self.targetFolder.GetValue()
 
211
        if not os.path.isdir(targetFolder):
 
212
            if wx.MessageBox('Target folder does not exist, create it?', 
 
213
                  'Create folder', wx.YES_NO | wx.ICON_QUESTION) == wx.NO:
 
214
                return
 
215
            os.mkdir(targetFolder)
 
216
            
 
217
        try:
 
218
            files = os.listdir(self.sourceFolder.GetValue())
 
219
            max = len(files)
 
220
            dlg = wx.ProgressDialog("Converting source files",
 
221
                                   "Starting conversion of source files",
 
222
                                   maximum = max,
 
223
                                   parent=self)
 
224
    
 
225
            keepGoing = True
 
226
            count = 0
 
227
       
 
228
            for name in files:
 
229
                count = count +1
 
230
                root, ext = os.path.splitext(name)
 
231
                if ext == '.py':
 
232
                    temp = 'Converting: %s' % name
 
233
                    keepGoing = dlg.Update(count, temp)
 
234
                    
 
235
                    fInputName = os.path.join(self.sourceFolder.GetValue(), name)
 
236
                    fInputLines = file(fInputName, 'r').readlines()
 
237
                    fInputData = self.reindentSource(fInputLines, fInputName)
 
238
                    fOutput = file(os.path.join(self.targetFolder.GetValue(), name), 'w')
 
239
                    try:
 
240
                        fOutput.write(self.u.upgrade(fInputData))
 
241
                    finally:
 
242
                        fOutput.close()
 
243
                        temp = 'Done converting: %s' % name
 
244
                        keepGoing = dlg.Update(count, temp)
 
245
 
 
246
            keepGoing = dlg.Update(count, "We are all done")
 
247
        finally:
 
248
            dlg.Destroy()
 
249
        
 
250
    def OnStartFileButton(self, event):
 
251
        max = 2
 
252
        dlg = wx.ProgressDialog("Converting source file",
 
253
                               "Starting conversion of source file\n\n",
 
254
                               maximum = max,
 
255
                               parent=self)
 
256
        keepGoing = True
 
257
        count = 0
 
258
 
 
259
        root, fileName = os.path.split(self.sourceFile.GetValue())
 
260
        newName, ext = os.path.splitext(self.sourceFile.GetValue())
 
261
        outName = newName+'Upg'+ext
 
262
        root2, newFileName = os.path.split(outName)
 
263
                
 
264
        fInputName = self.sourceFile.GetValue()
 
265
        fInputLines = file(fInputName, 'r').readlines()
 
266
        fInputData = self.reindentSource(fInputLines, fInputName)
 
267
 
 
268
        fOutput = file(outName, 'w')
 
269
 
 
270
        temp = 'Converting: %s,\n\nto: %s' % (fileName, newFileName)
 
271
        keepGoing = dlg.Update(count, temp)
 
272
        
 
273
        try:
 
274
            fOutput.write(self.u.upgrade(fInputData))
 
275
        finally:
 
276
            fOutput.close()
 
277
            
 
278
            count = count +1
 
279
            temp = 'Done converting: %s,\n\nto: %s' % (fileName, newFileName)
 
280
            keepGoing = dlg.Update(count, temp)
 
281
            print 'Converted: %s,\n\nnew name: %s' % (fileName, newFileName)
 
282
            count = count +1
 
283
            keepGoing = dlg.Update(count, "We are done")
 
284
            dlg.Destroy()
 
285
 
 
286
    def OnUpgradeGuideButton(self, event):
 
287
        import webbrowser
 
288
        webbrowser.open('http://wiki.wxpython.org/index.cgi/Boa040Upgrade')
 
289
 
 
290
    def reindentSource(self, srcLines, filename):
 
291
        data = ''.join(srcLines)
 
292
        eol = Utils.getEOLMode(data)
 
293
        file = SourcePseudoFile(srcLines)
 
294
        ri = reindent.Reindenter(file, eol=eol)
 
295
        try:
 
296
            if ri.run():
 
297
                file.output = []
 
298
                ri.write(file)
 
299
 
 
300
                return ''.join(file.output)
 
301
        except Exception, error:
 
302
            print 'Error on reindenting %s : %s'%(filename, str(error))
 
303
 
 
304
        return data
 
305
 
 
306
class SourcePseudoFile(Utils.PseudoFileOutStore):
 
307
    def readlines(self):
 
308
        return self.output
 
309
    
 
310
#-------------------------------------------------------------------------------
 
311
 
 
312
def showWx25CodeUpgradeDlg(editor):
 
313
    dlg = Wx25CodeUpgradeDlg(editor, editor.getOpenFromHereDir())
 
314
    try:
 
315
        dlg.ShowModal()
 
316
    finally:
 
317
        dlg.Destroy()
 
318
    
 
319
import Plugins
 
320
Plugins.registerTool('wxPython 2.4 to 2.5 code upgrader', showWx25CodeUpgradeDlg)