A-0.1 - Modularer Aufbau #13

Merged
edraft merged 118 commits from 0.1 into Alpha 2021-11-25 21:02:12 +01:00
7 changed files with 40 additions and 21 deletions
Showing only changes of commit f2ee3d64f6 - Show all commits

View File

@ -33,6 +33,6 @@ class Startup(StartupABC):
services.add_singleton(ModuleServiceABC, ModuleService) services.add_singleton(ModuleServiceABC, ModuleService)
services.add_singleton(BotServiceABC, BotService) services.add_singleton(BotServiceABC, BotService)
services.add_singleton(ModuleABC, BootLog) services.add_transient(ModuleABC, BootLog)
return services.build_service_provider() 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) commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None)
async def start_async(self): 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._logger.trace(__name__, 'Try to connect to discord')
self.run(self._discord_settings.token) self.run(self._discord_settings.token)
# continue at on_ready # continue at on_ready
async def on_ready(self):
await self._modules.start_modules()
async def stop_async(self): async def stop_async(self):
try: try:
pass pass
# save data # save data
except Exception as e: except Exception as e:
self._logger.error(__name__, 'Stop failed', 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): class BootLog(ModuleABC):
def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, bot: BotServiceABC): def __init__(self, logger: LoggerABC, bot: BotServiceABC):
self._logger = logger self._logger = logger
self._modules = modules
self._bot = bot 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}') self._logger.info(__name__, f'Bot started: {self._bot}')

View File

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