Added logic to handle event priority

This commit is contained in:
Sven Heidemann 2021-11-22 21:11:45 +01:00
parent be810a6533
commit 112b9d6208
4 changed files with 25 additions and 10 deletions

View File

@ -18,8 +18,7 @@
"Dependencies": [ "Dependencies": [
"sh_cpl-core>=2021.10.2", "sh_cpl-core>=2021.10.2",
"sh_cpl-query>=2021.10.2", "sh_cpl-query>=2021.10.2",
"discord.py==1.7.3", "discord.py==1.7.3"
"nest-asyncio==1.5.1"
], ],
"PythonVersion": ">=3.9.2", "PythonVersion": ">=3.9.2",
"PythonPath": { "PythonPath": {

View File

@ -33,9 +33,15 @@ class BootLog(ModuleABC, OnReadyABC):
self._bot = bot self._bot = bot
self._message_service = message_service self._message_service = message_service
# ModuleABC.__init__(self) self._priorities = {
OnReadyABC: 10
}
self._logger.trace(__name__, f'Module {type(self)} loaded') self._logger.trace(__name__, f'Module {type(self)} loaded')
def get_priority(self, t: type) -> int:
return self._priorities[t]
async def on_ready(self): async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started') self._logger.debug(__name__, f'Module {type(self)} started')
try: try:

View File

@ -6,3 +6,6 @@ class ModuleABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self): pass
@abstractmethod
def get_priority(self, t: type) -> int: pass

View File

@ -1,4 +1,4 @@
from abc import ABCMeta from abc import ABC, ABCMeta
from datetime import datetime from datetime import datetime
from typing import Optional, Sequence, Union from typing import Optional, Sequence, Union
import discord import discord
@ -26,16 +26,23 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC):
self._modules: List[ModuleABC] = List() self._modules: List[ModuleABC] = List()
self._modules.extend(ModuleABC.__subclasses__()) self._modules.extend(ModuleABC.__subclasses__())
def _get_modules(self, t: type) -> List[ModuleABC]:
module_types = self._modules.where(lambda m: issubclass(m, t))
modules = List(t)
for module_type in module_types:
modules.append(self._services.get_service(module_type))
return modules.order_by(lambda m: m.get_priority(t))
async def on_connect(self): pass async def on_connect(self): pass
async def on_disconnect(self): pass async def on_disconnect(self): pass
async def on_ready(self): async def on_ready(self):
self._logger.debug(__name__, 'Start on_ready modules') self._logger.debug(__name__, f'Start on_ready modules')
modules = self._modules.where(lambda m: issubclass(m, OnReadyABC)) for module in self._get_modules(OnReadyABC):
for module_type in modules:
module: OnReadyABC = self._services.get_service(module_type)
await module.on_ready() await module.on_ready()
self._logger.debug(__name__, 'Stopped on_ready modules') self._logger.debug(__name__, 'Stopped on_ready modules')
async def on_resume(self): pass async def on_resume(self): pass