-
Notifications
You must be signed in to change notification settings - Fork 1
/
easydos.py
69 lines (58 loc) · 2.02 KB
/
easydos.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
import inspect
import os
def parse(cmd):
command = cmd.split(" ")[0]
args = " ".join(cmd.split(" ")[1:])
return command, args
def _donothing():
return
class Application:
def __init__(self):
self.commands = {}
self.environment = {"cwdir": os.getcwd(), "echo": True}
self.onerror = _donothing
self.base = os.getcwd()
def set(self, name, value):
self.environment[name] = value
def error(self, fun):
self.onerror = fun
return fun
def command(self, name):
def f(fun):
self.commands[name] = fun
return fun
return f
def run(self):
while True:
try:
directory = "C:" + self.environment["cwdir"][len(self.base):] if self.environment["echo"] else ""
cmd = input(directory + r"> ")
parts = cmd.split(" | ")
carryover = ""
for i in parts:
command, args = parse(i)
if (args != "") or (carryover == ""):
args += carryover
else:
args = carryover[1:]
if command in self.commands:
handler = self.commands[command]
b = ""
if len(inspect.signature(handler).parameters) == 1:
b = handler(args)
else:
b = handler(args, self.environment)
if b is None:
b = ""
carryover = " " + b
print(b)
else:
print("Command not found")
break
except KeyboardInterrupt:
break
except:
if len(inspect.signature(self.onerror).parameters) == 0:
self.onerror()
else:
self.onerror(self.environment)