buz 2.22.1__py3-none-any.whl → 2.23.0__py3-none-any.whl
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.
- buz/command/synchronous/command_handler.py +7 -0
- buz/query/synchronous/query_handler.py +7 -0
- buz/wrapper/async_unsafe.py +67 -0
- buz/wrapper/synchronous_only_operation_exception.py +3 -0
- {buz-2.22.1.dist-info → buz-2.23.0.dist-info}/METADATA +1 -1
- {buz-2.22.1.dist-info → buz-2.23.0.dist-info}/RECORD +8 -6
- {buz-2.22.1.dist-info → buz-2.23.0.dist-info}/LICENSE +0 -0
- {buz-2.22.1.dist-info → buz-2.23.0.dist-info}/WHEEL +0 -0
|
@@ -3,11 +3,18 @@ from typing import Generic, Type, TypeVar
|
|
|
3
3
|
|
|
4
4
|
from buz import Handler
|
|
5
5
|
from buz.command import Command
|
|
6
|
+
from buz.wrapper.async_unsafe import async_unsafe
|
|
6
7
|
|
|
7
8
|
TCommand = TypeVar("TCommand", bound=Command)
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
class CommandHandler(Generic[TCommand], Handler[TCommand], ABC):
|
|
12
|
+
def __new__(cls, *args, **kwargs):
|
|
13
|
+
instance = super().__new__(cls)
|
|
14
|
+
original_handle = instance.handle
|
|
15
|
+
instance.handle = async_unsafe()(original_handle)
|
|
16
|
+
return instance
|
|
17
|
+
|
|
11
18
|
@classmethod
|
|
12
19
|
@abstractmethod
|
|
13
20
|
def handles(cls) -> Type[TCommand]:
|
|
@@ -3,12 +3,19 @@ from typing import Generic, Type, TypeVar
|
|
|
3
3
|
|
|
4
4
|
from buz import Handler
|
|
5
5
|
from buz.query import Query, QueryResponse
|
|
6
|
+
from buz.wrapper.async_unsafe import async_unsafe
|
|
6
7
|
|
|
7
8
|
TQuery = TypeVar("TQuery", bound=Query)
|
|
8
9
|
TQueryResponse = TypeVar("TQueryResponse", bound=QueryResponse)
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class QueryHandler(Generic[TQuery, TQueryResponse], Handler[TQuery], ABC):
|
|
13
|
+
def __new__(cls, *args, **kwargs):
|
|
14
|
+
instance = super().__new__(cls)
|
|
15
|
+
original_handle = instance.handle
|
|
16
|
+
instance.handle = async_unsafe()(original_handle)
|
|
17
|
+
return instance
|
|
18
|
+
|
|
12
19
|
@classmethod
|
|
13
20
|
@abstractmethod
|
|
14
21
|
def handles(cls) -> Type[TQuery]:
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from asyncio import get_running_loop
|
|
4
|
+
from functools import wraps
|
|
5
|
+
from typing import Callable, Any
|
|
6
|
+
|
|
7
|
+
from buz.wrapper.synchronous_only_operation_exception import SynchronousOnlyOperationException
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def async_unsafe() -> Callable[[Callable], Callable]:
|
|
13
|
+
"""
|
|
14
|
+
Copy-paste from Django source code. If BUZ_SYNC_HANDLERS_FAIL_ON_ASYNC env var is True then an exception is raised to
|
|
15
|
+
prevent event loop from being blocked. If the env var is False, a warning is logged.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def decorator(func: Callable) -> Callable:
|
|
19
|
+
@wraps(func)
|
|
20
|
+
def inner(*args: Any, **kwargs: Any) -> Any:
|
|
21
|
+
try:
|
|
22
|
+
get_running_loop()
|
|
23
|
+
except RuntimeError:
|
|
24
|
+
pass
|
|
25
|
+
else:
|
|
26
|
+
if __get_bool_env("BUZ_SYNC_HANDLERS_FAIL_ON_ASYNC") is True:
|
|
27
|
+
raise SynchronousOnlyOperationException()
|
|
28
|
+
|
|
29
|
+
logger.warning(
|
|
30
|
+
f"You should not call this from an async context - use a thread or sync_to_async. Class {__get_class_name(func, args)}"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return func(*args, **kwargs)
|
|
34
|
+
|
|
35
|
+
return inner
|
|
36
|
+
|
|
37
|
+
return decorator
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def __get_bool_env(var_name: str) -> bool:
|
|
41
|
+
"""
|
|
42
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
43
|
+
False
|
|
44
|
+
>>> os.environ["MY_BOOL_VAR"] = "1"
|
|
45
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
46
|
+
True
|
|
47
|
+
>>> os.environ["MY_BOOL_VAR"] = "true"
|
|
48
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
49
|
+
True
|
|
50
|
+
>>> os.environ["MY_BOOL_VAR"] = "True"
|
|
51
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
52
|
+
True
|
|
53
|
+
>>> os.environ["MY_BOOL_VAR"] = "0"
|
|
54
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
55
|
+
False
|
|
56
|
+
>>> os.environ["MY_BOOL_VAR"] = "false"
|
|
57
|
+
>>> get_bool_env("MY_BOOL_VAR")
|
|
58
|
+
False
|
|
59
|
+
"""
|
|
60
|
+
return os.environ.get(var_name, "0").lower() in ("true", "1")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def __get_class_name(func: Callable, args: tuple) -> str:
|
|
64
|
+
if hasattr(func, "__self__"):
|
|
65
|
+
return func.__self__.__class__.__name__
|
|
66
|
+
|
|
67
|
+
return args[0].__class__.__name__ if len(args) > 0 else "Unknown"
|
|
@@ -15,7 +15,7 @@ buz/command/more_than_one_command_handler_related_exception.py,sha256=dNCMXCAKVc
|
|
|
15
15
|
buz/command/synchronous/__init__.py,sha256=ZAH35jpte6zpxb70iQfZfGSg_OsXqVNYwNkCJy7Qnzk,268
|
|
16
16
|
buz/command/synchronous/base_command_handler.py,sha256=GkmxttA1_Wu2X5O7iR-tLRdnmT-JDgeyURPwkjLPGhg,1393
|
|
17
17
|
buz/command/synchronous/command_bus.py,sha256=FHJH4lerThu5fWRISM0VIK8ebyfqgDGqNNCqItbmGPI,175
|
|
18
|
-
buz/command/synchronous/command_handler.py,sha256=
|
|
18
|
+
buz/command/synchronous/command_handler.py,sha256=M9nQhW9Ilkix09U6Pv6ZpoqA-gezrm7H_gqjoLDc_A4,676
|
|
19
19
|
buz/command/synchronous/middleware/__init__.py,sha256=wE97veOW2cCGpTZ-3bvc3aU6NdQmSOYfdX9kAUETJ0w,406
|
|
20
20
|
buz/command/synchronous/middleware/base_handle_middleware.py,sha256=qHnbucL7j6Dnw7E8ACBf32pzDD9squYPp6UiK2heb5A,774
|
|
21
21
|
buz/command/synchronous/middleware/handle_middleware.py,sha256=oIsmB30mu-Cd9IQc1qMKbUIaIMHipSEXOw2swVrtbkk,420
|
|
@@ -250,7 +250,7 @@ buz/query/synchronous/middleware/base_handle_middleware.py,sha256=4lqJB2C-GHl3T5
|
|
|
250
250
|
buz/query/synchronous/middleware/handle_middleware.py,sha256=Xr2HSlRrW3slluyUiJ6zH0Rw0oeWD3uMN-1hjuFbC_k,433
|
|
251
251
|
buz/query/synchronous/middleware/handle_middleware_chain_resolver.py,sha256=OIpGLJ_2a8cGsp-LmA3Q1Lvb1pB65MIuA-geiPrQKHM,1070
|
|
252
252
|
buz/query/synchronous/query_bus.py,sha256=eYl_sGH5TcELkOXc4RnV4Tkmcs-VLc1vzd-YMduQ1YI,189
|
|
253
|
-
buz/query/synchronous/query_handler.py,sha256=
|
|
253
|
+
buz/query/synchronous/query_handler.py,sha256=NuolVPZelmjOe6-q_hFC3Y0gm7ih2A6aanKufo6wHR0,759
|
|
254
254
|
buz/query/synchronous/self_process/__init__.py,sha256=fU1OoXXXH5dMGKz8y7mwTVvyhNj6BCKDTxuxH_q-leM,125
|
|
255
255
|
buz/query/synchronous/self_process/self_process_query_bus.py,sha256=pKGJxXBWtqU4i0fzb30OCNhAVPCkUh7IlfNzgAhCUC8,1157
|
|
256
256
|
buz/query/synchronous/synced_async/__init__.py,sha256=TdFmIBeFIpl3Tvmh_FJpJMXJdPdfRxOstVqnPUi23mo,125
|
|
@@ -264,8 +264,10 @@ buz/serializer/message_to_bytes_serializer.py,sha256=uQ6JSTn24mLQvN48alwVXa6lfgf
|
|
|
264
264
|
buz/serializer/message_to_json_bytes_serializer.py,sha256=RGZJ64t4t4Pz2FCASZZCv-2LiWnertC8scE9ZzQkBsU,764
|
|
265
265
|
buz/wrapper/__init__.py,sha256=GnRdJFcncn-qp0hzDG9dBHLmTJSbHFVjE_yr-MdW_n4,77
|
|
266
266
|
buz/wrapper/async_to_sync.py,sha256=OfK-vrVUhuN-LLLvekLdMbQYtH0ue5lfbvuasj6ovMI,698
|
|
267
|
+
buz/wrapper/async_unsafe.py,sha256=vg-BM8Dh7MqKiMAc8qsMPexLUuHI1bn4APzMXeSrBgk,2026
|
|
267
268
|
buz/wrapper/event_loop.py,sha256=pfBJ1g-8A2a3YgW8Gf9Fg0kkewoh3-wgTy2KIFDyfHk,266
|
|
268
|
-
buz
|
|
269
|
-
buz-2.
|
|
270
|
-
buz-2.
|
|
271
|
-
buz-2.
|
|
269
|
+
buz/wrapper/synchronous_only_operation_exception.py,sha256=qCTaNSuIY7_HXTOmzlNGhks1nRApeW5K8W5aHC-dL-4,180
|
|
270
|
+
buz-2.23.0.dist-info/LICENSE,sha256=jcLgcIIVaBqaZNwe0kzGWSU99YgwMcI0IGv142wkYSM,1062
|
|
271
|
+
buz-2.23.0.dist-info/METADATA,sha256=YxHyoXLE0SlA_UxWPBACGbNIeXkeOnEW9_gfe6yDOo8,12580
|
|
272
|
+
buz-2.23.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
273
|
+
buz-2.23.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|