Added support for dynamic loaded modules

This commit is contained in:
Sven Heidemann 2021-11-15 21:23:06 +01:00
parent a3bf5535f6
commit 562caeb10b
12 changed files with 110 additions and 34 deletions

31
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,31 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Gismo",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}/src/gismo",
"program": "main.py",
"console": "integratedTerminal",
"args": [
"--customer=sh-edraft.de"
],
"env": {
"CPL_NAME": "Gismo",
"GISMO_ENVIRONMENT": "development",
"PYTHONPATH": "${workspaceFolder}/src/:$PATHONPATH"
}
}
]
}

View File

@ -4,7 +4,8 @@ from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.services.bot_service import BotService from gismo_core.service.bot_service import BotService
from modules_core.abc.module_service_abc import ModuleServiceABC
class Application(ApplicationABC): class Application(ApplicationABC):

View File

@ -1,10 +1,16 @@
from cpl_core.application import StartupABC from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC, ConsoleArgument
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.console.console import Console
from cpl_core.dependency_injection import (ServiceCollectionABC,
ServiceProviderABC)
from cpl_core.environment import ApplicationEnvironment from cpl_core.environment import ApplicationEnvironment
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.services.bot_service import BotService from gismo_core.service.bot_service import BotService
from modules.boot_log.main import BootLog
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
from modules_core.service.module_service import ModuleService
class Startup(StartupABC): class Startup(StartupABC):
@ -14,7 +20,7 @@ class Startup(StartupABC):
async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
configuration.add_environment_variables('GISMO_') configuration.add_environment_variables('GISMO_')
configuration.add_console_arguments()
configuration.add_json_file(f'appsettings.json', optional=False) configuration.add_json_file(f'appsettings.json', optional=False)
configuration.add_json_file(f'appsettings.{environment.environment_name}.json', optional=True) configuration.add_json_file(f'appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'appsettings.{environment.host_name}.json', optional=True) configuration.add_json_file(f'appsettings.{environment.host_name}.json', optional=True)
@ -24,6 +30,9 @@ class Startup(StartupABC):
async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
services.add_logging() services.add_logging()
services.add_singleton(ModuleServiceABC, ModuleService)
services.add_singleton(BotServiceABC, BotService) services.add_singleton(BotServiceABC, BotService)
services.add_singleton(ModuleABC, BootLog)
return services.build_service_provider() return services.build_service_provider()

View File

@ -4,19 +4,27 @@ from discord.ext import commands
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.configuration.bot_settings import BotSettings from gismo_core.configuration.bot_settings import BotSettings
from gismo_core.configuration.discord_settings import DiscordSettings from gismo_core.configuration.discord_settings import DiscordSettings
from modules_core.abc.module_service_abc import ModuleServiceABC
class BotService(BotServiceABC, commands.Bot): class BotService(BotServiceABC, commands.Bot):
def __init__(self, logger: LoggerABC, discord_settings: DiscordSettings, bot_settings: BotSettings): def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, discord_settings: DiscordSettings, bot_settings: BotSettings):
# services
self._logger = logger self._logger = logger
self._modules = modules
# settings
self._discord_settings = discord_settings self._discord_settings = discord_settings
self._bot_settings: BotSettings = bot_settings self._bot_settings: BotSettings = bot_settings
# setup self
# setup super
commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None) commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None)
async def start_async(self): async def start_async(self):
self._modules.start_modules()
self.run(self._discord_settings.token) self.run(self._discord_settings.token)
# continue at on_ready
async def stop_async(self): async def stop_async(self):
try: try:
@ -24,3 +32,5 @@ class BotService(BotServiceABC, commands.Bot):
# save data # save data
except Exception as e: except Exception as e:
self._logger.error(__name__, 'Stop failed', e) self._logger.error(__name__, 'Stop failed', e)
async def on_ready(self): pass

View File

@ -1,13 +1,18 @@
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
class BootLog: class BootLog(ModuleABC):
def __init__(self, logger: LoggerABC, bot: BotServiceABC): def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, bot: BotServiceABC):
self._logger = logger self._logger = logger
self._modules = modules
self._bot = bot self._bot = bot
modules.register(self)
def on_ready(self): def on_ready(self):
self._logger.info(__name__, f'Bot started: {self._bot}') self._logger.info(__name__, f'Bot started: {self._bot}')

View File

@ -1,25 +1 @@
# -*- coding: utf-8 -*- # imports
"""
gismo sh-edraft Gismo
~~~~~~~~~~~~~~~~~~~
sh-edraft Dicord bot Gismo
:copyright: (c) 2021 - 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'modules_core'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '0.1.0'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='1', micro='0')

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,7 @@
from abc import ABC, abstractmethod
class ModuleABC(ABC):
@abstractmethod
def __init__(self): pass

View File

@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
from codecs import register
class ModuleServiceABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def register(self): pass
@abstractmethod
def start_modules(self): pass

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,21 @@
from cpl_core.environment.application_environment_abc import \
ApplicationEnvironmentABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
class ModuleService(ModuleServiceABC):
def __init__(self, logger: LoggerABC, env: ApplicationEnvironmentABC):
self._logger = logger
self._env = env
self._modules: List[ModuleABC] = List()
def register(self, module: ModuleABC):
self._modules.append(module)
def start_modules(self):
self._modules.for_each(lambda m: m.echo())