forked from CAU-OSS-project-practice/OSS-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_old.py
336 lines (336 loc) · 13.7 KB
/
files_old.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os, shutil, configparser
import PySimpleGUI as sg
from pathlib import Path
from send2trash import send2trash
# Config files.ini
config = configparser.ConfigParser()
config.read('files.ini')
main_path = config['USER SETTINGS']['home_path']
if main_path == "":
main_path = str(Path.home())
# Variables
sg.theme("SystemDefault1")
icon = "data/icon.png"
folder_icon = "data/icon_folder.png"
file_icon = "data/icon_file.png"
home_icon = "data/icon_home.png"
up_icon = "data/icon_up.png"
treedata = sg.TreeData()
hidden = False
reverse = False
sort_size = False
size_list = []
count = 0
s = "☐ Show hidden files"
h = "☑ Show hidden files"
hidd = s
ab = "▼ Reverse sort"
ba = "▲ Reverse sort"
revers = ba
nsort = "⇵ Sort by name"
ssort = "⇵ Sort by size"
sort = ssort
pd = "❏ Paste"
pa = "!❏ Paste"
paste = pa
c1 = "❐ Copy"
c2 = "!❐ Copy"
copy = c2
d1 = "✘ Delete in trash"
d2 = "!✘ Delete in trash"
delete = d2
r1 = "✎ Rename"
r2 = "!✎ Rename"
rename = r2
# Convert size
def add_size(name):
s = os.stat(name).st_size
if s < 1000:
size = str(s) + " B"
if s >= 1000:
size = str(round(s/1000, 2)) + " KB"
if s >= 1000000:
size = str(round(s/1000000, 2)) + " MB"
if s >= 1000000000:
size = str(round(s/1000000000, 2)) + " GB"
if s >= 1000000000000:
size = str(round(s/1000000000000, 2)) + " TB"
return [size, s]
# Updating files and folders
def add_files_in_folder(parent, dirname):
files = os.listdir(dirname)
if reverse == False:
files.sort(key=str.lower)
if reverse == True:
files.sort(key=str.lower, reverse=True)
global count
global treedata
treedata = sg.TreeData()
for f in files:
fullname = os.path.join(dirname, f)
if hidden == False:
if f.startswith("."):
continue
elif os.path.isdir(fullname):
treedata.Insert(parent, fullname, f, values=[], icon=folder_icon)
count += 1
else:
continue
elif os.path.isdir(fullname):
treedata.Insert(parent, fullname, f, values=[], icon=folder_icon)
count += 1
else:
continue
for f in files:
fullname = os.path.join(dirname, f)
size = add_size(fullname)
if sort_size == True:
if hidden == False:
if f.startswith("."):
continue
elif os.path.isdir(fullname):
continue
else:
size_list_ml = [size[1], fullname, f, size[0]]
size_list.append(size_list_ml)
elif os.path.isdir(fullname):
continue
else:
size_list_ml = [size[1], fullname, f, size[0]]
size_list.append(size_list_ml)
else:
if hidden == False:
if f.startswith("."):
continue
elif os.path.isdir(fullname):
continue
else:
treedata.Insert(parent, fullname, f, values=[size[0]], icon=file_icon)
count += 1
elif os.path.isdir(fullname):
continue
else:
treedata.Insert(parent, fullname, f, values=[size[0]], icon=file_icon)
count += 1
if sort_size == True:
if reverse == False:
size_list.sort(key=lambda size_list: size_list[0], reverse=True)
if reverse == True:
size_list.sort(key=lambda size_list: size_list[0])
for s in size_list:
treedata.Insert(parent, s[1], s[2], values=[s[3]], icon=file_icon)
count += 1
size_list.clear()
add_files_in_folder("", main_path)
path_original = main_path
# The stuff inside app
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
layout = [ [sg.ButtonMenu("≡", menu_def, background_color="white", key="-MENU-"), sg.Button(image_size=(24,24), key="↑", image_filename=up_icon), sg.Button(image_size=(24,24), key="⌂", image_filename=home_icon), sg.Input(default_text=main_path, key="-OUT1-", expand_x=True, size=(50,1))],
[sg.Tree(data=treedata, font=("Helvetica",15), headings=["size"], auto_size_columns=False, col_widths=[10], num_rows=None, col0_width=40, max_col_width=10, row_height=30, key="-TREE-", enable_events=True)],
[sg.Text(text=str(count) + " objects", key="-OUT111-", justification="center", size=(20,1)), sg.Input(default_text=main_path, readonly=True, key="-OUT11-", size=(40,1), expand_x=True, visible=False)] ]
# Window params
window = sg.Window("Files", layout, return_keyboard_events=True, element_justification="left", debugger_enabled=False, resizable=True, margins=(0,0), finalize=True, font=("Helvetica",15), icon=icon)
window.set_min_size((800,500))
window["-TREE-"].expand(True, True)
# Main loop
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
up_string = values["-OUT1-"]
down_string = values["-OUT11-"]
window["-TREE-"].bind("<Double-Button-1>", "double")
window["-TREE-"].bind("<Button-1>", "click")
# Menus events
if event == "-MENU-":
event = values["-MENU-"]
if event == "☐ Show hidden files":
hidden = True
hidd = h
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "☑ Show hidden files":
hidden = False
hidd = s
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "▲ Reverse sort":
reverse = True
revers = ab
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "▼ Reverse sort":
reverse = False
revers = ba
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "⇵ Sort by size":
sort_size = True
sort = nsort
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "⇵ Sort by name":
sort_size = False
sort = ssort
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "★ About...":
sg.popup("Files", "0.5", "A simple file explorer", font=("Helvetica",13), icon=icon)
if event == "✇ Settings":
layout_set = [ [sg.Text("Home directory", size=(15,1)), sg.Input(default_text=main_path, key="-OUT2-", size=(60,1)), sg.Button("Save")],
[sg.Text(justification="right", key="-OUT22-", size=(60,1))] ]
window_set = sg.Window("Settings", layout_set, return_keyboard_events=True, element_justification="left", debugger_enabled=False, modal=True, font=("Helvetica",15), icon=icon)
while True:
event, values = window_set.read()
if event == sg.WIN_CLOSED:
break
set_home_str = values["-OUT2-"]
if event == "Save":
config['USER']['main_path'] = set_home_str
with open('files.ini', 'w') as configfile:
config.write(configfile)
main_path = set_home_str
window_set["-OUT22-"].update("settings saved")
window_set.close()
# File operations
if event == "❐ Copy":
source = down_string
source_edit = source.rsplit("/", 1)
paste = pd
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
if event == "▣ Paste":
if source != "":
source_1 = os.path.join(source_edit[0], source_edit[1])
if os.path.isdir(source_1):
if source_edit[0] == path_original:
destination = path_original + "/" + source_edit[1] + " (copy)"
else:
destination = path_original + "/" + source_edit[1]
shutil.copytree(source, destination)
source = ""
source_edit = ""
destination = ""
else:
if source_edit[0] == path_original:
destination = path_original + "/" + source_edit[1] + " (copy)"
else:
destination = path_original + "/"
shutil.copy2(source, destination)
source = ""
source_edit = ""
destination = ""
paste = pa
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
event = "-TREE-double"
if event == "✘ Delete in trash":
del_path = down_string
del_edit = del_path.rsplit("/", 1)
answer = sg.popup_yes_no(f"Delete '{del_edit[1]}' in trash?", font=("Helvetica",13), no_titlebar=True)
if answer == "Yes":
if os.path.exists(del_path):
send2trash(del_path)
up_string = path_original
down_string = path_original
window["-OUT1-"].update(up_string)
window["-OUT11-"].update(down_string)
event = "-TREE-double"
if event == "✎ Rename":
r_path = down_string
r_edit = r_path.rsplit("/", 1)
if os.path.exists(r_path):
save = "not"
layout_r = [ [sg.Input(default_text=r_edit[1], key="-OUT3-", size=(30,1))],
[sg.Button("Save"), sg.Button("Cancel")] ]
window_r = sg.Window("Rename", layout_r, return_keyboard_events=True, element_justification="center", debugger_enabled=False, modal=True, font=("Helvetica",15), no_titlebar=True)
while True:
event, values = window_r.read()
if event == sg.WIN_CLOSED:
break
rename_str = values["-OUT3-"]
if event == "Save":
save = "ok"
rename_str_f = r_edit[0] + "/" + rename_str
window_r.close()
if event == "Cancel":
window_r.close()
if save == "ok":
os.rename(r_path, rename_str_f)
event = "-TREE-double"
up_string = path_original
down_string = path_original
window["-OUT1-"].update(up_string)
window["-OUT11-"].update(down_string)
# Screen with files and catalogs events
if event == "-TREE-click":
up_string = path_original
down_string = path_original
window["-OUT1-"].update(up_string)
window["-OUT11-"].update(down_string)
if event == "-TREE-":
for row in values[event]:
down_string = str(row)
window["-OUT11-"].update(down_string)
for row in values[event]:
up_string = str(row)
path_string_e = up_string.rsplit("/", 1)
path_string_e1 = os.path.join(path_string_e[0], path_string_e[1])
if os.path.isfile(path_string_e1):
up_string = path_original
window["-OUT1-"].update(up_string)
# Buttons events
if event == "⌂":
up_string = main_path
down_string = main_path
event = "-TREE-double"
if event == "↑":
string_1 = path_original.rsplit("/", 1)
up_string = string_1[0]
down_string = up_string
if up_string == "":
up_string = up_string + "/"
down_string = up_string
event = "-TREE-double"
# Refresh layout
try:
if event == "-TREE-double" or event == "KP_Enter:104" or event == "Return:36":
count = 0
add_files_in_folder("", up_string)
path_original = up_string
window["-TREE-"].update(values=treedata)
window["-OUT1-"].update(up_string)
window["-OUT11-"].update(down_string)
window["-OUT111-"].update(str(count) + " objects")
except PermissionError:
sg.popup("No Access", font=("Helvetica",13), no_titlebar=True)
except FileNotFoundError:
None
# Strings, menus valid
try:
if up_string[-1] == "/" and up_string != "" and up_string != "/":
up_string = up_string[:-1]
if down_string[-1] == "/" and down_string != "" and down_string != "/":
down_string = down_string[:-1]
if path_original[-1] == "/" and path_original != "" and path_original != "/":
path_original = path_original[:-1]
except:
None
if down_string == "/" or down_string == path_original:
copy = c2
delete = d2
rename = r2
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)
elif down_string != "/" or down_string != path_original:
copy = c1
delete = d1
rename = r1
menu_def = ["≡", [hidd, revers, sort, copy, paste, delete, rename, "✇ Settings", "★ About..."]]
window["-MENU-"].update(menu_definition=menu_def)