From 112b9d6208db79932df9cea1e4586a6d6ebf9f6f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 22 Nov 2021 21:11:45 +0100 Subject: [PATCH] Added logic to handle event priority --- src/gismo/gismo.json | 3 +-- src/modules/boot_log/boot_log.py | 10 ++++++++-- src/modules_core/abc/module_abc.py | 3 +++ src/modules_core/service/module_service.py | 19 +++++++++++++------ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/gismo/gismo.json b/src/gismo/gismo.json index 95a329a..b8a447a 100644 --- a/src/gismo/gismo.json +++ b/src/gismo/gismo.json @@ -18,8 +18,7 @@ "Dependencies": [ "sh_cpl-core>=2021.10.2", "sh_cpl-query>=2021.10.2", - "discord.py==1.7.3", - "nest-asyncio==1.5.1" + "discord.py==1.7.3" ], "PythonVersion": ">=3.9.2", "PythonPath": { diff --git a/src/modules/boot_log/boot_log.py b/src/modules/boot_log/boot_log.py index 141ae5d..b0b95b7 100644 --- a/src/modules/boot_log/boot_log.py +++ b/src/modules/boot_log/boot_log.py @@ -32,10 +32,16 @@ class BootLog(ModuleABC, OnReadyABC): self._logger = logger self._bot = bot self._message_service = message_service - - # ModuleABC.__init__(self) + + self._priorities = { + OnReadyABC: 10 + } + 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): self._logger.debug(__name__, f'Module {type(self)} started') try: diff --git a/src/modules_core/abc/module_abc.py b/src/modules_core/abc/module_abc.py index a9645a7..d377d4b 100644 --- a/src/modules_core/abc/module_abc.py +++ b/src/modules_core/abc/module_abc.py @@ -6,3 +6,6 @@ class ModuleABC(ABC): @abstractmethod def __init__(self): pass + + @abstractmethod + def get_priority(self, t: type) -> int: pass diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index 48b011a..57bd415 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -1,4 +1,4 @@ -from abc import ABCMeta +from abc import ABC, ABCMeta from datetime import datetime from typing import Optional, Sequence, Union import discord @@ -25,17 +25,24 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): self._env = env self._modules: List[ModuleABC] = List() 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_disconnect(self): pass async def on_ready(self): - self._logger.debug(__name__, 'Start on_ready modules') - modules = self._modules.where(lambda m: issubclass(m, OnReadyABC)) - for module_type in modules: - module: OnReadyABC = self._services.get_service(module_type) + self._logger.debug(__name__, f'Start on_ready modules') + for module in self._get_modules(OnReadyABC): await module.on_ready() + self._logger.debug(__name__, 'Stopped on_ready modules') async def on_resume(self): pass