python-cq 0.21.1__tar.gz → 0.22.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_cq-0.21.1 → python_cq-0.22.1}/PKG-INFO +6 -1
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/cq.py +1 -3
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/di.py +14 -23
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/handler.py +1 -1
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/middleware.py +1 -1
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/pipetools.py +0 -3
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/queues/abc.py +3 -2
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/queues/memory.py +3 -3
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/ext/injection.py +8 -15
- python_cq-0.22.1/cq/middlewares/contextlib.py +28 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/docs/index.md +4 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/pyproject.toml +2 -1
- python_cq-0.21.1/cq/_core/middlewares/scope.py +0 -14
- python_cq-0.21.1/cq/middlewares/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/.gitignore +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/LICENSE +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/__init__.py +3 -3
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/common/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/common/typing.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/dispatchers/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/dispatchers/abc.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/dispatchers/bus.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/dispatchers/lazy.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/dispatchers/pipe.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/message.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/pump.py +0 -0
- {python_cq-0.21.1/cq/_core/middlewares → python_cq-0.22.1/cq/_core/queues}/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/_core/related_events.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/exceptions.py +0 -0
- {python_cq-0.21.1/cq/_core/queues → python_cq-0.22.1/cq/ext}/__init__.py +0 -0
- {python_cq-0.21.1/cq/ext → python_cq-0.22.1/cq/middlewares}/__init__.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/middlewares/exc.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/middlewares/retry.py +0 -0
- {python_cq-0.21.1 → python_cq-0.22.1}/cq/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-cq
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.22.1
|
|
4
4
|
Summary: CQRS library for async Python projects.
|
|
5
5
|
Project-URL: Documentation, https://python-cq.remimd.dev
|
|
6
6
|
Project-URL: Repository, https://github.com/100nm/python-cq
|
|
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
|
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
22
|
Classifier: Topic :: Software Development :: Libraries
|
|
22
23
|
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
23
24
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
@@ -80,23 +81,27 @@ from cq import CommandBus, command_handler
|
|
|
80
81
|
from dataclasses import dataclass
|
|
81
82
|
from injection import inject
|
|
82
83
|
|
|
84
|
+
|
|
83
85
|
@dataclass
|
|
84
86
|
class CreateUserCommand:
|
|
85
87
|
name: str
|
|
86
88
|
email: str
|
|
87
89
|
|
|
90
|
+
|
|
88
91
|
@command_handler
|
|
89
92
|
class CreateUserHandler:
|
|
90
93
|
async def handle(self, command: CreateUserCommand) -> int:
|
|
91
94
|
# ... persist the user, return its id
|
|
92
95
|
return 42
|
|
93
96
|
|
|
97
|
+
|
|
94
98
|
@inject
|
|
95
99
|
async def main(bus: CommandBus[int]) -> None:
|
|
96
100
|
command = CreateUserCommand(name="Ada", email="ada@example.com")
|
|
97
101
|
user_id = await bus.dispatch(command)
|
|
98
102
|
print(f"Created user {user_id}")
|
|
99
103
|
|
|
104
|
+
|
|
100
105
|
asyncio.run(main())
|
|
101
106
|
```
|
|
102
107
|
|
|
@@ -10,7 +10,6 @@ from cq._core.handler import (
|
|
|
10
10
|
SingleHandlerRegistry,
|
|
11
11
|
)
|
|
12
12
|
from cq._core.message import Command, Event, Query
|
|
13
|
-
from cq._core.middlewares.scope import CommandDispatchScopeMiddleware
|
|
14
13
|
|
|
15
14
|
|
|
16
15
|
class CQ:
|
|
@@ -57,8 +56,7 @@ class CQ:
|
|
|
57
56
|
|
|
58
57
|
def new_command_bus(self) -> Bus[Command, Any]:
|
|
59
58
|
bus = SimpleBus(self.__command_registry)
|
|
60
|
-
|
|
61
|
-
bus.add_middlewares(command_middleware)
|
|
59
|
+
bus.add_middlewares(self.__di.command_scope())
|
|
62
60
|
return bus
|
|
63
61
|
|
|
64
62
|
def new_event_bus(self) -> Bus[Event, None]:
|
|
@@ -3,10 +3,12 @@ from __future__ import annotations
|
|
|
3
3
|
from abc import abstractmethod
|
|
4
4
|
from collections.abc import Awaitable, Callable
|
|
5
5
|
from contextlib import nullcontext
|
|
6
|
-
from typing import TYPE_CHECKING, Any,
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
|
|
7
|
+
|
|
8
|
+
from cq.middlewares.contextlib import AsyncContextManagerMiddleware
|
|
7
9
|
|
|
8
10
|
if TYPE_CHECKING: # pragma: no cover
|
|
9
|
-
from cq import CommandBus, EventBus, QueryBus
|
|
11
|
+
from cq import Command, CommandBus, EventBus, Middleware, QueryBus
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
@runtime_checkable
|
|
@@ -22,30 +24,19 @@ class DIAdapter(Protocol):
|
|
|
22
24
|
__slots__ = ()
|
|
23
25
|
|
|
24
26
|
@abstractmethod
|
|
25
|
-
def command_scope(self) ->
|
|
27
|
+
def command_scope(self) -> Middleware[[Command], Any]:
|
|
26
28
|
"""
|
|
27
|
-
Return
|
|
28
|
-
command dispatch.
|
|
29
|
+
Return a middleware that wraps each command dispatch.
|
|
29
30
|
|
|
30
31
|
**Responsibilities**
|
|
31
32
|
|
|
32
|
-
The
|
|
33
|
-
instance and register it so that it is resolvable
|
|
34
|
-
the duration of the
|
|
35
|
-
|
|
36
|
-
**Nested calls**
|
|
37
|
-
|
|
38
|
-
``command_scope`` is entered in two distinct situations:
|
|
39
|
-
|
|
40
|
-
1. Around a standard command dispatch (via
|
|
41
|
-
``CommandDispatchScopeMiddleware``).
|
|
42
|
-
2. Around each step of a ``ContextCommandPipeline``, which itself
|
|
43
|
-
wraps a command dispatch.
|
|
33
|
+
The middleware must at minimum manage the lifecycle of a
|
|
34
|
+
``RelatedEvents`` instance and register it so that it is resolvable
|
|
35
|
+
via injection for the duration of the dispatch.
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
call instead of opening a second, conflicting scope.
|
|
37
|
+
If you already have an async context manager for the scope, wrap it
|
|
38
|
+
with ``cq.middlewares.contextlib.AsyncContextManagerMiddleware``
|
|
39
|
+
instead of writing the middleware by hand.
|
|
49
40
|
"""
|
|
50
41
|
|
|
51
42
|
raise NotImplementedError
|
|
@@ -97,8 +88,8 @@ class DIAdapter(Protocol):
|
|
|
97
88
|
class NoDI(DIAdapter):
|
|
98
89
|
__slots__ = ()
|
|
99
90
|
|
|
100
|
-
def command_scope(self) ->
|
|
101
|
-
return nullcontext()
|
|
91
|
+
def command_scope(self) -> Middleware[[Command], Any]:
|
|
92
|
+
return AsyncContextManagerMiddleware(nullcontext())
|
|
102
93
|
|
|
103
94
|
def lazy[T](self, tp: type[T], /) -> Callable[[], Awaitable[T]]:
|
|
104
95
|
tp_str = getattr(tp, "__name__", str(tp))
|
|
@@ -98,7 +98,7 @@ class _GeneratorMiddleware[**P, T]:
|
|
|
98
98
|
while True:
|
|
99
99
|
try:
|
|
100
100
|
value = await call_next(*args, **kwargs)
|
|
101
|
-
except BaseException as exc:
|
|
101
|
+
except BaseException as exc: # noqa: BLE001
|
|
102
102
|
await generator.athrow(exc)
|
|
103
103
|
else:
|
|
104
104
|
await generator.asend(value)
|
|
@@ -11,7 +11,6 @@ from cq._core.dispatchers.pipe import (
|
|
|
11
11
|
ConvertMethodSync,
|
|
12
12
|
)
|
|
13
13
|
from cq._core.message import Command, CommandBus, Query, QueryBus
|
|
14
|
-
from cq._core.middlewares.scope import CommandDispatchScopeMiddleware
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
class ContextCommandPipeline[C: Command](ContextPipeline[C]):
|
|
@@ -22,8 +21,6 @@ class ContextCommandPipeline[C: Command](ContextPipeline[C]):
|
|
|
22
21
|
def __init__(self, di: DIAdapter) -> None:
|
|
23
22
|
super().__init__(LazyDispatcher(CommandBus, di))
|
|
24
23
|
self.__query_dispatcher = LazyDispatcher(QueryBus, di)
|
|
25
|
-
command_middleware = CommandDispatchScopeMiddleware(di)
|
|
26
|
-
self.add_middlewares(command_middleware)
|
|
27
24
|
|
|
28
25
|
def add_static_query_step[Q: Query](self, query: Q, /) -> Self:
|
|
29
26
|
return self.add_static_step(query, dispatcher=self.__query_dispatcher)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from abc import abstractmethod
|
|
2
2
|
from collections.abc import AsyncIterable
|
|
3
|
-
from
|
|
3
|
+
from contextlib import AbstractAsyncContextManager
|
|
4
|
+
from typing import Protocol, runtime_checkable
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
@runtime_checkable
|
|
@@ -16,7 +17,7 @@ class Producer[T](Protocol):
|
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
@runtime_checkable
|
|
19
|
-
class Delivery[T](
|
|
20
|
+
class Delivery[T](AbstractAsyncContextManager[T], Protocol):
|
|
20
21
|
__slots__ = ()
|
|
21
22
|
|
|
22
23
|
|
|
@@ -51,10 +51,10 @@ class MemoryQueue[T](Queue[T]):
|
|
|
51
51
|
async with (
|
|
52
52
|
Pump(self, dispatcher, fail_silently)
|
|
53
53
|
.add_middlewares(*middlewares)
|
|
54
|
-
.draining(concurrency=concurrency, graceful=True)
|
|
54
|
+
.draining(concurrency=concurrency, graceful=True),
|
|
55
|
+
self,
|
|
55
56
|
):
|
|
56
|
-
|
|
57
|
-
yield self
|
|
57
|
+
yield self
|
|
58
58
|
|
|
59
59
|
async def send(self, message: T, /) -> None:
|
|
60
60
|
await self.__producer.send(message)
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
from collections.abc import AsyncIterator
|
|
2
|
-
from contextlib import AsyncExitStack
|
|
1
|
+
from collections.abc import AsyncIterator, Awaitable, Callable
|
|
2
|
+
from contextlib import AsyncExitStack
|
|
3
3
|
from dataclasses import dataclass, field
|
|
4
4
|
from enum import StrEnum
|
|
5
|
-
from typing import Any
|
|
5
|
+
from typing import Any
|
|
6
6
|
|
|
7
7
|
from injection import Module, adefine_scope, mod
|
|
8
8
|
from injection.exceptions import ScopeAlreadyDefinedError
|
|
9
9
|
|
|
10
10
|
from cq._core.di import DIAdapter
|
|
11
|
-
from cq._core.message import CommandBus, EventBus, QueryBus
|
|
12
|
-
from cq._core.middleware import MiddlewareResult
|
|
11
|
+
from cq._core.message import Command, CommandBus, EventBus, QueryBus
|
|
12
|
+
from cq._core.middleware import Middleware, MiddlewareResult
|
|
13
13
|
from cq._core.related_events import AnyIORelatedEvents, RelatedEvents
|
|
14
14
|
|
|
15
15
|
__all__ = ("CQScope", "InjectionAdapter", "InjectionScopeMiddleware")
|
|
@@ -24,12 +24,11 @@ class InjectionAdapter(DIAdapter):
|
|
|
24
24
|
module: Module = field(default_factory=mod)
|
|
25
25
|
threadsafe: bool | None = field(default=None)
|
|
26
26
|
|
|
27
|
-
def command_scope(self) ->
|
|
28
|
-
return InjectionScopeMiddleware(
|
|
27
|
+
def command_scope(self) -> Middleware[[Command], Any]:
|
|
28
|
+
return InjectionScopeMiddleware(
|
|
29
29
|
CQScope.COMMAND_DISPATCH,
|
|
30
|
-
exist_ok=True,
|
|
31
30
|
threadsafe=self.threadsafe,
|
|
32
|
-
)
|
|
31
|
+
)
|
|
33
32
|
|
|
34
33
|
def lazy[T](self, tp: type[T], /) -> Callable[[], Awaitable[T]]:
|
|
35
34
|
awaitable = self.module.aget_lazy_instance(tp, threadsafe=self.threadsafe)
|
|
@@ -83,12 +82,6 @@ class InjectionScopeMiddleware:
|
|
|
83
82
|
threadsafe: bool | None = field(default=None, kw_only=True)
|
|
84
83
|
|
|
85
84
|
async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
|
|
86
|
-
async with self._cm: # type: ignore[attr-defined]
|
|
87
|
-
yield
|
|
88
|
-
|
|
89
|
-
@property
|
|
90
|
-
@asynccontextmanager
|
|
91
|
-
async def _cm(self) -> AsyncIterator[None]:
|
|
92
85
|
async with AsyncExitStack() as stack:
|
|
93
86
|
try:
|
|
94
87
|
await stack.enter_async_context(
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from contextlib import AbstractAsyncContextManager, AbstractContextManager
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING: # pragma: no cover
|
|
8
|
+
from cq import MiddlewareResult
|
|
9
|
+
|
|
10
|
+
__all__ = ("AsyncContextManagerMiddleware", "ContextManagerMiddleware")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(repr=False, eq=False, frozen=True, slots=True)
|
|
14
|
+
class AsyncContextManagerMiddleware:
|
|
15
|
+
context: AbstractAsyncContextManager[Any]
|
|
16
|
+
|
|
17
|
+
async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
|
|
18
|
+
async with self.context:
|
|
19
|
+
yield
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(repr=False, eq=False, frozen=True, slots=True)
|
|
23
|
+
class ContextManagerMiddleware:
|
|
24
|
+
context: AbstractContextManager[Any]
|
|
25
|
+
|
|
26
|
+
async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
|
|
27
|
+
with self.context:
|
|
28
|
+
yield
|
|
@@ -49,23 +49,27 @@ from cq import CommandBus, command_handler
|
|
|
49
49
|
from dataclasses import dataclass
|
|
50
50
|
from injection import inject
|
|
51
51
|
|
|
52
|
+
|
|
52
53
|
@dataclass
|
|
53
54
|
class CreateUserCommand:
|
|
54
55
|
name: str
|
|
55
56
|
email: str
|
|
56
57
|
|
|
58
|
+
|
|
57
59
|
@command_handler
|
|
58
60
|
class CreateUserHandler:
|
|
59
61
|
async def handle(self, command: CreateUserCommand) -> int:
|
|
60
62
|
# ... persist the user, return its id
|
|
61
63
|
return 42
|
|
62
64
|
|
|
65
|
+
|
|
63
66
|
@inject
|
|
64
67
|
async def main(bus: CommandBus[int]) -> None:
|
|
65
68
|
command = CreateUserCommand(name="Ada", email="ada@example.com")
|
|
66
69
|
user_id = await bus.dispatch(command)
|
|
67
70
|
print(f"Created user {user_id}")
|
|
68
71
|
|
|
72
|
+
|
|
69
73
|
asyncio.run(main())
|
|
70
74
|
```
|
|
71
75
|
|
|
@@ -20,7 +20,7 @@ test = [
|
|
|
20
20
|
|
|
21
21
|
[project]
|
|
22
22
|
name = "python-cq"
|
|
23
|
-
version = "0.
|
|
23
|
+
version = "0.22.1"
|
|
24
24
|
description = "CQRS library for async Python projects."
|
|
25
25
|
license = "MIT"
|
|
26
26
|
license-files = ["LICENSE"]
|
|
@@ -39,6 +39,7 @@ classifiers = [
|
|
|
39
39
|
"Programming Language :: Python :: 3.12",
|
|
40
40
|
"Programming Language :: Python :: 3.13",
|
|
41
41
|
"Programming Language :: Python :: 3.14",
|
|
42
|
+
"Programming Language :: Python :: 3.15",
|
|
42
43
|
"Operating System :: OS Independent",
|
|
43
44
|
"Intended Audience :: Developers",
|
|
44
45
|
"Natural Language :: English",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
from dataclasses import dataclass
|
|
2
|
-
from typing import Any
|
|
3
|
-
|
|
4
|
-
from cq._core.di import DIAdapter
|
|
5
|
-
from cq._core.middleware import MiddlewareResult
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
@dataclass(repr=False, eq=False, frozen=True, slots=True)
|
|
9
|
-
class CommandDispatchScopeMiddleware:
|
|
10
|
-
di: DIAdapter
|
|
11
|
-
|
|
12
|
-
async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
|
|
13
|
-
async with self.di.command_scope():
|
|
14
|
-
yield
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -21,18 +21,17 @@ from ._core.queues.memory import MemoryQueue
|
|
|
21
21
|
from ._core.related_events import AnyIORelatedEvents, RelatedEvents
|
|
22
22
|
|
|
23
23
|
__all__ = (
|
|
24
|
-
"
|
|
24
|
+
"CQ",
|
|
25
25
|
"AnyCommandBus",
|
|
26
26
|
"AnyIORelatedEvents",
|
|
27
27
|
"Bus",
|
|
28
|
-
"CQ",
|
|
29
28
|
"Command",
|
|
30
29
|
"CommandBus",
|
|
31
30
|
"Consumer",
|
|
32
31
|
"ContextCommandPipeline",
|
|
33
32
|
"ContextPipeline",
|
|
34
|
-
"Delivery",
|
|
35
33
|
"DIAdapter",
|
|
34
|
+
"Delivery",
|
|
36
35
|
"Dispatcher",
|
|
37
36
|
"Event",
|
|
38
37
|
"EventBus",
|
|
@@ -46,6 +45,7 @@ __all__ = (
|
|
|
46
45
|
"QueryBus",
|
|
47
46
|
"Queue",
|
|
48
47
|
"RelatedEvents",
|
|
48
|
+
"__cq__",
|
|
49
49
|
"command_handler",
|
|
50
50
|
"event_handler",
|
|
51
51
|
"new_command_bus",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|