-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (82 loc) · 2.65 KB
/
main.py
File metadata and controls
94 lines (82 loc) · 2.65 KB
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
import sys
import os
nolog = False
def log(*e):
if not nolog:
print("\n" + "pray:", *e, "\n")
run_status = 0
run_statuses = {
"start": 0,
"success": 1,
"path_parse_error": 2,
"prayer_not_found": 3,
"parse_error": 4
}
try:
args = sys.argv[1:]
cmdname = "default"
cmdargs = ""
i = 0
while len(args) > 0:
item = args[0]
args = args[1:]
if i == 0:
cmdname = item
else:
cmdargs += " " + item
i += 1
curPath = os.getcwd()
if "/" in curPath:
curPath = curPath.split("/")
elif "\\" in curPath:
curPath = curPath.split("\\")
else:
run_status = run_statuses["path_parse_error"]
curPath = []
while len(curPath) > 0:
try:
prayer_path = "/".join(curPath) + "/.prayer"
with open(prayer_path, "r") as file:
lines = file.readlines()
line_num = 0
for line in lines:
line_num += 1
if line == "nolog":
nolog = True
elif line.startswith("#"):
pass
else:
record = line.split(":", maxsplit=1)
if not (len(record) == 2):
log(".prayer contains invalid record on line ", line_num)
run_status = run_statuses["parse_error"]
break
cmd = record[1].lstrip()
if len(cmd) < 1:
log(".prayer contains invalid command on line ", line_num)
run_status = run_statuses["parse_error"]
break
if cmdname == record[0]:
curPath = []
cmd_built = cmd + cmdargs
log("running command:", cmd_built)
os.system(cmd_built)
run_status = run_statuses["success"]
break
except FileNotFoundError:
run_status = run_statuses["prayer_not_found"]
except Exception as e:
log(e)
break
# curPath can be an empty list
if len(curPath) > 0:
curPath.pop()
if not run_status == run_statuses["success"]:
status_text = "?"
for k in run_statuses:
if run_statuses[k] == run_status:
status_text = k
break
log("error:", status_text)
except Exception as error:
log("exception raised:", error)