diff --git a/.gitignore b/.gitignore index f8b73e7..c337315 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,6 @@ dmypy.json # Cython debug symbols cython_debug/ +# gismo +g-env/ + diff --git a/LICENSE b/LICENSE index 204b93d..a23c753 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -MIT License Copyright (c) +MIT License Copyright (c) 2021-2022 sh-edraft.de Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/gismo/application.py b/src/gismo/application.py index b2df025..a85f7d3 100644 --- a/src/gismo/application.py +++ b/src/gismo/application.py @@ -1,16 +1,16 @@ -from cpl_core.application import ApplicationABC -from cpl_core.configuration import ConfigurationABC -from cpl_core.console import Console -from cpl_core.dependency_injection import ServiceProviderABC - - class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) + self._bot: BotService = services.get_service(BotServiceABC) + self._logger: LoggerABC = services.get_service(LoggerABC) + async def configure(self): pass async def main(self): - Console.write_line('Hello World') + try: + await self._bot.run() + except Exception as e: + self._logger.error(__name__, 'A fatal error occured starting the bot', e) diff --git a/src/gismo/appsettings.development.json b/src/gismo/appsettings.development.json new file mode 100644 index 0000000..c18c91e --- /dev/null +++ b/src/gismo/appsettings.development.json @@ -0,0 +1,15 @@ +{ + "TimeFormatSettings": { + "DateFormat": "%Y-%m-%d", + "TimeFormat": "%H:%M:%S", + "DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f", + "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" + }, + + "LoggingSettings": { + "Path": "logs/", + "Filename": "log_$start_time.log", + "ConsoleLogLevel": "TRACE", + "FileLogLevel": "TRACE" + } +} diff --git a/src/gismo/appsettings.edrafts-pc.json b/src/gismo/appsettings.edrafts-pc.json new file mode 100644 index 0000000..07e013f --- /dev/null +++ b/src/gismo/appsettings.edrafts-pc.json @@ -0,0 +1,5 @@ +{ + "Discord": { + "Token": "" + } +} diff --git a/src/gismo/appsettings.production.json b/src/gismo/appsettings.production.json new file mode 100644 index 0000000..629e6eb --- /dev/null +++ b/src/gismo/appsettings.production.json @@ -0,0 +1,15 @@ +{ + "TimeFormatSettings": { + "DateFormat": "%Y-%m-%d", + "TimeFormat": "%H:%M:%S", + "DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f", + "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" + }, + + "LoggingSettings": { + "Path": "logs/", + "Filename": "log_$start_time.log", + "ConsoleLogLevel": "ERROR", + "FileLogLevel": "WARN" + } +} diff --git a/src/gismo/appsettings.staging.json b/src/gismo/appsettings.staging.json new file mode 100644 index 0000000..1fce9ac --- /dev/null +++ b/src/gismo/appsettings.staging.json @@ -0,0 +1,15 @@ +{ + "TimeFormatSettings": { + "DateFormat": "%Y-%m-%d", + "TimeFormat": "%H:%M:%S", + "DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f", + "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" + }, + + "LoggingSettings": { + "Path": "logs/", + "Filename": "log_$start_time.log", + "ConsoleLogLevel": "DEBUG", + "FileLogLevel": "DEBUG" + } +} diff --git a/src/gismo/gismo.json b/src/gismo/gismo.json index d5dda70..b8a447a 100644 --- a/src/gismo/gismo.json +++ b/src/gismo/gismo.json @@ -17,7 +17,8 @@ "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ "sh_cpl-core>=2021.10.2", - "sh_cpl-query>=2021.10.2" + "sh_cpl-query>=2021.10.2", + "discord.py==1.7.3" ], "PythonVersion": ">=3.9.2", "PythonPath": { diff --git a/src/gismo/main.py b/src/gismo/main.py index 4311d0e..808c102 100644 --- a/src/gismo/main.py +++ b/src/gismo/main.py @@ -2,8 +2,8 @@ import asyncio from cpl_core.application import ApplicationBuilder -from gismo.application import Application -from gismo.startup import Startup +from application import Application +from startup import Startup async def main(): diff --git a/src/gismo/startup.py b/src/gismo/startup.py index dadaee3..333f888 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -3,6 +3,9 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.environment import ApplicationEnvironment +from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.services.bot_service import BotService + class Startup(StartupABC): @@ -10,7 +13,17 @@ class Startup(StartupABC): StartupABC.__init__(self) 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) + return configuration async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: + services.add_logging() + + services.add_singleton(BotServiceABC, BotService) + return services.build_service_provider() diff --git a/src/gismo_core/__init__.py b/src/gismo_core/__init__.py index 671be7b..425ab6c 100644 --- a/src/gismo_core/__init__.py +++ b/src/gismo_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__ = 'gismo_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/gismo_core/abc/__init__.py b/src/gismo_core/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/gismo_core/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/gismo_core/abc/bot_service_abc.py b/src/gismo_core/abc/bot_service_abc.py new file mode 100644 index 0000000..62972ca --- /dev/null +++ b/src/gismo_core/abc/bot_service_abc.py @@ -0,0 +1,13 @@ +from abc import ABC, abstractmethod + + +class BotServiceABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + async def run(self): pass + + @abstractmethod + async def exit(self): pass diff --git a/src/gismo_core/configuration/__init__.py b/src/gismo_core/configuration/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/gismo_core/configuration/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/gismo_core/configuration/discord_settings.py b/src/gismo_core/configuration/discord_settings.py new file mode 100644 index 0000000..948729c --- /dev/null +++ b/src/gismo_core/configuration/discord_settings.py @@ -0,0 +1,23 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class DiscordSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._token: str = '' + + @property + def token(self) -> str: + return self._token + + def from_dict(self, settings: dict): + try: + self._token = settings['Token'] + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/gismo_core/services/__init__.py b/src/gismo_core/services/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/gismo_core/services/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/gismo_core/services/bot_service.py b/src/gismo_core/services/bot_service.py new file mode 100644 index 0000000..e70f973 --- /dev/null +++ b/src/gismo_core/services/bot_service.py @@ -0,0 +1,15 @@ +from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.configuration.discord_settings import DiscordSettings + + +class BotService(BotServiceABC): + + def __init__(self, discord_settings: DiscordSettings): + # setup self + commands.Bot.__init__(self, command_prefix=discord_settings.bot.prefix, help_command=None) + + async def run(self): + pass + + async def exit(self): + pass