|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Build a JSON mapping of component class names to their module paths. |
| 4 | +
|
| 5 | +Scans the lfx/components directory and creates a mapping file. |
| 6 | +""" |
| 7 | + |
| 8 | +import ast |
| 9 | +import json |
| 10 | +import os |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def find_component_classes(directory: str) -> dict[str, str]: |
| 15 | + """Scan directory for component classes and build mapping. |
| 16 | + |
| 17 | + Args: |
| 18 | + directory: Root directory to scan (e.g., lfx/src/lfx/components) |
| 19 | + |
| 20 | + Returns: |
| 21 | + Dictionary mapping class names to module paths |
| 22 | + """ |
| 23 | + mapping = {} |
| 24 | + components_dir = Path(directory) |
| 25 | + |
| 26 | + if not components_dir.exists(): |
| 27 | + print(f"Warning: Components directory not found: {directory}") |
| 28 | + return mapping |
| 29 | + |
| 30 | + # Walk through all Python files in components directory |
| 31 | + for py_file in components_dir.rglob("*.py"): |
| 32 | + # Skip __init__.py and __pycache__ |
| 33 | + if py_file.name.startswith("__") or "__pycache__" in str(py_file): |
| 34 | + continue |
| 35 | + |
| 36 | + try: |
| 37 | + # Read and parse the file |
| 38 | + with open(py_file, 'r', encoding='utf-8') as f: |
| 39 | + content = f.read() |
| 40 | + |
| 41 | + # Parse AST to find component classes |
| 42 | + tree = ast.parse(content, filename=str(py_file)) |
| 43 | + |
| 44 | + for node in ast.walk(tree): |
| 45 | + if isinstance(node, ast.ClassDef): |
| 46 | + # Check if it's a component class (ends with Component or inherits from Component) |
| 47 | + class_name = node.name |
| 48 | + if class_name.endswith("Component") or any( |
| 49 | + base.id == "Component" or base.id.endswith("Component") |
| 50 | + for base in node.bases |
| 51 | + if isinstance(base, ast.Name) |
| 52 | + ): |
| 53 | + # Build module path from file path |
| 54 | + # e.g., lfx/src/lfx/components/input_output/text.py |
| 55 | + # -> lfx.components.input_output.text |
| 56 | + rel_path = py_file.relative_to(components_dir.parent.parent) |
| 57 | + module_parts = list(rel_path.parts[:-1]) + [rel_path.stem] |
| 58 | + module_path = ".".join(module_parts) |
| 59 | + |
| 60 | + # Only add if not already in mapping (first occurrence wins) |
| 61 | + if class_name not in mapping: |
| 62 | + mapping[class_name] = module_path |
| 63 | + print(f"Found: {class_name} -> {module_path}") |
| 64 | + |
| 65 | + except (SyntaxError, UnicodeDecodeError) as e: |
| 66 | + print(f"Warning: Could not parse {py_file}: {e}") |
| 67 | + continue |
| 68 | + except Exception as e: |
| 69 | + print(f"Error processing {py_file}: {e}") |
| 70 | + continue |
| 71 | + |
| 72 | + return mapping |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + """Main function to build component mapping.""" |
| 77 | + # Get the script directory |
| 78 | + script_dir = Path(__file__).parent |
| 79 | + |
| 80 | + # Try to find lfx components directory |
| 81 | + lfx_paths = [ |
| 82 | + script_dir / "lfx" / "src" / "lfx" / "components", |
| 83 | + script_dir.parent / "app" / "src" / "lfx" / "src" / "lfx" / "components", |
| 84 | + ] |
| 85 | + |
| 86 | + components_dir = None |
| 87 | + for path in lfx_paths: |
| 88 | + if path.exists(): |
| 89 | + components_dir = path |
| 90 | + break |
| 91 | + |
| 92 | + if not components_dir: |
| 93 | + print("Error: Could not find lfx/components directory") |
| 94 | + print(f"Tried: {lfx_paths}") |
| 95 | + return 1 |
| 96 | + |
| 97 | + print(f"Scanning components directory: {components_dir}") |
| 98 | + mapping = find_component_classes(str(components_dir)) |
| 99 | + |
| 100 | + # Save to JSON file |
| 101 | + output_file = script_dir / "components.json" |
| 102 | + with open(output_file, 'w') as f: |
| 103 | + json.dump(mapping, f, indent=2, sort_keys=True) |
| 104 | + |
| 105 | + print(f"\n✅ Created component mapping: {output_file}") |
| 106 | + print(f" Found {len(mapping)} components") |
| 107 | + |
| 108 | + return 0 |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + exit(main()) |
| 113 | + |
0 commit comments