Coverage for formkit_ninja / parser / generation_pipeline.py: 68.97%
25 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-20 04:40 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-20 04:40 +0000
1"""
2Pipeline helpers for code generation.
4This module provides a minimal pipeline abstraction so generation steps can be
5composed and reused without inflating CodeGenerator.generate.
6"""
8from __future__ import annotations
10from dataclasses import dataclass, field
11from typing import TYPE_CHECKING, Any, Callable, List, Protocol, Union
13from formkit_ninja.formkit_schema import FormKitSchema
15if TYPE_CHECKING: 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true
16 from formkit_ninja.parser.generator import CodeGenerator
18SchemaInput = Union[List[dict], FormKitSchema]
21@dataclass
22class GenerationContext:
23 """State container for code generation steps."""
25 schema: SchemaInput
26 generator: "CodeGenerator"
27 data: dict[str, Any] = field(default_factory=dict)
30class GenerationStep(Protocol):
31 """Protocol for pipeline steps."""
33 def run(self, context: GenerationContext) -> None:
34 """Execute a generation step."""
37class CallableStep:
38 """Adapter to use simple callables as pipeline steps."""
40 def __init__(self, func: Callable[[GenerationContext], None]) -> None:
41 self.func = func
43 def run(self, context: GenerationContext) -> None:
44 self.func(context)
47class GenerationPipeline:
48 """Execute a list of steps in order."""
50 def __init__(self, steps: list[GenerationStep]) -> None:
51 self.steps = steps
53 def run(self, context: GenerationContext) -> None:
54 for step in self.steps:
55 step.run(context)