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

1""" 

2Pipeline helpers for code generation. 

3 

4This module provides a minimal pipeline abstraction so generation steps can be 

5composed and reused without inflating CodeGenerator.generate. 

6""" 

7 

8from __future__ import annotations 

9 

10from dataclasses import dataclass, field 

11from typing import TYPE_CHECKING, Any, Callable, List, Protocol, Union 

12 

13from formkit_ninja.formkit_schema import FormKitSchema 

14 

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 

17 

18SchemaInput = Union[List[dict], FormKitSchema] 

19 

20 

21@dataclass 

22class GenerationContext: 

23 """State container for code generation steps.""" 

24 

25 schema: SchemaInput 

26 generator: "CodeGenerator" 

27 data: dict[str, Any] = field(default_factory=dict) 

28 

29 

30class GenerationStep(Protocol): 

31 """Protocol for pipeline steps.""" 

32 

33 def run(self, context: GenerationContext) -> None: 

34 """Execute a generation step.""" 

35 

36 

37class CallableStep: 

38 """Adapter to use simple callables as pipeline steps.""" 

39 

40 def __init__(self, func: Callable[[GenerationContext], None]) -> None: 

41 self.func = func 

42 

43 def run(self, context: GenerationContext) -> None: 

44 self.func(context) 

45 

46 

47class GenerationPipeline: 

48 """Execute a list of steps in order.""" 

49 

50 def __init__(self, steps: list[GenerationStep]) -> None: 

51 self.steps = steps 

52 

53 def run(self, context: GenerationContext) -> None: 

54 for step in self.steps: 

55 step.run(context)