This repository was archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
100 lines (76 loc) · 2.41 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import subprocess
import os
from pprint import pprint
class debug:
def __init__(self, s=True):
self.s = s
def step(self):
if self.s:
input("Step...")
def pp(self, _dict):
pprint(_dict)
self.step()
def pr(self, string):
print(f" </> \x1b[1;3m{string}\x1b[0m")
self.step()
def get_script_dir():
return os.path.dirname(os.path.abspath(__file__))
def msg(message):
print(f" \x1b[35;3m*\x1b[39m {message}\x1b[0m")
def erm(message):
print(f" \x1b[31;1m*\x1b[39m {message}\x1b[0m")
def title(string):
print(f"\x1b[1;4m{string}\x1b[0m")
def display_list(_list):
for item in _list:
print(f"\x1b[3m - {item}\x1b[0m")
def cmd(command, co=False, v=False):
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
if v:
print(result.stdout.strip())
if co:
return result.stdout.strip()
return True
except subprocess.CalledProcessError as e:
erm(f"Command '{command}' failed with error: {e.stderr}")
return False
except Exception as e:
erm(f"An unexpected error occurred: {e}")
return False
def vcmd(command):
try:
subprocess.run(command, shell=True, check=True)
return True
except subprocess.CalledProcessError as e:
erm(f"Command '{command}' failed with error: {e.stderr}")
return False
except Exception as e:
erm(f"An unexpected error occurred: {e}")
return False
def str_to_bool(string):
if string.lower() == "ask":
return "ask"
return string.lower() in ('true', 'yes', '1', 'on', 'enabled')
def prompt(message, default='n'):
match default:
case 'n':
prompt_message = f" \x1b[33;3m*\x1b[39m {message} (y/N)\x1b[0m\n \x1b[33;1;3m>\x1b[0m "
ret = False
case 'y':
prompt_message = f" \x1b[33;3m*\x1b[39m {message} (Y/n)\x1b[0m\n \x1b[33;1;3m>\x1b[0m "
ret = True
case _:
erm(f"Invalid default '{default}' for prompt()")
exit()
while True:
a = input(prompt_message).strip().lower()
match a:
case 'y' | 'yes':
return True
case 'n' | 'no':
return False
case '':
return ret
case _:
erm(f"Invalid input '{a}'")