forked from danielmoumeny212/pynest_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pynest_app_context.py
56 lines (40 loc) · 1.61 KB
/
pynest_app_context.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
from module import ModuleCompiler, Module
from typing import Union
from pynest_container import PyNestContainer
from exceptions import UnknownModuleException
import logging
class PyNestApplicationContext:
_is_initialized = False
_module_compiler = ModuleCompiler()
@property
def is_initialized(self):
return self._is_initialized
@is_initialized.setter
def is_initialized(self, value: bool):
self._is_initialized = value
def init(self):
if self._is_initialized:
return self
self._is_initialized = True
return self
def __init__(self,
container: PyNestContainer,
context_module: Union[Module, None] = None):
self.container = container
self.context_module: Module = context_module
self.logger = logging.getLogger(PyNestApplicationContext.__name__)
def select_context_module(self):
modules = self.container.modules.values()
self.context_module = next(iter(modules), None)
def select(self, module):
modules_container = self.container.modules
module_token_factory = self.container.module_token_factory
metadata = self._module_compiler.extract_metadata(module)
token = module_token_factory.create(metadata["type"], metadata["dynamic_metadata"])
selected_module = modules_container.get(token)
if selected_module is None:
raise UnknownModuleException()
return PyNestApplicationContext(
self.container,
selected_module
)