anyenv 1.2.3__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.
Files changed (56) hide show
  1. {anyenv-1.2.3 → anyenv-1.2.4}/PKG-INFO +1 -1
  2. {anyenv-1.2.3 → anyenv-1.2.4}/pyproject.toml +1 -1
  3. anyenv-1.2.4/src/anyenv/calling/multieventhandler.py +182 -0
  4. anyenv-1.2.3/src/anyenv/calling/multieventhandler.py +0 -150
  5. {anyenv-1.2.3 → anyenv-1.2.4}/LICENSE +0 -0
  6. {anyenv-1.2.3 → anyenv-1.2.4}/README.md +0 -0
  7. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/__init__.py +0 -0
  8. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/anypath.py +0 -0
  9. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/async_run.py +0 -0
  10. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/calling/__init__.py +0 -0
  11. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/calling/async_executor.py +0 -0
  12. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/calling/threadgroup.py +0 -0
  13. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/__init__.py +0 -0
  14. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/base.py +0 -0
  15. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/beam_provider.py +0 -0
  16. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/daytona_provider.py +0 -0
  17. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/docker_provider.py +0 -0
  18. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/e2b_provider.py +0 -0
  19. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/local_provider.py +0 -0
  20. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/mcp_python_provider.py +0 -0
  21. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/models.py +0 -0
  22. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/code_execution/subprocess_provider.py +0 -0
  23. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/__init__.py +0 -0
  24. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/aiohttp_backend/__init__.py +0 -0
  25. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/aiohttp_backend/backend.py +0 -0
  26. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/base.py +0 -0
  27. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/exceptions.py +0 -0
  28. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/functional.py +0 -0
  29. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/http_types.py +0 -0
  30. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/__init__.py +0 -0
  31. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/backend.py +0 -0
  32. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/js_transport.py +0 -0
  33. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/httpx_backend/serializer.py +0 -0
  34. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/pyodide_backend/__init__.py +0 -0
  35. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/download/pyodide_backend/backend.py +0 -0
  36. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/__init__.py +0 -0
  37. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/base.py +0 -0
  38. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/msgspec_provider/__init__.py +0 -0
  39. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/msgspec_provider/provider.py +0 -0
  40. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/orjson_provider/__init__.py +0 -0
  41. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/orjson_provider/provider.py +0 -0
  42. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/pydantic_provider/__init__.py +0 -0
  43. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/pydantic_provider/provider.py +0 -0
  44. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/stdlib_provider/__init__.py +0 -0
  45. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/stdlib_provider/provider.py +0 -0
  46. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/json_tools/utils.py +0 -0
  47. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/log.py +0 -0
  48. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/package_install/__init__.py +0 -0
  49. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/package_install/base.py +0 -0
  50. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/package_install/functional.py +0 -0
  51. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/package_install/micropip_install.py +0 -0
  52. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/package_install/pip_install.py +0 -0
  53. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/parse_time.py +0 -0
  54. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/py.typed +0 -0
  55. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/testing.py +0 -0
  56. {anyenv-1.2.3 → anyenv-1.2.4}/src/anyenv/validate.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: anyenv
3
- Version: 1.2.3
3
+ Version: 1.2.4
4
4
  Summary: Compatibility layer for some basic operations to allow painless operation in PyOdide and Python pre-releases
5
5
  Keywords:
6
6
  Author: Philipp Temminghoff
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "anyenv"
3
- version = "1.2.3"
3
+ version = "1.2.4"
4
4
  description = "Compatibility layer for some basic operations to allow painless operation in PyOdide and Python pre-releases"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -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)
@@ -1,150 +0,0 @@
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)
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