|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from collections.abc import AsyncIterator, Awaitable, Callable, Coroutine, Iterator |
| 5 | +from contextlib import asynccontextmanager, contextmanager |
| 6 | +from dataclasses import dataclass, field |
| 7 | +from functools import wraps |
| 8 | +from types import MethodType |
| 9 | +from types import ModuleType as PythonModule |
| 10 | +from typing import Any, Self, final, overload |
| 11 | + |
| 12 | +from injection import Module, mod |
| 13 | +from injection.loaders import PythonModuleLoader |
| 14 | + |
| 15 | +__all__ = ("AsyncEntrypoint", "Entrypoint", "autocall", "entrypointmaker") |
| 16 | + |
| 17 | +type AsyncEntrypoint[**P, T] = Entrypoint[P, Coroutine[Any, Any, T]] |
| 18 | +type EntrypointDecorator[**P, T1, T2] = Callable[[Callable[P, T1]], Callable[P, T2]] |
| 19 | +type EntrypointSetupMethod[*Ts, **P, T1, T2] = Callable[ |
| 20 | + [Entrypoint[P, T1], *Ts], |
| 21 | + Entrypoint[P, T2], |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +def autocall[**P, T](wrapped: Callable[P, T] | None = None, /) -> Any: |
| 26 | + def decorator(wp: Callable[P, T]) -> Callable[P, T]: |
| 27 | + wp() # type: ignore[call-arg] |
| 28 | + return wp |
| 29 | + |
| 30 | + return decorator(wrapped) if wrapped else decorator |
| 31 | + |
| 32 | + |
| 33 | +@overload |
| 34 | +def entrypointmaker[*Ts, **P, T1, T2]( |
| 35 | + wrapped: EntrypointSetupMethod[*Ts, P, T1, T2], |
| 36 | + /, |
| 37 | + *, |
| 38 | + module: Module | None = ..., |
| 39 | +) -> EntrypointDecorator[P, T1, T2]: ... |
| 40 | + |
| 41 | + |
| 42 | +@overload |
| 43 | +def entrypointmaker[*Ts, **P, T1, T2]( |
| 44 | + wrapped: None = ..., |
| 45 | + /, |
| 46 | + *, |
| 47 | + module: Module | None = ..., |
| 48 | +) -> Callable[ |
| 49 | + [EntrypointSetupMethod[*Ts, P, T1, T2]], |
| 50 | + EntrypointDecorator[P, T1, T2], |
| 51 | +]: ... |
| 52 | + |
| 53 | + |
| 54 | +def entrypointmaker[*Ts, **P, T1, T2]( |
| 55 | + wrapped: EntrypointSetupMethod[*Ts, P, T1, T2] | None = None, |
| 56 | + /, |
| 57 | + *, |
| 58 | + module: Module | None = None, |
| 59 | +) -> Any: |
| 60 | + def decorator( |
| 61 | + wp: EntrypointSetupMethod[*Ts, P, T1, T2], |
| 62 | + ) -> EntrypointDecorator[P, T1, T2]: |
| 63 | + return Entrypoint._make_decorator(wp, module) |
| 64 | + |
| 65 | + return decorator(wrapped) if wrapped else decorator |
| 66 | + |
| 67 | + |
| 68 | +@final |
| 69 | +@dataclass(repr=False, eq=False, frozen=True, slots=True) |
| 70 | +class Entrypoint[**P, T]: |
| 71 | + function: Callable[P, T] |
| 72 | + module: Module = field(default_factory=mod) |
| 73 | + |
| 74 | + def __call__(self, /, *args: P.args, **kwargs: P.kwargs) -> T: |
| 75 | + return self.function(*args, **kwargs) |
| 76 | + |
| 77 | + def async_to_sync[_T]( |
| 78 | + self: AsyncEntrypoint[P, _T], |
| 79 | + run: Callable[[Coroutine[Any, Any, _T]], _T] = asyncio.run, |
| 80 | + /, |
| 81 | + ) -> Entrypoint[P, _T]: |
| 82 | + function = self.function |
| 83 | + |
| 84 | + @wraps(function) |
| 85 | + def wrapper(*args: P.args, **kwargs: P.kwargs) -> _T: |
| 86 | + return run(function(*args, **kwargs)) |
| 87 | + |
| 88 | + return self.__recreate(wrapper) |
| 89 | + |
| 90 | + def decorate( |
| 91 | + self, |
| 92 | + decorator: Callable[[Callable[P, T]], Callable[P, T]], |
| 93 | + /, |
| 94 | + ) -> Self: |
| 95 | + return self.__recreate(decorator(self.function)) |
| 96 | + |
| 97 | + def inject(self) -> Self: |
| 98 | + return self.decorate(self.module.make_injected_function) |
| 99 | + |
| 100 | + def load_modules( |
| 101 | + self, |
| 102 | + /, |
| 103 | + loader: PythonModuleLoader, |
| 104 | + *packages: PythonModule | str, |
| 105 | + ) -> Self: |
| 106 | + return self.setup(lambda: loader.load(*packages)) |
| 107 | + |
| 108 | + def setup(self, function: Callable[..., Any], /) -> Self: |
| 109 | + @contextmanager |
| 110 | + def decorator() -> Iterator[Any]: |
| 111 | + yield function() |
| 112 | + |
| 113 | + return self.decorate(decorator()) |
| 114 | + |
| 115 | + def async_setup[_T]( |
| 116 | + self: AsyncEntrypoint[P, _T], |
| 117 | + function: Callable[..., Awaitable[Any]], |
| 118 | + /, |
| 119 | + ) -> AsyncEntrypoint[P, _T]: |
| 120 | + @asynccontextmanager |
| 121 | + async def decorator() -> AsyncIterator[Any]: |
| 122 | + yield await function() |
| 123 | + |
| 124 | + return self.decorate(decorator()) |
| 125 | + |
| 126 | + def __recreate[**_P, _T]( |
| 127 | + self: Entrypoint[Any, Any], |
| 128 | + function: Callable[_P, _T], |
| 129 | + /, |
| 130 | + ) -> Entrypoint[_P, _T]: |
| 131 | + return type(self)(function, self.module) |
| 132 | + |
| 133 | + @classmethod |
| 134 | + def _make_decorator[*Ts, _T]( |
| 135 | + cls, |
| 136 | + setup_method: EntrypointSetupMethod[*Ts, P, T, _T], |
| 137 | + /, |
| 138 | + module: Module | None = None, |
| 139 | + ) -> EntrypointDecorator[P, T, _T]: |
| 140 | + module = module or mod() |
| 141 | + setup_method = module.make_injected_function(setup_method) |
| 142 | + |
| 143 | + def decorator(function: Callable[P, T]) -> Callable[P, _T]: |
| 144 | + self = cls(function, module) |
| 145 | + return MethodType(setup_method, self)().function |
| 146 | + |
| 147 | + return decorator |
0 commit comments