# utils/script_registry.py from typing import Dict, Callable, List, Optional import importlib import inspect import os from pathlib import Path from config.profile_manager import Profile, ProfileManager class ScriptRegistry: """Registry for script operations""" def __init__(self): self.operations: Dict[str, Callable] = {} self.descriptions: Dict[str, str] = {} def register(self, name: str, operation: Callable, description: str = ""): """Register a new operation""" self.operations[name] = operation self.descriptions[name] = description def auto_discover(self, scripts_dir: str = "scripts"): """Auto-discover scripts in the scripts directory""" scripts_path = Path(__file__).parent.parent / scripts_dir for file in scripts_path.glob("script_*.py"): module_name = f"{scripts_dir}.{file.stem}" try: module = importlib.import_module(module_name) # Look for main function and docstring if hasattr(module, 'main'): name = file.stem.replace('script_', '') description = module.__doc__ or "" self.register(name, module.main, description) except Exception as e: print(f"Error loading script {file}: {e}") def get_operations(self) -> List[tuple]: """Get list of available operations""" return [(name, self.descriptions[name]) for name in self.operations] def run_operation(self, name: str, profile: Optional[Profile] = None, **kwargs): """ Run a registered operation Args: name: Name of the operation to run profile: Current profile instance (optional) **kwargs: Additional arguments for the operation """ if name in self.operations: # Prepare arguments operation = self.operations[name] sig = inspect.signature(operation) # Check if operation accepts profile parameter call_args = {} if 'profile' in sig.parameters: call_args['profile'] = profile # Add other kwargs that match the signature for param_name in sig.parameters: if param_name in kwargs: call_args[param_name] = kwargs[param_name] return operation(**call_args) raise ValueError(f"Unknown operation: {name}")