anyenv 1.2.2__tar.gz → 1.2.4__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.
- {anyenv-1.2.2 → anyenv-1.2.4}/PKG-INFO +1 -1
- {anyenv-1.2.2 → anyenv-1.2.4}/pyproject.toml +1 -1
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/__init__.py +7 -1
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/calling/__init__.py +2 -1
- anyenv-1.2.4/src/anyenv/calling/multieventhandler.py +182 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/base.py +3 -4
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/functional.py +2 -2
- {anyenv-1.2.2 → anyenv-1.2.4}/LICENSE +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/README.md +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/anypath.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/async_run.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/calling/async_executor.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/calling/threadgroup.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/beam_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/daytona_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/docker_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/e2b_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/local_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/mcp_python_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/models.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/code_execution/subprocess_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/aiohttp_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/aiohttp_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/exceptions.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/http_types.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/js_transport.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/serializer.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/pyodide_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/download/pyodide_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/msgspec_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/msgspec_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/orjson_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/orjson_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/pydantic_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/pydantic_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/stdlib_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/stdlib_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/json_tools/utils.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/log.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/package_install/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/package_install/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/package_install/functional.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/package_install/micropip_install.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/package_install/pip_install.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/parse_time.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/py.typed +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/testing.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.4}/src/anyenv/validate.py +0 -0
|
@@ -45,7 +45,12 @@ from anyenv.download.functional import (
|
|
|
45
45
|
post_json_sync,
|
|
46
46
|
)
|
|
47
47
|
from anyenv.download.exceptions import RequestError, ResponseError, HttpError
|
|
48
|
-
from anyenv.calling import
|
|
48
|
+
from anyenv.calling import (
|
|
49
|
+
ThreadGroup,
|
|
50
|
+
method_spawner,
|
|
51
|
+
function_spawner,
|
|
52
|
+
MultiEventHandler,
|
|
53
|
+
)
|
|
49
54
|
from anyenv.package_install.functional import install, install_sync
|
|
50
55
|
from anyenv.testing import open_in_playground
|
|
51
56
|
from anyenv.json_tools import load_json, JsonLoadError, dump_json, JsonDumpError
|
|
@@ -58,6 +63,7 @@ __all__ = [
|
|
|
58
63
|
"HttpResponse",
|
|
59
64
|
"JsonDumpError",
|
|
60
65
|
"JsonLoadError",
|
|
66
|
+
"MultiEventHandler",
|
|
61
67
|
"RequestError",
|
|
62
68
|
"ResponseError",
|
|
63
69
|
"Session",
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from anyenv.calling.threadgroup import ThreadGroup
|
|
6
|
+
from anyenv.calling.multieventhandler import MultiEventHandler
|
|
6
7
|
from anyenv.calling.async_executor import method_spawner, function_spawner
|
|
7
8
|
|
|
8
|
-
__all__ = ["ThreadGroup", "function_spawner", "method_spawner"]
|
|
9
|
+
__all__ = ["MultiEventHandler", "ThreadGroup", "function_spawner", "method_spawner"]
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Generic async callback manager for handling multiple event handlers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from collections.abc import Callable, Sequence
|
|
7
|
+
import contextlib
|
|
8
|
+
import inspect
|
|
9
|
+
from typing import TYPE_CHECKING, Any, Literal, ParamSpec
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from collections.abc import Awaitable
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
ExecutionMode = Literal["sequential", "parallel"]
|
|
17
|
+
|
|
18
|
+
# Generic handler type bound to callable returning Any or Awaitable[Any]
|
|
19
|
+
P = ParamSpec("P")
|
|
20
|
+
HandlerT = Callable[P, Any]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MultiEventHandler[HandlerT]:
|
|
24
|
+
"""Manages multiple callbacks/event handlers with sequential or parallel execution.
|
|
25
|
+
|
|
26
|
+
Provides a unified interface for executing multiple callbacks either sequentially
|
|
27
|
+
or in parallel, with support for dynamic handler management. Sync functions are
|
|
28
|
+
automatically wrapped to work with the async interface.
|
|
29
|
+
|
|
30
|
+
The generic parameter HandlerT should be a callable type (function signature).
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
handlers: Initial list of async or sync callable event handlers, or single handler
|
|
34
|
+
mode: Execution mode - "sequential" or "parallel"
|
|
35
|
+
|
|
36
|
+
Example:
|
|
37
|
+
```python
|
|
38
|
+
from collections.abc import Awaitable
|
|
39
|
+
from typing import Callable
|
|
40
|
+
|
|
41
|
+
async def async_handler(x: int, y: str) -> str:
|
|
42
|
+
return f"Async Handler: {x}, {y}"
|
|
43
|
+
|
|
44
|
+
def sync_handler(x: int, y: str) -> str:
|
|
45
|
+
return f"Sync Handler: {x}, {y}"
|
|
46
|
+
|
|
47
|
+
# Handler type specification
|
|
48
|
+
Handler = Callable[[int, str], str | Awaitable[str]]
|
|
49
|
+
manager: MultiEventHandler[Handler] = MultiEventHandler([
|
|
50
|
+
async_handler, sync_handler
|
|
51
|
+
])
|
|
52
|
+
results = await manager(42, "test")
|
|
53
|
+
```
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
handlers: Sequence[HandlerT] | HandlerT | None = None,
|
|
59
|
+
mode: ExecutionMode = "parallel",
|
|
60
|
+
) -> None:
|
|
61
|
+
self._handlers: list[HandlerT] = []
|
|
62
|
+
self._wrapped_handlers: list[Callable[..., Awaitable[Any]]] = []
|
|
63
|
+
self._handler_mapping: dict[HandlerT, Callable[..., Awaitable[Any]]] = {}
|
|
64
|
+
self._mode: ExecutionMode = mode
|
|
65
|
+
|
|
66
|
+
if handlers is not None:
|
|
67
|
+
match handlers:
|
|
68
|
+
case Sequence():
|
|
69
|
+
for handler in handlers:
|
|
70
|
+
self.add_handler(handler)
|
|
71
|
+
case _:
|
|
72
|
+
# Single handler
|
|
73
|
+
self.add_handler(handlers)
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def __call__(self) -> HandlerT:
|
|
77
|
+
"""Execute all handlers with the given arguments.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
List of results from all handlers.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
async def event_handler(*args, **kwargs):
|
|
84
|
+
if not self._wrapped_handlers:
|
|
85
|
+
return []
|
|
86
|
+
|
|
87
|
+
if self._mode == "sequential":
|
|
88
|
+
return await self._execute_sequential(*args, **kwargs)
|
|
89
|
+
return await self._execute_parallel(*args, **kwargs)
|
|
90
|
+
|
|
91
|
+
return event_handler # type: ignore
|
|
92
|
+
|
|
93
|
+
async def _execute_sequential(self, *args: Any, **kwargs: Any) -> list[Any]:
|
|
94
|
+
"""Execute handlers sequentially."""
|
|
95
|
+
return [await handler(*args, **kwargs) for handler in self._wrapped_handlers]
|
|
96
|
+
|
|
97
|
+
async def _execute_parallel(self, *args: Any, **kwargs: Any) -> list[Any]:
|
|
98
|
+
"""Execute handlers in parallel using asyncio.gather."""
|
|
99
|
+
tasks = [handler(*args, **kwargs) for handler in self._wrapped_handlers]
|
|
100
|
+
return await asyncio.gather(*tasks)
|
|
101
|
+
|
|
102
|
+
def add_handler(self, handler: HandlerT) -> None:
|
|
103
|
+
"""Add a new handler to the manager.
|
|
104
|
+
|
|
105
|
+
Both sync and async handlers are supported.
|
|
106
|
+
"""
|
|
107
|
+
if handler in self._handlers:
|
|
108
|
+
return
|
|
109
|
+
|
|
110
|
+
# Check if handler is already async
|
|
111
|
+
if inspect.iscoroutinefunction(handler):
|
|
112
|
+
wrapped_handler = handler
|
|
113
|
+
else:
|
|
114
|
+
# Wrap sync handler
|
|
115
|
+
wrapped_handler = self._wrap_sync_handler(handler) # type: ignore[assignment]
|
|
116
|
+
|
|
117
|
+
self._handlers.append(handler)
|
|
118
|
+
self._wrapped_handlers.append(wrapped_handler)
|
|
119
|
+
self._handler_mapping[handler] = wrapped_handler
|
|
120
|
+
|
|
121
|
+
def remove_handler(self, handler: HandlerT) -> None:
|
|
122
|
+
"""Remove a handler from the manager.
|
|
123
|
+
|
|
124
|
+
Note: For sync handlers, you must pass the original sync function,
|
|
125
|
+
not the wrapped async version.
|
|
126
|
+
"""
|
|
127
|
+
if handler not in self._handlers:
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
with contextlib.suppress(ValueError):
|
|
131
|
+
# Remove from all tracking structures
|
|
132
|
+
wrapped_handler = self._handler_mapping[handler]
|
|
133
|
+
self._handlers.remove(handler)
|
|
134
|
+
self._wrapped_handlers.remove(wrapped_handler)
|
|
135
|
+
del self._handler_mapping[handler]
|
|
136
|
+
|
|
137
|
+
def _wrap_sync_handler(self, handler: HandlerT) -> Callable[..., Awaitable[Any]]:
|
|
138
|
+
"""Wrap a synchronous handler to work with async interface."""
|
|
139
|
+
|
|
140
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
141
|
+
return handler(*args, **kwargs) # type: ignore[misc,operator]
|
|
142
|
+
|
|
143
|
+
# Store reference to original handler for removal
|
|
144
|
+
async_wrapper._original_handler = handler # type: ignore[attr-defined] # noqa: SLF001
|
|
145
|
+
return async_wrapper
|
|
146
|
+
|
|
147
|
+
def clear(self) -> None:
|
|
148
|
+
"""Remove all handlers."""
|
|
149
|
+
self._handlers.clear()
|
|
150
|
+
self._wrapped_handlers.clear()
|
|
151
|
+
self._handler_mapping.clear()
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def mode(self) -> ExecutionMode:
|
|
155
|
+
"""Current execution mode."""
|
|
156
|
+
return self._mode
|
|
157
|
+
|
|
158
|
+
@mode.setter
|
|
159
|
+
def mode(self, value: ExecutionMode) -> None:
|
|
160
|
+
"""Set execution mode."""
|
|
161
|
+
self._mode = value
|
|
162
|
+
|
|
163
|
+
def __len__(self) -> int:
|
|
164
|
+
"""Return number of handlers."""
|
|
165
|
+
return len(self._handlers)
|
|
166
|
+
|
|
167
|
+
def __bool__(self) -> bool:
|
|
168
|
+
"""Return True if there are handlers registered."""
|
|
169
|
+
return bool(self._handlers)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
if __name__ == "__main__":
|
|
173
|
+
type HandlerType = Callable[[int, str], Any]
|
|
174
|
+
|
|
175
|
+
def handler(a: int, b: str):
|
|
176
|
+
"""Handler function."""
|
|
177
|
+
|
|
178
|
+
def invalid_handler(a: str, b: str):
|
|
179
|
+
"""Invalid handler function."""
|
|
180
|
+
|
|
181
|
+
multi_handler = MultiEventHandler[HandlerType]()
|
|
182
|
+
multi_handler.add_handler(handler)
|
|
@@ -18,7 +18,6 @@ from typing import (
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
if TYPE_CHECKING:
|
|
21
|
-
from os import PathLike
|
|
22
21
|
import types
|
|
23
22
|
|
|
24
23
|
from anyenv.download.http_types import (
|
|
@@ -189,7 +188,7 @@ class HttpBackend(abc.ABC):
|
|
|
189
188
|
|
|
190
189
|
def __init__(
|
|
191
190
|
self,
|
|
192
|
-
cache_dir:
|
|
191
|
+
cache_dir: StrPath | None = None,
|
|
193
192
|
cache_ttl: int | str | None = None,
|
|
194
193
|
):
|
|
195
194
|
"""Initialize HTTP backend.
|
|
@@ -267,7 +266,7 @@ class HttpBackend(abc.ABC):
|
|
|
267
266
|
async def download(
|
|
268
267
|
self,
|
|
269
268
|
url: str,
|
|
270
|
-
path:
|
|
269
|
+
path: StrPath,
|
|
271
270
|
*,
|
|
272
271
|
headers: HeaderType | None = None,
|
|
273
272
|
progress_callback: ProgressCallback | None = None,
|
|
@@ -280,7 +279,7 @@ class HttpBackend(abc.ABC):
|
|
|
280
279
|
def download_sync(
|
|
281
280
|
self,
|
|
282
281
|
url: str,
|
|
283
|
-
path:
|
|
282
|
+
path: StrPath,
|
|
284
283
|
*,
|
|
285
284
|
headers: HeaderType | None = None,
|
|
286
285
|
progress_callback: ProgressCallback | None = None,
|
|
@@ -324,7 +324,7 @@ async def post(
|
|
|
324
324
|
|
|
325
325
|
async def download(
|
|
326
326
|
url: str,
|
|
327
|
-
path:
|
|
327
|
+
path: StrPath,
|
|
328
328
|
*,
|
|
329
329
|
headers: HeaderType | None = None,
|
|
330
330
|
progress_callback: ProgressCallback | None = None,
|
|
@@ -621,7 +621,7 @@ def post_sync(
|
|
|
621
621
|
|
|
622
622
|
def download_sync(
|
|
623
623
|
url: str,
|
|
624
|
-
path:
|
|
624
|
+
path: StrPath,
|
|
625
625
|
*,
|
|
626
626
|
headers: HeaderType | None = None,
|
|
627
627
|
progress_callback: ProgressCallback | None = None,
|
|
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
|
|
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
|
|
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
|