Skip to content

Commit 9f140fd

Browse files
authored
Merge pull request #216 from JdeRobot/ros-native
ROS 2 Native Support for Robotics Academy Exercises
2 parents eba0ec4 + 76bcead commit 9f140fd

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

manager/manager/lint/linter.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,38 @@ def evaluate_code(
105105
code = re.sub(r"from MAP import MAP", "from map import MAP", code)
106106
code = re.sub(r"\nimport cv2\n", "\nfrom cv2 import cv2\n", code)
107107

108-
# Avoids EOF error when iterative code is empty
109-
# (which prevents other errors from showing)
110-
while_position = re.search(
111-
r"[^ ]while\s*\(\s*True\s*\)\s*:|[^ ]while\s*True\s*:|[^ ]while\s*1\s*:|[^ ]while\s*\(\s*1\s*\)\s*:",
112-
code,
108+
# Avoids EOF error when iterative code is empty (which prevents other errors from showing)
109+
loop_regex = (
110+
r"[^ ]while\s*\(\s*True\s*\)\s*:|"
111+
r"[^ ]while\s*True\s*:|"
112+
r"[^ ]while\s*1\s*:|"
113+
r"[^ ]while\s*\(\s*1\s*\)\s*:|"
114+
r"rclpy\.spin\(\s*\w+\s*\)|"
115+
r"rclpy\.spin_once\(\s*\w+\s*.*?\)|"
116+
r"\.create_rate\(\s*[\w.]+\s*\)"
113117
)
114-
if while_position is None:
115-
while_error = "ERROR: While loop is required and was not found.\n"
116-
return while_error.strip()
117-
sequential_code = code[: while_position.start()]
118-
iterative_code = code[while_position.start() :]
119-
iterative_code = re.sub(
120-
r"[^ ]while\s*\(\s*True\s*\)\s*:|[^ ]while\s*True\s*:|[^ ]while\s*1\s*:|[^ ]while\s*\(\s*1\s*\)\s*:",
121-
"\n",
122-
iterative_code,
123-
1,
124-
)
125-
iterative_code = re.sub(r"^[ ]{4}", "", iterative_code, flags=re.M)
126-
code = sequential_code + iterative_code
127-
128-
f = open("user_code.py", "w")
129-
f.write(code)
130-
f.close()
118+
loop_match = re.search(loop_regex, code)
119+
120+
if loop_match is None:
121+
return "ERROR: No event loop found. Add 'while True:', 'rclpy.spin(node)', or 'rclpy.spin_once(node)' or use 'Rate'"
122+
123+
if (
124+
"rclpy.spin" in loop_match.group()
125+
or "rclpy.spin_once" in loop_match.group()
126+
or ".create_rate" in loop_match.group()
127+
):
128+
# Keep the code as-is; don't modify it
129+
pass
130+
else:
131+
# Modify code for while True (add frequency control)
132+
sequential_code = code[: loop_match.start()]
133+
iterative_code = code[loop_match.start() :]
134+
iterative_code = re.sub(loop_regex, "\n", iterative_code, 1)
135+
iterative_code = re.sub(r"^[ ]{4}", "", iterative_code, flags=re.M)
136+
code = sequential_code + iterative_code
137+
138+
with open("user_code.py", "w") as f:
139+
f.write(code)
131140

132141
command = ""
133142
if "humble" in str(ros_version):

0 commit comments

Comments
 (0)