Formatted code
This commit is contained in:
@@ -28,7 +28,12 @@ class ServiceProvider(ServiceProviderABC):
|
||||
Database representation
|
||||
"""
|
||||
|
||||
def __init__(self, service_descriptors: list[ServiceDescriptor], config: ConfigurationABC, db_context: Optional[DatabaseContextABC]):
|
||||
def __init__(
|
||||
self,
|
||||
service_descriptors: list[ServiceDescriptor],
|
||||
config: ConfigurationABC,
|
||||
db_context: Optional[DatabaseContextABC],
|
||||
):
|
||||
ServiceProviderABC.__init__(self)
|
||||
|
||||
self._service_descriptors: list[ServiceDescriptor] = service_descriptors
|
||||
@@ -45,7 +50,9 @@ class ServiceProvider(ServiceProviderABC):
|
||||
|
||||
def _get_service(self, parameter: Parameter) -> Optional[object]:
|
||||
for descriptor in self._service_descriptors:
|
||||
if descriptor.service_type == parameter.annotation or issubclass(descriptor.service_type, parameter.annotation):
|
||||
if descriptor.service_type == parameter.annotation or issubclass(
|
||||
descriptor.service_type, parameter.annotation
|
||||
):
|
||||
if descriptor.implementation is not None:
|
||||
return descriptor.implementation
|
||||
|
||||
@@ -77,8 +84,7 @@ class ServiceProvider(ServiceProviderABC):
|
||||
params = []
|
||||
for param in sig.parameters.items():
|
||||
parameter = param[1]
|
||||
if parameter.name != 'self' and parameter.annotation != Parameter.empty:
|
||||
|
||||
if parameter.name != "self" and parameter.annotation != Parameter.empty:
|
||||
if typing.get_origin(parameter.annotation) == list:
|
||||
params.append(self._get_services(typing.get_args(parameter.annotation)[0]))
|
||||
|
||||
@@ -121,7 +127,9 @@ class ServiceProvider(ServiceProviderABC):
|
||||
self._scope = scope
|
||||
|
||||
def create_scope(self) -> ScopeABC:
|
||||
sb = ScopeBuilder(ServiceProvider(copy.deepcopy(self._service_descriptors), self._configuration, self._database_context))
|
||||
sb = ScopeBuilder(
|
||||
ServiceProvider(copy.deepcopy(self._service_descriptors), self._configuration, self._database_context)
|
||||
)
|
||||
return sb.build()
|
||||
|
||||
def get_service(self, service_type: T, *args, **kwargs) -> Optional[T]:
|
||||
@@ -134,7 +142,11 @@ class ServiceProvider(ServiceProviderABC):
|
||||
return result.implementation
|
||||
|
||||
implementation = self.build_service(service_type, *args, **kwargs)
|
||||
if result.lifetime == ServiceLifetimeEnum.singleton or result.lifetime == ServiceLifetimeEnum.scoped and self._scope is not None:
|
||||
if (
|
||||
result.lifetime == ServiceLifetimeEnum.singleton
|
||||
or result.lifetime == ServiceLifetimeEnum.scoped
|
||||
and self._scope is not None
|
||||
):
|
||||
result.implementation = implementation
|
||||
|
||||
return implementation
|
||||
@@ -143,7 +155,7 @@ class ServiceProvider(ServiceProviderABC):
|
||||
implementations = []
|
||||
|
||||
if typing.get_origin(service_type) != list:
|
||||
raise Exception(f'Invalid type {service_type}! Expected list of type')
|
||||
raise Exception(f"Invalid type {service_type}! Expected list of type")
|
||||
|
||||
implementations.extend(self._get_services(typing.get_args(service_type)[0]))
|
||||
|
||||
|
@@ -10,14 +10,14 @@ from cpl_core.type import T
|
||||
class ServiceProviderABC(ABC):
|
||||
r"""ABC for the class :class:`cpl_core.dependency_injection.service_provider.ServiceProvider`"""
|
||||
|
||||
_provider: Optional['ServiceProviderABC'] = None
|
||||
_provider: Optional["ServiceProviderABC"] = None
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def set_global_provider(cls, provider: 'ServiceProviderABC'):
|
||||
def set_global_provider(cls, provider: "ServiceProviderABC"):
|
||||
cls._provider = provider
|
||||
|
||||
@abstractmethod
|
||||
@@ -108,7 +108,7 @@ class ServiceProviderABC(ABC):
|
||||
@functools.wraps(f)
|
||||
def inner(*args, **kwargs):
|
||||
if cls._provider is None:
|
||||
raise Exception(f'{cls.__name__} not build!')
|
||||
raise Exception(f"{cls.__name__} not build!")
|
||||
|
||||
injection = [x for x in cls._provider.build_by_signature(signature(f)) if x is not None]
|
||||
return f(*args, *injection, **kwargs)
|
||||
|
Reference in New Issue
Block a user