-
Notifications
You must be signed in to change notification settings - Fork 1
/
Search_n_Replace.py
executable file
·164 lines (141 loc) · 5.18 KB
/
Search_n_Replace.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# MUST BE RUN FROM TERMINAL!
# python3 Search_n_Replace.py
import sys
import os
from PySide.QtCore import SIGNAL
from PySide import QtGui
import shutil
def main():
"""Even though this is a GUI application there is a fault in it that requires
it to be run from the comand line. As it is, if it is exicuted from
Nautilus it creates three files titled sys, os, and shutil (upon mouse
clicks) then quits.
Created: August 13, 2014
"""
app = QtGui.QApplication([])
form = Form()
form.show()
#print form.Replace
choice = form.exec_()
global var
var = [form.Search.encode('ascii', errors='backslashreplace'), form.Replace.encode('ascii', errors='backslashreplace')]
#print choice
#print "var = %s"%var
if choice == 1:
dialog = QtGui.QFileDialog()
dialog.setDirectory("/home/damon/Documents/CODE/Programing/Python/IPython Notebook Folders (links)")
dialog.setFileMode(QtGui.QFileDialog.ExistingFiles) #allow selection of multiple files
dialog.filesSelected.connect(callback)
dialog.setWindowTitle('Select Notebooks to have the git Path Updated')
dialog.show()
#dialog.change = [form.Search, form.Replace]
chose_ = dialog.exec_()
sys.exit()
class Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.Replace = ''
self.Search = ''
self.resize(450, 100)
#self.browser = QtGui.QTextBrowser()
self.search_label = QtGui.QLabel("Type the text to be replaced here")
self.search = QtGui.QLineEdit()
self.search.setPlaceholderText("Type the text to be replaced here")
#self.search.selectAll()
self.replace_label = QtGui.QLabel("Type the replacement text here")
self.replace = QtGui.QLineEdit()
self.replace.setPlaceholderText("Type the replacement text here")
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.search_label)
layout.addWidget(self.search)
layout.addWidget(self.replace_label)
layout.addWidget(self.replace)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.search.setFocus()
self.connect(self.search, SIGNAL('editingFinished()'),self.Next)
self.connect(self.replace, SIGNAL('editingFinished()'),self.Final)
self.connect(self.buttonBox, SIGNAL('accepted()'),self.OK)
self.connect(self.buttonBox, SIGNAL('rejected()'),self.Cancel)
self.setWindowTitle('Update git Path in IPython Notebooks')
def Next(self):
#self.replace.selectAll()
self.Search = self.search.text()
#print self.Search
self.replace.setFocus()
def Final(self):
#self.buttonBox.setFocus()
self.Replace = self.replace.text()
#print self.Replace
self.search.setFocus()
def OK(self):
print 'OK pressed'
Search = self.Search
Replace = self.Replace
self.accept()
self._end()
def Cancel(self):
print 'Cancel pressed'
self.reject()
self._end()
def _end(self):
print 'exit'
def callback(files):
"""This searches all the selected files for the search string and replaces
them with the replacement string.
"""
print var
#cmd = 'ipython nbconvert "%s" --to slides'
path = files[0].rsplit('/',1)[0]
os.chdir(path)
parts = path.rsplit('/',1)
back_path = path + '/backup/'
if not os.path.exists(back_path):
os.mkdir(back_path)
er = 0
for f in files:
name = f.split('/')[-1]
new_path = back_path + name
try:
#subprocess.call(cmd%f,shell=True)
out = []
#print '1 here'
with open(name, 'r') as FF:
for line in FF:
out.append(line.replace(var[0],var[1]))
#print '2 here'
shutil.move(name, new_path)
#print '3 here'
new_name = name#.replace('.','_dh.')
#print '4 here - len(out) = %d'%len(out)
with open(new_name, 'w') as FF:
#print '4.1 here'
#text = '\n'.join(out)
#print '4.2 here'
for line in out:
FF.write(line)
#print '5 here'
pass
except:
error = "Failed for %s"%name
print error
msgBox = QtGui.QMessageBox()
msgBox.setText(error)
msgBox.exec_()
er += 1
if er>2:
msgBox = QtGui.QMessageBox()
msgBox.setText('Too many errors occured.\nEnding conversion')
msgBox.exec_()
break
msgBox = QtGui.QMessageBox()
Path = back_path[:-1]
bak_txt = 'Files where backedup in:\n%s'%Path
print(bak_txt)
msgBox.setText(bak_txt)
msgBox.exec_()
if __name__ == '__main__':
main()
#app = QtGui.QApplication(sys.argv)