Skip to content

Commit bb3e8dc

Browse files
authored
feat: ✨ load_profile method for entrypoint
1 parent 1d6ba28 commit bb3e8dc

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

injection/_core/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,11 @@ def load_profile(self, *names: str) -> ContextManager[Self]:
816816
self.unlock().init_modules(*modules)
817817

818818
@contextmanager
819-
def cleaner() -> Iterator[Self]:
819+
def unload() -> Iterator[Self]:
820820
yield self
821821
self.unlock().init_modules()
822822

823-
return cleaner()
823+
return unload()
824824

825825
async def all_ready(self) -> None:
826826
for broker in self.__brokers:

injection/entrypoint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ def load_modules(
105105
) -> Self:
106106
return self.setup(lambda: loader.load(*packages))
107107

108+
def load_profile(self, /, *names: str) -> Self:
109+
@contextmanager
110+
def decorator(module: Module) -> Iterator[None]:
111+
with module.load_profile(*names):
112+
yield
113+
114+
return self.decorate(decorator(self.module))
115+
108116
def setup(self, function: Callable[..., Any], /) -> Self:
109117
@contextmanager
110118
def decorator() -> Iterator[Any]:

tests/entrypoint/test_entrypoint.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Iterator
22
from contextlib import contextmanager
33

4-
from injection import injectable
4+
from injection import injectable, mod
55
from injection.entrypoint import Entrypoint
66

77

@@ -41,6 +41,18 @@ def function(service: Service) -> bool:
4141
entrypoint = Entrypoint(function).inject()
4242
assert entrypoint()
4343

44+
def test_load_profile_with_success_return_entrypoint(self):
45+
profile_name = "test"
46+
47+
@mod(profile_name).injectable
48+
class Service: ...
49+
50+
def function(service: Service) -> bool:
51+
return isinstance(service, Service)
52+
53+
entrypoint = Entrypoint(function).inject().load_profile(profile_name)
54+
assert entrypoint()
55+
4456
def test_setup_with_success_return_entrypoint(self):
4557
count = 0
4658

0 commit comments

Comments
 (0)