Menu

[r100]: / plugins / CleanUpFile.py  Maximize  Restore  History

Download this file

328 lines (269 with data), 14.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Programmer: Franz Steinhaeusler
# E-mail: francescoa@users.sourceforge.net
# Note: Initial Release 05/01/2004
#
# Copyright 2005 Franz Steinhaeusler
#
# Distributed under the terms of the GPL (GNU Public License)
#
# DrPython is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#Plugin
#CleanUpFile
#Version: 0.0.0, 05.01.2005:
import wx
import os
import re
import string
import drPrefsFile
def OnAbout(DrFrame):
Verision = "0.0.0"
NameAndVersion = "CleanUp File\nVersion: " + Verision + "\n"
AboutString = NameAndVersion + "By Franz Steinhaeusler\n\nReleased under the GPL."
d = DrFrame.ShowMessage (AboutString, "About")
def OnHelp(DrFrame):
d = DrFrame.ShowMessage ("CleanUpFile", "Help")
class CleanUpPrefsDialog(wx.Dialog):
def __init__(self, parent, id):
wx.Dialog.__init__(self, parent, id, "CleanUpFile", wx.Point(50, 50), \
wx.Size(300, 196), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
self.parent = parent
self.chkeol = wx.CheckBox(self, -1, "")
self.chkeol.SetValue(parent.CleanUpFile_CheckEol)
self.chkindentation = wx.CheckBox(self, -1, "")
self.chkindentation.SetValue(parent.CleanUpFile_CheckIndentation)
self.chktrailingspaces = wx.CheckBox(self, -1, "")
self.chktrailingspaces.SetValue(parent.CleanUpFile_CheckTrailingSpaces)
self.theSizer = wx.FlexGridSizer(5, 3, 5, 10)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, "Check Eol:"),1,wx.GROW)
self.theSizer.Add(self.chkeol, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, "Check Indentation:"),1,wx.GROW)
self.theSizer.Add(self.chkindentation, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, "Check Trailing Spaces:"),1,wx.GROW)
self.theSizer.Add(self.chktrailingspaces, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.btnCancel = wx.Button(self, wx.ID_CANCEL, "&Close")
self.btnSave = wx.Button(self, wx.ID_OK, "&Save")
self.btnCancel.SetDefault()
self.buttonSizer.Add(self.btnCancel, 1, wx.SHAPED)
self.buttonSizer.Add(self.btnSave, 1, wx.GROW)
self.theSizer.Add(self.buttonSizer, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.SHAPED | wx.ALIGN_CENTER)
self.SetAutoLayout(True)
self.SetSizer(self.theSizer)
if parent.PLATFORM_IS_WIN:
self.SetSizerAndFit(self.theSizer)
self.Bind(wx.EVT_BUTTON, self.OnbtnClose, id = wx.ID_CANCEL)
self.Bind(wx.EVT_BUTTON, self.OnbtnSave, id = wx.ID_OK)
def OnbtnClose(self, event):
self.EndModal(0)
def OnbtnSave (self, event):
self.parent.CleanUpFile_CheckEol = int(self.chkeol.GetValue())
self.parent.CleanUpFile_CheckIndentation = int(self.chkindentation.GetValue())
self.parent.CleanUpFile_CheckTrailingSpaces= int(self.chktrailingspaces.GetValue())
f = file(self.parent.pluginspreferencesdirectory + "/CleanUpFile.preferences.dat", 'w')
f.write("<cleanupfile.checkeol>" + str(self.parent.CleanUpFile_CheckEol) + "</cleanupfile.checkeol>\n")
f.write("<cleanupfile.checkindentation>" + str(self.parent.CleanUpFile_CheckIndentation) + "</cleanupfile.checkindentation>\n")
f.write("<cleanupfile.checktrailingspaces>" + str(self.parent.CleanUpFile_CheckTrailingSpaces) + "</cleanupfile.checktrailingspaces>\n")
f.close()
def OnPreferences(DrFrame):
d = CleanUpPrefsDialog(DrFrame, -1)
d.ShowModal()
d.Destroy()
#def AddPluginOpenFileFunction (zrdz, OnPreferences, RunFirst):
# print "hi"
def Plugin(DrFrame):
def OnRemoveTrailingWhiteSpaces (self):
#if os.access(self.txtDocument.filename, os.W_OK):
eol = self.txtDocument.GetEndOfLineCharacter()
lines = self.txtDocument.GetText().split(eol)
new_lines = []
nr_lines = 0
nr_clines = 0
regex = re.compile('\s+' + eol, re.MULTILINE)
for line in lines:
nr_lines += 1
result = regex.search(line + eol)
if result is not None:
end = result.start()
nr_clines += 1
new_lines.append (line [:end])
else:
new_lines.append(line)
#file has trailing whitespaces
if nr_clines > 0:
answer = wx.MessageBox("File %s has tr ailing Whitespaces\nCorrect?" % self.txtDocument.filename, "Remove trailing Whitespace", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
if answer == wx.OK:
newtext = string.join(new_lines, eol)
#save current line
curline = self.txtDocument.GetCurrentLine()
self.txtDocument.SetText(newtext)
#jump to saved current line
self.txtDocument.GotoLine(curline)
return nr_clines > 0
def OnCheckAndCleanUpFile (event):
self = DrFrame
userinvoked = False
if event is not None:
userinvoked = True
messedup = False
filename = self.txtDocument.filename
#print filename, self.txtDocument.filename
if os.access(self.txtDocument.filename, os.W_OK) or userinvoked:
ext = os.path.splitext(self.txtDocument.filename)
#print ext
#if ext[1] in ('.txt', '.pyw', '.py', '.h', '.cpp'):
#manuell (auch settings ignorieren)
#if ext[1] in ('.pyw', '.py') or userinvoked:
if ext[1] in ('.pyw', '.py', '.txt') or userinvoked:
indentation = self.txtDocument.CheckIndentation(self.txtDocument.GetText())
if self.CleanUpFile_CheckIndentation or userinvoked: #self.prefs.checkindentation: #todo: replace with my preferences
#todo fileextensionmask
if indentation == 0:
u = 0
indentation = -1
if self.txtDocument.GetUseTabs():
u = 2
indentation = 1
messedup = True
answer = wx.MessageBox((filename + ' is currently ' + "MIXED" + ".\nWould you like to change it to " \
+ self.TABMESSAGE[u] + " ?" ), "Indentation Not Default", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
if answer == wxOK:
if indentation == 1:
self.txtDocument.SetToTabs(4)
else:
self.txtDocument.SetToSpaces(2)
if indentation == -1:
usetabs = False
elif indentation == 1:
usetabs = True
else:
usetabs = self.prefs.docusetabs[self.txtDocument.filetype]
self.txtDocument.SetUseTabs(usetabs)
self.txtDocument.SetupTabs(usetabs)
#dmodenum = self.prefs.eolmode
#if self.prefs.checkeol:
if self.CleanUpFile_CheckEol or userinvoked:
#if not emodenum == self.prefs.eolmode:
if self.txtDocument.lineendingsaremixed:
oof = self.txtDocument.GetText()
winresult = self.relewin.search(oof)
unixresult = self.releunix.search(oof)
macresult = self.relemac.search(oof)
win = winresult is not None
unix = unixresult is not None
mac = macresult is not None
if (win + unix + mac) > 1:
#Which came first, unix, mac, or win?
first = -1
useemode = 0
if winresult is not None:
first = winresult.start()
useemode = 1
if unixresult is not None:
if first == -1:
first = unixresult.start()
else:
i = unixresult.start()
if i < first:
first = i
useemode = 0
if macresult is not None:
if first == -1:
first = macresult.start()
else:
i = macresult.start()
if i < first:
first = i
useemode = 2
self.txtDocument.lineendingsaremixed = 1
emodenum = useemode
else:
if win:
emodenum = 1
elif unix:
emodenum = 0
elif mac:
emodenum = 2
else:
emodenum = self.prefs.eolmode
messedup = True
#print self.txtDocument.GetEOLMode(), self.FFMESSAGE[self.txtDocument.GetEOLMode()]
answer = wx.MessageBox((filename + " is currently " + self.FFMESSAGE[emodenum] + \
"(Mixed).\nWould you like to change it to " + self.FFMESSAGE[emodenum]), \
"Mixed Line Ending", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
if answer == wx.OK:
#Bugfix, Thanks Stephen Anderson.
if emodenum == 1:
self.OnFormatWinMode(None)
elif emodenum == 2:
self.OnFormatMacMode(None)
else:
self.OnFormatUnixMode(None)
self.txtDocument.lineendingsaremixed = 0
#emodenum = dmodenum
if emodenum == 1:
emode = wx.stc.STC_EOL_CRLF
elif emodenum == 2:
emode = wx.stc.STC_EOL_CR
else:
emode = wx.stc.STC_EOL_LF
self.txtDocument.SetEOLMode(emode)
if self.CleanUpFile_CheckTrailingSpaces and ext[1] in ('.txt') or userinvoked:
#print "remt"
if OnRemoveTrailingWhiteSpaces(DrFrame):
messedup = True
if userinvoked:
if not messedup:
wx.MessageBox("File %s is ok" % self.txtDocument.filename, "CleanUp File", wx.ICON_EXCLAMATION)
#if filename = None:
# OnCheckAndCleanUpFile (None, filename)
def OnCheckAndCleanUpFileStart():# (event): #, filename):
#print self
#print "OnCleanUpFile"
#print filename, self.txtDocument.filename
#OnCheckAndCleanUpFile (None, DrFrame.txtDocument.filename)
#print "f:", DrFrame.txtDocument.filename
OnCheckAndCleanUpFile (None)
#event.Skip()
DrFrame.CleanUpFile_CheckEol = False
DrFrame.CleanUpFile_CheckIndentation = False
DrFrame.CleanUpFile_CheckTrailingSpaces = False
if os.path.exists(DrFrame.pluginspreferencesdirectory + "/CleanUpFile.preferences.dat"):
f = file(DrFrame.pluginspreferencesdirectory + "/CleanUpFile.preferences.dat", 'r')
text = f.read()
f.close()
DrFrame.CleanUpFile_CheckEol = drPrefsFile.GetPrefFromText(DrFrame.CleanUpFile_CheckEol, text, "cleanupfile.checkeol", True)
DrFrame.CleanUpFile_CheckIndentation = drPrefsFile.GetPrefFromText(DrFrame.CleanUpFile_CheckIndentation, text, "cleanupfile.checkindentation", True)
DrFrame.CleanUpFile_CheckTrailingSpaces = drPrefsFile.GetPrefFromText(DrFrame.CleanUpFile_CheckTrailingSpaces, text, "cleanupfile.checktrailingspaces", True)
ID_CLEANUPFILE = DrFrame.GetNewId()
DrFrame.AddPluginShortcutFunction("CleanUpFile", "Cleanup File", OnCheckAndCleanUpFile)
DrFrame.AddPluginPopUpMenuFunction("CleanUpFile", "Cleanup File", OnCheckAndCleanUpFile)
DrFrame.editmenu.AppendSeparator()
DrFrame.LoadPluginShortcuts('CleanUpFile')
menutext = DrFrame.GetPluginMenuLabel('CleanUpFile', 'Cleanup File', "Cleanup File...")
DrFrame.editmenu.Append(ID_CLEANUPFILE, menutext)
DrFrame.Bind(wx.EVT_MENU, OnCheckAndCleanUpFile, id = ID_CLEANUPFILE)
DrFrame.PBind(DrFrame.EVT_DRPY_FILE_OPENED, OnCheckAndCleanUpFileStart)
#DrFrame.AddPluginOpenFileFunction ("CleanUpFile", OnCheckAndCleanUpFileStart, False)