From 4ed1ddad3358b82431a609be4d2d4d8e32ed1e05 Mon Sep 17 00:00:00 2001 From: donnyjang Date: Mon, 5 Jan 2026 14:49:28 -0700 Subject: [PATCH] updated schema generator to format the yaml files better, confirmed cli still works --- cli.py | 43 +++++++++++++++++++++++++++++++++++++++++-- schema_generator.py | 44 ++++++++++++++++++++++++++++++++++++++++++-- workflows.py | 12 +++++++++++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/cli.py b/cli.py index 2b64053..f4b2fa5 100644 --- a/cli.py +++ b/cli.py @@ -36,6 +36,45 @@ def find_workflow_files(workflows_dir: Path) -> List[Path]: return sorted(workflow_files) +def format_schema_yaml(schema: dict) -> str: + """Format schema as nicely-formatted YAML string (matches write_yaml output).""" + lines = [] + + # Metadata section + lines.append(f"name: {schema['name']}") + lines.append(f"description: {schema.get('description', '')}") + lines.append(f"category: {schema.get('category', 'Uncategorized')}") + + # Model types + model_types = schema.get('model_types', []) + if model_types: + lines.append("model_types:") + for mt in model_types: + lines.append(f" - {mt}") + else: + lines.append("model_types: []") + + # Double newline before parameters + lines.append("") + lines.append("parameters:") + + # Format each parameter with a blank line between them + parameters = schema.get('parameters', []) + for i, param in enumerate(parameters): + # Add blank line between parameters (not before first one) + if i > 0: + lines.append("") + + # Dump the parameter to YAML and indent it + param_yaml = yaml.dump([param], default_flow_style=False, allow_unicode=True, sort_keys=False) + # Remove the leading "- " and adjust indentation + param_lines = param_yaml.strip().split('\n') + for line in param_lines: + lines.append(f" {line}") + + return '\n'.join(lines) + + def cmd_generate(args: argparse.Namespace) -> int: """Generate YAML schema(s) from workflow files.""" workflows_dir = get_workflows_dir() @@ -55,7 +94,7 @@ def cmd_generate(args: argparse.Namespace) -> int: print(f"{action}: {yaml_path.name}") if args.dry_run: - print(yaml.dump(schema, default_flow_style=False, sort_keys=False)) + print(format_schema_yaml(schema)) print("-" * 40) except Exception as e: print(f"Error processing {wf_path.name}: {e}") @@ -75,7 +114,7 @@ def cmd_generate(args: argparse.Namespace) -> int: if args.dry_run: print(f"Would generate: {yaml_path.name}") - print(yaml.dump(schema, default_flow_style=False, sort_keys=False)) + print(format_schema_yaml(schema)) else: print(f"Generated: {yaml_path.name}") diff --git a/schema_generator.py b/schema_generator.py index adf2bfe..b0c45f4 100644 --- a/schema_generator.py +++ b/schema_generator.py @@ -350,7 +350,7 @@ def load_existing_yaml(yaml_path: Path) -> Optional[Dict[str, Any]]: def write_yaml(schema: Dict[str, Any], yaml_path: Path) -> None: - """Write schema to YAML file.""" + """Write schema to YAML file with nice formatting.""" # Custom representer to handle empty lists nicely def represent_list(dumper, data): if len(data) == 0: @@ -359,8 +359,48 @@ def write_yaml(schema: Dict[str, Any], yaml_path: Path) -> None: yaml.add_representer(list, represent_list) + # Build YAML in parts for better formatting + lines = [] + + # Metadata section + lines.append(f"name: {schema['name']}") + lines.append(f"description: {schema.get('description', '')}") + lines.append(f"category: {schema.get('category', 'Uncategorized')}") + + # Model types + model_types = schema.get('model_types', []) + if model_types: + lines.append("model_types:") + for mt in model_types: + lines.append(f" - {mt}") + else: + lines.append("model_types: []") + + # Double newline before parameters + lines.append("") + lines.append("parameters:") + + # Format each parameter with a blank line between them + parameters = schema.get('parameters', []) + for i, param in enumerate(parameters): + # Add blank line between parameters (not before first one) + if i > 0: + lines.append("") + + # Dump the parameter to YAML and indent it + param_yaml = yaml.dump([param], default_flow_style=False, allow_unicode=True, sort_keys=False) + # Remove the leading "- " and adjust indentation + param_lines = param_yaml.strip().split('\n') + for j, line in enumerate(param_lines): + if j == 0: + # First line starts with "- " + lines.append(f" {line}") + else: + # Subsequent lines need proper indentation + lines.append(f" {line}") + with open(yaml_path, "w", encoding="utf-8") as f: - yaml.dump(schema, f, default_flow_style=False, allow_unicode=True, sort_keys=False) + f.write('\n'.join(lines) + '\n') def generate_workflow_schema( diff --git a/workflows.py b/workflows.py index cb1af56..d50a9ae 100644 --- a/workflows.py +++ b/workflows.py @@ -13,10 +13,16 @@ from pathlib import Path from typing import Callable, Dict, List, Optional # Support both relative and absolute imports +# Resources module requires ComfyUI context +resources = None try: from . import resources except ImportError: - import resources + try: + import resources + except ImportError: + # Resources not available (running outside ComfyUI) + pass # Cache for discovered workflows (refreshed on each list_workflows call) _workflow_cache: Dict[str, dict] = {} @@ -77,7 +83,11 @@ def ensure_yaml_schema(workflow_name: str, module) -> Optional[Dict]: def get_options_resolver() -> Dict[str, Callable[[], List[Dict]]]: """ Get mapping from options_source to resolver functions. + Returns empty dict if resources module not available. """ + if resources is None: + return {} + return { # Enums "samplers": resources.get_samplers,