- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprogram.py
More file actions
Latest commit
292 lines (267 loc) · 13.3 KB
/
Copy pathprogram.py
File metadata and controls
292 lines (267 loc) · 13.3 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
fromPyQt5.QtCoreimport*
fromPyQt5.QtGuiimport*
importos, pathlib, sys, traceback, pysftp, time
fromdatetimeimportdatetime
frompysftpimportparamiko
fromPyQt5.QtWidgetsimport (QApplication, QCheckBox, QComboBox,
QGridLayout, QLabel, QStyleFactory, QListWidget,
QListWidgetItem, QMainWindow, QTreeWidget, QTreeWidgetItem, QMessageBox)
fromPyQt5.QtCoreimportQt
fromPyQt5.QtGuiimportQIcon, QBrush
fromlocal_transferimportstartFTP, localToRemoteTransfer, getLocalFileList
fromremote_transferimportremoteToLocalTransfer
fromdeleteimportdeleteFile, showDeleteFileSuccessMsg
fromdrawimport (createTopTextBoxes, createBottomCenterBox, createDeleteButton,
createNotificationBox, createBottomLeftBox, createBottomRightBox)
classProgram(QMainWindow):
def__init__(self, parent=None):
super(Program, self).__init__(parent)
self.originalPalette=QApplication.palette()
self.setWindowTitle("FTP Program")
self.setFixedWidth(1500)
self.setFixedHeight(750)
styleComboBox=QComboBox()
styleComboBox.addItems(QStyleFactory.keys())
styleLabel=QLabel("&Style:")
styleLabel.setBuddy(styleComboBox)
self.useStylePaletteCheckBox=QCheckBox("&Use style's standard palette")
self.useStylePaletteCheckBox.setChecked(True)
disableWidgetsCheckBox=QCheckBox("&Disable widgets")
self.currentDir=os.path.dirname(os.path.realpath(__file__))
self.LocalFilesList=QTreeWidget(self)
self.RemoteFilesList=QTreeWidget(self)
self.currentRemotePathLabel=QLabel(self)
self.currentLocalPathLabel=QLabel(self)
self.currentRemotePathDisplay=QLabel(self)
self.currentLocalPathDisplay=QLabel(self)
self.currentRemotePath="/"
self.currentLocalPath="\\"
self.currentFile="/"
self.currentFileList=""
self.localSelectedFile= []
self.hostName=''
self.username=''
self.password=''
createNotificationBox(self)
createBottomLeftBox(self)
createBottomRightBox(self)
createBottomCenterBox(self)
createTopTextBoxes(self)
createDeleteButton(self)
mainLayout=QGridLayout()
self.setLayout(mainLayout)
# eventlisteners
self.quickConnectButton.clicked.connect(lambda:startFTP(self, self.hostnameTextBox.text(), self.usernameTextBox.text(), self.passwordTextBox.text()))
self.rightArrowButton.clicked.connect(lambda:remoteToLocalTransfer(self, self.remoteSelectedFile))
self.leftArrowButton.clicked.connect(lambda:localToRemoteTransfer(self, self.localSelectedFile))
self.deleteButton.clicked.connect(lambda:deleteFile(self))
# Misc
self.directoryIcon=QIcon(self.currentDir+"/icons/directory.png")
self.fileIcon=QIcon(self.currentDir+"/icons/file.png")
defkeyPressEvent(self, event):
ifevent.key() ==Qt.Key_Return:
startFTP(self, self.hostnameTextBox.text(), self.usernameTextBox.text(), self.passwordTextBox.text())
defclearAllData(self):
self.LocalFilesList.clear()
self.RemoteFilesList.clear()
self.currentLocalPath="\\"
self.currentRemotePath="/"
self.currentFile=""
self.currentFileList=""
defchangePalette(self):
if(self.useStylePaletteCheckBox.isChecked()):
QApplication.setPalette(QApplication.style().standardPalette())
else:
QApplication.setPalette(self.originalPalette)
defgetRemoteFileList(self, *args):
ifargs:
newRemoteArr=self.currentRemotePath.split("/")
arrLength=len(newRemoteArr)
i=0
whilei<arrLength:
ifnewRemoteArr[i] =="":
del(newRemoteArr[i])
arrLength-=1
continue
i+=1
iflen(newRemoteArr) ==0:
# self.currentRemotePath is "/"
self.currentRemotePathDisplay.setText(self.currentRemotePath)
returnFalse
self.RemoteFilesList.clear() # clear current list since back navigation is valid
newRemoteArr.pop()
newRemotePath=""
iflen(newRemoteArr) !=0:
foriinnewRemoteArr:
newRemotePath=newRemotePath+'/'
newRemotePath=newRemotePath+i
# self.currentRemotePath = newRemotePath + "/"
self.currentRemotePath=newRemotePath
else:
newRemotePath="/"
self.currentRemotePath=newRemotePath
remoteFiles=self.connection.listdir_attr(self.currentRemotePath)
else:
self.RemoteFilesList.clear()
# remoteDir = self.connection.normalize(".")
ifself.currentRemotePath=="/":
remoteFiles=self.connection.listdir_attr("./")
self.currentRemotePath=self.connection.pwd
else:
remoteFiles=self.connection.listdir_attr(self.currentRemotePath)
self.currentRemotePathDisplay.setText(self.currentRemotePath)
self.createRemoteFilesList(remoteFiles)
returnTrue
defcreateRemoteFilesList(self, remoteFiles):
remote_list= []
self.RemoteFilesList.addTopLevelItem(QTreeWidgetItem(["..", " ", " "]))
forfileinremoteFiles:
fileType=file.st_mode//10000
file_name=file.filename
date_modified=datetime.fromtimestamp(file.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
file_size=str(file.st_size)
iffileType==1: # for dir
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.directoryIcon)
foriinrange(0, self.RemoteFilesList.columnCount()):
item_file.setBackground(i, QColor(100,100,150))
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.RemoteFilesList.addTopLevelItem(item_file)
eliffileType==3: # for files
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.fileIcon)
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.RemoteFilesList.addTopLevelItem(item_file)
defcreateLocalFilesList(self, localFiles):
self.LocalFilesList.addTopLevelItem(QTreeWidgetItem(["..", " ", " "]))
try:
forfileinlocalFiles:
fileType=list(file.stat())[0] //10000
stat_file=os.stat(file)
date_modified=datetime.fromtimestamp(os.path.getmtime(file.path)).strftime("%Y-%m-%d %H:%M:%S")
file_name=file.name
file_size=str(stat_file.st_size)
iffileType==1:
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.directoryIcon)
item_file.setStatusTip(0, "d")
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.LocalFilesList.addTopLevelItem(item_file)
eliffileType==3:
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.fileIcon)
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.LocalFilesList.addTopLevelItem(item_file)
self.currentLocalPathDisplay.setText(self.currentLocalPath)
exceptFileNotFoundErrorase:
self.currentLocalPathDisplay.setText(self.currentLocalPath)
error_message=QMessageBox()
error_message.setWindowTitle("File not found error")
error_message.setText(str(e))
error_message.setIcon(QMessageBox.Critical)
error_message.exec_()
deflocalFileSelectionChangedSingleClick(self):
self.currentFile=self.LocalFilesList.selectedItems()[0]
self.currentFileList="Local"
deflocalFileSelectionChanged(self):
self.localSelectedFile=self.LocalFilesList.selectedItems()[0]
item=self.localSelectedFile
ifitem.text(0) =="..":
getLocalFileList(self, "..")
self.localSelectedFile=""
else:
ifitem.statusTip(0) =='d':
# selection is dir, switch dirs
ifself.currentLocalPath=="/":
self.currentLocalPath=item.text(0).split(" -")[0] +self.currentLocalPath
else:
ifself.currentLocalPath.endswith("\\"):
# removing the 'def\\' in path: '\\abc\def\\'
ifself.currentLocalPath=="C:\\\\":
self.currentLocalPath="C:"
else:
self.currentLocalPath=self.currentLocalPath[:-1]
self.currentLocalPath+="\\"+item.text(0)
try:
localFiles=os.scandir(self.currentLocalPath)
exceptPermissionErrorase:
error_message=QMessageBox()
error_message.setWindowTitle("Permission Error")
error_message.setText(str(e))
error_message.setIcon(QMessageBox.Critical)
error_message.exec_()
localArr=list(os.path.split(self.currentLocalPath))
self.currentLocalPath=localArr[0]
return
self.LocalFilesList.clear()
self.createLocalFilesList(localFiles)
self.localSelectedFile=""
defremoteFileSelectionChangedSingleClick(self):
self.currentFile=self.RemoteFilesList.selectedItems()[0]
self.currentFileList="Remote"
defremoteFileSelectionChanged(self):
self.remoteSelectedFile=self.RemoteFilesList.selectedItems()[0]
item=self.remoteSelectedFile
ifitem.text(0) =="..":
self.getRemoteFileList("..")
self.remoteSelectedFile=""
else:
ifitem.statusTip(0) =="d":
# selection is dir
ifself.currentRemotePath=="/":
# self.currentRemotePath = item.text(0).split(" -")[0] + self.currentRemotePath
self.currentRemotePath+=item.text(0)
else:
ifself.currentRemotePath.endswith("/"):
# removing the 'def/' in path: '/abc/def/'
self.currentRemotePath=self.currentRemotePath[:-1]
self.currentRemotePath+="/"+item.text(0)
try:
withself.connection.cd(self.currentRemotePath):
self.RemoteFilesList.clear()
remote_files=self.connection.listdir_attr()
self.createRemoteFilesList(remote_files)
exceptPermissionErrorase:
error_message=QMessageBox()
error_message.setWindowTitle("Permission Error")
error_message.setText(str(e))
error_message.setIcon(QMessageBox.Critical)
error_message.exec_()
remoteArr=list(os.path.split(self.currentRemotePath))
self.currentRemotePath=remoteArr[0]
exceptExceptionase:
error_message=QMessageBox()
error_message.setWindowTitle("Remote File System Error")
error_message.setText(str(e))
error_message.setIcon(QMessageBox.Critical)
error_message.exec_()
remoteArr=list(os.path.split(self.currentRemotePath))
self.currentRemotePath=remoteArr[0]
defcreateRemoteFilesList(self, remote_files):
self.RemoteFilesList.addTopLevelItem(QTreeWidgetItem(["..", " ", " "]))
forfileinremote_files:
fileType=file.st_mode//10000
file_name=file.filename
file_size=str(file.st_size)
date_modified=datetime.fromtimestamp(file.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
iffileType==1:
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.directoryIcon)
item_file.setStatusTip(0, 'd')
# for i in range(0, self.RemoteFilesList.columnCount()):
# item_file.setBackground(i, QColor(100,100,150))
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.RemoteFilesList.addTopLevelItem(item_file)
iffileType==3:
item_file=QTreeWidgetItem()
item_file.setIcon(0, self.fileIcon)
forn, iinenumerate((file_name, date_modified, file_size)):
item_file.setText(n, i)
self.RemoteFilesList.addTopLevelItem(item_file)
self.remoteSelectedFile=""
self.currentRemotePathDisplay.setText(self.currentRemotePath)