-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysimpleparser.py
More file actions
134 lines (118 loc) · 3.35 KB
/
mysimpleparser.py
File metadata and controls
134 lines (118 loc) · 3.35 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import re
import shlex #SHLEX AAAAAAAAAAAAA
content = "Test"
def post(val):
global content
if val is not None:
if isinstance(val, list) and len(val) == 1:
content = val[0]
elif not isinstance(val, list):
content = val
else:
print("Recommended to use POST on a single value")
content = val
else:
return "ERROR"
return content
def delete():
global content
content = None
return "OK"
def get():
return content
def head():
value = get()
if value is None:
return "ERROR"
elif value == content:
return "OK"
elif value: # Check if it's not an empty string
return "OK"
return "ERROR"
def put(val):
global content
try:
content = f"FILE: {str(val)}"
return "OK"
except Exception:
return "ERROR"
def connect():
if content is None:
return "ERROR"
else:
return "OK"
def patch(val):
global content
if val is None:
return "ERROR"
if isinstance(val, list):
content += " ".join(val)
else:
content += str(val)
return content
def options():
table = {
"Value": get(),
"Version": "1.0 / SimulateRequests"
}
return table
def parser(code):
if code is None or not isinstance(code, str):
raise Exception("Tried to use parser on a non-string, or non-existent string")
lines = re.split(r"\n", code)
results = []
for line in lines:
line = line.strip()
if not line:
continue
try:
partofcode = shlex.split(line)
if not partofcode:
continue
command = partofcode[0].lower()
args = partofcode[1:]
result = None
if command == "post":
result = post(args[0] if args else None)
elif command == "delete":
result = delete()
elif command == "get":
result = get()
elif command == "head":
result = head()
elif command == "put":
result = put(args[0] if args else None)
elif command == "connect":
result = connect()
elif command == "patch":
result = patch(args[0] if args else None)
elif command == "options":
result = options()
else:
raise Exception(f"No command found: {command}")
if result is not None:
results.append(result)
except ValueError as e:
print(f"Error parsing line '{line}': {e}")
except Exception as e:
print(f"Error executing command on line '{line}': {e}")
if not results:
print("No results.")
else:
for num, res in enumerate(results, start=1): # Start numbering from 1
if isinstance(res, dict):
for key, value in res.items():
print(f"Result {num}: {key} - {value}")
else:
print(f"Result {num}: {res}")
if __name__ == "__main__":
codee = '''
get
patch Hello
post World
get
options
put "My File Content"
get
'''
parser(codee)