|
| 1 | +from collections import deque |
1 | 2 | from collections.abc import Awaitable, Callable |
2 | 3 | from dataclasses import dataclass, field |
3 | | -from typing import Any, Self, overload |
| 4 | +from typing import Any, Protocol, Self, overload |
4 | 5 |
|
5 | 6 | from cq._core.dispatcher.base import BaseDispatcher, Dispatcher |
| 7 | +from cq._core.middleware import Middleware |
6 | 8 |
|
7 | 9 | type PipeConverter[I, O] = Callable[[O], Awaitable[I]] |
8 | 10 |
|
9 | 11 |
|
| 12 | +class PipeConverterMethod[I, O](Protocol): |
| 13 | + def __get__( |
| 14 | + self, |
| 15 | + instance: object, |
| 16 | + owner: type | None = ..., |
| 17 | + ) -> PipeConverter[I, O]: ... |
| 18 | + |
| 19 | + |
10 | 20 | @dataclass(repr=False, eq=False, frozen=True, slots=True) |
11 | 21 | class PipeStep[I, O]: |
12 | 22 | converter: PipeConverter[I, O] |
@@ -77,6 +87,110 @@ async def __execute(self, input_value: I) -> O: |
77 | 87 | for step in self.__steps: |
78 | 88 | output_value = await dispatcher.dispatch(input_value) |
79 | 89 | input_value = await step.converter(output_value) |
| 90 | + |
| 91 | + if input_value is None: |
| 92 | + return NotImplemented |
| 93 | + |
80 | 94 | dispatcher = step.dispatcher or self.__dispatcher |
81 | 95 |
|
82 | 96 | return await dispatcher.dispatch(input_value) |
| 97 | + |
| 98 | + |
| 99 | +@dataclass(repr=False, eq=False, frozen=True, slots=True) |
| 100 | +class ContextPipelineStep[I, O]: |
| 101 | + converter: PipeConverterMethod[I, O] |
| 102 | + dispatcher: Dispatcher[I, Any] | None = field(default=None) |
| 103 | + |
| 104 | + |
| 105 | +class ContextPipeline[I]: |
| 106 | + __slots__ = ("__dispatcher", "__middlewares", "__steps") |
| 107 | + |
| 108 | + __dispatcher: Dispatcher[Any, Any] |
| 109 | + __middlewares: deque[Middleware[Any, Any]] |
| 110 | + __steps: list[ContextPipelineStep[Any, Any]] |
| 111 | + |
| 112 | + def __init__(self, dispatcher: Dispatcher[Any, Any]) -> None: |
| 113 | + self.__dispatcher = dispatcher |
| 114 | + self.__middlewares = deque() |
| 115 | + self.__steps = [] |
| 116 | + |
| 117 | + @overload |
| 118 | + def __get__[O]( |
| 119 | + self, |
| 120 | + instance: O, |
| 121 | + owner: type[O] | None = ..., |
| 122 | + ) -> Dispatcher[I, O]: ... |
| 123 | + |
| 124 | + @overload |
| 125 | + def __get__(self, instance: None = ..., owner: type | None = ...) -> Self: ... |
| 126 | + |
| 127 | + def __get__[O]( |
| 128 | + self, |
| 129 | + instance: O | None = None, |
| 130 | + owner: type[O] | None = None, |
| 131 | + ) -> Self | Dispatcher[I, O]: |
| 132 | + if instance is None: |
| 133 | + return self |
| 134 | + |
| 135 | + pipeline = self.__new_pipeline(instance, owner) |
| 136 | + return BoundContextPipeline(instance, pipeline) |
| 137 | + |
| 138 | + def add_middlewares(self, *middlewares: Middleware[[I], Any]) -> Self: |
| 139 | + self.__middlewares.extendleft(reversed(middlewares)) |
| 140 | + return self |
| 141 | + |
| 142 | + @overload |
| 143 | + def step[T]( |
| 144 | + self, |
| 145 | + wrapped: PipeConverterMethod[T, Any], |
| 146 | + /, |
| 147 | + *, |
| 148 | + dispatcher: Dispatcher[T, Any] | None = ..., |
| 149 | + ) -> PipeConverterMethod[T, Any]: ... |
| 150 | + |
| 151 | + @overload |
| 152 | + def step[T]( |
| 153 | + self, |
| 154 | + wrapped: None = ..., |
| 155 | + /, |
| 156 | + *, |
| 157 | + dispatcher: Dispatcher[T, Any] | None = ..., |
| 158 | + ) -> Callable[[PipeConverterMethod[T, Any]], PipeConverterMethod[T, Any]]: ... |
| 159 | + |
| 160 | + def step[T]( |
| 161 | + self, |
| 162 | + wrapped: PipeConverterMethod[T, Any] | None = None, |
| 163 | + /, |
| 164 | + *, |
| 165 | + dispatcher: Dispatcher[T, Any] | None = None, |
| 166 | + ) -> Any: |
| 167 | + def decorator(wp: PipeConverterMethod[T, Any]) -> PipeConverterMethod[T, Any]: |
| 168 | + step = ContextPipelineStep(wp, dispatcher) |
| 169 | + self.__steps.append(step) |
| 170 | + return wp |
| 171 | + |
| 172 | + return decorator(wrapped) if wrapped else decorator |
| 173 | + |
| 174 | + def __new_pipeline[T]( |
| 175 | + self, |
| 176 | + context: T, |
| 177 | + context_type: type[T] | None, |
| 178 | + ) -> Pipe[I, Any]: |
| 179 | + pipeline: Pipe[I, Any] = Pipe(self.__dispatcher) |
| 180 | + pipeline.add_middlewares(*self.__middlewares) |
| 181 | + |
| 182 | + for step in self.__steps: |
| 183 | + converter = step.converter.__get__(context, context_type) |
| 184 | + pipeline.step(converter, dispatcher=step.dispatcher) |
| 185 | + |
| 186 | + return pipeline |
| 187 | + |
| 188 | + |
| 189 | +@dataclass(repr=False, eq=False, frozen=True, slots=True) |
| 190 | +class BoundContextPipeline[I, O](Dispatcher[I, O]): |
| 191 | + context: O |
| 192 | + pipeline: Pipe[I, Any] |
| 193 | + |
| 194 | + async def dispatch(self, input_value: I, /) -> O: |
| 195 | + await self.pipeline.dispatch(input_value) |
| 196 | + return self.context |
0 commit comments