Skip to content

Commit edc76a6

Browse files
authored
Merge pull request #4 from EnderModuBot/doc/3-doku-zu-allen-funktionen-hinzufügen
Füge Dokumentation zu Klassen und Methoden hinzu und erweitere die Re…
2 parents 6e49316 + 332237e commit edc76a6

6 files changed

Lines changed: 100 additions & 1 deletion

File tree

.github/release-drafter.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ categories:
1616
- title: '✅ Tests'
1717
labels:
1818
- 'tests'
19+
- title: '📚 Documentation'
20+
labels:
21+
- 'documentation'
22+
1923
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
2024
no-changes-template: '- No changes'
2125
template: |

ModuBotCore/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,29 @@
1111

1212

1313
class 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)

ModuBotCore/config/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88

99
class BaseConfig:
10+
"""
11+
Base configuration class with runtime type checking for class variables.
12+
13+
This class automatically validates ClassVar attributes on subclass creation.
14+
It ensures that the declared types match the actual values provided.
15+
16+
Supported inner types for ClassVar are:
17+
- Primitive types: str, int, float, bool
18+
- Type wrappers, e.g., Type[SomeClass]
19+
20+
:raises TypeError: If a ClassVar has an invalid or unsupported value/type.
21+
"""
22+
1023
def __init_subclass__(cls) -> None:
1124
hints = get_type_hints(cls)
1225

ModuBotCore/config/logger.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
@dataclass(frozen=True)
99
class LoggerConfig(BaseConfig):
10+
"""
11+
Configuration class for the logging system.
12+
13+
Defines the log level, formatting, and timestamp format used by the logger.
14+
15+
:cvar LEVEL: Logging level (e.g., DEBUG, INFO). Defaults to the LOG_LEVEL environment variable or 'INFO'.
16+
:cvar FORMAT: Format string for log messages.
17+
:cvar DATEFMT: Format string for timestamps in log messages.
18+
"""
19+
1020
LEVEL: ClassVar[str] = os.getenv("LOG_LEVEL", "INFO")
1121
FORMAT: ClassVar[str] = "[%(asctime)s - %(levelname)s - %(name)s]: %(message)s"
1222
DATEFMT: ClassVar[str] = "%m/%d/%Y %H:%M:%S"

ModuBotCore/helpers/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
def str2bool(value: str) -> bool:
2-
"""Convert string value to boolean."""
2+
"""
3+
Convert a string representation to a boolean value.
4+
5+
Common truthy values include: "yes", "true", "t", "y", "1" (case-insensitive).
6+
7+
:param value: Input string to convert.
8+
:type value: str
9+
:return: True if the value is considered truthy, False otherwise.
10+
:rtype: bool
11+
"""
312
return value.lower() in ("yes", "true", "t", "y", "1")

ModuBotCore/modules/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,40 @@
66

77

88
class BaseModule(ABC, BaseConfig):
9+
"""
10+
Abstract base class for all ModuBot modules.
11+
12+
All modules must inherit from this class and implement the `on_enable` and `on_disable` lifecycle methods.
13+
14+
:cvar NAME: The name of the module.
15+
:cvar ENABLING: Whether this module should be loaded and enabled.
16+
"""
17+
918
NAME: ClassVar[str] = "BaseModule"
1019
ENABLING: ClassVar[bool] = True
1120

1221
@property
1322
def logger(self) -> logging.Logger:
23+
"""
24+
Returns a logger instance specific to the module.
25+
26+
:return: Logger named after the module.
27+
:rtype: logging.Logger
28+
"""
1429
return logging.getLogger(self.NAME)
1530

1631
@abstractmethod
1732
def on_enable(self):
33+
"""
34+
Hook that is called when the module is enabled.
35+
Must be implemented by subclasses.
36+
"""
1837
pass
1938

2039
@abstractmethod
2140
def on_disable(self):
41+
"""
42+
Hook that is called when the module is disabled.
43+
Must be implemented by subclasses.
44+
"""
2245
pass

0 commit comments

Comments
 (0)