python-injection 0.9.1__tar.gz → 0.9.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of python-injection might be problematic. Click here for more details.
- {python_injection-0.9.1 → python_injection-0.9.2}/PKG-INFO +1 -1
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/invertible.py +1 -1
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/lazy.py +2 -2
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/core/module.py +23 -17
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/testing/__init__.py +2 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/utils.py +12 -3
- {python_injection-0.9.1 → python_injection-0.9.2}/pyproject.toml +1 -1
- {python_injection-0.9.1 → python_injection-0.9.2}/documentation/basic-usage.md +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/__init__.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/__init__.pyi +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/__init__.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/event.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/tools/__init__.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/tools/threading.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/common/tools/type.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/core/__init__.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/exceptions.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/integrations/__init__.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/integrations/blacksheep.py +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/py.typed +0 -0
- {python_injection-0.9.1 → python_injection-0.9.2}/injection/testing/__init__.pyi +0 -0
|
@@ -9,7 +9,7 @@ __all__ = ("Lazy", "LazyMapping")
|
|
|
9
9
|
class Lazy[T](Invertible[T]):
|
|
10
10
|
__slots__ = ("__cache", "__is_set")
|
|
11
11
|
|
|
12
|
-
def __init__(self, factory: Callable[
|
|
12
|
+
def __init__(self, factory: Callable[..., T]):
|
|
13
13
|
self.__setup_cache(factory)
|
|
14
14
|
|
|
15
15
|
def __invert__(self) -> T:
|
|
@@ -19,7 +19,7 @@ class Lazy[T](Invertible[T]):
|
|
|
19
19
|
def is_set(self) -> bool:
|
|
20
20
|
return self.__is_set
|
|
21
21
|
|
|
22
|
-
def __setup_cache(self, factory: Callable[
|
|
22
|
+
def __setup_cache(self, factory: Callable[..., T]):
|
|
23
23
|
def cache_generator() -> Iterator[T]:
|
|
24
24
|
nonlocal factory
|
|
25
25
|
cached = factory()
|
|
@@ -17,7 +17,7 @@ from dataclasses import dataclass, field
|
|
|
17
17
|
from enum import StrEnum
|
|
18
18
|
from functools import partialmethod, singledispatchmethod, update_wrapper
|
|
19
19
|
from inspect import Signature, isclass
|
|
20
|
-
from queue import Queue
|
|
20
|
+
from queue import Empty, Queue
|
|
21
21
|
from types import MethodType, UnionType
|
|
22
22
|
from typing import (
|
|
23
23
|
Any,
|
|
@@ -161,7 +161,7 @@ class Injectable[T](Protocol):
|
|
|
161
161
|
|
|
162
162
|
@dataclass(repr=False, frozen=True, slots=True)
|
|
163
163
|
class BaseInjectable[T](Injectable[T], ABC):
|
|
164
|
-
factory: Callable[
|
|
164
|
+
factory: Callable[..., T]
|
|
165
165
|
|
|
166
166
|
|
|
167
167
|
class NewInjectable[T](BaseInjectable[T]):
|
|
@@ -476,7 +476,7 @@ class Module(EventListener, Broker):
|
|
|
476
476
|
|
|
477
477
|
function = InjectedFunction(wp)
|
|
478
478
|
|
|
479
|
-
@function.on_setup
|
|
479
|
+
@function.on_setup
|
|
480
480
|
def listen():
|
|
481
481
|
function.update(self)
|
|
482
482
|
self.add_listener(function)
|
|
@@ -723,10 +723,8 @@ class InjectedFunction(EventListener):
|
|
|
723
723
|
update_wrapper(self, wrapped, updated=())
|
|
724
724
|
self.__dependencies = Dependencies.empty()
|
|
725
725
|
self.__owner = None
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
queue.put_nowait(self.__set_signature)
|
|
729
|
-
self.__setup_queue = queue
|
|
726
|
+
self.__setup_queue = Queue[Callable[..., Any]](maxsize=2)
|
|
727
|
+
self.on_setup(self.__set_signature)
|
|
730
728
|
|
|
731
729
|
def __repr__(self) -> str: # pragma: no cover
|
|
732
730
|
return repr(self.wrapped)
|
|
@@ -735,13 +733,7 @@ class InjectedFunction(EventListener):
|
|
|
735
733
|
return str(self.wrapped)
|
|
736
734
|
|
|
737
735
|
def __call__(self, /, *args, **kwargs) -> Any:
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
while not queue.empty():
|
|
741
|
-
setup = queue.get()
|
|
742
|
-
setup()
|
|
743
|
-
queue.task_done()
|
|
744
|
-
|
|
736
|
+
self.__setup()
|
|
745
737
|
arguments = self.bind(args, kwargs)
|
|
746
738
|
return self.wrapped(*arguments.args, **arguments.kwargs)
|
|
747
739
|
|
|
@@ -796,15 +788,15 @@ class InjectedFunction(EventListener):
|
|
|
796
788
|
self.__dependencies = Dependencies.resolve(self.signature, module, self.__owner)
|
|
797
789
|
return self
|
|
798
790
|
|
|
799
|
-
def on_setup(self, wrapped: Callable[
|
|
791
|
+
def on_setup(self, wrapped: Callable[..., Any] = None, /):
|
|
800
792
|
def decorator(wp):
|
|
801
|
-
self.__setup_queue.
|
|
793
|
+
self.__setup_queue.put_nowait(wp)
|
|
802
794
|
return wp
|
|
803
795
|
|
|
804
796
|
return decorator(wrapped) if wrapped else decorator
|
|
805
797
|
|
|
806
798
|
@singledispatchmethod
|
|
807
|
-
def on_event(self, event: Event, /) -> None: # type: ignore
|
|
799
|
+
def on_event(self, event: Event, /) -> ContextManager | None: # type: ignore
|
|
808
800
|
return None
|
|
809
801
|
|
|
810
802
|
@on_event.register
|
|
@@ -813,6 +805,20 @@ class InjectedFunction(EventListener):
|
|
|
813
805
|
yield
|
|
814
806
|
self.update(event.on_module)
|
|
815
807
|
|
|
808
|
+
def __setup(self):
|
|
809
|
+
queue = self.__setup_queue
|
|
810
|
+
|
|
811
|
+
while True:
|
|
812
|
+
try:
|
|
813
|
+
task = queue.get_nowait()
|
|
814
|
+
except Empty:
|
|
815
|
+
break
|
|
816
|
+
|
|
817
|
+
task()
|
|
818
|
+
queue.task_done()
|
|
819
|
+
|
|
820
|
+
queue.join()
|
|
821
|
+
|
|
816
822
|
def __set_signature(self) -> Self:
|
|
817
823
|
self.__signature__ = inspect.signature(self.wrapped, eval_str=True)
|
|
818
824
|
return self
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
1
2
|
from importlib import import_module
|
|
2
3
|
from pkgutil import walk_packages
|
|
3
4
|
from types import ModuleType
|
|
@@ -5,9 +6,10 @@ from types import ModuleType
|
|
|
5
6
|
__all__ = ("load_package",)
|
|
6
7
|
|
|
7
8
|
|
|
8
|
-
def load_package(package: ModuleType | str):
|
|
9
|
+
def load_package(package: ModuleType | str, predicate: Callable[[str], bool] = None):
|
|
9
10
|
"""
|
|
10
11
|
Function for importing all modules in a Python package.
|
|
12
|
+
Pass the `predicate` parameter if you want to filter the modules to be imported.
|
|
11
13
|
"""
|
|
12
14
|
|
|
13
15
|
if isinstance(package, str):
|
|
@@ -20,8 +22,15 @@ def load_package(package: ModuleType | str):
|
|
|
20
22
|
"Package has no `__path__` attribute, as it's probably a module."
|
|
21
23
|
) from exc
|
|
22
24
|
|
|
25
|
+
if predicate is None:
|
|
26
|
+
|
|
27
|
+
def predicate(_: str) -> bool:
|
|
28
|
+
return True
|
|
29
|
+
|
|
23
30
|
for info in walk_packages(path=path, prefix=f"{package.__name__}."):
|
|
24
|
-
|
|
31
|
+
name = info.name
|
|
32
|
+
|
|
33
|
+
if info.ispkg or not predicate(name):
|
|
25
34
|
continue
|
|
26
35
|
|
|
27
|
-
import_module(
|
|
36
|
+
import_module(name)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|