-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (75 loc) · 2.92 KB
/
main.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
from os import path, listdir, mkdir, rmdir, rename
from typing import Any
import json
import re
## Paths
this_path: str = path.dirname(path.realpath(__file__))
sort_path: str = path.join(path.expanduser("~"), "Downloads")
## Funções
# Pegar um dicionário de um json
def json_to_dict(file_path: str) -> dict:
with open(file_path, "r", encoding="utf8") as json_file:
return json.load(json_file)
# Pegar a chave por um valor em um dicionário
def get_dict_key(dict: dict, value_to_get: Any) -> Any:
key: list = [key for key, value in dict.items() if value == value_to_get]
return key[0]
# Seleciona em qual folder o arquivo ten que ir dependendo da extensão dele
def correct_folder(json_dict: dict, file_ext: str, file_types: dict) -> str:
for folder in json_dict["folders"].values():
if folder != "Others":
if file_ext.lower() in file_types[folder]:
return folder
return "Others"
# Se o arquivo existir, o novo nome dele vai ter um (num) no final
def dont_overwrite(file_name: str, file_ext: str) -> str:
pattern: str = r"\((\d+)\)"
found: list = re.findall(pattern, file_name)
if found:
number = int(found[-1])
return re.sub(pattern, f"({number + 1})", file_name) + file_ext
return f"{file_name} (1){file_ext}"
def main() -> None:
## Definindo variáveis
file_types: dict = json_to_dict(path.join(this_path, "file_types.json"))
preset: dict = json_to_dict(path.join(this_path, "preset.json"))
p_folders: dict = preset["folders"]
files: list = listdir(sort_path)
move_to: dict = {}
destination: str = ""
## Associar nomes das pastas aos tipos
for file in files:
if file not in preset["ignore"]:
if file not in p_folders:
if path.isfile(path.join(sort_path, file)):
extension: str = path.splitext(file)[-1]
folder_to_go: str = correct_folder(preset, extension, file_types)
destination: str = path.join(
sort_path, get_dict_key(p_folders, folder_to_go), file
)
else:
if file not in p_folders.keys():
destination: str = path.join(
sort_path, get_dict_key(p_folders, "Others"), file
)
while path.exists(destination):
destination: str = path.join(
path.dirname(destination), dont_overwrite(*path.splitext(destination))
)
move_to[file] = destination
# Mover os arquivos (e criar as pastas caso elas não existam)
for file in move_to:
prev_path: str = path.join(sort_path, file)
next_path: str = move_to[file]
parent_dir: str = path.dirname(move_to[file])
if not path.exists(parent_dir):
mkdir(parent_dir)
rename(prev_path, next_path)
# Excluir as pastas que não tem nenhum arquivo dentro
for folder in p_folders:
folder_path: str = path.join(sort_path, folder)
if path.exists(folder_path):
if listdir(folder_path) == []:
rmdir(folder_path)
if __name__ == "__main__":
main()