Added cpl g & n discord support #143
This commit is contained in:
		
							
								
								
									
										54
									
								
								src/cpl_discord/.cpl/project_discord_bot.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/cpl_discord/.cpl/project_discord_bot.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| import os | ||||
|  | ||||
| from cpl_cli.abc.project_type_abc import ProjectTypeABC | ||||
| from cpl_cli.configuration import WorkspaceSettings | ||||
| from cpl_core.utils import String | ||||
|  | ||||
|  | ||||
| class DiscordBot(ProjectTypeABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             base_path: str, | ||||
|             project_name: str, | ||||
|             workspace: WorkspaceSettings, | ||||
|             use_application_api: bool, | ||||
|             use_startup: bool, | ||||
|             use_service_providing: bool, | ||||
|             use_async: bool, | ||||
|             project_file_data: dict, | ||||
|     ): | ||||
|         from project_file_discord import DiscordBotProjectFile | ||||
|         from project_file_discord_appsettings import DiscordBotProjectFileAppsettings | ||||
|         from project_file_discord_code_application import DiscordBotProjectFileApplication | ||||
|         from project_file_discord_code_main import DiscordBotProjectFileMain | ||||
|         from project_file_discord_code_startup import DiscordBotProjectFileStartup | ||||
|         from project_file_discord_readme import DiscordBotProjectFileReadme | ||||
|         from project_file_discord_license import DiscordBotProjectFileLicense | ||||
|         from schematic_discord_init import DiscordBotInit | ||||
|         from schematic_discord_event import Event | ||||
|         from schematic_discord_command import Command | ||||
|  | ||||
|         use_application_api, use_startup, use_service_providing, use_async = True, True, True, True | ||||
|  | ||||
|         ProjectTypeABC.__init__(self, base_path, project_name, workspace, use_application_api, use_startup, use_service_providing, use_async, project_file_data) | ||||
|  | ||||
|         project_path = f'{base_path}{String.convert_to_snake_case(project_name.split("/")[-1])}/' | ||||
|  | ||||
|         self.add_template(DiscordBotProjectFile(project_name.split('/')[-1], project_path, project_file_data)) | ||||
|         if workspace is None: | ||||
|             self.add_template(DiscordBotProjectFileLicense('')) | ||||
|             self.add_template(DiscordBotProjectFileReadme('')) | ||||
|             self.add_template(DiscordBotInit('', 'init', f'{base_path}tests/')) | ||||
|  | ||||
|         self.add_template(DiscordBotInit('', 'init', project_path)) | ||||
|         self.add_template(DiscordBotProjectFileAppsettings(project_path)) | ||||
|  | ||||
|         self.add_template(DiscordBotInit('', 'init', f'{project_path}events/')) | ||||
|         self.add_template(Event('OnReady', 'event', f'{project_path}events/')) | ||||
|         self.add_template(DiscordBotInit('', 'init', f'{project_path}commands/')) | ||||
|         self.add_template(Command('Ping', 'command', f'{project_path}commands/')) | ||||
|  | ||||
|         self.add_template(DiscordBotProjectFileApplication(project_path, use_application_api, use_startup, use_service_providing, use_async)) | ||||
|         self.add_template(DiscordBotProjectFileStartup(project_name.split('/')[-1], project_path, use_application_api, use_startup, use_service_providing, use_async)) | ||||
|         self.add_template(DiscordBotProjectFileMain(project_name.split('/')[-1], project_path, use_application_api, use_startup, use_service_providing, use_async)) | ||||
							
								
								
									
										14
									
								
								src/cpl_discord/.cpl/project_file_discord.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/cpl_discord/.cpl/project_file_discord.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| import json | ||||
|  | ||||
| from cpl_cli.abc.file_template_abc import FileTemplateABC | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFile(FileTemplateABC): | ||||
|  | ||||
|     def __init__(self, name: str, path: str, code: dict): | ||||
|         FileTemplateABC.__init__(self, '', path, '{}') | ||||
|         self._name = f'{name}.json' | ||||
|         self._code = code | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         return json.dumps(self._code, indent=2) | ||||
							
								
								
									
										34
									
								
								src/cpl_discord/.cpl/project_file_discord_appsettings.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/cpl_discord/.cpl/project_file_discord_appsettings.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| import textwrap | ||||
|  | ||||
| from cpl_cli.abc.file_template_abc import FileTemplateABC | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileAppsettings(FileTemplateABC): | ||||
|  | ||||
|     def __init__(self, path: str): | ||||
|         FileTemplateABC.__init__(self, '', path, '{}') | ||||
|         self._name = 'appsettings.json' | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         return textwrap.dedent("""\ | ||||
|         { | ||||
|           "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" | ||||
|           }, | ||||
|            | ||||
|           "DiscordBotSettings": { | ||||
|             "Token": "", | ||||
|             "Prefix": "!bot " | ||||
|           } | ||||
|         } | ||||
|         """) | ||||
| @@ -0,0 +1,53 @@ | ||||
| from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileApplication(CodeFileTemplateABC): | ||||
|  | ||||
|     def __init__(self, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool): | ||||
|         CodeFileTemplateABC.__init__(self, 'application', path, '', use_application_api, use_startup, use_service_providing, use_async) | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         import textwrap | ||||
|  | ||||
|         return textwrap.dedent("""\ | ||||
|         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 | ||||
|         from cpl_core.logging import LoggerABC | ||||
|         from cpl_discord.application.discord_bot_application_abc import DiscordBotApplicationABC | ||||
|         from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings | ||||
|         from cpl_discord.service.discord_bot_service import DiscordBotService | ||||
|         from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC | ||||
|          | ||||
|          | ||||
|         class Application(DiscordBotApplicationABC): | ||||
|          | ||||
|             def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): | ||||
|                 ApplicationABC.__init__(self, config, services) | ||||
|          | ||||
|                 self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC) | ||||
|                 self._logger: LoggerABC = services.get_service(LoggerABC) | ||||
|                 self._bot_settings: DiscordBotSettings = config.get_configuration(DiscordBotSettings) | ||||
|          | ||||
|             async def configure(self): | ||||
|                 pass | ||||
|          | ||||
|             async def main(self): | ||||
|                 try: | ||||
|                     self._logger.debug(__name__, f'Starting...\\n') | ||||
|                     self._logger.trace(__name__, f'Try to start {DiscordBotService.__name__}') | ||||
|                     await self._bot.start_async() | ||||
|                 except Exception as e: | ||||
|                     self._logger.error(__name__, 'Start failed', e) | ||||
|          | ||||
|             async def stop_async(self): | ||||
|                 try: | ||||
|                     self._logger.trace(__name__, f'Try to stop {DiscordBotService.__name__}') | ||||
|                     await self._bot.close() | ||||
|                     self._logger.trace(__name__, f'Stopped {DiscordBotService.__name__}') | ||||
|                 except Exception as e: | ||||
|                     self._logger.error(__name__, 'stop failed', e) | ||||
|          | ||||
|                 Console.write_line() | ||||
|         """) | ||||
							
								
								
									
										47
									
								
								src/cpl_discord/.cpl/project_file_discord_code_main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/cpl_discord/.cpl/project_file_discord_code_main.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC | ||||
| from cpl_core.utils import String | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileMain(CodeFileTemplateABC): | ||||
|  | ||||
|     def __init__(self, name: str, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool): | ||||
|         CodeFileTemplateABC.__init__(self, 'main', path, '', use_application_api, use_startup, use_service_providing, use_async) | ||||
|  | ||||
|         import textwrap | ||||
|  | ||||
|         import_pkg = f'{String.convert_to_snake_case(name)}.' | ||||
|  | ||||
|         self._main_with_application_host_and_startup = textwrap.dedent(f"""\ | ||||
|             import asyncio | ||||
|             from typing import Optional | ||||
|              | ||||
|             from cpl_core.application import ApplicationBuilder, ApplicationABC | ||||
|             from {import_pkg}application import Application | ||||
|             from {import_pkg}startup import Startup | ||||
|              | ||||
|              | ||||
|             class Program: | ||||
|              | ||||
|                 def __init__(self): | ||||
|                     self._app: Optional[Application] = None | ||||
|              | ||||
|                 async def main(self): | ||||
|                     app_builder = ApplicationBuilder(Application) | ||||
|                     app_builder.use_startup(Startup) | ||||
|                     self._app: ApplicationABC = await app_builder.build_async() | ||||
|                     await self._app.run_async() | ||||
|              | ||||
|                 async def stop(self): | ||||
|                     await self._app.stop_async() | ||||
|              | ||||
|              | ||||
|             if __name__ == '__main__': | ||||
|                 program = Program() | ||||
|                 try: | ||||
|                     asyncio.run(program.main()) | ||||
|                 except KeyboardInterrupt: | ||||
|                     asyncio.run(program.stop()) | ||||
|         """) | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         return self._main_with_application_host_and_startup | ||||
							
								
								
									
										47
									
								
								src/cpl_discord/.cpl/project_file_discord_code_startup.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/cpl_discord/.cpl/project_file_discord_code_startup.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC | ||||
| from cpl_core.utils import String | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileStartup(CodeFileTemplateABC): | ||||
|  | ||||
|     def __init__(self, project_name: str, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool): | ||||
|         CodeFileTemplateABC.__init__(self, 'startup', path, '', use_application_api, use_startup, use_service_providing, use_async) | ||||
|         self._project_name = project_name | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         import textwrap | ||||
|  | ||||
|         import_pkg = f'{String.convert_to_snake_case(self._project_name)}.' | ||||
|  | ||||
|         return textwrap.dedent(f"""\ | ||||
|         from cpl_core.application import StartupABC | ||||
|         from cpl_core.configuration import ConfigurationABC | ||||
|         from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC | ||||
|         from cpl_core.environment import ApplicationEnvironment | ||||
|         from cpl_discord import get_discord_collection | ||||
|         from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum | ||||
|         from {import_pkg}commands.ping_command import PingCommand | ||||
|         from {import_pkg}events.on_ready_event import OnReadyEvent | ||||
|          | ||||
|          | ||||
|         class Startup(StartupABC): | ||||
|          | ||||
|             def __init__(self): | ||||
|                 StartupABC.__init__(self) | ||||
|          | ||||
|             def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: | ||||
|                 configuration.add_json_file('appsettings.json', optional=False) | ||||
|                 configuration.add_environment_variables('CPL_') | ||||
|                 configuration.add_environment_variables('DISCORD_') | ||||
|          | ||||
|                 return configuration | ||||
|          | ||||
|             def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: | ||||
|                 services.add_logging() | ||||
|                 services.add_discord() | ||||
|                 dc_collection = get_discord_collection(services) | ||||
|                 dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyEvent) | ||||
|                 dc_collection.add_command(PingCommand) | ||||
|          | ||||
|                 return services.build_service_provider() | ||||
|         """) | ||||
							
								
								
									
										11
									
								
								src/cpl_discord/.cpl/project_file_discord_license.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/cpl_discord/.cpl/project_file_discord_license.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| from cpl_cli.abc.file_template_abc import FileTemplateABC | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileLicense(FileTemplateABC): | ||||
|  | ||||
|     def __init__(self, path: str): | ||||
|         FileTemplateABC.__init__(self, '', path, '') | ||||
|         self._name = 'LICENSE' | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         return self._code | ||||
							
								
								
									
										11
									
								
								src/cpl_discord/.cpl/project_file_discord_readme.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/cpl_discord/.cpl/project_file_discord_readme.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| from cpl_cli.abc.file_template_abc import FileTemplateABC | ||||
|  | ||||
|  | ||||
| class DiscordBotProjectFileReadme(FileTemplateABC): | ||||
|  | ||||
|     def __init__(self, path: str): | ||||
|         FileTemplateABC.__init__(self, '', path, '') | ||||
|         self._name = 'README.md' | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         return self._code | ||||
| @@ -6,7 +6,7 @@ from cpl_core.console import Console | ||||
| from cpl_core.utils import String | ||||
| 
 | ||||
| 
 | ||||
| class Command(GenerateSchematicABC): | ||||
| class Event(GenerateSchematicABC): | ||||
| 
 | ||||
|     def __init__(self, name: str, schematic: str, path: str): | ||||
|         GenerateSchematicABC.__init__(self, name, schematic, path) | ||||
| @@ -24,11 +24,17 @@ class Command(GenerateSchematicABC): | ||||
| 
 | ||||
|         if event is None: | ||||
|             Console.error(f'No valid event found in name {name}') | ||||
|             Console.write_line('Available events:') | ||||
|             for event_type in DiscordEventTypesEnum: | ||||
|                 Console.write_line(f'\t{event_type.value.__name__.replace("ABC", "")}') | ||||
|             sys.exit() | ||||
| 
 | ||||
|         self._event_class = f'{event}ABC' | ||||
|         self._name = f'{String.convert_to_snake_case(name)}_{String.convert_to_snake_case(self._event_class.replace("ABC", ""))}_{schematic}.py' | ||||
|         self._class_name = f'{String.first_to_upper(name)}{self._event_class.replace("ABC", "")}{String.first_to_upper(schematic)}' | ||||
|         self._name = f'{String.convert_to_snake_case(self._event_class.replace("ABC", ""))}_{schematic}.py' | ||||
|         self._class_name = f'{self._event_class.replace("ABC", "")}{String.first_to_upper(schematic)}' | ||||
|         if name != '': | ||||
|             self._name = f'{String.convert_to_snake_case(name)}_{self._name}' | ||||
|             self._class_name = f'{String.first_to_upper(name)}{self._class_name}' | ||||
| 
 | ||||
|     def get_code(self) -> str: | ||||
|         code = """\ | ||||
							
								
								
									
										24
									
								
								src/cpl_discord/.cpl/schematic_discord_init.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/cpl_discord/.cpl/schematic_discord_init.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| import textwrap | ||||
|  | ||||
| from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC | ||||
|  | ||||
|  | ||||
| class DiscordBotInit(GenerateSchematicABC): | ||||
|  | ||||
|     def __init__(self, *args: str): | ||||
|         GenerateSchematicABC.__init__(self, *args) | ||||
|         self._name = f'__init__.py' | ||||
|  | ||||
|     def get_code(self) -> str: | ||||
|         code = """\ | ||||
|         # imports | ||||
|         """ | ||||
|         return self.build_code_str(code, Name=self._class_name) | ||||
|  | ||||
|     @classmethod | ||||
|     def register(cls): | ||||
|         GenerateSchematicABC.register( | ||||
|             cls, | ||||
|             'init', | ||||
|             [] | ||||
|         ) | ||||
		Reference in New Issue
	
	Block a user