-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
65 lines (48 loc) · 1.98 KB
/
base.py
File metadata and controls
65 lines (48 loc) · 1.98 KB
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
60
61
62
63
64
65
import abc
import logging
import typing
import warnings
from lite_bootstrap.instruments.base import BaseConfig, BaseInstrument
from lite_bootstrap.types import ApplicationT
try:
import structlog
logger = structlog.getLogger(__name__)
except ImportError:
logger = logging.getLogger(__name__)
InstrumentT = typing.TypeVar("InstrumentT", bound=BaseInstrument)
class BaseBootstrapper(abc.ABC, typing.Generic[ApplicationT]):
instruments_types: typing.ClassVar[list[type[BaseInstrument]]]
instruments: list[BaseInstrument]
bootstrap_config: BaseConfig
def __init__(self, bootstrap_config: BaseConfig) -> None:
self.is_bootstrapped = False
if not self.is_ready():
msg = f"{type(self).__name__} is not ready because {self.not_ready_message}"
raise RuntimeError(msg)
self.bootstrap_config = bootstrap_config
self.instruments = []
for instrument_type in self.instruments_types:
instrument = instrument_type(bootstrap_config=bootstrap_config)
if not instrument.check_dependencies():
warnings.warn(instrument.missing_dependency_message, stacklevel=2)
continue
if not instrument.is_ready():
logger.info(f"{instrument_type.__name__} is not ready, because {instrument.not_ready_message}")
continue
self.instruments.append(instrument)
@property
@abc.abstractmethod
def not_ready_message(self) -> str: ...
@abc.abstractmethod
def _prepare_application(self) -> ApplicationT: ...
@abc.abstractmethod
def is_ready(self) -> bool: ...
def bootstrap(self) -> ApplicationT:
self.is_bootstrapped = True
for one_instrument in self.instruments:
one_instrument.bootstrap()
return self._prepare_application()
def teardown(self) -> None:
self.is_bootstrapped = False
for one_instrument in self.instruments:
one_instrument.teardown()