A-0.1 - Modularer Aufbau #13
@ -32,7 +32,7 @@ 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()
|
||||
|
@ -22,13 +22,12 @@ 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:
|
||||
@ -36,3 +35,4 @@ class BotService(BotServiceABC, commands.Bot):
|
||||
# save data
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, 'Stop failed', e)
|
||||
|
||||
|
@ -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
|
||||
|
||||
self._logger.info(__name__, 'LOADED')
|
||||
ModuleABC.__init__(self)
|
||||
|
||||
modules.register(self)
|
||||
|
||||
def on_ready(self):
|
||||
async def on_ready(self):
|
||||
self._logger.info(__name__, f'Bot started: {self._bot}')
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
6
src/modules_core/events_enum.py
Normal file
6
src/modules_core/events_enum.py
Normal file
@ -0,0 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class EventsEnum(Enum):
|
||||
|
||||
on_ready = 'on_ready'
|
@ -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()
|
||||
|
Reference in New Issue
Block a user