Improved dynamic modules support

This commit is contained in:
Sven Heidemann 2021-11-15 21:52:28 +01:00
parent 4763c41b10
commit f2ee3d64f6
7 changed files with 40 additions and 21 deletions

View File

@ -33,6 +33,6 @@ class Startup(StartupABC):
services.add_singleton(ModuleServiceABC, ModuleService)
services.add_singleton(BotServiceABC, BotService)
services.add_singleton(ModuleABC, BootLog)
services.add_transient(ModuleABC, BootLog)
return services.build_service_provider()

View File

@ -22,17 +22,17 @@ class BotService(BotServiceABC, commands.Bot):
commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None)
async def start_async(self):
self._logger.trace(__name__, 'Loading modules:')
self._modules.start_modules()
self._logger.trace(__name__, 'Finished loading modules')
self._logger.trace(__name__, 'Try to connect to discord')
self.run(self._discord_settings.token)
# continue at on_ready
async def on_ready(self):
await self._modules.start_modules()
async def stop_async(self):
try:
pass
# save data
except Exception as e:
self._logger.error(__name__, 'Stop failed', e)

View File

@ -7,12 +7,12 @@ from modules_core.abc.module_service_abc import ModuleServiceABC
class BootLog(ModuleABC):
def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, bot: BotServiceABC):
def __init__(self, logger: LoggerABC, bot: BotServiceABC):
self._logger = logger
self._modules = modules
self._bot = bot
modules.register(self)
self._logger.info(__name__, 'LOADED')
ModuleABC.__init__(self)
def on_ready(self):
async def on_ready(self):
self._logger.info(__name__, f'Bot started: {self._bot}')

View File

@ -1,7 +1,13 @@
from abc import ABC, abstractmethod
from cpl_query.extension import List
from modules_core.events_enum import EventsEnum
class ModuleABC(ABC):
@abstractmethod
def __init__(self): pass
# @property
# @abstractmethod
# def events(self) -> List[EventsEnum]: pass

View File

@ -8,7 +8,4 @@ class ModuleServiceABC(ABC):
def __init__(self): pass
@abstractmethod
def register(self): pass
@abstractmethod
def start_modules(self): pass
async def start_modules(self): pass

View File

@ -0,0 +1,6 @@
from enum import Enum
class EventsEnum(Enum):
on_ready = 'on_ready'

View File

@ -1,7 +1,13 @@
from os import path
import discord
from cpl_core.dependency_injection.service_provider_abc import \
ServiceProviderABC
from cpl_core.environment.application_environment_abc import \
ApplicationEnvironmentABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List
from discord.ext import commands
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
@ -9,13 +15,17 @@ from modules_core.abc.module_service_abc import ModuleServiceABC
class ModuleService(ModuleServiceABC):
def __init__(self, logger: LoggerABC, env: ApplicationEnvironmentABC):
def __init__(self, logger: LoggerABC, services: ServiceProviderABC, env: ApplicationEnvironmentABC):
self._logger = logger
self._services = services
self._env = env
self._modules: List[ModuleABC] = List()
self._modules.extend(ModuleABC.__subclasses__())
def register(self, module: ModuleABC):
self._modules.append(module)
def start_modules(self):
self._modules.for_each(lambda m: m.echo())
async def start_modules(self):
self._logger.info(__name__, self._modules)
modules = self._modules.where(lambda m: hasattr(m, 'on_ready'))
self._logger.info(__name__, f'HIER: {modules}')
for module_type in modules:
module = self._services.get_service(module_type)
await module.on_ready()