|
1 | | -from typing import Self |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import AsyncIterator, Iterator, Mapping |
| 4 | +from contextlib import asynccontextmanager, contextmanager |
| 5 | +from dataclasses import dataclass |
| 6 | +from types import MappingProxyType |
| 7 | +from typing import Any, Self, get_args, get_origin, get_type_hints |
2 | 8 |
|
3 | 9 | from injection._core.common.invertible import Invertible |
4 | 10 | from injection._core.common.type import InputType |
5 | 11 | from injection._core.module import Module, mod |
| 12 | +from injection._core.scope import ScopeKind, ScopeKindStr, adefine_scope, define_scope |
| 13 | +from injection._core.slots import SlotKey |
| 14 | + |
| 15 | +type Scoped[T] = T |
| 16 | + |
| 17 | + |
| 18 | +class MappedScope: |
| 19 | + __slots__ = ("__keys", "__module", "__name", "__owner") |
| 20 | + |
| 21 | + __keys: Mapping[str, SlotKey[Any]] |
| 22 | + __module: Module |
| 23 | + __name: str |
| 24 | + __owner: type | None |
| 25 | + |
| 26 | + def __init__(self, name: str, /, module: Module | None = None) -> None: |
| 27 | + self.__module = module or mod() |
| 28 | + self.__name = name |
| 29 | + self.__owner = None |
| 30 | + |
| 31 | + def __get__( |
| 32 | + self, |
| 33 | + instance: object | None = None, |
| 34 | + owner: type | None = None, |
| 35 | + ) -> Self | BoundMappedScope: |
| 36 | + if instance is None: |
| 37 | + return self |
| 38 | + |
| 39 | + mapping = self.__mapping_from(instance) |
| 40 | + return BoundMappedScope(self.__name, mapping) |
| 41 | + |
| 42 | + def __set_name__(self, owner: type, name: str) -> None: |
| 43 | + if self.__owner: |
| 44 | + raise TypeError(f"`{self}` owner is already defined.") |
| 45 | + |
| 46 | + self.__keys = MappingProxyType(dict(self.__generate_keys(owner))) |
| 47 | + self.__owner = owner |
| 48 | + |
| 49 | + def __generate_keys(self, cls: type) -> Iterator[tuple[str, SlotKey[Any]]]: |
| 50 | + for name, hint in get_type_hints(cls).items(): |
| 51 | + if get_origin(hint) is not Scoped: |
| 52 | + continue |
| 53 | + |
| 54 | + annotation = get_args(hint)[0] |
| 55 | + key = self.__module.reserve_scoped_slot(annotation, scope_name=self.__name) |
| 56 | + yield name, key |
| 57 | + |
| 58 | + def __mapping_from(self, instance: object) -> dict[SlotKey[Any], Any]: |
| 59 | + return { |
| 60 | + key: value |
| 61 | + for name, key in self.__keys.items() |
| 62 | + if (value := getattr(instance, name, None)) is not None |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +@dataclass(repr=False, eq=False, frozen=True, slots=True) |
| 67 | +class BoundMappedScope: |
| 68 | + name: str |
| 69 | + mapping: Mapping[SlotKey[Any], Any] |
| 70 | + |
| 71 | + @asynccontextmanager |
| 72 | + async def adefine( |
| 73 | + self, |
| 74 | + /, |
| 75 | + kind: ScopeKind | ScopeKindStr = ScopeKind.get_default(), |
| 76 | + threadsafe: bool | None = None, |
| 77 | + ) -> AsyncIterator[None]: |
| 78 | + async with adefine_scope(self.name, kind, threadsafe) as scope: |
| 79 | + if mapping := self.mapping: |
| 80 | + scope.slot_map(mapping) |
| 81 | + |
| 82 | + yield |
| 83 | + |
| 84 | + @contextmanager |
| 85 | + def define( |
| 86 | + self, |
| 87 | + /, |
| 88 | + kind: ScopeKind | ScopeKindStr = ScopeKind.get_default(), |
| 89 | + threadsafe: bool | None = None, |
| 90 | + ) -> Iterator[None]: |
| 91 | + with define_scope(self.name, kind, threadsafe) as scope: |
| 92 | + if mapping := self.mapping: |
| 93 | + scope.slot_map(mapping) |
| 94 | + |
| 95 | + yield |
6 | 96 |
|
7 | 97 |
|
8 | 98 | class LazyInstance[T]: |
|
0 commit comments