-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindfiles.py
executable file
·171 lines (130 loc) · 6 KB
/
findfiles.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
163
164
165
166
167
168
169
170
171
from PyQt4 import QtCore, QtGui
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
browseButton = self.createButton("&Browse...", self.browse)
findButton = self.createButton("&Find", self.find)
self.fileComboBox = self.createComboBox("*")
self.textComboBox = self.createComboBox()
self.directoryComboBox = self.createComboBox(QtCore.QDir.currentPath())
fileLabel = QtGui.QLabel("Named:")
textLabel = QtGui.QLabel("Containing text:")
directoryLabel = QtGui.QLabel("In directory:")
self.filesFoundLabel = QtGui.QLabel()
self.createFilesTable()
buttonsLayout = QtGui.QHBoxLayout()
buttonsLayout.addStretch()
buttonsLayout.addWidget(findButton)
mainLayout = QtGui.QGridLayout()
#mainLayout.addWidget(fileLabel, 0, 0)
#mainLayout.addWidget(self.fileComboBox, 0, 1, 1, 2)
#mainLayout.addWidget(textLabel, 1, 0)
#mainLayout.addWidget(self.textComboBox, 1, 1, 1, 2)
mainLayout.addWidget(directoryLabel, 2, 0)
mainLayout.addWidget(self.directoryComboBox, 2, 1)
mainLayout.addWidget(browseButton, 2, 2)
mainLayout.addWidget(self.filesTable, 3, 0, 1, 3)
mainLayout.addWidget(self.filesFoundLabel, 4, 0)
mainLayout.addLayout(buttonsLayout, 5, 0, 1, 3)
self.setLayout(mainLayout)
self.setWindowTitle("Find Files")
self.resize(700, 300)
def browse(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Find Files",
QtCore.QDir.currentPath())
if directory:
if self.directoryComboBox.findText(directory) == -1:
self.directoryComboBox.addItem(directory)
self.directoryComboBox.setCurrentIndex(self.directoryComboBox.findText(directory))
@staticmethod
def updateComboBox(comboBox):
if comboBox.findText(comboBox.currentText()) == -1:
comboBox.addItem(comboBox.currentText())
def find(self):
self.filesTable.setRowCount(0)
fileName = self.fileComboBox.currentText()
text = self.textComboBox.currentText()
path = self.directoryComboBox.currentText()
self.updateComboBox(self.fileComboBox)
self.updateComboBox(self.textComboBox)
self.updateComboBox(self.directoryComboBox)
self.currentDir = QtCore.QDir(path)
if not fileName:
fileName = "*"
files = self.currentDir.entryList([fileName],
QtCore.QDir.Files | QtCore.QDir.NoSymLinks)
if text:
files = self.findFiles(files, text)
self.showFiles(files)
def findFiles(self, files, text):
progressDialog = QtGui.QProgressDialog(self)
progressDialog.setCancelButtonText("&Cancel")
progressDialog.setRange(0, files.count())
progressDialog.setWindowTitle("Find Files")
foundFiles = []
for i in range(files.count()):
progressDialog.setValue(i)
progressDialog.setLabelText("Searching file number %d of %d..." % (i, files.count()))
QtGui.qApp.processEvents()
if progressDialog.wasCanceled():
break
inFile = QtCore.QFile(self.currentDir.absoluteFilePath(files[i]))
if inFile.open(QtCore.QIODevice.ReadOnly):
stream = QtCore.QTextStream(inFile)
while not stream.atEnd():
if progressDialog.wasCanceled():
break
line = stream.readLine()
if text in line:
foundFiles.append(files[i])
break
progressDialog.close()
return foundFiles
def showFiles(self, files):
for fn in files:
if(fn[-3:]=="csv"):
file = QtCore.QFile(self.currentDir.absoluteFilePath(fn))
size = QtCore.QFileInfo(file).size()
fileNameItem = QtGui.QTableWidgetItem(fn)
fileNameItem.setFlags(fileNameItem.flags() ^ QtCore.Qt.ItemIsEditable)
sizeItem = QtGui.QTableWidgetItem("%d KB" % (int((size + 1023) / 1024)))
sizeItem.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
sizeItem.setFlags(sizeItem.flags() ^ QtCore.Qt.ItemIsEditable)
row = self.filesTable.rowCount()
self.filesTable.insertRow(row)
self.filesTable.setItem(row, 0, fileNameItem)
self.filesTable.setItem(row, 1, sizeItem)
print fn
self.filesFoundLabel.setText("%d file(s) found (Double click on a file to open it)" % len(files))
def createButton(self, text, member):
button = QtGui.QPushButton(text)
button.clicked.connect(member)
return button
def createComboBox(self, text=""):
comboBox = QtGui.QComboBox()
comboBox.setEditable(True)
comboBox.addItem(text)
comboBox.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Preferred)
return comboBox
def createFilesTable(self):
self.filesTable = QtGui.QTableWidget(0, 2)
self.filesTable.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.filesTable.setHorizontalHeaderLabels(("File Name", "Size"))
self.filesTable.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
self.filesTable.verticalHeader().hide()
self.filesTable.setShowGrid(False)
self.filesTable.cellActivated.connect(self.openFileOfItem)
def openFileOfItem(self, row, column):
item = self.filesTable.item(row, 0)
print "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
choosen_one = item.text()
choosen_url = (QtCore.QUrl(self.currentDir.absoluteFilePath(item.text())))
return choosen_url
#QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.currentDir.absoluteFilePath(item.text())))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())