|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import ast |
16 | | - |
17 | | -from parser.context import ParseContext |
18 | | -from parser.parser.utils.ast_utils import collect_function_defs |
19 | | -from parser.resolution.resolution_engine import ResolutionEngine |
| 15 | +from parser.entrypoint.common import detect_format_from_content |
| 16 | +from parser.entrypoint.python_runner import parse_python_launch_file |
| 17 | +from parser.entrypoint.xml_runner import parse_xml_launch_file |
20 | 18 |
|
21 | 19 |
|
22 | 20 | def parse_launch_file(filepath: str) -> dict: |
23 | | - """ |
24 | | - Entrypoint: parses a launch file and returns structured output |
25 | | - Detects LaunchDescription([...]) or ld.add_action(...) usage. |
26 | | - """ |
27 | 21 | with open(filepath, "r", encoding="utf-8") as f: |
28 | 22 | code = f.read() |
29 | 23 |
|
30 | | - tree = ast.parse(code, filename=filepath) |
31 | | - |
32 | | - # Set up shared context and resolution engine |
33 | | - context = ParseContext() |
34 | | - context.current_file = filepath |
35 | | - engine = ResolutionEngine(context) |
36 | | - context.engine = engine |
37 | | - |
38 | | - parsed = [] |
39 | | - |
40 | | - collect_function_defs(tree.body, context) |
41 | | - |
42 | | - # Simulate top-level execution |
43 | | - for node in tree.body: |
44 | | - if isinstance(node, ast.Assign): |
45 | | - engine.resolve(node) |
46 | | - |
47 | | - elif isinstance(node, ast.Expr): |
48 | | - engine.resolve(node) |
49 | | - |
50 | | - # Now extract and run generate_launch_description |
51 | | - main_fn = context.lookup_function("generate_launch_description") |
52 | | - if not main_fn: |
53 | | - raise ValueError("No generate_launch_description() function found.") |
54 | | - |
55 | | - parsed.extend(_parse_launch_function_body(main_fn.body, context, engine)) |
56 | | - |
57 | | - return { |
58 | | - "file": filepath, |
59 | | - "parsed": parsed, |
60 | | - "used_launch_config": sorted(context.introspection.used_launch_configs), |
61 | | - "declared_arguments": sorted(context.introspection.declared_launch_args.keys()), |
62 | | - "undeclared_launch_configurations": sorted( |
63 | | - context.introspection.get_undeclared_launch_configs() |
64 | | - ), |
65 | | - "environment_variables": context.introspection.get_environment_variables(), |
66 | | - "python_expressions": context.introspection.get_python_expressions(), |
67 | | - "composable_containers": context.get_composable_node_groups(), |
68 | | - "additional_components": context.introspection.get_registered_entities(), |
69 | | - } |
70 | | - |
71 | | - |
72 | | -def _parse_launch_function_body( |
73 | | - body: list[ast.stmt], context: ParseContext, engine: ResolutionEngine |
74 | | -) -> list: |
75 | | - parsed = [] |
76 | | - for stmt in body: |
77 | | - if isinstance(stmt, ast.Assign): |
78 | | - engine.resolve(stmt) |
79 | | - |
80 | | - elif isinstance(stmt, ast.If): |
81 | | - engine.resolve(stmt) |
82 | | - |
83 | | - elif isinstance(stmt, ast.Expr): |
84 | | - func_name = context.get_func_name(stmt.value.func) |
85 | | - if func_name.endswith("add_action"): |
86 | | - arg = stmt.value.args[0] |
87 | | - result = engine.resolve(arg) |
88 | | - if result: |
89 | | - parsed.append(result) |
90 | | - else: |
91 | | - engine.resolve(stmt) |
92 | | - |
93 | | - elif isinstance(stmt, ast.Return) and isinstance(stmt.value, ast.Call): |
94 | | - resolved = engine.resolve(stmt.value) |
95 | | - if isinstance(resolved, list): |
96 | | - parsed.extend(resolved) |
97 | | - elif resolved is not None: |
98 | | - parsed.append(resolved) |
| 24 | + kind = detect_format_from_content(code) |
99 | 25 |
|
100 | | - return parsed |
| 26 | + if kind == "xml": |
| 27 | + return parse_xml_launch_file(filepath) |
| 28 | + return parse_python_launch_file(filepath) |
0 commit comments