Add session-mode code generation that produces a reusable WorkflowSession class instead of a simple oneshot script. The session mode generates code with an in-class run/close lifecycle for warm ComfyUI reuse. Also refactors render.py to extract shared code sections into common methods, reducing duplication between oneshot and session renderers. Adds WorkflowSessionRuntime with cleanup policies (per_run, session, manual) and reset_every_n_runs support, plus 72 new tests covering session rendering, runtime session, and session export pipeline.
60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
from typing import TextIO
|
|
|
|
from .app import ExportApplication
|
|
from .cli import DEFAULT_INPUT_FILE, DEFAULT_OUTPUT_FILE, DEFAULT_QUEUE_SIZE, main
|
|
from .node_runtime import get_node_class_mappings, import_custom_nodes
|
|
from .runtime_session import WorkflowSession
|
|
|
|
|
|
class ComfyUItoPython:
|
|
"""Public compatibility facade for the exporter package."""
|
|
|
|
def __init__(
|
|
self,
|
|
workflow: str = "",
|
|
frontend_workflow: str | dict | None = None,
|
|
input_file: str = "",
|
|
output_file: str | TextIO = "",
|
|
queue_size: int = 1,
|
|
node_class_mappings: dict | None = None,
|
|
needs_init_custom_nodes: bool = False,
|
|
execution_mode: str = "oneshot",
|
|
):
|
|
self._app = ExportApplication(
|
|
workflow=workflow,
|
|
frontend_workflow=frontend_workflow,
|
|
input_file=input_file,
|
|
output_file=output_file,
|
|
queue_size=queue_size,
|
|
node_class_mappings=node_class_mappings,
|
|
needs_init_custom_nodes=needs_init_custom_nodes,
|
|
execution_mode=execution_mode,
|
|
node_mapping_loader=get_node_class_mappings,
|
|
custom_node_importer=import_custom_nodes,
|
|
)
|
|
self._app.execute()
|
|
|
|
|
|
def run(
|
|
input_file: str = DEFAULT_INPUT_FILE,
|
|
output_file: str = DEFAULT_OUTPUT_FILE,
|
|
queue_size: int = DEFAULT_QUEUE_SIZE,
|
|
) -> None:
|
|
"""Generate Python code from a ComfyUI workflow_api.json file."""
|
|
ComfyUItoPython(
|
|
input_file=input_file,
|
|
output_file=output_file,
|
|
queue_size=queue_size,
|
|
needs_init_custom_nodes=True,
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
"ComfyUItoPython",
|
|
"WorkflowSession",
|
|
"run",
|
|
"main",
|
|
"get_node_class_mappings",
|
|
"import_custom_nodes",
|
|
]
|