|
1 |
| -import ast |
2 | 1 | from typing import Self
|
3 | 2 |
|
4 | 3 | import dill
|
@@ -81,14 +80,7 @@ def from_rocket_model(cls, rocket: Rocket) -> Self:
|
81 | 80 |
|
82 | 81 | # Parachutes
|
83 | 82 | for parachute in rocket.parachutes:
|
84 |
| - if cls.check_parachute_trigger( |
85 |
| - trigger_expression := parachute.trigger |
86 |
| - ): |
87 |
| - parachute.trigger = eval( # pylint: disable=eval-used |
88 |
| - trigger_expression, |
89 |
| - {"__builtins__": None}, |
90 |
| - {"apogee": "apogee"}, |
91 |
| - ) |
| 83 | + if cls.check_parachute_trigger(parachute.trigger): |
92 | 84 | rocketpy_parachute = cls.get_rocketpy_parachute(parachute)
|
93 | 85 | rocketpy_rocket.parachutes.append(rocketpy_parachute)
|
94 | 86 | else:
|
@@ -219,67 +211,19 @@ def get_rocketpy_parachute(parachute: Parachute) -> RocketPyParachute:
|
219 | 211 | return rocketpy_parachute
|
220 | 212 |
|
221 | 213 | @staticmethod
|
222 |
| - def check_parachute_trigger(expression) -> bool: |
| 214 | + def check_parachute_trigger(trigger) -> bool: |
223 | 215 | """
|
224 | 216 | Check if the trigger expression is valid.
|
225 | 217 |
|
226 | 218 | Args:
|
227 |
| - expression: str |
| 219 | + trigger: str | float |
228 | 220 |
|
229 | 221 | Returns:
|
230 | 222 | bool: True if the expression is valid, False otherwise.
|
231 | 223 | """
|
232 | 224 |
|
233 |
| - # Parsing the expression into an AST |
234 |
| - try: |
235 |
| - parsed_expression = ast.parse(expression, mode="eval") |
236 |
| - except SyntaxError as e: |
237 |
| - raise InvalidParachuteTrigger( |
238 |
| - f"Invalid expression syntax: {str(e)}" |
239 |
| - ) from None |
240 |
| - |
241 |
| - # Constant case (supported after beta v1) |
242 |
| - if isinstance(parsed_expression.body, ast.Constant): |
| 225 | + if trigger == "apogee": |
243 | 226 | return True
|
244 |
| - # Name case (supported after beta v1) |
245 |
| - if ( |
246 |
| - isinstance(parsed_expression.body, ast.Name) |
247 |
| - and parsed_expression.body.id == "apogee" |
248 |
| - ): |
| 227 | + if isinstance(trigger, (int, float)): |
249 | 228 | return True
|
250 |
| - |
251 |
| - # Validating the expression structure |
252 |
| - if not isinstance(parsed_expression.body, ast.Lambda): |
253 |
| - raise InvalidParachuteTrigger( |
254 |
| - "Invalid expression structure: not a lambda." |
255 |
| - ) from None |
256 |
| - |
257 |
| - lambda_node = parsed_expression.body |
258 |
| - if len(lambda_node.args.args) != 3: |
259 |
| - raise InvalidParachuteTrigger( |
260 |
| - "Invalid expression structure: lambda should have 3 arguments." |
261 |
| - ) from None |
262 |
| - |
263 |
| - if not isinstance(lambda_node.body, ast.Compare): |
264 |
| - try: |
265 |
| - for operand in lambda_node.body.values: |
266 |
| - if not isinstance(operand, ast.Compare): |
267 |
| - raise InvalidParachuteTrigger( |
268 |
| - "Invalid expression structure: not a Compare." |
269 |
| - ) from None |
270 |
| - except AttributeError: |
271 |
| - raise InvalidParachuteTrigger( |
272 |
| - "Invalid expression structure: not a Compare." |
273 |
| - ) from None |
274 |
| - |
275 |
| - # Restricting access to functions or attributes |
276 |
| - for node in ast.walk(lambda_node): |
277 |
| - if isinstance(node, ast.Call): |
278 |
| - raise InvalidParachuteTrigger( |
279 |
| - "Calling functions is not allowed in the expression." |
280 |
| - ) from None |
281 |
| - if isinstance(node, ast.Attribute): |
282 |
| - raise InvalidParachuteTrigger( |
283 |
| - "Accessing attributes is not allowed in the expression." |
284 |
| - ) from None |
285 |
| - return True |
| 229 | + return False |
0 commit comments