-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSFileMergerUI.py
More file actions
299 lines (272 loc) · 12.4 KB
/
Copy pathMSFileMergerUI.py
File metadata and controls
299 lines (272 loc) · 12.4 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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MSFileMerger.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import os
import subprocess
from SEDSupplements import UIMessage
from PyQt5 import QtCore, QtGui, QtWidgets
class fileMergerUI(object):
def setupUi(self, Dialog):
"""
this is a standard method created by QC
"""
Dialog.setObjectName("Dialog")
Dialog.resize(578, 316)
Dialog.setFixedSize(578, 316)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(440, 270, 131, 32))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.cleanAndMerge)
self.pushButton.setEnabled(False)
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(325, 270, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(QtWidgets.QApplication.instance().quit)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 10, 541, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.line = QtWidgets.QFrame(Dialog)
self.line.setGeometry(QtCore.QRect(7, 30, 561, 20))
font = QtGui.QFont()
font.setPointSize(12)
font.setKerning(True)
self.line.setFont(font)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setLineWidth(2)
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setObjectName("line")
self.layoutWidget = QtWidgets.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(10, 80, 561, 33))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_3 = QtWidgets.QLabel(self.layoutWidget)
self.label_3.setObjectName("label_3")
self.lineEdit_3 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_3.setGeometry(91, 4, 150, 21)
self.label_5 = QtWidgets.QLabel(self.layoutWidget)
self.label_5.setObjectName("label_5")
self.label_5.setGeometry(250, 4, 150, 20)
self.label_5.setStyleSheet("background-color: yellow;")
self.vlaidateOutputFN()
self.listView = QtWidgets.QListView(Dialog)
self.listView.setGeometry(QtCore.QRect(10, 140, 561, 126))
self.listView.setObjectName("listView")
self.widget1 = QtWidgets.QWidget(Dialog)
self.widget1.setGeometry(QtCore.QRect(10, 110, 416, 32))
self.widget1.setObjectName("widget1")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_4 = QtWidgets.QLabel(self.widget1)
self.label_4.setObjectName("label_4")
self.horizontalLayout_3.addWidget(self.label_4)
self.pushButton_5 = QtWidgets.QPushButton(self.widget1)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.listFiles)
self.horizontalLayout_3.addWidget(self.pushButton_5)
self.pushButton_6 = QtWidgets.QPushButton(self.widget1)
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_6.setEnabled(False)
self.pushButton_6.clicked.connect(self.selectAll)
self.horizontalLayout_3.addWidget(self.pushButton_6)
self.pushButton_7 = QtWidgets.QPushButton(self.widget1)
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_7.setEnabled(False)
self.pushButton_7.clicked.connect(self.deselectAll)
self.horizontalLayout_3.addWidget(self.pushButton_7)
self.widget = QtWidgets.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(10, 50, 561, 33))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setEnabled(False)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton_3 = QtWidgets.QPushButton(self.widget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_3.clicked.connect(self.inputFolderDialog)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
"""
this is a standard method created by QC
"""
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "SED | 2theta file merger"))
self.pushButton.setText(_translate("Dialog", "Clean and Merge"))
self.pushButton_2.setText(_translate("Dialog", "Cancel"))
self.label.setText(_translate("Dialog", "SED | MS Beamline | 2theta files merger "))
self.label_3.setText(_translate("Dialog", "Output file name:"))
self.label_5.setText(_translate("Dialog", "Allowed: [A-Z][a-z][0-9][_-]"))
self.label_4.setText(_translate("Dialog", "Files to be merged:"))
self.label_2.setText(_translate("Dialog", "Source files path:"))
self.pushButton_3.setText(_translate("Dialog", "..."))
self.pushButton_5.setText(_translate("Dialog", "Show"))
self.pushButton_6.setText(_translate("Dialog", "Select all"))
self.pushButton_7.setText(_translate("Dialog", "Deselect all"))
def inputFolderDialog(self):
"""
a method being used to open choose directory window
"""
self.pushButton.setEnabled(False)
self.startOver()
self.inputDirectory = str(QtWidgets.QFileDialog.getExistingDirectory())
self.lineEdit.setText('{}'.format(self.inputDirectory))
if self.lineEdit.text() != "":
self.pushButton.setEnabled(True)
def listFiles(self):
"""
a method used to scan and then list all
files in a path
"""
pathChoosen = False
try:
entries = os.listdir(self.inputDirectory)
pathChoosen = True
except:
UIMessage("Path is not defined!!","Source files path:"\
,"Please provide the source file location (folder path)").showCritical()
if pathChoosen:
self.model = QtGui.QStandardItemModel()
self.listView.setModel(self.model)
for i in entries:
item = QtGui.QStandardItem(i)
item.setCheckable(True)
self.model.appendRow(item)
self.pushButton_6.setEnabled(True)
self.pushButton_7.setEnabled(True)
def selectAll(self):
"""
a method used to select all items in the QListView
"""
for i in range(self.model.rowCount()):
item = self.model.item(i)
item.setCheckState(QtCore.Qt.Checked)
def deselectAll(self):
"""
a method used to deselect all items in the QListView
"""
for i in range(self.model.rowCount()):
item = self.model.item(i)
item.setCheckState(QtCore.Qt.Unchecked)
def startOver(self):
"""
a method used to setup some parameters before start
proccessing the files..
"""
try:
self.model = QtGui.QStandardItemModel()
self.listView.setModel(self.model)
self.inputDirectory=""
self.pushButton_6.setEnabled(False)
self.pushButton_7.setEnabled(False)
self.pushButton.setEnabled(False)
self.lineEdit.setText("")
self.lineEdit_3.setText("")
except:
pass
def vlaidateOutputFN(self):
"""
This method validates the output file name
The follwoing RegExp just accepts characters A-Z, a-z and _
Also, numbers 0-9
"""
regexp = QtCore.QRegExp('^[A-Z0-9a-z_-]{0,30}$')
validator = QtGui.QRegExpValidator(regexp)
self.lineEdit_3.setValidator(validator)
def isLineEmpty(self, line):
"""
a method used to check if the line blank or not
"""
#print (len(line.strip()))
return len(line.strip()) == 0
def cleanAndMerge(self):
"""
This method goes through the files and does the follwoing:
- Read the files
- Proccess the files
- merge all the files in one temp file
- go the lines of the temp file, line by line
- ignore any line starts with #
- ignore any blank lines
- save the lines in a list
- sort the lines
- write them back in a .dat file
"""
fileNames = []
rawData = list()
outputFileName = self.lineEdit_3.text()
if not self.lineEdit_3.text():
UIMessage("Output file name","Output file name:","Please enter the output file name").showCritical()
"""
get file names from QListView object
"""
else:
for i in range(self.model.rowCount()):
item = self.model.item(i)
if item.checkState() == QtCore.Qt.Checked:
fileNames.append(item.text())
if not fileNames:
UIMessage("No files to be merged have been selected!!","No files have been selected"\
,"Please click on Show then select the files that you want to merge").showCritical()
else:
with open(self.inputDirectory+"/"+"output_file.txt", "w") as outfile:
for filename in fileNames:
with open(self.inputDirectory+"/"+filename) as infile:
contents = infile.read()
outfile.write(contents)
with open (self.inputDirectory+"/"+"output_file.txt", "r") as fin:
for line in fin:
ignoreLine = line.strip()
if not ignoreLine.startswith('#'):
if not self.isLineEmpty(line):
rawData.append(line)
rawData.sort()
with open (self.inputDirectory+"/"+self.lineEdit_3.text()+".dat", "w") as fOut:
for line in rawData:
fOut.write(line)
outfile.close()
fin.close()
fOut.close()
UIMessage("Done", "The file: {} has been successfully created".format(self.lineEdit_3.text()), "The file "\
"can be found at this path {}".format(self.inputDirectory)).showInformation()
os.remove(self.inputDirectory+"/"+"output_file.txt")
self.startOver()
self.openDestinationFolder()
def openDestinationFolder(self):
"""
This method is used to open Windows window, which is the window
that contains the output file.
"""
FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe') # path to explorer.exe
path = os.path.normpath(self.inputDirectory)
if os.path.isdir(path):
subprocess.run([FILEBROWSER_PATH, path])
elif os.path.isfile(path):
subprocess.run([FILEBROWSER_PATH, '/select,', os.path.normpath(path)])
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
TestQFileDialog = QtWidgets.QDialog()
ui = fileMergerUI()
ui.setupUi(TestQFileDialog)
TestQFileDialog.show()
sys.exit(app.exec_())