-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmakerReplace.py
executable file
·141 lines (116 loc) · 5.02 KB
/
makerReplace.py
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
import makerController
import makerReplaceDialog
import os
import makerFileTemplates
from makerUtilities import readFile, writeFile
class Controller(makerController.SuperController):
def drawDialog(self):
self.dialog = makerReplaceDialog.xrcFindReplace(self.view)
self.dialog.Cancel.Bind(self.view.wx.EVT_BUTTON, self.close)
self.dialog.OK.Bind(self.view.wx.EVT_BUTTON, self.doReplaceCurrent)
self.dialog.findInCurrent.Bind(self.view.wx.EVT_RADIOBUTTON, self.findInCurrent)
self.dialog.findInProject.Bind(
self.view.wx.EVT_RADIOBUTTON, self.replaceInProject
)
self.dialog.Find.Bind(self.view.wx.EVT_TEXT, self.showOccurences)
self.dialog.Find.Bind(self.view.wx.EVT_TEXT_ENTER, self.showOccurences)
self.dialog.findInProject.Enable(True)
self.dialog.findInOpen.Enable(False)
self.dialog.SetTitle("Find / Replace...")
self.rawText = self.editor.GetText()
# set selected text in Find field
self.dialog.Find.SetValue(self.editor.GetSelectedText())
def showOccurences(self, evt):
""" update status label """
s = self.dialog.Find.GetValue()
if s != "":
occ = self.rawText.count(s)
if occ > 0:
self.dialog.Status.SetLabel(str(occ) + " found")
elif occ == 0:
self.dialog.Status.SetLabel("Not found")
else:
self.dialog.Status.SetLabel("")
def showDialog(self):
self.dialog.Show()
def setEditor(self, editor):
self.editor = editor
def close(self, event):
self.dialog.Close()
def findInCurrent(self, evt):
""" just updating bindings """
self.dialog.OK.Unbind(self.view.wx.EVT_BUTTON)
self.dialog.OK.Bind(self.view.wx.EVT_BUTTON, self.doReplaceCurrent)
self.dialog.Replace.Unbind(self.view.wx.EVT_SET_FOCUS)
self.dialog.Find.Bind(self.view.wx.EVT_TEXT_ENTER, self.showOccurences)
self.dialog.Find.Bind(self.view.wx.EVT_TEXT, self.showOccurences)
self.showOccurences(None)
def replaceInProjectFinal(self, evt):
old = self.dialog.Find.GetValue()
new = self.dialog.Replace.GetValue()
# replace in current open file
self.replaceCurrent(True)
self.model.replaceInProject(old, new)
self.dialog.Destroy()
def showOccurencesInProject(self, evt):
project = self.model.project
pathToProject = project.getPathParts()
string = self.dialog.Find.GetValue()
if string:
count = 0
for file in os.listdir(pathToProject):
try:
count += (readFile(os.path.join(pathToProject, file))).count(string)
except:
pass
if count > 0:
self.dialog.Status.SetLabel(str(count) + " Found")
else:
self.dialog.Status.SetLabel("Not Found")
def findInFocus(self, evt):
self.dialog.Status.SetLabel("")
def replaceInProject(self, evt):
""" just updates bindings """
self.dialog.Find.Bind(self.view.wx.EVT_SET_FOCUS, self.findInFocus)
self.dialog.Replace.Bind(
self.view.wx.EVT_SET_FOCUS, self.showOccurencesInProject
)
self.dialog.Find.Unbind(self.view.wx.EVT_TEXT)
self.dialog.Find.Unbind(self.view.wx.EVT_TEXT_ENTER)
self.dialog.OK.Unbind(self.view.wx.EVT_BUTTON)
self.dialog.OK.Bind(self.view.wx.EVT_BUTTON, self.replaceInProjectFinal)
def doReplaceCurrent(self, event):
""" calls replaceCurrent catches event """
self.replaceCurrent(doProject=False)
def replaceCurrent(self, doProject=False):
"""if do project is True, this
method will replace in all open files
belonging to this project
"""
findText = self.dialog.Find.GetValue()
replaceText = self.dialog.Replace.GetValue()
if findText:
if doProject:
# replace in open files belonging to project
for file in self.model.project.projectManager.openFiles:
if file.getProject() == self.model.project.getProject():
ed = file.fileController.editor
pageText = ed.GetText()
if replaceText:
ed.SetText(pageText.replace(findText, replaceText))
else:
# replace just in the open file
pageText = self.editor.GetText()
if replaceText:
self.editor.SetText(pageText.replace(findText, replaceText))
self.dialog.Destroy()
class Replace:
def __init__(self, mainView, editor, project):
self.controller = Controller(self, mainView)
self.controller.setEditor(editor)
self.controller.drawDialog()
self.controller.showDialog()
self.project = project
def replaceInProject(self, old, new):
""" replace in project method"""
self.project.replaceStringInAllItems(old, new)