-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuppaal_variable_modifier.py
79 lines (61 loc) · 2.43 KB
/
uppaal_variable_modifier.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
78
79
import os
import pathlib
def modify_variables(file_path, variable_dict: dict[str, str]):
# open file with read write permissions
with open(file_path, "r") as file:
# read all lines
lines = file.readlines()
changed_lines = []
# iterate over all lines
# lines marked with MARK: are the lines one above those that a variable
for i, line in enumerate(lines):
# check if line contains MARK:
if "MARK:" in line:
# iterate over all variables
for key, new_line in variable_dict.items():
# check if variable is in line
if key.upper() in line:
# save the line that will be changed
changed_lines.append((i+1, lines[i+1]))
# update variable
lines[i + 1] = f"{new_line}\n"
break
with open(file_path, "w") as file:
file.writelines(lines)
return changed_lines
def reset_variables(file_path, changed_lines):
with open(file_path, "r") as file:
lines = file.readlines()
for line_number, line in changed_lines:
lines[line_number] = line
with open(file_path, "w") as file:
file.writelines(lines)
def add_new_line_under_marks(file_path, new_lines:dict[str, str]):
with open(file_path, "r") as file:
lines = file.readlines()
added_lines = []
for i, line in enumerate(lines):
if "MARK:" in line:
for key, value in new_lines.items():
if key.upper() in line:
lines.insert(i+1, f"{value}\n")
added_lines.append((i+1, value))
with open(file_path, "w") as file:
file.writelines(lines)
return added_lines
def remove_changed_lines(file_path, changed_lines):
with open(file_path, "r") as file:
lines = file.readlines()
for line_number, _ in changed_lines:
# remove element line_number from lines
lines.pop(line_number)
with open(file_path, "w") as file:
file.writelines(lines)
if __name__ == "__main__":
# define variables
variableToChange = {"psk": "bool psk[N] = {true};",
"requestInterval": "const int requestInterval = 40000;"}
path = pathlib.Path("./Models/NoHeartbeat.xml")
cl = modify_variables(path, variableToChange)
print(cl)
reset_variables(path, cl)