Added *args and **kwargs support to discord bot and DI

This commit is contained in:
2023-01-12 08:57:01 +01:00
parent e0ca7c2ae6
commit 7f46fbe87a
6 changed files with 27 additions and 18 deletions

View File

@@ -4,7 +4,7 @@
"Version": {
"Major": "2022",
"Minor": "12",
"Micro": "1.post1"
"Micro": "1.post2"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@@ -57,7 +57,7 @@ class ServiceProvider(ServiceProviderABC):
# raise Exception(f'Service {parameter.annotation} not found')
def _get_services(self, t: type) -> list[Optional[object]]:
def _get_services(self, t: type, *args, **kwargs) -> list[Optional[object]]:
implementations = []
for descriptor in self._service_descriptors:
if descriptor.service_type == t or issubclass(descriptor.service_type, t):
@@ -65,7 +65,7 @@ class ServiceProvider(ServiceProviderABC):
implementations.append(descriptor.implementation)
continue
implementation = self.build_service(descriptor.service_type)
implementation = self.build_service(descriptor.service_type, *args, **kwargs)
if descriptor.lifetime == ServiceLifetimeEnum.singleton:
descriptor.implementation = implementation
@@ -102,7 +102,7 @@ class ServiceProvider(ServiceProviderABC):
return params
def build_service(self, service_type: type) -> object:
def build_service(self, service_type: type, *args, **kwargs) -> object:
for descriptor in self._service_descriptors:
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
if descriptor.implementation is not None:
@@ -115,7 +115,7 @@ class ServiceProvider(ServiceProviderABC):
sig = signature(service_type.__init__)
params = self.build_by_signature(sig)
return service_type(*params)
return service_type(*params, *args, **kwargs)
def set_scope(self, scope: ScopeABC):
self._scope = scope
@@ -124,7 +124,7 @@ class ServiceProvider(ServiceProviderABC):
sb = ScopeBuilder(ServiceProvider(copy.deepcopy(self._service_descriptors), self._configuration, self._database_context))
return sb.build()
def get_service(self, service_type: T) -> Optional[T]:
def get_service(self, service_type: T, *args, **kwargs) -> Optional[T]:
result = self._find_service(service_type)
if result is None:
@@ -133,13 +133,13 @@ class ServiceProvider(ServiceProviderABC):
if result.implementation is not None:
return result.implementation
implementation = self.build_service(service_type)
implementation = self.build_service(service_type, *args, **kwargs)
if result.lifetime == ServiceLifetimeEnum.singleton or result.lifetime == ServiceLifetimeEnum.scoped and self._scope is not None:
result.implementation = implementation
return implementation
def get_services(self, service_type: T) -> list[Optional[T]]:
def get_services(self, service_type: T, *args, **kwargs) -> list[Optional[T]]:
implementations = []
if typing.get_origin(service_type) != list:

View File

@@ -25,7 +25,7 @@ class ServiceProviderABC(ABC):
pass
@abstractmethod
def build_service(self, service_type: type) -> object:
def build_service(self, service_type: type, *args, **kwargs) -> object:
r"""Creates instance of given type
Parameter
@@ -61,7 +61,7 @@ class ServiceProviderABC(ABC):
pass
@abstractmethod
def get_service(self, instance_type: T) -> Optional[T]:
def get_service(self, instance_type: T, *args, **kwargs) -> Optional[T]:
r"""Returns instance of given type
Parameter
@@ -76,7 +76,7 @@ class ServiceProviderABC(ABC):
pass
@abstractmethod
def get_services(self, service_type: T) -> list[Optional[T]]:
def get_services(self, service_type: T, *args, **kwargs) -> list[Optional[T]]:
r"""Returns instance of given type
Parameter