-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorter_utils.py
377 lines (331 loc) · 16.9 KB
/
sorter_utils.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
# Author: Andrii Valchuk
import os
import glob
import shutil
import re
import stat
import threading
import time
from datetime import datetime
from plyer import notification
class SortingFilesUtils(object):
def __init__(self):
self.sort_thread = None
def setHome(self, home):
self.home = home
def setNewhome(self, newhome):
self.newhome = newhome
def __has_hidden_attribute(self, filepath):
return bool(os.stat(filepath).st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)
def setDestination(self, destination):
self.destination = destination
def __mascfiles(self, masc):
mfiles = []
names = glob.glob(os.path.join(self.home, masc))
for name in names:
if os.path.isfile(name) and not self.__has_hidden_attribute(name):
mfiles.append(name)
return mfiles
def __masclist(self, mascs):
mlist = []
for masc in mascs:
mlist = mlist + self.__mascfiles(masc)
return mlist
def __sizesortingfiles(self, sfiles):
sizefiles = 0
for sf in sfiles:
size = os.path.getsize(sf)
sizefiles = sizefiles + size
return sizefiles
def allfiles(self):
sortinglist = []
names = os.listdir(self.home)
for name in names:
fullname = os.path.join(self.home, name)
if os.path.isfile(fullname) and not self.__has_hidden_attribute(fullname):
sortinglist.append(fullname)
return sortinglist
def audiofiles(self):
mascs = ['*.aac', '*.aif', '*.aiff', '*.aob', '*.ape', '*.asf', '*.aud', '*.bwg',
'*.cdr', '*.flac', '*.ics', '*.iff', '*.m', '*.m4a', '*.m4b', '*.m4p',
'*.m4r', '*.mid', '*.midi', '*.mod', '*.mp3', '*.mpa', '*.mpp', '*.msv',
'*.mts', '*.nkc', '*.ogg', '*.ps', '*.ra', '*.sdf', '*.sib', '*.sln',
'*.spl', '*.srt', '*.vb', '*.wav', '*.wave', '*.wm', '*.wma', '*.wpd']
return self.__masclist(mascs)
def documentfiles(self):
mascs = ['*.gp*', '*.txt', '*.ods', '*.csv', '*.asp', '*.cdd', '*.cpp', '*.djvu',
'*.doc', '*.docm', '*.docx', '*.dot', '*.dotm', '*.dotx', '*.epub', '*.fb2',
'*.gpx', '*.ibooks', '*.indd', '*.kdc', '*.key', '*.kml', '*.mdb', '*.mdf',
'*.mobi', '*.mso', '*.odt', '*.one', '*.oxps', '*.pages', '*.pdf', '*.pkg',
'*.pl', '*.pot', '*.potm', '*.potx', '*.pps', '*.ppsm', '*.ppsx', '*.ppt',
'*.pptm', '*.pptx', '*.ps', '*.rtf', '*.sdf', '*.snb', '*.wpd', '*.wps',
'*.xar', '*.xlr', '*.xls', '*.xlsb', '*.xlsm', '*.xlsx', '*.xlt', '*.xltm',
'*.xltx', '*.xps']
return self.__masclist(mascs)
def imagefiles(self):
mascs = ['*.svg', '*.webp', '*.skb', '*.skp', '*.apt', '*.bmp', '*.dds', '*.dng',
'*.gbr', '*.gif', '*.hta', '*.iff', '*.jpeg', '*.jpg', '*.kdc', '*.mng',
'*.png', '*.pot', '*.psd', '*.pspimage', '*.scr', '*.tga', '*.thm', '*.tif',
'*.tif', '*.tiff', '*.vst', '*.xcf', '*.yuv']
return self.__masclist(mascs)
def videofiles(self):
mascs = ['*.3g2', '*.3gp', '*.3gp2', '*.3gpp', '*.3gpp2', '*.asf', '*.asx', '*.avi',
'*.drv', '*.f4v', '*.flv', '*.gtp', '*.h264', '*.m4v', '*.mkv', '*.mod',
'*.moov', '*.mov', '*.mp4', '*.mpeg', '*.mpg', '*.mts', '*.rm', '*.spl',
'*.srt', '*.stl', '*.swf', '*.vcd', '*.vid', '*.vob', '*.wm', '*.wmv', '*.yuv']
return self.__masclist(mascs)
def arhivefiles(self):
mascs = ["*.7z", "*.arj", "*.pkg", "*.rar", "*.tar", "*.gz", "*.z", "*.zip"]
return self.__masclist(mascs)
def programsfiles(self):
mascs = ["*.apk", "*.bat", "*.bin", "*.cgi", "*.pl", "*.com", "*.exe", "*.gadget",
"*.jar", "*.py", "*.wsf", "*.ino", "*.rpm", "*.deb"]
return self.__masclist(mascs)
def othersfiles(self):
return list(set(self.allfiles()) - set(self.imagefiles()) - set(self.audiofiles()) - set(self.videofiles())
- set(self.documentfiles()) - set(self.arhivefiles()) - set(self.programsfiles()))
def sizeall(self):
return self.__sizesortingfiles(self.allfiles())
def sizeaudio(self):
return self.__sizesortingfiles(self.audiofiles())
def sizedocument(self):
return self.__sizesortingfiles(self.documentfiles())
def sizeimage(self):
return self.__sizesortingfiles(self.imagefiles())
def sizevideo(self):
return self.__sizesortingfiles(self.videofiles())
def sizearhive(self):
return self.__sizesortingfiles(self.arhivefiles())
def sizeprograms(self):
return self.__sizesortingfiles(self.programsfiles())
def sizeothers(self):
return self.__sizesortingfiles(self.othersfiles())
def list_move(self, myhome, target):
if not os.path.exists(target):
os.makedirs(target)
for homefile in myhome:
tempfile = os.path.split(homefile)[-1]
if os.path.exists(os.path.join(target, tempfile)):
n = 1
newfile = tempfile
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(tempfile)[0] + '(' + str(n) + ')' + os.path.splitext(tempfile)[1]
n += 1
shutil.move(homefile, os.path.join(target, newfile))
else:
shutil.move(homefile, target)
def list_copy(self, myhome, target):
if not os.path.exists(target):
os.makedirs(target)
for homefile in myhome:
tempfile = os.path.split(homefile)[-1]
if os.path.exists(os.path.join(target, tempfile)):
n = 1
newfile = tempfile
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(tempfile)[0] + '(' + str(n) + ')' + os.path.splitext(tempfile)[1]
n += 1
shutil.copy2(homefile, os.path.join(target, newfile))
else:
shutil.copy2(homefile, target)
def move(self):
try:
self.list_move(self.imagefiles(), self.newhome[0])
self.list_move(self.audiofiles(), self.newhome[1])
self.list_move(self.videofiles(), self.newhome[2])
self.list_move(self.documentfiles(), self.newhome[3])
self.list_move(self.programsfiles(), self.newhome[4])
self.list_move(self.arhivefiles(), self.newhome[5])
self.list_move(self.othersfiles(), self.newhome[6])
notification.notify(title='File Sorter: Complete',
message='Advanced sorting files is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def move_daemon(self):
self.sort_thread = threading.Thread(target=self.move)
self.sort_thread.daemon = True
self.sort_thread.start()
def simple_move(self):
try:
self.list_move(self.imagefiles(), os.path.join(self.destination, "Pictures"))
self.list_move(self.audiofiles(), os.path.join(self.destination, "Music"))
self.list_move(self.videofiles(), os.path.join(self.destination, "Videos"))
self.list_move(self.documentfiles(), os.path.join(self.destination, "Documents"))
self.list_move(self.programsfiles(), os.path.join(self.destination, "Programs"))
self.list_move(self.arhivefiles(), os.path.join(self.destination, "Compressed"))
self.list_move(self.othersfiles(), os.path.join(self.destination, "Others"))
notification.notify(title='File Sorter: Complete',
message='Simple sorting files is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def simple_move_daemon(self):
self.sort_thread = threading.Thread(target=self.simple_move)
self.sort_thread.daemon = True
self.sort_thread.start()
def move_by_ext(self):
try:
for homefile in self.allfiles():
file_ext = os.path.split(homefile)[-1]
match = re.search(r'([\w]+)', os.path.splitext(file_ext)[1])
if match:
folder_ext = match.group().upper()
target = os.path.join(self.destination, folder_ext)
if not os.path.exists(target):
os.makedirs(target)
if os.path.exists(os.path.join(target, file_ext)):
n = 1
newfile = file_ext
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(file_ext)[0] + '(' + str(n) + ')' + os.path.splitext(file_ext)[1]
n += 1
shutil.move(homefile, os.path.join(target, newfile))
else:
shutil.move(homefile, target)
notification.notify(title='File Sorter: Complete',
message='Sort by file extension is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def move_by_ext_daemon(self):
self.sort_thread = threading.Thread(target=self.move_by_ext)
self.sort_thread.daemon = True
self.sort_thread.start()
def move_by_date(self):
try:
for homefile in self.allfiles():
file_date = os.path.split(homefile)[-1]
datestring = time.ctime(os.path.getctime(homefile))
dt = datetime.strptime(datestring, '%a %b %d %H:%M:%S %Y')
folder_date = str(dt.day) + '-' + str(dt.month) + '-' + str(dt.year)
target = os.path.join(self.destination, folder_date)
if not os.path.exists(target):
os.makedirs(target)
if os.path.exists(os.path.join(target, file_date)):
n = 1
newfile = file_ext
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(file_ext)[0] + '(' + str(n) + ')' + os.path.splitext(file_ext)[1]
n += 1
shutil.move(homefile, os.path.join(target, newfile))
else:
shutil.move(homefile, target)
notification.notify(title='File Sorter: Complete',
message='Sort by file date is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def move_by_date_daemon(self):
self.sort_thread = threading.Thread(target=self.move_by_date)
self.sort_thread.daemon = True
self.sort_thread.start()
def copy(self):
try:
self.list_copy(self.imagefiles(), self.newhome[0])
self.list_copy(self.audiofiles(), self.newhome[1])
self.list_copy(self.videofiles(), self.newhome[2])
self.list_copy(self.documentfiles(), self.newhome[3])
self.list_copy(self.programsfiles(), self.newhome[4])
self.list_copy(self.arhivefiles(), self.newhome[5])
self.list_copy(self.othersfiles(), self.newhome[6])
notification.notify(title='File Sorter: Complete',
message='Advanced copy file is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def copy_daemon(self):
self.sort_thread = threading.Thread(target=self.copy)
self.sort_thread.daemon = True
self.sort_thread.start()
def simple_copy(self):
try:
self.list_copy(self.imagefiles(), os.path.join(self.destination, "Pictures"))
self.list_copy(self.audiofiles(), os.path.join(self.destination, "Music"))
self.list_copy(self.videofiles(), os.path.join(self.destination, "Videos"))
self.list_copy(self.documentfiles(), os.path.join(self.destination, "Documents"))
self.list_copy(self.programsfiles(), os.path.join(self.destination, "Programs"))
self.list_copy(self.arhivefiles(), os.path.join(self.destination, "Compressed"))
self.list_copy(self.othersfiles(), os.path.join(self.destination, "Others"))
notification.notify(title='File Sorter: Complete',
message='Simple copy files is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def simple_copy_daemon(self):
self.sort_thread = threading.Thread(target=self.simple_copy)
self.sort_thread.daemon = True
self.sort_thread.start()
def copy_by_ext(self):
try:
for homefile in self.allfiles():
file_ext = os.path.split(homefile)[-1]
match = re.search(r'([\w]+)', os.path.splitext(file_ext)[1])
if match:
folder_ext = match.group().upper()
target = os.path.join(self.destination, folder_ext)
if not os.path.exists(target):
os.makedirs(target)
if os.path.exists(os.path.join(target, file_ext)):
n = 1
newfile = file_ext
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(file_ext)[0] + '(' + str(n) + ')' + os.path.splitext(file_ext)[1]
n += 1
shutil.copy2(homefile, os.path.join(target, newfile))
else:
shutil.copy2(homefile, target)
notification.notify(title='File Sorter: Complete',
message='Copy by file extension is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def copy_by_ext_daemon(self):
self.sort_thread = threading.Thread(target=self.copy_by_ext)
self.sort_thread.daemon = True
self.sort_thread.start()
def copy_by_date(self):
try:
for homefile in self.allfiles():
file_date = os.path.split(homefile)[-1]
datestring = time.ctime(os.path.getctime(homefile))
dt = datetime.strptime(datestring, '%a %b %d %H:%M:%S %Y')
folder_date = str(dt.day) + '-' + str(dt.month) + '-' + str(dt.year)
target = os.path.join(self.destination, folder_date)
if not os.path.exists(target):
os.makedirs(target)
if os.path.exists(os.path.join(target, file_date)):
n = 1
newfile = file_date
while os.path.exists(os.path.join(target, newfile)):
newfile = os.path.splitext(file_date)[0] + '(' + str(n) + ')' + os.path.splitext(file_date)[1]
n += 1
shutil.copy2(homefile, os.path.join(target, newfile))
else:
shutil.copy2(homefile, target)
notification.notify(title='File Sorter: Complete',
message='Copy by file date is complete',
app_icon=None, timeout=60,)
except Exception as e:
notification.notify(title='File Sorter: ERROR!',
message=str(e),
app_icon=None, timeout=60,)
def copy_by_date_daemon(self):
self.sort_thread = threading.Thread(target=self.copy_by_date)
self.sort_thread.daemon = True
self.sort_thread.start()