-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
95 lines (76 loc) · 2.98 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
93
94
95
"""DieKnow main interface.
"""
import faulthandler
import re
import dieknow
faulthandler.enable()
def main():
"""Main starting point."""
dieknow.validate()
print()
print("DieKnow Shell\n=============")
while True:
user_input = input(">>> ").strip().lower()
if "help" in user_input:
match = re.match(
r"\bhelp\b(?:\s+(\w+))?",
user_input.strip(),
re.IGNORECASE)
if match.group(1):
attr = getattr(dieknow, match.group(1), None)
if attr:
title = "Documentation for the function %s at %s:" % \
(match.group(1), attr)
print(title)
print("=" * len(title), "\n")
print(attr.__doc__)
else:
print("Unknown command!")
else:
print("Welcome to DieKnow's help utility!\n\nType \"help\" "
"and then a function name below to get started!\n\n"
"Ex. for help on the function \"validate\", type \"help "
"validate\".")
attrs = [attr for attr in dir(dieknow) if not attr.startswith("__")]
column_width = (len(attrs) + 1) // 2
left_column = attrs[:column_width]
right_column = attrs[column_width:]
# Print each column side by side
for left, right in zip(
left_column,
right_column + [""] * \
(len(left_column) - len(right_column))):
print(f"{left:<30} {right}")
match user_input:
case "start":
dieknow.start_monitoring(dieknow.folder_path)
case "directory":
executables = dieknow.get_executables_in_folder(
dieknow.folder_path
)
print(f"Files in {dieknow.folder_path.decode('utf-8')}:")
print(executables.decode())
case "count":
killed = dieknow.get_killed_count()
print(f"Executables killed: {killed}")
case "exit":
if dieknow.is_running():
dieknow.stop_monitoring()
break
case _:
if not "help" in user_input:
func = getattr(dieknow, user_input, None)
if func:
try:
if callable(func):
result = func()
else: result = func
if result:
print(result)
else:
print("Invalid input!")
except (TypeError, AttributeError):
pass
else: print("Invalid input!")
if __name__ == "__main__":
main()