-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.py
312 lines (295 loc) · 14 KB
/
sort.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
import os
import shutil
from customtkinter import *
from tkinter import filedialog
from tkinter import messagebox
from tkinter.colorchooser import askcolor
import json
from PIL import Image
import zipfile
import matplotlib.pyplot as plt
import logging as log
from magika import Magika
import threading
from sort import *
from help import help_engine as help_engine
class sort:
def __init__(self):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
self.folder_path = folder_to_sort
if self.folder_path == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder_path):
count_folder_sortet_add += 1
log.info("Folder")
if button_sub_sort.get():
sorting_subdir(self.folder_path, file_ending)
else:
sorting_normal( file_ending)
class sort_advanced_script: # get file ending dict as .json file
def __init__(self, folder, conf_file):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
self.folder = folder
self.conf_file = conf_file
with open(conf_file, "r") as f:
file_ending_conf = json.load(f)
if self.folder == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder):
count_folder_sortet_add += 1
log.info("Folder")
if button_sub_sort.get():
sorting_subdir(self.folder, file_ending_conf)
else:
sorting_normal(file_ending_conf)
class advanced_sort:
def __init__(self, Images, Videos, Audio, Documents, Archives, Models, PCB, Code, Executables, Fonts, Other):
self.folder_path = folder_to_sort
self.Images = Images
self.Videos = Videos
self.Audio = Audio
self.Documents = Documents
self.Archives = Archives
self.Models = Models
self.PCB = PCB
self.Code = Code
self.Executables = Executables
self.Fonts = Fonts
self.Other = Other
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
if self.folder_path == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder_path):
self.prepare()
count_folder_sortet_add += 1
log.info("Folder")
if button_sub_sort.get():
sorting_subdir(self.folder_path, self.file_ending_)
else:
sorting_normal(self.file_ending_)
def prepare(self):
self.file_ending_ = {
"Images": [],
"Videos": [],
"Audio": [],
"Documents": [],
"Archives": [],
"3D Models": [],
"PCB": [],
"Code": [],
"Executables": [],
"Fonts": [],
"Other": []
}
if self.Images:
self.file_ending_["Images"] = file_ending["Images"]
if self.Videos:
self.file_ending_["Videos"] = file_ending["Videos"]
if self.Audio:
self.file_ending_["Audio"] = file_ending["Audio"]
if self.Documents:
self.file_ending_["Documents"] = file_ending["Documents"]
if self.Archives:
self.file_ending_["Archives"] = file_ending["Archives"]
if self.Models:
self.file_ending_["3D Models"] = file_ending["3D Models"]
if self.PCB:
self.file_ending_["PCB"] = file_ending["PCB"]
if self.Code:
self.file_ending_["Code"] = file_ending["Code"]
if self.Executables:
self.file_ending_["Executables"] = file_ending["Executables"]
if self.Fonts:
self.file_ending_["Fonts"] = file_ending["Fonts"]
if self.Other:
self.file_ending_["Other"] = file_ending["Other"]
class sorting_magika:
def __init__(self, file_endings):
self.folder_path = folder_to_sort
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
if self.folder_path == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder_path):
self.sort_files_magika(file_endings)
def sort_files_magika(self, file_endings):
pass
class sorting_normal:
def __init__(self, file_endings):
self.folder_path = folder_to_sort
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
if self.folder_path == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder_path):
if button_magika:
self.sort_files_normal_magika(file_endings)
else:
self.sort_files_normal(file_endings)
def sort_files_normal(self, file_endings):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
self.count_elements = 1
for file in os.listdir(self.folder_path):
if os.path.isdir(os.path.join(self.folder_path, file)):
if not os.listdir(file_path):
ask = messagebox.showinfo("Folder Sorter",f"Folder {file_path} is empty and will be deleted.")
if ask:
os.rmdir(file_path)
continue
else:
for key in file_endings:
if file.endswith(tuple(file_endings[key])):
count_file_type_add[key] += 1
log.info(f"File: {file}: {key}")
self.count_elements += 1
count_file_sortet_add += 1
if not os.path.exists(os.path.join(self.folder_path, key)):
os.makedirs(os.path.join(self.folder_path, key))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, key, file))
break
elif key == "Other":
count_file_type_add["Other"] += 1
log.info(f"File: {file}: Other")
if not os.path.exists(os.path.join(self.folder_path, "Other")):
os.makedirs(os.path.join(self.folder_path, "Other"))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, "Other", file))
self.count_elements += 1
count_file_sortet_add += 1
break
messagebox.showinfo("Folder Sorter",f"Finisched sorting of {str(self.count_elements)} elements \n in the folder {folder_to_sort}.")
def sort_files_normal_magika(self, file_endings):
magika = Magika()
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
self.count_elements = 1
for file in os.listdir(self.folder_path):
if os.path.isdir(os.path.join(self.folder_path, file)):
if not os.listdir(file_path):
ask = messagebox.showinfo("Folder Sorter",f"Folder {file_path} is empty and will be deleted.")
if ask:
os.rmdir(file_path)
continue
else:
result = magika.identify_path(Path(file))
for key in file_endings_magika:
if result.output.ct_label == key:
count_file_type_add[key] += 1
log.info(f"File: {file}: {key}")
self.count_elements += 1
count_file_sortet_add += 1
if not os.path.exists(os.path.join(self.folder_path, key)):
os.makedirs(os.path.join(self.folder_path, key))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, key, file))
break
elif key == "Other":
count_file_type_add["Other"] += 1
log.info(f"File: {file}: Other")
if not os.path.exists(os.path.join(self.folder_path, "Other")):
os.makedirs(os.path.join(self.folder_path, "Other"))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, "Other", file))
self.count_elements += 1
count_file_sortet_add += 1
break
messagebox.showinfo("Folder Sorter",f"Finisched sorting of {str(self.count_elements)} elements \n in the folder {folder_to_sort}.")
class sorting_subdir:
def __init__(self, folder_path, file_endings):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
self.count_elements = 1
self.folder_path = folder_path
if self.folder_path == "":
messagebox.showerror("Folder Sorter", "Please select a folder")
elif os.path.exists(self.folder_path):
match file_endings:
case "Images":
pass
case "Videos":
pass
case "Audio":
pass
case "Documents":
pass
case "Archives":
pass
case "3D Models":
pass
case "PCB":
pass
case "Code":
pass
case "Executables":
pass
case "Fonts":
pass
case "Other":
pass
case _:
self.sort_files_subdir(file_endings)
def sort_files_subdir(self, file_endings):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
for file in os.listdir(self.folder_path):
file_path = os.path.join(self.folder_path, file)
if os.path.isdir(file_path):
if not os.listdir(file_path):
ask = messagebox.showinfo("Folder Sorter",f"Folder {file_path} is empty and will be deleted.")
if ask:
os.rmdir(file_path)
sorting_subdir(file_path, file_endings)
else:
for key in file_endings:
try:
if file_endings[key] == []:
continue
elif file.endswith(tuple(file_endings[key])):
count_file_type_add[key] += 1
log.info(f"File: {file}: {key}")
if not os.path.exists(os.path.join(self.folder_path, key)):
os.makedirs(os.path.join(self.folder_path, key))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, key, file))
self.count_elements += 1
count_file_sortet_add += 1
break
elif key == "Other":
count_file_type_add["Other"] += 1
log.info(f"File: {file}: Other")
if not os.path.exists(os.path.join(self.folder_path, "Other")):
os.makedirs(os.path.join(self.folder_path, "Other"))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, "Other", file))
self.count_elements += 1
count_file_sortet_add += 1
break
except:
pass
messagebox.showinfo("Folder Sorter",f"Finisched sorting of {str(self.count_elements)} elements \n in the folder {folder_to_sort}.")
def sort_files_subdir_magika(self, file_endings):
global count_file_sortet_add, count_folder_sortet_add, count_file_type_add
for file in os.listdir(self.folder_path):
file_path = os.path.join(self.folder_path, file)
if os.path.isdir(file_path):
if not os.listdir(file_path):
ask = messagebox.showinfo("Folder Sorter",f"Folder {file_path} is empty and will be deleted.")
if ask:
os.rmdir(file_path)
sorting_subdir(file_path, file_endings)
else:
for key in file_endings:
try:
if file_endings[key] == []:
continue
elif file.endswith(tuple(file_endings[key])):
count_file_type_add[key] += 1
log.info(f"File: {file}: {key}")
if not os.path.exists(os.path.join(self.folder_path, key)):
os.makedirs(os.path.join(self.folder_path, key))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, key, file))
self.count_elements += 1
count_file_sortet_add += 1
break
elif key == "Other":
count_file_type_add["Other"] += 1
log.info(f"File: {file}: Other")
if not os.path.exists(os.path.join(self.folder_path, "Other")):
os.makedirs(os.path.join(self.folder_path, "Other"))
shutil.move(os.path.join(self.folder_path, file), os.path.join(self.folder_path, "Other", file))
self.count_elements += 1
count_file_sortet_add += 1
break
except:
pass
messagebox.showinfo("Folder Sorter",f"Finisched sorting of {str(self.count_elements)} elements \n in the folder {folder_to_sort}.")