From 7f46fbe87ab287b9152ea1c5f06588e1fb3f1ac8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 12 Jan 2023 08:57:01 +0100 Subject: [PATCH] Added *args and **kwargs support to discord bot and DI --- src/cpl_core/cpl-core.json | 2 +- .../dependency_injection/service_provider.py | 14 +++++++------- .../dependency_injection/service_provider_abc.py | 6 +++--- src/cpl_discord/cpl-discord.json | 4 ++-- src/cpl_discord/service/discord_bot_service.py | 15 ++++++++++++--- .../service/discord_bot_service_abc.py | 4 ++-- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/cpl_core/cpl-core.json b/src/cpl_core/cpl-core.json index 2072dda3..681f8c9c 100644 --- a/src/cpl_core/cpl-core.json +++ b/src/cpl_core/cpl-core.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "12", - "Micro": "1.post1" + "Micro": "1.post2" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/cpl_core/dependency_injection/service_provider.py b/src/cpl_core/dependency_injection/service_provider.py index 4e388440..0be8f567 100644 --- a/src/cpl_core/dependency_injection/service_provider.py +++ b/src/cpl_core/dependency_injection/service_provider.py @@ -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: diff --git a/src/cpl_core/dependency_injection/service_provider_abc.py b/src/cpl_core/dependency_injection/service_provider_abc.py index f2890d3c..bf7e851e 100644 --- a/src/cpl_core/dependency_injection/service_provider_abc.py +++ b/src/cpl_core/dependency_injection/service_provider_abc.py @@ -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 diff --git a/src/cpl_discord/cpl-discord.json b/src/cpl_discord/cpl-discord.json index cd29ffcf..d75b5cf7 100644 --- a/src/cpl_discord/cpl-discord.json +++ b/src/cpl_discord/cpl-discord.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "12", - "Micro": "1" + "Micro": "1.post1" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", @@ -16,7 +16,7 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core>=2022.12.1", + "cpl-core>=2022.12.1.post2", "discord.py==2.1.0", "cpl-query>=2022.12.2.post1" ], diff --git a/src/cpl_discord/service/discord_bot_service.py b/src/cpl_discord/service/discord_bot_service.py index fe86b908..4c4716d7 100644 --- a/src/cpl_discord/service/discord_bot_service.py +++ b/src/cpl_discord/service/discord_bot_service.py @@ -21,7 +21,9 @@ class DiscordBotService(DiscordBotServiceABC): discord_bot_settings: DiscordBotSettings, env: ApplicationEnvironmentABC, logging_st: LoggingSettings, - discord_service: DiscordServiceABC + discord_service: DiscordServiceABC, + *args, + **kwargs ): # services self._config = config @@ -34,7 +36,12 @@ class DiscordBotService(DiscordBotServiceABC): self._discord_settings = self._get_settings(discord_bot_settings) # setup super - DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all()) + DiscordBotServiceABC.__init__( + self, + *args, + command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all(), + **kwargs + ) self._base = super(DiscordBotServiceABC, self) @staticmethod @@ -50,7 +57,9 @@ class DiscordBotService(DiscordBotServiceABC): new_settings.from_dict({ 'Token': env_token if token is None or token == '' else token, - 'Prefix': ('! ' if self._is_string_invalid(env_prefix) else env_prefix) if self._is_string_invalid(prefix) else prefix + 'Prefix': + ('! ' if self._is_string_invalid(env_prefix) else env_prefix) + if self._is_string_invalid(prefix) else prefix }) if new_settings.token is None or new_settings.token == '': raise Exception('You have to configure discord token by appsettings or environment variables') diff --git a/src/cpl_discord/service/discord_bot_service_abc.py b/src/cpl_discord/service/discord_bot_service_abc.py index b96a5ee8..399e5a3f 100644 --- a/src/cpl_discord/service/discord_bot_service_abc.py +++ b/src/cpl_discord/service/discord_bot_service_abc.py @@ -8,8 +8,8 @@ from cpl_query.extension.list import List class DiscordBotServiceABC(commands.Bot): - def __init__(self, **kwargs): - commands.Bot.__init__(self, **kwargs) + def __init__(self, *args, **kwargs): + commands.Bot.__init__(self, *args, **kwargs) @abstractmethod async def start_async(self): pass