-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_runner.py
55 lines (41 loc) · 1.3 KB
/
cli_runner.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
from sys import stdout, stderr, exit, argv, exc_info
from exceptions import EelExcBaseException, EelExcSystemException
from exceptions import EelExcCommandNotFound, EelExcArgumentError
def try_import_command(name: str):
try:
return getattr(__import__(f"stdlib.{name}"), name)
except ModuleNotFoundError:
raise EelExcCommandNotFound(f"{name} is not defined in Eel")
def main():
if len(argv) == 1:
raise EelExcArgumentError(f"Command name not provided")
command_name = argv[1]
command_args = argv[2:]
command = try_import_command(command_name)
result = getattr(command, command_name)(*command_args)
stdout.write(result.eel_encode_repr())
stdout.flush()
if __name__ == "__main__":
try:
main()
exit(0)
except EelExcSystemException as e:
stderr.write(f"{e.name}: {e}")
stderr.write("\n")
stderr.flush()
stdout.write(f"err@{e.exit_code}")
stdout.flush()
exit(e.exit_code)
except EelExcBaseException as e:
stderr.write(f"{e.name}: {e}\n")
stderr.flush()
stdout.write(f"err@{e.exit_code}")
stdout.flush()
exit(e.exit_code)
except Exception as e:
ex_type, ex_value, ex_traceback = exc_info()
stderr.write(f"python@{ex_type.__name__}: {e}\n")
stderr.flush()
stdout.write(f"err@120")
stdout.flush()
exit(120)