1
|
|
|
""" |
2
|
|
|
Code in this module is used to wrap and decorate functions that have been |
3
|
|
|
bound to a container |
4
|
|
|
""" |
5
|
1 |
|
import functools |
6
|
1 |
|
import inspect |
7
|
1 |
|
from typing import Callable |
8
|
|
|
|
9
|
1 |
|
from .exceptions import UnableToInvokeBoundFunction |
10
|
1 |
|
from .injection_context import TemporaryInjectionContext |
11
|
1 |
|
from .interfaces import ReadableContainer, ContainerBoundFunction |
12
|
1 |
|
from .util.reflection import FunctionSpec |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
class ContainerBoundFunc: |
16
|
|
|
""" |
17
|
|
|
Represents a function that has been bound to a container |
18
|
|
|
""" |
19
|
|
|
|
20
|
1 |
|
_argument_updater: Callable |
21
|
1 |
|
_base_injection_context: TemporaryInjectionContext |
22
|
1 |
|
_inner_func: Callable |
23
|
|
|
|
24
|
1 |
|
def __init__(self, inner_func, base_injection_context, argument_updater): |
25
|
1 |
|
self._inner_func = inner_func |
26
|
1 |
|
self._base_injection_context = base_injection_context |
27
|
1 |
|
self._argument_updater = argument_updater |
28
|
|
|
|
29
|
1 |
|
def __call__(self, *args, **kwargs): |
30
|
1 |
|
argument_updater = self._argument_updater |
31
|
1 |
|
inner_func = self._inner_func |
32
|
1 |
|
bound_args, bound_kwargs = argument_updater( |
33
|
|
|
self._base_injection_context, args, kwargs |
34
|
|
|
) |
35
|
1 |
|
return inner_func(*bound_args, **bound_kwargs) |
36
|
|
|
|
37
|
1 |
|
def rebind(self, container: ReadableContainer): |
38
|
1 |
|
return ContainerBoundFunc( |
39
|
|
|
self._inner_func, |
40
|
|
|
self._base_injection_context.rebound_to(container), |
41
|
|
|
self._argument_updater, |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
class ContainerAsyncBoundFunc: |
46
|
|
|
""" |
47
|
|
|
Represents an async function that has been bound to a container |
48
|
|
|
""" |
49
|
|
|
|
50
|
1 |
|
_argument_updater: Callable |
51
|
1 |
|
_base_injection_context: TemporaryInjectionContext |
52
|
1 |
|
_inner_func: Callable |
53
|
|
|
|
54
|
1 |
|
def __init__(self, inner_func, base_injection_context, argument_updater): |
55
|
1 |
|
self._inner_func = inner_func |
56
|
1 |
|
self._base_injection_context = base_injection_context |
57
|
1 |
|
self._argument_updater = argument_updater |
58
|
|
|
|
59
|
1 |
|
async def __async_call__(self, *args, **kwargs): |
60
|
1 |
|
argument_updater = self._argument_updater |
61
|
1 |
|
inner_func = self._inner_func |
62
|
1 |
|
bound_args, bound_kwargs = argument_updater( |
63
|
|
|
self._base_injection_context, args, kwargs |
64
|
|
|
) |
65
|
1 |
|
return await inner_func(*bound_args, **bound_kwargs) |
66
|
|
|
|
67
|
1 |
|
def as_coroutine(self): |
68
|
|
|
""" |
69
|
|
|
returns a coroutine that wraps this class. Some class methods |
70
|
|
|
also get added to the coroutine. This is so it acts like a class with |
71
|
|
|
an __asynccall__ magic method. |
72
|
|
|
""" |
73
|
|
|
|
74
|
1 |
|
async def _coroutine_func(*args, **kwargs): |
75
|
1 |
|
return await self.__async_call__(*args, **kwargs) |
76
|
|
|
|
77
|
1 |
|
_coroutine_func.rebind = self.rebind # type: ignore |
78
|
|
|
|
79
|
1 |
|
return _coroutine_func |
80
|
|
|
|
81
|
1 |
|
def rebind(self, container: ReadableContainer): |
82
|
|
|
return ContainerAsyncBoundFunc( |
83
|
|
|
self._inner_func, |
84
|
|
|
self._base_injection_context.rebound_to(container), |
85
|
|
|
self._argument_updater, |
86
|
|
|
).as_coroutine() |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
def apply_argument_updater( |
90
|
|
|
func, |
91
|
|
|
base_injection_context, |
92
|
|
|
argument_updater, |
93
|
|
|
spec: FunctionSpec, |
94
|
|
|
catch_errors=False, |
95
|
|
|
) -> ContainerBoundFunction: |
96
|
|
|
""" |
97
|
|
|
Takes a function and binds it to a container with an update function |
98
|
|
|
""" |
99
|
1 |
|
inner_func = func if not catch_errors else _wrap_func_in_error_handling(func, spec) |
100
|
1 |
|
if inspect.iscoroutinefunction(func): |
101
|
|
|
|
102
|
1 |
|
_bound_func = ContainerAsyncBoundFunc( |
103
|
|
|
inner_func, base_injection_context, argument_updater |
104
|
|
|
).as_coroutine() |
105
|
|
|
|
106
|
|
|
else: |
107
|
|
|
|
108
|
1 |
|
_bound_func = ContainerBoundFunc( |
109
|
|
|
inner_func, base_injection_context, argument_updater |
110
|
|
|
) |
111
|
|
|
|
112
|
1 |
|
return functools.wraps(func)(_bound_func) |
113
|
|
|
|
114
|
|
|
|
115
|
1 |
|
def _wrap_func_in_error_handling(func, spec: FunctionSpec): |
116
|
|
|
""" |
117
|
|
|
Takes a func and its spec and returns a function that's the same |
118
|
|
|
but with more useful TypeError messages |
119
|
|
|
:param func: |
120
|
|
|
:param spec: |
121
|
|
|
:return: |
122
|
|
|
""" |
123
|
|
|
|
124
|
1 |
|
@functools.wraps(func) |
125
|
1 |
|
def _error_handling_func(*args, **kwargs): |
126
|
1 |
|
try: |
127
|
1 |
|
return func(*args, **kwargs) |
128
|
1 |
|
except TypeError as error: |
129
|
|
|
# if it wasn't in kwargs the container couldn't build it |
130
|
1 |
|
unresolvable_deps = [ |
131
|
|
|
dep_type |
132
|
|
|
for (name, dep_type) in spec.annotations.items() |
133
|
|
|
if name not in kwargs.keys() |
134
|
|
|
] |
135
|
1 |
|
raise UnableToInvokeBoundFunction(str(error), unresolvable_deps) |
136
|
|
|
|
137
|
|
|
return _error_handling_func |
138
|
|
|
|