Passed
Pull Request — master (#226)
by Steve
02:52
created

_AsyncContextBoundFunction.rebind()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 5
ccs 1
cts 2
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1 1
import inspect
2 1
import logging
3 1
from asyncio import Lock
4 1
from contextlib import AsyncExitStack
5 1
from copy import copy
6 1
from typing import (
7
    Optional,
8
    Type,
9
    TypeVar,
10
    Awaitable,
11
    Generic,
12
    Collection,
13
    Union,
14
    ContextManager,
15
    AsyncContextManager,
16
    Iterator,
17
    Generator,
18
    AsyncGenerator,
19
    Callable,
20
    List,
21
    Dict,
22
)
23
24 1
from lagom.container import Container
25 1
from lagom.functools import wraps
26 1
from lagom.definitions import Alias, ConstructionWithContainer, SingletonWrapper
27 1
from lagom.exceptions import InvalidDependencyDefinition, MissingFeature
28 1
from lagom.experimental.definitions import AsyncConstructionWithContainer
29 1
from lagom.interfaces import (
30
    ReadableContainer,
31
    SpecialDepDefinition,
32
    CallTimeContainerUpdate,
33
    ContainerBoundFunction,
34
)
35
36 1
T = TypeVar("T")
37 1
X = TypeVar("X")
38
39
40 1
class AwaitableSingleton(Generic[T]):
41 1
    instance: Optional[T]
42 1
    constructor: ConstructionWithContainer[Awaitable[T]]
43 1
    container: Container
44 1
    _lock: Lock
45
46 1
    def __init__(self, constructor: ConstructionWithContainer, container: Container):
47 1
        self.instance = None
48 1
        self.constructor = constructor  # type: ignore
49 1
        self.container = container
50 1
        self._lock = Lock()
51
52 1
    async def get(self) -> T:
53 1
        if not self.instance:
54 1
            async with self._lock:
55 1
                if not self.instance:
56 1
                    self.instance = await self.constructor.get_instance(self.container)
57 1
        return self.instance
58
59
60 1
class _AsyncContextBoundFunction(ContainerBoundFunction[X]):
61
    """
62
    Represents an instance of a function bound to an async context container
63
    """
64
65 1
    async_context_container: "AsyncContextContainer"
66 1
    partially_bound_function: ContainerBoundFunction
67
68 1
    __slots__ = ("async_context_container", "partially_bound_function")
69
70 1
    def __init__(
71
        self,
72
        async_context_container: "AsyncContextContainer",
73
        partially_bound_function: ContainerBoundFunction,
74
    ):
75 1
        self.async_context_container = async_context_container
76 1
        self.partially_bound_function = partially_bound_function
77
78 1
    def __call__(self, *args, **kwargs) -> X:
79 1
        return self.__async_call__(*args, **kwargs)
80
81 1
    async def __async_call__(self, *args, **kwargs):
82 1
        async with self.async_context_container as c:
83 1
            return await self.partially_bound_function.rebind(c)(*args, **kwargs)
84
85 1
    def rebind(self, container: ReadableContainer) -> "ContainerBoundFunction[X]":
86
        return wraps(self.partially_bound_function)(
87
            _AsyncContextBoundFunction(
88
                self.async_context_container,
89
                self.partially_bound_function.rebind(container),
90
            )
91
        )
92
93
94 1
class AsyncContextContainer(Container):
95 1
    async_exit_stack: Optional[AsyncExitStack] = None
96 1
    _context_types: Collection[Type]
97 1
    _context_singletons: Collection[Type]
98 1
    _root_context: bool = True
99
100 1
    def __init__(
101
        self,
102
        container: Container,
103
        context_types: Collection[Type],
104
        context_singletons: Collection[Type] = tuple(),
105
        log_undefined_deps: Union[bool, logging.Logger] = False,
106
    ):
107 1
        super().__init__(container, log_undefined_deps)
108 1
        self._context_types = set(context_types)
109 1
        self._context_singletons = set(context_singletons)
110
111 1
    def clone(self) -> "AsyncContextContainer":
112
        """returns a copy of the container
113
        :return:
114
        """
115 1
        return AsyncContextContainer(
116
            self,
117
            context_types=self._context_types,
118
            context_singletons=self._context_singletons,
119
            log_undefined_deps=self._undefined_logger,
120
        )
121
122 1
    async def __aenter__(self):
123 1
        if not self.async_exit_stack and self._root_context:
124 1
            self.async_exit_stack = AsyncExitStack()
125
126 1
        if self.async_exit_stack and self._root_context:
127
            # All actual context definitions happen on a clone so that there's isolation between invocations
128 1
            in_context = self.clone()
129 1
            in_context.async_exit_stack = AsyncExitStack()
130 1
            in_context._root_context = False
131
132 1
            for dep_type in self._context_types:
133 1
                managed_dep = self._context_type_def(dep_type)
134 1
                key = Awaitable[dep_type] if isinstance(managed_dep, AsyncConstructionWithContainer) else dep_type  # type: ignore
135 1
                in_context[key] = managed_dep  # type: ignore
136 1
            for dep_type in self._context_singletons:
137 1
                managed_singleton = self._singleton_type_def(dep_type)
138 1
                key = AwaitableSingleton[dep_type] if isinstance(managed_singleton, AwaitableSingleton) else dep_type  # type: ignore
139 1
                in_context[key] = managed_singleton  # type: ignore
140
141
            # The parent context manager keeps track of the inner clone
142 1
            await self.async_exit_stack.enter_async_context(in_context)
143 1
            return in_context
144 1
        return self
145
146 1
    async def __aexit__(self, exc_type, exc_val, exc_tb):
147 1
        if self.async_exit_stack:
148 1
            await self.async_exit_stack.aclose()
149 1
            self.async_exit_stack = None
150
151 1
    def partial(
152
        self,
153
        func: Callable[..., X],
154
        shared: Optional[List[Type]] = None,
155
        container_updater: Optional[CallTimeContainerUpdate] = None,
156
    ) -> ContainerBoundFunction[X]:
157 1
        if not inspect.iscoroutinefunction(func):
158
            raise MissingFeature(
159
                "AsyncContextManager currently can only deal with async functions"
160
            )
161 1
        base_partial = super(AsyncContextContainer, self).partial(
162
            func, shared, container_updater
163
        )
164
165 1
        return wraps(base_partial)(_AsyncContextBoundFunction(self, base_partial))
166
167 1
    def magic_partial(
168
        self,
169
        func: Callable[..., X],
170
        shared: Optional[List[Type]] = None,
171
        keys_to_skip: Optional[List[str]] = None,
172
        skip_pos_up_to: int = 0,
173
        container_updater: Optional[CallTimeContainerUpdate] = None,
174
    ) -> ContainerBoundFunction[X]:
175 1
        if not inspect.iscoroutinefunction(func):
176
            raise MissingFeature(
177
                "AsyncContextManager currently can only deal with async functions"
178
            )
179 1
        base_partial = super(AsyncContextContainer, self).magic_partial(
180
            func, shared, keys_to_skip, skip_pos_up_to, container_updater
181
        )
182
183 1
        return wraps(base_partial)(_AsyncContextBoundFunction(self, base_partial))
184
185 1
    def _context_type_def(self, dep_type: Type):
186 1
        type_def = self.get_definition(ContextManager[dep_type]) or self.get_definition(Iterator[dep_type]) or self.get_definition(Generator[dep_type, None, None]) or self.get_definition(AsyncGenerator[dep_type, None]) or self.get_definition(AsyncContextManager[dep_type])  # type: ignore
187 1
        if type_def is None:
188 1
            raise InvalidDependencyDefinition(
189
                f"A ContextManager[{dep_type}] should be defined. "
190
                f"This could be an Iterator[{dep_type}] or Generator[{dep_type}, None, None] "
191
                f"with the @contextmanager decorator"
192
            )
193 1
        if isinstance(type_def, Alias):
194
            # Without this we create a definition that points to
195
            # itself.
196 1
            type_def = copy(type_def)
197 1
            type_def.skip_definitions = True
198 1
        if self.get_definition(AsyncGenerator[dep_type, None]) or self.get_definition(AsyncContextManager[dep_type]):  # type: ignore
199 1
            return AsyncConstructionWithContainer(lambda c: self._async_context_resolver(c, type_def))  # type: ignore
200 1
        return ConstructionWithContainer(lambda c: self._context_resolver(c, type_def))  # type: ignore
201
202 1
    def _context_resolver(self, c: ReadableContainer, type_def: SpecialDepDefinition):
203
        """
204
        Takes an existing definition which must be a context manager. Returns
205
        the value of the context manager from __enter__ and then places the
206
        __exit__ in this container's exit stack
207
        """
208 1
        assert self.async_exit_stack, "Types can only be resolved within an async with"
209 1
        context_manager = type_def.get_instance(c)
210 1
        return self.async_exit_stack.enter_context(context_manager)
211
212 1
    def _async_context_resolver(
213
        self, c: ReadableContainer, type_def: SpecialDepDefinition
214
    ):
215
        """
216
        Takes an existing definition which must be a context manager. Returns
217
        the value of the context manager from __aenter__ and then places the
218
        __aexit__ in this container's exit stack
219
        """
220 1
        assert self.async_exit_stack, "Types can only be resolved within an async with"
221 1
        context_manager = type_def.get_instance(c)
222 1
        return self.async_exit_stack.enter_async_context(context_manager)
223
224 1
    def _singleton_type_def(self, dep_type: Type):
225
        """
226
        The same as context_type_def but acts as a singleton within this container
227
        """
228 1
        type_def = self._context_type_def(dep_type)
229 1
        if isinstance(type_def, AsyncConstructionWithContainer):
230 1
            return AwaitableSingleton(type_def, self)
231
        return SingletonWrapper(type_def)
232