-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaquelarre_interpreter.py
78 lines (59 loc) · 1.96 KB
/
aquelarre_interpreter.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
from aquelarre_syntax import *
import subprocess
def scripts_table(code: Code) -> dict:
result:dict = {}
for elem in code:
if type(elem) == ScriptDeclaration:
result[elem.script_name.replace(' ', '')] = elem.script.replace(' ', '')
return result
def run_script(name:str,stdin=None) -> str:
result = subprocess.check_output(name,input=stdin, shell = True)
result = result.decode()
result.replace('\n','')
return str(result)
def run_cond(code: Code, cond: Cond, args: List[str],stdin=None) -> str:
args_ = ' '.join(args)
scripts_table_ = scripts_table(code)
if_script = scripts_table_[cond.if_script] + ' ' + args_
response = ''
if cond.json_payload != None:
response = cond.json_payload
else:
then_script = scripts_table_[cond.then_script] + ' ' + args_
response = run_script(then_script,stdin)
# print(run_script(if_script,stdin))
if_script_ouput = run_script(if_script,stdin)
if cond.neg:
if 'true' in if_script_ouput:
return 'false'
else:
return response
else:
if 'true' in if_script_ouput:
return response
else:
return 'false'
def get_params_or_args(path,option) -> List[str]:
result = []
mode = 0
if option == 'params':
mode = 0
if option == 'args':
mode = 1
if '?' in path:
params = path.split('?')
params = params[1]
params = params.split('&')
for pair in params:
result.append(pair.split('=')[mode])
return result
def get_fun(method,url, code) -> Fun:
args = get_params_or_args(url,'args')
funs = list(filter(lambda elm:
type(elm) == Fun, code))
result = list(filter(lambda fun:
fun.method == method and
fun.eval_params(args) == url, funs))
if result == []:
raise Exception('URL and method match not found!')
return result[0]