anyenv 1.2.2__tar.gz → 1.2.3__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.3}/PKG-INFO +1 -1
- {anyenv-1.2.2 → anyenv-1.2.3}/pyproject.toml +1 -1
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/__init__.py +7 -1
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/calling/__init__.py +2 -1
- anyenv-1.2.3/src/anyenv/calling/multieventhandler.py +150 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/base.py +3 -4
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/functional.py +2 -2
- {anyenv-1.2.2 → anyenv-1.2.3}/LICENSE +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/README.md +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/anypath.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/async_run.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/calling/async_executor.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/calling/threadgroup.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/beam_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/daytona_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/docker_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/e2b_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/local_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/mcp_python_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/models.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/code_execution/subprocess_provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/aiohttp_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/aiohttp_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/exceptions.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/http_types.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/httpx_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/httpx_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/httpx_backend/js_transport.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/httpx_backend/serializer.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/pyodide_backend/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/download/pyodide_backend/backend.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/msgspec_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/msgspec_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/orjson_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/orjson_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/pydantic_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/pydantic_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/stdlib_provider/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/stdlib_provider/provider.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/json_tools/utils.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/log.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/package_install/__init__.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/package_install/base.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/package_install/functional.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/package_install/micropip_install.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/package_install/pip_install.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/parse_time.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/py.typed +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/src/anyenv/testing.py +0 -0
- {anyenv-1.2.2 → anyenv-1.2.3}/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,150 @@
|
|
|
1
|
+
"""Generic async callback manager for handling multiple event handlers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import contextlib
|
|
7
|
+
import inspect
|
|
8
|
+
from typing import TYPE_CHECKING, Literal
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from collections.abc import Awaitable, Callable, Sequence
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
ExecutionMode = Literal["sequential", "parallel"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MultiEventHandler[**P, T]:
|
|
19
|
+
"""Manages multiple callbacks/event handlers with sequential or parallel execution.
|
|
20
|
+
|
|
21
|
+
Provides a unified interface for executing multiple callbacks either sequentially
|
|
22
|
+
or in parallel, with support for dynamic handler management. All handlers must
|
|
23
|
+
have matching signatures enforced by ParamSpec. Sync functions are automatically
|
|
24
|
+
wrapped to work with the async interface.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
handlers: Initial list of async or sync callable event handlers
|
|
28
|
+
mode: Execution mode - "sequential" or "parallel"
|
|
29
|
+
|
|
30
|
+
Example:
|
|
31
|
+
```python
|
|
32
|
+
async def async_handler(x: int, y: str) -> str:
|
|
33
|
+
return f"Async Handler: {x}, {y}"
|
|
34
|
+
|
|
35
|
+
def sync_handler(x: int, y: str) -> str:
|
|
36
|
+
return f"Sync Handler: {x}, {y}"
|
|
37
|
+
|
|
38
|
+
manager = MultiEventHandler([async_handler, sync_handler])
|
|
39
|
+
results = await manager(42, "test")
|
|
40
|
+
```
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
handlers: Sequence[Callable[P, T] | Callable[P, Awaitable[T]]] | None = None,
|
|
46
|
+
mode: ExecutionMode = "sequential",
|
|
47
|
+
) -> None:
|
|
48
|
+
self._handlers: list[Callable[P, Awaitable[T]]] = []
|
|
49
|
+
if handlers:
|
|
50
|
+
for handler in handlers:
|
|
51
|
+
self.add_handler(handler)
|
|
52
|
+
self._mode: ExecutionMode = mode
|
|
53
|
+
|
|
54
|
+
async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> list[T]:
|
|
55
|
+
"""Execute all handlers with the given arguments.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
List of results from all handlers.
|
|
59
|
+
"""
|
|
60
|
+
if not self._handlers:
|
|
61
|
+
return []
|
|
62
|
+
|
|
63
|
+
if self._mode == "sequential":
|
|
64
|
+
return await self._execute_sequential(*args, **kwargs)
|
|
65
|
+
return await self._execute_parallel(*args, **kwargs)
|
|
66
|
+
|
|
67
|
+
async def _execute_sequential(self, *args: P.args, **kwargs: P.kwargs) -> list[T]:
|
|
68
|
+
"""Execute handlers sequentially."""
|
|
69
|
+
return [await handler(*args, **kwargs) for handler in self._handlers]
|
|
70
|
+
|
|
71
|
+
async def _execute_parallel(self, *args: P.args, **kwargs: P.kwargs) -> list[T]:
|
|
72
|
+
"""Execute handlers in parallel using asyncio.gather."""
|
|
73
|
+
tasks = [handler(*args, **kwargs) for handler in self._handlers]
|
|
74
|
+
return await asyncio.gather(*tasks)
|
|
75
|
+
|
|
76
|
+
def add_handler(self, handler: Callable[P, T] | Callable[P, Awaitable[T]]) -> None:
|
|
77
|
+
"""Add a new handler to the manager.
|
|
78
|
+
|
|
79
|
+
The handler must match the signature enforced by ParamSpec.
|
|
80
|
+
Both sync and async handlers are supported.
|
|
81
|
+
"""
|
|
82
|
+
# Check if handler is already async
|
|
83
|
+
if inspect.iscoroutinefunction(handler):
|
|
84
|
+
wrapped_handler = handler # type: ignore[assignment]
|
|
85
|
+
else:
|
|
86
|
+
# Wrap sync handler
|
|
87
|
+
wrapped_handler = self._wrap_sync_handler(handler) # type: ignore
|
|
88
|
+
|
|
89
|
+
if wrapped_handler not in self._handlers:
|
|
90
|
+
self._handlers.append(wrapped_handler)
|
|
91
|
+
|
|
92
|
+
def remove_handler(self, handler: Callable[P, T] | Callable[P, Awaitable[T]]) -> None:
|
|
93
|
+
"""Remove a handler from the manager.
|
|
94
|
+
|
|
95
|
+
Note: For sync handlers, you must pass the original sync function,
|
|
96
|
+
not the wrapped async version.
|
|
97
|
+
"""
|
|
98
|
+
# Try to find and remove the handler
|
|
99
|
+
# For sync handlers, we need to find the wrapped version
|
|
100
|
+
to_remove = None
|
|
101
|
+
|
|
102
|
+
if inspect.iscoroutinefunction(handler):
|
|
103
|
+
# Handler is async, find it directly
|
|
104
|
+
to_remove = handler # type: ignore[assignment]
|
|
105
|
+
else:
|
|
106
|
+
# Handler is sync, find the wrapped version
|
|
107
|
+
# We'll compare by checking the __wrapped__ attribute we can add
|
|
108
|
+
for wrapped in self._handlers:
|
|
109
|
+
if (
|
|
110
|
+
hasattr(wrapped, "_original_handler")
|
|
111
|
+
and wrapped._original_handler is handler # noqa: SLF001
|
|
112
|
+
): # type: ignore[attr-defined]
|
|
113
|
+
to_remove = wrapped # type: ignore
|
|
114
|
+
break
|
|
115
|
+
|
|
116
|
+
if to_remove:
|
|
117
|
+
with contextlib.suppress(ValueError):
|
|
118
|
+
self._handlers.remove(to_remove)
|
|
119
|
+
|
|
120
|
+
def _wrap_sync_handler(self, handler: Callable[P, T]) -> Callable[P, Awaitable[T]]:
|
|
121
|
+
"""Wrap a synchronous handler to work with async interface."""
|
|
122
|
+
|
|
123
|
+
async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
|
|
124
|
+
return handler(*args, **kwargs)
|
|
125
|
+
|
|
126
|
+
# Store reference to original handler for removal
|
|
127
|
+
async_wrapper._original_handler = handler # type: ignore[attr-defined] # noqa: SLF001
|
|
128
|
+
return async_wrapper
|
|
129
|
+
|
|
130
|
+
def clear(self) -> None:
|
|
131
|
+
"""Remove all handlers."""
|
|
132
|
+
self._handlers.clear()
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def mode(self) -> ExecutionMode:
|
|
136
|
+
"""Current execution mode."""
|
|
137
|
+
return self._mode
|
|
138
|
+
|
|
139
|
+
@mode.setter
|
|
140
|
+
def mode(self, value: ExecutionMode) -> None:
|
|
141
|
+
"""Set execution mode."""
|
|
142
|
+
self._mode = value
|
|
143
|
+
|
|
144
|
+
def __len__(self) -> int:
|
|
145
|
+
"""Return number of handlers."""
|
|
146
|
+
return len(self._handlers)
|
|
147
|
+
|
|
148
|
+
def __bool__(self) -> bool:
|
|
149
|
+
"""Return True if there are handlers registered."""
|
|
150
|
+
return bool(self._handlers)
|
|
@@ -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
|