From 1a71a6eb4db496ed7360761a42d13efb47b7070e Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Thu, 30 Oct 2025 00:35:50 +0100 Subject: [PATCH] 1st version of module interface! --- core/classes/interfaces/imodule.py | 122 +++++++++++++++++++++++++++++ mods/test/mod_test.py | 91 +++------------------ 2 files changed, 134 insertions(+), 79 deletions(-) create mode 100644 core/classes/interfaces/imodule.py diff --git a/core/classes/interfaces/imodule.py b/core/classes/interfaces/imodule.py new file mode 100644 index 0000000..c719e3f --- /dev/null +++ b/core/classes/interfaces/imodule.py @@ -0,0 +1,122 @@ +from abc import ABC, abstractmethod +from typing import TYPE_CHECKING, Optional +from dataclasses import dataclass + +from mods.clone.schemas import ModConfModel + +if TYPE_CHECKING: + from core.irc import Irc + +class IModule(ABC): + + @abstractmethod + @dataclass + class ModConfModel: + """The Model containing the module parameters + """ + + def __init__(self, uplink: 'Irc') -> None: + + # Module name (Mandatory) + self.module_name = 'mod_' + str(self.__class__.__name__).lower() + + # Add Irc Object to the module (Mandatory) + self.Irc = uplink + + # Add Protocol to the module (Mandatory) + self.Protocol = uplink.Protocol + + # Add Global Configuration to the module (Mandatory) + self.Config = uplink.Config + + # Add Base object to the module (Mandatory) + self.Base = uplink.Base + + # Add Main Utils (Mandatory) + self.MainUtils = uplink.Utils + + # Add logs object to the module (Mandatory) + self.Logs = uplink.Loader.Logs + + # Add User object to the module (Mandatory) + self.User = uplink.User + + # Add Channel object to the module (Mandatory) + self.Channel = uplink.Channel + + # Add Reputation object to the module (Optional) + self.Reputation = uplink.Reputation + + self.ModConfig = ModConfModel() + + self.load_module_configuration() + """Load module configuration""" + + self.create_tables() + """Create custom module tables""" + + # Sync the configuration with core configuration (Mandatory) + uplink.Base.db_sync_core_config(self.module_name, self.ModConfig) + + # Log the module + self.Logs.debug(f'Module {self.module_name} loaded ...') + + def update_configuration(self, param_key: str, param_value: str) -> None: + """Update the local and core configuration + + Args: + param_key (str): The parameter key + param_value (str): The parameter value + """ + self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value) + + @abstractmethod + def create_tables(self) -> None: + """Methode qui va créer la base de donnée si elle n'existe pas. + Une Session unique pour cette classe sera crée, qui sera utilisé dans cette classe / module + Args: + database_name (str): Nom de la base de données ( pas d'espace dans le nom ) + + Returns: + None: Aucun retour n'es attendu + """ + + @abstractmethod + def load_module_configuration(self) -> None: + """### Load Module Configuration + """ + try: + # Build the default configuration model (Mandatory) + self.ModConfig = self.ModConfModel(jsonrpc=0) + + # Sync the configuration with core configuration (Mandatory) + self.Base.db_sync_core_config(self.module_name, self.ModConfig) + + return None + + except TypeError as te: + self.Logs.critical(te) + + @abstractmethod + def unload(self) -> None: + """This method is executed when the module is unloaded or reloaded. + """ + + @abstractmethod + def cmd(self, data: list) -> None: + """When recieving server messages. + + Args: + data (list): The recieved message + """ + + @abstractmethod + def hcmds(self, user: str, channel: Optional[str], cmd: list, fullcmd: list = []) -> None: + """These are the commands recieved from a client + + Args: + user (str): The client + channel (str|None): The channel if available + cmd (list): The user command sent + fullcmd (list, optional): The full server message. Defaults to []. + """ \ No newline at end of file diff --git a/mods/test/mod_test.py b/mods/test/mod_test.py index 4fa746f..8f79be5 100644 --- a/mods/test/mod_test.py +++ b/mods/test/mod_test.py @@ -1,10 +1,7 @@ -from typing import TYPE_CHECKING -from dataclasses import dataclass, fields +from core.classes.interfaces.imodule import IModule +from dataclasses import dataclass -if TYPE_CHECKING: - from core.irc import Irc - -class Test(): +class Test(IModule): @dataclass class ModConfModel: @@ -13,37 +10,9 @@ class Test(): param_exemple1: str param_exemple2: int - def __init__(self, ircInstance: 'Irc') -> None: - - # Module name (Mandatory) - self.module_name = 'mod_' + str(self.__class__.__name__).lower() - - # Add Irc Object to the module (Mandatory) - self.Irc = ircInstance - - # Add Loader Object to the module (Mandatory) - self.Loader = ircInstance.Loader - - # Add server protocol Object to the module (Mandatory) - self.Protocol = ircInstance.Protocol - - # Add Global Configuration to the module (Mandatory) - self.Config = ircInstance.Config - - # Add Base object to the module (Mandatory) - self.Base = ircInstance.Base - - # Add logs object to the module (Mandatory) - self.Logs = ircInstance.Loader.Logs - - # Add User object to the module (Mandatory) - self.User = ircInstance.User - - # Add Channel object to the module (Mandatory) - self.Channel = ircInstance.Channel - - # Add Reputation object to the module (Optional) - self.Reputation = ircInstance.Reputation + def load_module_configuration(self) -> None: + """### Load Module Configuration + """ # Create module commands (Mandatory) self.Irc.build_command(0, self.module_name, 'test-command', 'Execute a test command') @@ -51,25 +20,12 @@ class Test(): self.Irc.build_command(2, self.module_name, 'test_level_2', 'Execute a level 2 test command') self.Irc.build_command(3, self.module_name, 'test_level_3', 'Execute a level 3 test command') - - # Init the module - self.__init_module() - - # Log the module - self.Logs.debug(f'Module {self.module_name} loaded ...') - - def __init_module(self) -> None: - - # Create you own tables (Mandatory) - self.__create_tables() - - # Load module configuration and sync with core one (Mandatory) - self.__load_module_configuration() - # End of mandatory methods you can start your customization # + # Build the default configuration model (Mandatory) + self.ModConfig = self.ModConfModel(param_exemple1='str', param_exemple2=1) return None - def __create_tables(self) -> None: + def create_tables(self) -> None: """Methode qui va créer la base de donnée si elle n'existe pas. Une Session unique pour cette classe sera crée, qui sera utilisé dans cette classe / module Args: @@ -86,33 +42,9 @@ class Test(): ) ''' - self.Base.db_execute_query(table_logs) + # self.Base.db_execute_query(table_logs) return None - def __load_module_configuration(self) -> None: - """### Load Module Configuration - """ - try: - # Build the default configuration model (Mandatory) - self.ModConfig = self.ModConfModel(param_exemple1='param value 1', param_exemple2=1) - - # Sync the configuration with core configuration (Mandatory) - self.Base.db_sync_core_config(self.module_name, self.ModConfig) - - return None - - except TypeError as te: - self.Logs.critical(te) - - def __update_configuration(self, param_key: str, param_value: str): - """Update the local and core configuration - - Args: - param_key (str): The parameter key - param_value (str): The parameter value - """ - self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value) - def unload(self) -> None: self.Irc.Commands.drop_command_by_module(self.module_name) return None @@ -148,7 +80,8 @@ class Test(): self.Protocol.send_priv_msg(nick_from=dnickname, msg=f"This is private message to the sender ...", channel=fromchannel) # How to update your module configuration - self.__update_configuration('param_exemple2', 7) + self.update_configuration('param_exemple2', 7) + self.update_configuration('param_exemple1', 'my_value') # Log if you want the result self.Logs.debug(f"Test logs ready")