Skip to content

Commit b4a4d6a

Browse files
authored
Merge pull request #14 from modern-python/13-refactor-rename-bootstrap-to-bootstrapper
rename bootstrap to bootstrapper, refactor
2 parents d4c5e5a + 12ff53a commit b4a4d6a

14 files changed

+98
-45
lines changed

lite_bootstrap/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from lite_bootstrap.bootstrappers.fastapi_bootstrapper import (
2+
FastAPIBootstrapper,
3+
FastAPIHealthChecksInstrument,
4+
FastAPILoggingInstrument,
5+
FastAPIOpenTelemetryInstrument,
6+
FastAPISentryInstrument,
7+
)
8+
from lite_bootstrap.bootstrappers.free_bootstrapper import FreeBootstrapper
9+
from lite_bootstrap.bootstrappers.litestar_bootstrapper import (
10+
LitestarBootstrapper,
11+
LitestarHealthChecksInstrument,
12+
LitestarLoggingInstrument,
13+
LitestarOpenTelemetryInstrument,
14+
LitestarSentryInstrument,
15+
)
16+
from lite_bootstrap.instruments.healthchecks_instrument import HealthChecksInstrument
17+
from lite_bootstrap.instruments.logging_instrument import LoggingInstrument
18+
from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument
19+
from lite_bootstrap.instruments.sentry_instrument import SentryInstrument
20+
21+
22+
__all__ = [
23+
"FastAPIBootstrapper",
24+
"FastAPIHealthChecksInstrument",
25+
"FastAPILoggingInstrument",
26+
"FastAPIOpenTelemetryInstrument",
27+
"FastAPISentryInstrument",
28+
"FreeBootstrapper",
29+
"HealthChecksInstrument",
30+
"LitestarBootstrapper",
31+
"LitestarHealthChecksInstrument",
32+
"LitestarLoggingInstrument",
33+
"LitestarOpenTelemetryInstrument",
34+
"LitestarSentryInstrument",
35+
"LoggingInstrument",
36+
"OpenTelemetryInstrument",
37+
"SentryInstrument",
38+
]
File renamed without changes.

lite_bootstrap/bootstraps/base.py renamed to lite_bootstrap/bootstrappers/base.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33

44
from lite_bootstrap.instruments.base import BaseInstrument
55
from lite_bootstrap.service_config import ServiceConfig
6-
from lite_bootstrap.types import ApplicationT
6+
from lite_bootstrap.types import ApplicationT, BootstrapObjectT
77

88

9-
class BaseBootstrap(abc.ABC, typing.Generic[ApplicationT]):
10-
application: ApplicationT
9+
class BaseBootstrapper(abc.ABC, typing.Generic[BootstrapObjectT, ApplicationT]):
10+
bootstrap_object: BootstrapObjectT
1111
instruments: typing.Sequence[BaseInstrument]
1212
service_config: ServiceConfig
1313

14-
def bootstrap(self) -> None:
14+
@abc.abstractmethod
15+
def _prepare_application(self) -> ApplicationT: ...
16+
17+
def bootstrap(self) -> ApplicationT:
1518
for one_instrument in self.instruments:
1619
if one_instrument.is_ready(self.service_config):
17-
one_instrument.bootstrap(self.service_config, self.application)
20+
one_instrument.bootstrap(self.service_config, self.bootstrap_object)
21+
return self._prepare_application()
1822

1923
def teardown(self) -> None:
2024
for one_instrument in self.instruments:
2125
if one_instrument.is_ready(self.service_config):
22-
one_instrument.teardown(self.application)
26+
one_instrument.teardown(self.bootstrap_object)

lite_bootstrap/bootstraps/fastapi_bootstrap.py renamed to lite_bootstrap/bootstrappers/fastapi_bootstrapper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import dataclasses
33
import typing
44

5-
from lite_bootstrap.bootstraps.base import BaseBootstrap
5+
from lite_bootstrap.bootstrappers.base import BaseBootstrapper
66
from lite_bootstrap.instruments.healthchecks_instrument import HealthChecksInstrument, HealthCheckTypedDict
77
from lite_bootstrap.instruments.logging_instrument import LoggingInstrument
88
from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument
@@ -66,12 +66,15 @@ class FastAPISentryInstrument(SentryInstrument): ...
6666

6767

6868
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
69-
class FastAPIBootstrap(BaseBootstrap[fastapi.FastAPI]):
70-
application: fastapi.FastAPI
69+
class FastAPIBootstrapper(BaseBootstrapper[fastapi.FastAPI, fastapi.FastAPI]):
70+
bootstrap_object: fastapi.FastAPI
7171
instruments: typing.Sequence[
7272
FastAPIOpenTelemetryInstrument
7373
| FastAPISentryInstrument
7474
| FastAPIHealthChecksInstrument
7575
| FastAPILoggingInstrument
7676
]
7777
service_config: ServiceConfig
78+
79+
def _prepare_application(self) -> fastapi.FastAPI:
80+
return self.bootstrap_object
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import dataclasses
22
import typing
33

4-
from lite_bootstrap.bootstraps.base import BaseBootstrap
4+
from lite_bootstrap.bootstrappers.base import BaseBootstrapper
55
from lite_bootstrap.instruments.logging_instrument import LoggingInstrument
66
from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument
77
from lite_bootstrap.instruments.sentry_instrument import SentryInstrument
88
from lite_bootstrap.service_config import ServiceConfig
99

1010

1111
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
12-
class FreeBootstrap(BaseBootstrap[None]):
13-
application: None = None
12+
class FreeBootstrapper(BaseBootstrapper[None, None]):
13+
bootstrap_object: None = None
1414
instruments: typing.Sequence[OpenTelemetryInstrument | SentryInstrument | LoggingInstrument]
1515
service_config: ServiceConfig
16+
17+
def _prepare_application(self) -> None:
18+
return self.bootstrap_object

lite_bootstrap/bootstraps/litestar_bootstrap.py renamed to lite_bootstrap/bootstrappers/litestar_bootstrapper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import dataclasses
33
import typing
44

5-
from lite_bootstrap.bootstraps.base import BaseBootstrap
5+
from lite_bootstrap.bootstrappers.base import BaseBootstrapper
66
from lite_bootstrap.instruments.healthchecks_instrument import HealthChecksInstrument, HealthCheckTypedDict
77
from lite_bootstrap.instruments.logging_instrument import LoggingInstrument
88
from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument
@@ -64,12 +64,15 @@ class LitestarSentryInstrument(SentryInstrument): ...
6464

6565

6666
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
67-
class LitestarBootstrap(BaseBootstrap[AppConfig]):
68-
application: AppConfig
67+
class LitestarBootstrapper(BaseBootstrapper[AppConfig, litestar.Litestar]):
68+
bootstrap_object: AppConfig
6969
instruments: typing.Sequence[
7070
LitestarOpenTelemetryInstrument
7171
| LitestarSentryInstrument
7272
| LitestarHealthChecksInstrument
7373
| LitestarLoggingInstrument
7474
]
7575
service_config: ServiceConfig
76+
77+
def _prepare_application(self) -> litestar.Litestar:
78+
return litestar.Litestar.from_config(self.bootstrap_object)

lite_bootstrap/instruments/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import abc
22

33
from lite_bootstrap.service_config import ServiceConfig
4-
from lite_bootstrap.types import ApplicationT
4+
from lite_bootstrap.types import BootstrapObjectT
55

66

77
class BaseInstrument(abc.ABC):
8-
def bootstrap(self, service_config: ServiceConfig, application: ApplicationT | None = None) -> None: ... # noqa: B027
8+
def bootstrap(self, service_config: ServiceConfig, bootstrap_object: BootstrapObjectT | None = None) -> None: ... # noqa: B027
99

10-
def teardown(self, application: ApplicationT | None = None) -> None: ... # noqa: B027
10+
def teardown(self, bootstrap_object: BootstrapObjectT | None = None) -> None: ... # noqa: B027
1111

1212
@abc.abstractmethod
1313
def is_ready(self, service_config: ServiceConfig) -> bool: ...

lite_bootstrap/instruments/logging_instrument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from lite_bootstrap.instruments.base import BaseInstrument
88
from lite_bootstrap.service_config import ServiceConfig
9-
from lite_bootstrap.types import ApplicationT
9+
from lite_bootstrap.types import BootstrapObjectT
1010

1111

1212
if typing.TYPE_CHECKING:
@@ -109,7 +109,7 @@ class LoggingInstrument(BaseInstrument):
109109
def is_ready(self, service_config: ServiceConfig) -> bool:
110110
return not service_config.service_debug
111111

112-
def bootstrap(self, _: ServiceConfig, __: ApplicationT | None = None) -> None:
112+
def bootstrap(self, _: ServiceConfig, __: BootstrapObjectT | None = None) -> None:
113113
for unset_handlers_logger in self.logging_unset_handlers:
114114
logging.getLogger(unset_handlers_logger).handlers = []
115115

@@ -129,5 +129,5 @@ def bootstrap(self, _: ServiceConfig, __: ApplicationT | None = None) -> None:
129129
cache_logger_on_first_use=True,
130130
)
131131

132-
def teardown(self, _: ApplicationT | None = None) -> None:
132+
def teardown(self, _: BootstrapObjectT | None = None) -> None:
133133
structlog.reset_defaults()

lite_bootstrap/instruments/opentelemetry_instrument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from lite_bootstrap.instruments.base import BaseInstrument
88
from lite_bootstrap.service_config import ServiceConfig
9-
from lite_bootstrap.types import ApplicationT
9+
from lite_bootstrap.types import BootstrapObjectT
1010

1111

1212
with contextlib.suppress(ImportError):
@@ -35,7 +35,7 @@ class OpenTelemetryInstrument(BaseInstrument):
3535
def is_ready(self, _: ServiceConfig) -> bool:
3636
return bool(self.endpoint)
3737

38-
def bootstrap(self, service_config: ServiceConfig, _: ApplicationT | None = None) -> None:
38+
def bootstrap(self, service_config: ServiceConfig, _: BootstrapObjectT | None = None) -> None:
3939
attributes = {
4040
resources.SERVICE_NAME: service_config.service_name,
4141
resources.TELEMETRY_SDK_LANGUAGE: "python",
@@ -66,7 +66,7 @@ def bootstrap(self, service_config: ServiceConfig, _: ApplicationT | None = None
6666
one_instrumentor.instrument(tracer_provider=tracer_provider)
6767
set_tracer_provider(tracer_provider)
6868

69-
def teardown(self, _: ApplicationT | None = None) -> None:
69+
def teardown(self, _: BootstrapObjectT | None = None) -> None:
7070
for one_instrumentor in self.instrumentors:
7171
if isinstance(one_instrumentor, InstrumentorWithParams):
7272
one_instrumentor.instrumentor.uninstrument(**one_instrumentor.additional_params)

lite_bootstrap/instruments/sentry_instrument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from lite_bootstrap.instruments.base import BaseInstrument
66
from lite_bootstrap.service_config import ServiceConfig
7-
from lite_bootstrap.types import ApplicationT
7+
from lite_bootstrap.types import BootstrapObjectT
88

99

1010
with contextlib.suppress(ImportError):
@@ -27,7 +27,7 @@ class SentryInstrument(BaseInstrument):
2727
def is_ready(self, _: ServiceConfig) -> bool:
2828
return bool(self.dsn)
2929

30-
def bootstrap(self, service_config: ServiceConfig, _: ApplicationT | None = None) -> None:
30+
def bootstrap(self, service_config: ServiceConfig, _: BootstrapObjectT | None = None) -> None:
3131
sentry_sdk.init(
3232
dsn=self.dsn,
3333
sample_rate=self.sample_rate,
@@ -42,4 +42,4 @@ def bootstrap(self, service_config: ServiceConfig, _: ApplicationT | None = None
4242
tags: dict[str, str] = self.tags or {}
4343
sentry_sdk.set_tags(tags)
4444

45-
def teardown(self, application: ApplicationT | None = None) -> None: ...
45+
def teardown(self, bootstrap_object: BootstrapObjectT | None = None) -> None: ...

0 commit comments

Comments
 (0)