-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesscode.py
More file actions
160 lines (132 loc) · 5.53 KB
/
processcode.py
File metadata and controls
160 lines (132 loc) · 5.53 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import ast
import utils
from utils import dbg
def extract_pub_vars_from_module(data_array, rm_line=True):
"""
Extract all pub_* variables from data_array, assign them to utils.py module,
optionally remove the lines, and return the edited code as a string.
Parameters:
data_array: list of strings, each line from DSL
dbg: optional debug function
rm_line: if True, remove lines that set pub_* variables
Returns:
Edited DSL code as a single string.
"""
pub_vars = ["pub_var", "pub_bool", "pub_int", "pub_str", "pub_float", "pub_array", "pub_json"]
# Track indices of lines to remove
lines_to_remove = set()
for i, line in enumerate(data_array):
line_strip = line.strip()
for pub_name in pub_vars:
if line_strip.startswith(pub_name):
try:
key, value = line_strip.split("=", 1)
value = value.strip()
# Evaluate it in the current Python context
# parsed_value = eval(value, globals(), locals()) # safer: maybe restrict locals/globals
# Safe parsing
try:
parsed_value = ast.literal_eval(value)
except Exception:
try:
parsed_value = eval(value, utils.EXEC_CONTEXT, utils.EXEC_CONTEXT)
except Exception as e:
dbg(f"[Error] {value}")
parsed_value = value
# Assign to utils
setattr(utils, key.upper(), parsed_value)
# Assign to utils
setattr(utils, pub_name.upper(), parsed_value)
dbg(f"[{pub_name.upper()}]: {parsed_value}")
if rm_line:
lines_to_remove.add(i)
except Exception as e:
dbg(f"[ERROR parsing {pub_name}]: {e}")
break # stop checking other pub_* for this line
# Remove lines in reverse order to not mess up indices
if rm_line:
for index in sorted(lines_to_remove, reverse=True):
data_array.pop(index)
# Return edited code as single string
return "\n".join(data_array)
import utils
from utils import dbg
import ast
def process_code(code: str):
"""
Process DSL code in correct order:
1. Execute all ASSIGN lines and extract pub_* variables
2. Replace all PUB_* placeholders in remaining lines
3. Remove RM-LINE lines
Returns cleaned DSL code ready for execution.
"""
lines = code.splitlines()
final_lines = []
pub_vars_lower = ["pub_var", "pub_bool", "pub_int", "pub_str",
"pub_float", "pub_array", "pub_json"]
pub_vars_upper = ["PUB_VAR", "PUB_BOOL", "PUB_INT", "PUB_STR",
"PUB_FLOAT", "PUB_ARRAY", "PUB_JSON"]
# Step 1: Assign pub_* variables and execute ASSIGN lines
for raw_line in lines:
line = raw_line.strip()
# Handle pub_* assignments
for pub_name in pub_vars_lower:
if line.startswith(pub_name):
try:
key, value = line.split("=", 1)
value = value.strip()
# Try safe literal_eval first
try:
parsed_value = ast.literal_eval(value)
except:
# fallback to EXEC_CONTEXT eval
parsed_value = eval(value, utils.EXEC_CONTEXT, utils.EXEC_CONTEXT)
# Assign to utils
setattr(utils, key.upper(), parsed_value)
setattr(utils, pub_name.upper(), parsed_value)
dbg(f"[{pub_name.upper()}]: {parsed_value}")
except Exception as e:
dbg(f"[Error parsing {pub_name}]: {e}")
break # only one pub_* per line
# Execute ASSIGN lines
if "ASSIGN" in line:
try:
dbg(f"[Executing] {line}")
exec(line, utils.EXEC_CONTEXT, utils.EXEC_CONTEXT)
except Exception as e:
dbg(f"[Error] {e} \n[Line] {line}")
# Step 2: Replace PUB_* placeholders in all lines
processed_lines = []
for raw_line in lines:
line = raw_line.strip()
for pub_name in pub_vars_upper:
if hasattr(utils, pub_name):
pub_val = getattr(utils, pub_name)
line = line.replace(pub_name, repr(pub_val))
processed_lines.append(line)
# Step 3: Remove RM-LINE lines
for line in processed_lines:
if "RM-LINE" in line:
dbg("[Removing]", line)
continue
final_lines.append(line)
# Return the final code
return "\n".join(final_lines)
import include_file
def assign_debug(is_debug: str):
"""Set utils.DEBUG based on input or fallback to utils.DATA_ARRAY."""
is_debug = (is_debug or "").strip().lower()
if is_debug == "on":
utils.DEBUG = True
elif is_debug == "off":
utils.DEBUG = False
else:
# fallback: check utils.DATA_ARRAY
for line in utils.DATA_ARRAY:
line = line.strip().lower()
if line.startswith("@"):
# debug stamements
if line == "@debug on":
utils.DEBUG = True
elif line == "@debug off":
utils.DEBUG = False