diff --git a/__main__.py b/__main__.py index f4eb231..cbea04b 100644 --- a/__main__.py +++ b/__main__.py @@ -3,11 +3,12 @@ Entry point for running external-api as a module. Usage: python -m external-api generate --all - python -m external-api generate text2img - python -m external-api generate text2img --dry-run + python -m external-api generate basic_sdxl + python -m external-api generate basic_sdxl --dry-run python -m external-api validate --all - python -m external-api validate text2img + python -m external-api validate basic_sdxl python -m external-api list + python -m external-api watch # requires: pip install watchdog """ import sys from .cli import main diff --git a/cli.py b/cli.py index f4b2fa5..af63639 100644 --- a/cli.py +++ b/cli.py @@ -180,6 +180,87 @@ def cmd_list(args: argparse.Namespace) -> int: return 0 +def cmd_watch(args: argparse.Namespace) -> int: + """Watch for changes and auto-regenerate YAML schemas.""" + try: + from watchdog.observers import Observer + from watchdog.events import FileSystemEventHandler, FileModifiedEvent, FileCreatedEvent + except ImportError: + print("Error: watchdog not installed.") + print("Install it with: pip install watchdog") + return 1 + + workflows_dir = get_workflows_dir() + + class WorkflowChangeHandler(FileSystemEventHandler): + def __init__(self): + self.last_modified = {} + + def on_modified(self, event): + self._handle_change(event) + + def on_created(self, event): + self._handle_change(event) + + def _handle_change(self, event): + if event.is_directory: + return + + src_path = Path(event.src_path) + + # Only process .py files + if src_path.suffix != ".py": + return + + # Skip special files + if src_path.name.startswith("_") or src_path.name == "base.py": + return + + # Debounce: skip if modified within last second + import time + now = time.time() + last = self.last_modified.get(str(src_path), 0) + if now - last < 1.0: + return + self.last_modified[str(src_path)] = now + + # Regenerate YAML + print(f"\n[watch] Detected change: {src_path.name}") + try: + schema = generate_workflow_schema(src_path) + yaml_path = src_path.with_suffix(".yaml") + print(f"[watch] Regenerated: {yaml_path.name}") + except Exception as e: + print(f"[watch] Error: {e}") + + print(f"Watching for changes in: {workflows_dir}") + print("Press Ctrl+C to stop.\n") + + # List current workflows + workflow_files = find_workflow_files(workflows_dir) + for wf_path in workflow_files: + yaml_path = wf_path.with_suffix(".yaml") + status = "✓" if yaml_path.exists() else "✗" + print(f" {status} {wf_path.stem}") + print() + + event_handler = WorkflowChangeHandler() + observer = Observer() + observer.schedule(event_handler, str(workflows_dir), recursive=False) + observer.start() + + try: + import time + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + print("\n[watch] Stopped.") + + observer.join() + return 0 + + def main(argv: Optional[List[str]] = None) -> int: """Main CLI entry point.""" parser = argparse.ArgumentParser( @@ -235,6 +316,13 @@ def main(argv: Optional[List[str]] = None) -> int: ) list_parser.set_defaults(func=cmd_list) + # watch command + watch_parser = subparsers.add_parser( + "watch", + help="Watch for changes and auto-regenerate YAML (requires: pip install watchdog)" + ) + watch_parser.set_defaults(func=cmd_watch) + args = parser.parse_args(argv) if args.command is None: