1111
1212
1313class ModuBotCore (BaseConfig ):
14+ """
15+ Core class for the ModuBot framework.
16+
17+ Handles logging, module loading, and lifecycle management (run/stop).
18+ Modules are expected to reside in the "modules" directory and inherit from the defined MODULE_BASE_CLASS.
19+
20+ :cvar NAME: The name of the bot core.
21+ :cvar VERSION: Current version of the bot core.
22+ :cvar LOGGER_CONFIG: Logger configuration class.
23+ :cvar MODULE_BASE_CLASS: Base class for all modules to be loaded.
24+ """
25+
1426 NAME : ClassVar [str ] = "ModuBotCore"
1527 VERSION : ClassVar [str ] = "0.0.1"
1628 LOGGER_CONFIG : ClassVar [Type [LoggerConfig ]] = LoggerConfig
1729 MODULE_BASE_CLASS : ClassVar [Type [BaseModule ]] = BaseModule
1830
1931 def __init__ (self ):
32+ """
33+ Initializes logging and prepares the module list.
34+
35+ Registers the stop() method to be called automatically on application exit.
36+ """
2037 logging .basicConfig (
2138 level = self .LOGGER_CONFIG .LEVEL ,
2239 format = self .LOGGER_CONFIG .FORMAT ,
@@ -26,19 +43,36 @@ def __init__(self):
2643 atexit .register (self .stop )
2744
2845 def run (self ):
46+ """
47+ Starts the bot and enables all loaded modules.
48+
49+ Calls the on_enable() method of each module after loading.
50+ """
2951 self .logger .info (f"Starting { self .NAME } " )
3052 self ._load_modules ()
3153 for module in self .modules :
3254 self .logger .info (f'Enabling module "{ module .NAME } "' )
3355 module .on_enable ()
3456
3557 def stop (self ):
58+ """
59+ Disables all loaded modules and logs shutdown.
60+
61+ Calls the on_disable() method of each module.
62+ """
3663 for module in self .modules :
3764 self .logger .info (f'Disabling module "{ module .NAME } "' )
3865 module .on_disable ()
3966 self .logger .info (f"Stopping { self .NAME } " )
4067
4168 def _load_modules (self ):
69+ """
70+ Dynamically loads all modules from the "modules" directory.
71+
72+ Each module must be in its own directory with an `__init__.py` file.
73+ The module must contain a class that inherits from MODULE_BASE_CLASS.
74+ Only modules with ENABLING = True will be instantiated and added.
75+ """
4276 root = Path ().resolve ()
4377 module_dir = root / "modules"
4478 self .logger .debug (f'Loading modules from "{ module_dir } "' )
@@ -78,4 +112,10 @@ def _load_modules(self):
78112
79113 @property
80114 def logger (self ) -> logging .Logger :
115+ """
116+ Returns the logger instance for the bot.
117+
118+ :return: Logger bound to the bot's NAME.
119+ :rtype: logging.Logger
120+ """
81121 return logging .getLogger (self .NAME )
0 commit comments