-
Notifications
You must be signed in to change notification settings - Fork 0
/
m6-filesort.py
205 lines (156 loc) · 6.6 KB
/
m6-filesort.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
from pathlib import Path
import argparse, re, shutil
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', required=True, help='Source folder')
# parser.add_argument('-d', '--destination', default='_SORTED')
ARGUMENTS = vars(parser.parse_args())
ARGUMENTS['source'] = Path(ARGUMENTS['source'])
ARGUMENTS['destination'] = ARGUMENTS['source']
# ARGUMENTS['destination'] = Path(ARGUMENTS['destination'])
def main(folder_name: str) -> None:
SORT_FOLDERS_LIST = {
'zip': 'archives',
'gz': 'archives',
'tar': 'archives',
'jpeg': 'images',
'png': 'images',
'jpg': 'images',
'svg': 'images',
'avi': 'video',
'mp4': 'video',
'mov': 'video',
'mkv': 'video',
'doc': 'documents',
'docx': 'documents',
'txt': 'documents',
'pdf': 'documents',
'xlsx': 'documents',
'pptx': 'documents',
'mp3': 'music',
'ogg': 'music',
'wav': 'music',
'amr': 'music',
}
file_list_per_category = {
'archives': [],
'images': [],
'video': [],
'documents': [],
'music': [],
'unknown': [],
}
while True:
try:
is_unknown_sorted = int(input('Master, should I move the files of uknown type? (0 - No, 1 - Yes): '))
except TypeError:
print(f'I do not understand!')
except ValueError:
print(f'I do not understand!')
else:
break
print('STARTED')
try:
file_list_per_category, sort_unknown_list, sort_known_list = dir_walk(folder_name, SORT_FOLDERS_LIST,
file_list_per_category, is_unknown_sorted)
except FileNotFoundError as error:
print(f'Source folder "{ARGUMENTS["source"]}" does not exist: {error} OR output folder' /
'"{ARGUMENTS["source"]}" were removed before finish.')
exit()
# for format, folder in SORT_FOLDERS_LIST.items():
# print('For "{:^10}" < ext: "{:<3}"'.format(folder, format))
print('My master, I have found "{}" files!'.format(', '.join(sort_known_list)))
for item in file_list_per_category:
print('Total files in "{:^10}": #{:<5} | files: {:<20}'.format(item, len(file_list_per_category[item]),
', '.join(file_list_per_category[item])))
if '' in sort_unknown_list:
sort_unknown_list.remove('')
print(' Uknown file types: {:^20}'.format(', '.join(sort_unknown_list)))
print('COMPLETED')
def copy_file(file: Path, folder_path: Path, new_name: str) -> None:
shutil.move(file, folder_path / new_name)
def extract_archive(file: Path, folder_path: Path) -> None:
print(f'EXTRACTING {file} to {folder_path / file.stem}')
shutil.unpack_archive(file, folder_path / file.stem)
def dir_walk(path: Path, SORT_FOLDERS_LIST: dict, file_list_per_category: list, is_unknown_sorted: int) -> tuple:
print(f'WALKING {path}')
sort_unknown_set = set()
sort_known_set = set()
unknown = ''
if path.name in file_list_per_category:
return file_list_per_category, sort_unknown_set, sort_known_set
for item in path.iterdir():
if item.is_dir():
if not any(item.iterdir()):
print(f'REMOVING EMPTY {item}')
item.rmdir()
continue
try:
file_list_per_category, new_sort_unknown_set, new_sort_known_set = dir_walk(item, SORT_FOLDERS_LIST,
file_list_per_category, is_unknown_sorted)
sort_unknown_set.update(new_sort_unknown_set)
sort_known_set.update(new_sort_known_set)
except OSError as error:
print(f'Not possible to perform operation: {error}')
exit()
except ValueError as error:
print(f'Impossible to walk a file: {error}')
exit()
else:
file_list_per_category, unknown, known_ext = sort(item, SORT_FOLDERS_LIST, file_list_per_category,
is_unknown_sorted)
if unknown is not None:
sort_unknown_set.add(unknown)
if known_ext is not None:
sort_known_set.update(known_ext)
if not any(path.iterdir()) and path.is_dir:
print(f'REMOVING EMPTY {path}')
path.rmdir()
return file_list_per_category, sort_unknown_set, sort_known_set
def name_translate(name: str) -> str:
CYRILLIC_SYMBOLS = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяєіїґ'
TRANSLATION = ("a", "b", "v", "g", "d", "e", "e", "j", "z", "i", "j", "k", "l", "m", "n", "o", "p",
"r", "s", "t", "u", "f", "h", "ts", "ch", "sh", "sch", "", "y", "", "e", "yu", "u",
"ja", "je", "ji", "g")
trans_map = {}
for c, l in zip(CYRILLIC_SYMBOLS, TRANSLATION):
trans_map[ord(c)] = l
trans_map[ord(c.upper())] = l.upper()
return name.translate(trans_map)
def normalize(name: str) -> str:
name = re.sub(r'[^a-zA-Z0-9_.]', '_', name)
return name
def sort(file: Path, SORT_FOLDERS_LIST: dict, file_list_per_category: dict, is_unknown_sorted: int) -> tuple:
print(f'SORTING {file}')
unknown = ''
known_ext = set()
try:
new_name = name_translate(file.name)
except TypeError as error:
print(f'Translation error in "{file.name}": {error}')
new_name = normalize(new_name)
suffix = file.suffix[1:]
if suffix.casefold() in SORT_FOLDERS_LIST:
folder_path = ARGUMENTS['destination'] / SORT_FOLDERS_LIST.get(suffix)
known_ext.add(suffix.casefold())
else:
if suffix != '':
unknown = suffix
else:
unknown = '*files_without_suffix'
if is_unknown_sorted:
folder_path = ARGUMENTS['destination'] / 'unknown'
else:
file_list_per_category['unknown'].append(file.name)
return file_list_per_category, unknown.casefold(), known_ext
if SORT_FOLDERS_LIST.get(suffix) == 'archives':
extract_archive(file, folder_path)
file_list_per_category[folder_path.name].append(file.name)
folder_path.mkdir(exist_ok=True, parents=True)
copy_file(file, folder_path, new_name)
return file_list_per_category, unknown.casefold(), known_ext
if __name__ == '__main__':
try:
main(ARGUMENTS['source'])
except:
print('Unexpected exception!')
exit()