|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to generate .pyi stub files for the synchronous API wrappers. |
| 4 | +This allows generating stubs without running the full ComfyUI application. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import logging |
| 10 | +import importlib |
| 11 | + |
| 12 | +# Add ComfyUI to path so we can import modules |
| 13 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | + |
| 15 | +from comfy_api.internal.async_to_sync import AsyncToSyncConverter |
| 16 | +from comfy_api.version_list import supported_versions |
| 17 | + |
| 18 | + |
| 19 | +def generate_stubs_for_module(module_name: str) -> None: |
| 20 | + """Generate stub files for a specific module that exports ComfyAPI and ComfyAPISync.""" |
| 21 | + try: |
| 22 | + # Import the module |
| 23 | + module = importlib.import_module(module_name) |
| 24 | + |
| 25 | + # Check if module has ComfyAPISync (the sync wrapper) |
| 26 | + if hasattr(module, "ComfyAPISync"): |
| 27 | + # Module already has a sync class |
| 28 | + api_class = getattr(module, "ComfyAPI", None) |
| 29 | + sync_class = getattr(module, "ComfyAPISync") |
| 30 | + |
| 31 | + if api_class: |
| 32 | + # Generate the stub file |
| 33 | + AsyncToSyncConverter.generate_stub_file(api_class, sync_class) |
| 34 | + logging.info(f"Generated stub file for {module_name}") |
| 35 | + else: |
| 36 | + logging.warning( |
| 37 | + f"Module {module_name} has ComfyAPISync but no ComfyAPI" |
| 38 | + ) |
| 39 | + |
| 40 | + elif hasattr(module, "ComfyAPI"): |
| 41 | + # Module only has async API, need to create sync wrapper first |
| 42 | + from comfy_api.internal.async_to_sync import create_sync_class |
| 43 | + |
| 44 | + api_class = getattr(module, "ComfyAPI") |
| 45 | + sync_class = create_sync_class(api_class) |
| 46 | + |
| 47 | + # Generate the stub file |
| 48 | + AsyncToSyncConverter.generate_stub_file(api_class, sync_class) |
| 49 | + logging.info(f"Generated stub file for {module_name}") |
| 50 | + else: |
| 51 | + logging.warning( |
| 52 | + f"Module {module_name} does not export ComfyAPI or ComfyAPISync" |
| 53 | + ) |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + logging.error(f"Failed to generate stub for {module_name}: {e}") |
| 57 | + import traceback |
| 58 | + |
| 59 | + traceback.print_exc() |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + """Main function to generate all API stub files.""" |
| 64 | + logging.basicConfig(level=logging.INFO) |
| 65 | + |
| 66 | + logging.info("Starting stub generation...") |
| 67 | + |
| 68 | + # Dynamically get module names from supported_versions |
| 69 | + api_modules = [] |
| 70 | + for api_class in supported_versions: |
| 71 | + # Extract module name from the class |
| 72 | + module_name = api_class.__module__ |
| 73 | + if module_name not in api_modules: |
| 74 | + api_modules.append(module_name) |
| 75 | + |
| 76 | + logging.info(f"Found {len(api_modules)} API modules: {api_modules}") |
| 77 | + |
| 78 | + # Generate stubs for each module |
| 79 | + for module_name in api_modules: |
| 80 | + generate_stubs_for_module(module_name) |
| 81 | + |
| 82 | + logging.info("Stub generation complete!") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments