diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..255ae11 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + } + ] +} \ No newline at end of file diff --git a/src/gismo/application.py b/src/gismo/application.py index c5bf158..a000017 100644 --- a/src/gismo/application.py +++ b/src/gismo/application.py @@ -4,7 +4,8 @@ from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging import LoggerABC 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): diff --git a/src/gismo/startup.py b/src/gismo/startup.py index 333f888..9155d35 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -1,10 +1,16 @@ from cpl_core.application import StartupABC -from cpl_core.configuration import ConfigurationABC -from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC +from cpl_core.configuration import ConfigurationABC, ConsoleArgument +from cpl_core.console.console import Console +from cpl_core.dependency_injection import (ServiceCollectionABC, + ServiceProviderABC) from cpl_core.environment import ApplicationEnvironment 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): @@ -14,7 +20,7 @@ class Startup(StartupABC): async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: configuration.add_environment_variables('GISMO_') - configuration.add_console_arguments() + 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.host_name}.json', optional=True) @@ -24,6 +30,9 @@ class Startup(StartupABC): async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: services.add_logging() + services.add_singleton(ModuleServiceABC, ModuleService) services.add_singleton(BotServiceABC, BotService) + + services.add_singleton(ModuleABC, BootLog) return services.build_service_provider() diff --git a/src/gismo_core/services/__init__.py b/src/gismo_core/service/__init__.py similarity index 100% rename from src/gismo_core/services/__init__.py rename to src/gismo_core/service/__init__.py diff --git a/src/gismo_core/services/bot_service.py b/src/gismo_core/service/bot_service.py similarity index 65% rename from src/gismo_core/services/bot_service.py rename to src/gismo_core/service/bot_service.py index ceff6de..da34f9c 100644 --- a/src/gismo_core/services/bot_service.py +++ b/src/gismo_core/service/bot_service.py @@ -4,19 +4,27 @@ from discord.ext import commands from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.configuration.bot_settings import BotSettings from gismo_core.configuration.discord_settings import DiscordSettings +from modules_core.abc.module_service_abc import ModuleServiceABC 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._modules = modules + + # settings self._discord_settings = discord_settings self._bot_settings: BotSettings = bot_settings - # setup self + + # setup super commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None) async def start_async(self): + self._modules.start_modules() self.run(self._discord_settings.token) + # continue at on_ready async def stop_async(self): try: @@ -24,3 +32,5 @@ class BotService(BotServiceABC, commands.Bot): # save data except Exception as e: self._logger.error(__name__, 'Stop failed', e) + + async def on_ready(self): pass diff --git a/src/modules/boot_log/main.py b/src/modules/boot_log/main.py index 592134f..70634f0 100644 --- a/src/modules/boot_log/main.py +++ b/src/modules/boot_log/main.py @@ -1,13 +1,18 @@ from cpl_core.logging import LoggerABC 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._modules = modules self._bot = bot + modules.register(self) + def on_ready(self): self._logger.info(__name__, f'Bot started: {self._bot}') diff --git a/src/modules_core/__init__.py b/src/modules_core/__init__.py index 5a29729..425ab6c 100644 --- a/src/modules_core/__init__.py +++ b/src/modules_core/__init__.py @@ -1,25 +1 @@ -# -*- coding: utf-8 -*- - -""" -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') +# imports diff --git a/src/modules_core/abc/__init__.py b/src/modules_core/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules_core/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules_core/abc/module_abc.py b/src/modules_core/abc/module_abc.py new file mode 100644 index 0000000..bdf1b63 --- /dev/null +++ b/src/modules_core/abc/module_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class ModuleABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/modules_core/abc/module_service_abc.py b/src/modules_core/abc/module_service_abc.py new file mode 100644 index 0000000..b510857 --- /dev/null +++ b/src/modules_core/abc/module_service_abc.py @@ -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 diff --git a/src/modules_core/service/__init__.py b/src/modules_core/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules_core/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py new file mode 100644 index 0000000..de14dd7 --- /dev/null +++ b/src/modules_core/service/module_service.py @@ -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())