-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathregistry.py
59 lines (43 loc) · 1.56 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import Any, Callable, Dict, Optional, Tuple, Type
from .constructor import Constructor
class InjectionRegistry:
def __init__(self):
self._registry: Dict[Type, Optional[Callable[..., Any]]] = {}
def __getitem__(self, key):
return self._registry[key]
def __str__(self) -> str:
return str(self._registry)
def __contains__(self, other: Any):
return other in self._registry
def get(self, key, default=None):
return self._registry.get(key, default)
def register(
self, _type: Type, constructor: Optional[Callable[..., Any]]
) -> None:
if constructor:
constructor = Constructor(constructor)
self._registry[_type] = constructor
def finalize(self, allowed_types):
for constructor in self._registry.values():
if isinstance(constructor, Constructor):
constructor.prepare(self, allowed_types)
@property
def length(self):
return len(self._registry)
class SignatureRegistry:
def __init__(self):
self._registry: Dict[
str, Dict[str, Tuple[Type, Optional[Callable[..., Any]]]]
] = {}
def __getitem__(self, key):
return self._registry[key]
def __str__(self) -> str:
return str(self._registry)
def get(self, key, default=None):
return self._registry.get(key, default)
def register(
self,
route_name: str,
injections: Dict[str, Tuple[Type, Optional[Callable[..., Any]]]],
) -> None:
self._registry[route_name] = injections