-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariable_detector.py
45 lines (33 loc) · 1.69 KB
/
variable_detector.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
import re
def extract_variables_and_identify_executions(script_text):
variables = dict()
variable_executions = dict()
variable_assignment_pattern = re.compile(r'(\w+)\s*:=\s*"(.*?)"')
variable_execution_pattern = re.compile(r'%\s*(.*?)\s*%')
for line in script_text.splitlines():
variable_match = variable_assignment_pattern.search(line)
if variable_match:
variable_name = variable_match.group(1)
variable_value = variable_match.group(2)
variables[variable_name] = variable_value
execution_match = variable_execution_pattern.search(line)
if execution_match:
executed_variables = execution_match.group(1).split('.')
executed_variables = [var.strip() for var in executed_variables]
variable_executions[line.strip()] = executed_variables
return variables, variable_executions
def replace_executed_variables_with_values(script_text):
variables, variable_executions = extract_variables_and_identify_executions(
script_text)
# Iterate through each line where variables are executed
for line, executed_variables in variable_executions.items():
assigned_values = list()
for var in executed_variables:
# If the variable exists in our dictionary, use its value, if not then keep original value
assigned_value = variables.get(var, var)
assigned_values.append(assigned_value)
full_command = ''.join(assigned_values)
# Replace the original line with the resolved command
script_text = script_text.replace(line, full_command)
# Return the modified script with all variables replaced
return script_text