-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
81 lines (59 loc) · 2.22 KB
/
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
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
def clear_screen():
os.system("clear")
def path_dnd_input(*prompts, is_dir=False, allow_empty=False):
path = input("\n".join(prompts))
path = path.strip().strip("'")
if allow_empty and path == "":
return path
if is_dir:
if not os.path.exists(path):
raise FileNotFoundError("Podany folder nie istnieje")
if not os.path.isdir(path):
raise NotADirectoryError("Podano plik zamiast folderu")
else:
if not os.path.exists(path):
raise FileNotFoundError("Podany plik nie istnieje")
if os.path.isdir(path):
raise IsADirectoryError("Podano folder zamiast pliku")
return path
def press_enter_to_continue(message):
clear_screen()
print(message)
input("Naciśnij Enter aby kontynuować...")
def multiple_files_input(*prompts, allowed_extensions=None):
allowed_extensions = [] if allowed_extensions is None else allowed_extensions
file_path = None
file_paths = set()
file_paths_in_order = []
while file_path != "":
clear_screen()
print("\n".join(prompts))
if len(file_paths_in_order) > 0:
print("\nDodane pliki:")
for file in file_paths_in_order:
print(f"- {file.split('/')[-1]}")
try:
file_path = path_dnd_input("", "Plik: ", allow_empty=True, is_dir=False)
except KeyboardInterrupt as e:
raise e
except Exception as e:
press_enter_to_continue(str(e))
continue
if file_path == "":
break
if len(allowed_extensions) > 0:
if not any(file_path.endswith(ext) for ext in allowed_extensions):
press_enter_to_continue(
f"Podany plik nie jest obsługiwanym plikiem: {', '.join(allowed_extensions)}"
)
continue
if file_path in file_paths:
press_enter_to_continue("Podany plik został już dodany")
continue
file_paths.add(file_path)
file_paths_in_order.append(file_path)
return file_paths_in_order
def print_special(text):
print("\033[1;7m" + text + "\033[0m")