|
1
|
|
|
""" |
|
2
|
|
|
Interfaces shared by modules within the lagom package |
|
3
|
|
|
""" |
|
4
|
1 |
|
from abc import ABC, abstractmethod |
|
5
|
1 |
|
from typing import ( |
|
6
|
|
|
Generic, |
|
7
|
|
|
TypeVar, |
|
8
|
|
|
Type, |
|
9
|
|
|
Any, |
|
10
|
|
|
Callable, |
|
11
|
|
|
Union, |
|
12
|
|
|
List, |
|
13
|
|
|
Set, |
|
14
|
|
|
Optional, |
|
15
|
|
|
Dict, |
|
16
|
|
|
NoReturn, |
|
17
|
|
|
Protocol, |
|
18
|
|
|
) |
|
19
|
|
|
|
|
20
|
1 |
|
X = TypeVar("X") |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
1 |
|
BuildingFunction = Callable[[Any], Any] |
|
24
|
|
|
|
|
25
|
|
|
# A function which takes a container, args and kwargs and updates |
|
26
|
|
|
# the container before any dependency resolution happens |
|
27
|
1 |
|
CallTimeContainerUpdate = Callable[["WriteableContainer", List, Dict], None] |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
1 |
|
class ReadableContainer(ABC): |
|
31
|
|
|
""" |
|
32
|
|
|
Represents a container that can resolve dependencies |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
1 |
|
@abstractmethod |
|
36
|
1 |
|
def resolve( |
|
37
|
|
|
self, dep_type: Type[X], suppress_error=False, skip_definitions=False |
|
38
|
|
|
) -> X: |
|
39
|
|
|
"""Constructs an object of type X""" |
|
40
|
|
|
pass |
|
41
|
|
|
|
|
42
|
1 |
|
@abstractmethod |
|
43
|
1 |
|
def partial( |
|
44
|
|
|
self, |
|
45
|
|
|
func: Callable[..., X], |
|
46
|
|
|
shared: Optional[List[Type]] = None, |
|
47
|
|
|
container_updater: Optional[CallTimeContainerUpdate] = None, |
|
48
|
|
|
) -> Callable[..., X]: |
|
49
|
|
|
pass |
|
50
|
|
|
|
|
51
|
1 |
|
@abstractmethod |
|
52
|
1 |
|
def magic_partial( |
|
53
|
|
|
self, |
|
54
|
|
|
func: Callable[..., X], |
|
55
|
|
|
shared: Optional[List[Type]] = None, |
|
56
|
|
|
keys_to_skip: Optional[List[str]] = None, |
|
57
|
|
|
skip_pos_up_to: int = 0, |
|
58
|
|
|
container_updater: Optional[CallTimeContainerUpdate] = None, |
|
59
|
|
|
) -> Callable[..., X]: |
|
60
|
|
|
pass |
|
61
|
|
|
|
|
62
|
1 |
|
@abstractmethod |
|
63
|
1 |
|
def __getitem__(self, dep: Type[X]) -> X: |
|
64
|
|
|
"""Shortcut to calling resolve""" |
|
65
|
|
|
pass |
|
66
|
|
|
|
|
67
|
1 |
|
@property |
|
68
|
1 |
|
@abstractmethod |
|
69
|
1 |
|
def defined_types(self) -> Set[Type]: |
|
70
|
|
|
"""Set of all the types defined in the container""" |
|
71
|
|
|
pass |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
1 |
|
class WriteableContainer(ReadableContainer): |
|
75
|
|
|
""" |
|
76
|
|
|
Represents a container that is mutable and can have |
|
77
|
|
|
new definitions added. |
|
78
|
|
|
|
|
79
|
|
|
""" |
|
80
|
|
|
|
|
81
|
1 |
|
@abstractmethod |
|
82
|
1 |
|
def __setitem__(self, dep: Type[X], resolver: "TypeResolver[X]"): |
|
83
|
|
|
""" |
|
84
|
|
|
forwards to WriteableContainer.define |
|
85
|
|
|
|
|
86
|
|
|
:param dep: |
|
87
|
|
|
:param resolver: |
|
88
|
|
|
:return: |
|
89
|
|
|
""" |
|
90
|
|
|
pass |
|
91
|
|
|
|
|
92
|
1 |
|
@abstractmethod |
|
93
|
1 |
|
def define( |
|
94
|
|
|
self, dep: Type[X], resolver: "TypeResolver[X]" |
|
95
|
|
|
) -> "SpecialDepDefinition": |
|
96
|
|
|
""" |
|
97
|
|
|
Sets the resolver for type "dep" |
|
98
|
|
|
|
|
99
|
|
|
:param dep: |
|
100
|
|
|
:param resolver: |
|
101
|
|
|
:return: |
|
102
|
|
|
""" |
|
103
|
|
|
pass |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
1 |
|
class ExtendableContainer(ReadableContainer): |
|
107
|
|
|
""" |
|
108
|
|
|
A container that is extentable can be cloned with the clone |
|
109
|
|
|
being mutable enabling extension. |
|
110
|
|
|
""" |
|
111
|
|
|
|
|
112
|
1 |
|
@abstractmethod |
|
113
|
1 |
|
def clone(self) -> WriteableContainer: |
|
114
|
|
|
"""returns a copy of the container in a mutable state |
|
115
|
|
|
so new updates can be applied |
|
116
|
|
|
:return: |
|
117
|
|
|
""" |
|
118
|
|
|
pass |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
1 |
|
class SpecialDepDefinition(ABC, Generic[X]): |
|
122
|
|
|
""" |
|
123
|
|
|
Represents a special way of loading a dependency. |
|
124
|
|
|
""" |
|
125
|
|
|
|
|
126
|
1 |
|
@abstractmethod |
|
127
|
1 |
|
def get_instance(self, container: ReadableContainer) -> X: |
|
128
|
|
|
"""constructs the represented type(X). |
|
129
|
|
|
|
|
130
|
|
|
:param container: an instance of the current container |
|
131
|
|
|
:return: |
|
132
|
|
|
""" |
|
133
|
|
|
pass |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
1 |
|
class DefinitionsSource(ABC): |
|
137
|
|
|
""" |
|
138
|
|
|
Stores the mapppings between a type and the definition of how |
|
139
|
|
|
to construct that type |
|
140
|
|
|
""" |
|
141
|
|
|
|
|
142
|
1 |
|
@abstractmethod |
|
143
|
1 |
|
def get_definition(self, dep_type: Type[X]) -> Optional[SpecialDepDefinition[X]]: |
|
144
|
|
|
""" |
|
145
|
|
|
For a supplied type returns the definition of how to build that type. |
|
146
|
|
|
If unknown None is returned |
|
147
|
|
|
:param dep_type: |
|
148
|
|
|
""" |
|
149
|
|
|
pass |
|
150
|
|
|
|
|
151
|
1 |
|
@property |
|
152
|
1 |
|
@abstractmethod |
|
153
|
1 |
|
def defined_types(self) -> Set[Type]: |
|
154
|
|
|
""" |
|
155
|
|
|
The list of types that have been explicitly defined |
|
156
|
|
|
:return: |
|
157
|
|
|
""" |
|
158
|
|
|
pass |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
1 |
|
class ContainerDebugInfo(ABC): |
|
162
|
|
|
""" |
|
163
|
|
|
This object provides an overview of the state of a dependency injection |
|
164
|
|
|
container |
|
165
|
|
|
""" |
|
166
|
|
|
|
|
167
|
1 |
|
@property |
|
168
|
1 |
|
@abstractmethod |
|
169
|
1 |
|
def defined_types(self) -> Set[Type]: |
|
170
|
|
|
""" |
|
171
|
|
|
The list of types that have been explicitly defined |
|
172
|
|
|
:return: |
|
173
|
|
|
""" |
|
174
|
|
|
pass |
|
175
|
|
|
|
|
176
|
1 |
|
@property |
|
177
|
1 |
|
@abstractmethod |
|
178
|
1 |
|
def reflection_cache_overview(self) -> Dict[str, str]: |
|
179
|
|
|
""" |
|
180
|
|
|
A summary of what runtime reflection has been performed by lagom |
|
181
|
|
|
This will be empty if types have only be loaded from explicit |
|
182
|
|
|
definitions |
|
183
|
|
|
:return: |
|
184
|
|
|
""" |
|
185
|
|
|
pass |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
1 |
|
class ContainerBoundItem(Protocol): |
|
189
|
1 |
|
def rebind(self, container: ReadableContainer) -> "ContainerBoundItem": |
|
190
|
|
|
pass |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
1 |
|
class ContainerBoundFunction(ContainerBoundItem, Protocol[X]): |
|
194
|
1 |
|
def __call__(self, *args, **kwargs) -> X: |
|
195
|
|
|
pass |
|
196
|
|
|
|
|
197
|
1 |
|
def rebind(self, container: ReadableContainer) -> "ContainerBoundFunction[X]": |
|
198
|
|
|
pass |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
1 |
|
T = TypeVar("T") |
|
202
|
|
|
|
|
203
|
|
|
""" |
|
204
|
|
|
The TypeResolver represents the way that lagom can be |
|
205
|
|
|
told about how to define a type. Any of these types |
|
206
|
|
|
can be assigned to the container. |
|
207
|
|
|
""" |
|
208
|
1 |
|
TypeResolver = Union[ |
|
209
|
|
|
Type[T], # An alias from one type to the next |
|
210
|
|
|
Callable[[], T], # A resolution function |
|
211
|
|
|
Callable[[ReadableContainer], T], # A resolution function that takes the container |
|
212
|
|
|
SpecialDepDefinition[T], # From the definitions module |
|
213
|
|
|
SpecialDepDefinition[NoReturn], # Some types don't resolve |
|
214
|
|
|
T, # Just an instance of the type - A singleton |
|
215
|
|
|
] |
|
216
|
|
|
|