Added discord command & event to cpl g schematics #143

This commit is contained in:
Sven Heidemann 2022-12-08 10:36:23 +01:00
parent 4fbb3ec838
commit 2f8dc63cdc
10 changed files with 146 additions and 16 deletions

View File

@ -1,10 +1,14 @@
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
from cpl_core.utils import String
class ABC(GenerateSchematicABC): class ABC(GenerateSchematicABC):
def __init__(self, *args): def __init__(self, name: str, schematic: str, path: str):
GenerateSchematicABC.__init__(self, *args) GenerateSchematicABC.__init__(self, name, schematic, path)
self._class_name = name
if name != '':
self._class_name = f'{String.first_to_upper(name.replace(schematic, ""))}ABC'
def get_code(self) -> str: def get_code(self) -> str:
code = """\ code = """\

View File

@ -1,3 +1,4 @@
import importlib
import os import os
import sys import sys
import textwrap import textwrap
@ -7,6 +8,7 @@ from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
from cpl_cli.command_abc import CommandABC from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration import WorkspaceSettings from cpl_cli.configuration import WorkspaceSettings
from cpl_cli.configuration.schematic_collection import SchematicCollection from cpl_cli.configuration.schematic_collection import SchematicCollection
from cpl_cli.helper.dependencies import Dependencies
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
@ -33,7 +35,10 @@ class GenerateService(CommandABC):
self._env = self._config.environment self._env = self._config.environment
self._schematics = {} self._schematics = {}
self._read_custom_schematics_from_path(self._env.runtime_directory) for package_name in Dependencies.get_cpl_packages():
package = importlib.import_module(String.convert_to_snake_case(package_name[0]))
self._read_custom_schematics_from_path(os.path.dirname(package.__file__))
self._read_custom_schematics_from_path(self._env.working_directory) self._read_custom_schematics_from_path(self._env.working_directory)
if len(GenerateSchematicABC.__subclasses__()) == 0: if len(GenerateSchematicABC.__subclasses__()) == 0:

View File

@ -5,6 +5,7 @@ import pkg_resources
import textwrap import textwrap
import cpl_cli import cpl_cli
from cpl_cli.helper.dependencies import Dependencies
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_cli.command_abc import CommandABC from cpl_cli.command_abc import CommandABC
@ -31,16 +32,6 @@ class VersionService(CommandABC):
:param args: :param args:
:return: :return:
""" """
packages = []
cpl_packages = []
dependencies = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
for p in dependencies:
if str(p).startswith('cpl-'):
cpl_packages.append([p, dependencies[p]])
continue
packages.append([p, dependencies[p]])
Console.set_foreground_color(ForegroundColorEnum.yellow) Console.set_foreground_color(ForegroundColorEnum.yellow)
Console.banner('CPL CLI') Console.banner('CPL CLI')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
@ -52,6 +43,6 @@ class VersionService(CommandABC):
Console.write(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}') Console.write(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')
Console.write_line(f'OS: {platform.system()} {platform.processor()}') Console.write_line(f'OS: {platform.system()} {platform.processor()}')
Console.write_line('\nCPL packages:') Console.write_line('\nCPL packages:')
Console.table(['Name', 'Version'], cpl_packages) Console.table(['Name', 'Version'], Dependencies.get_cpl_packages())
Console.write_line('\nPython packages:') Console.write_line('\nPython packages:')
Console.table(['Name', 'Version'], packages) Console.table(['Name', 'Version'], Dependencies.get_packages())

View File

View File

@ -0,0 +1,22 @@
import pkg_resources
class Dependencies:
_packages = []
_cpl_packages = []
_dependencies = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
for p in _dependencies:
if str(p).startswith('cpl-'):
_cpl_packages.append([p, _dependencies[p]])
continue
_packages.append([p, _dependencies[p]])
@classmethod
def get_cpl_packages(cls) -> list[list]:
return cls._cpl_packages
@classmethod
def get_packages(cls) -> list[list]:
return cls._packages

View File

View File

@ -0,0 +1,44 @@
import textwrap
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
class Command(GenerateSchematicABC):
def __init__(self, *args: str):
GenerateSchematicABC.__init__(self, *args)
def get_code(self) -> str:
code = """\
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from discord.ext import commands
from discord.ext.commands import Context
class $Name(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._bot = bot
@commands.hybrid_command()
async def ping(self, ctx: Context):
await ctx.send('Pong')
"""
return self.build_code_str(code, Name=self._class_name)
@classmethod
def register(cls):
GenerateSchematicABC.register(
cls,
'command',
[]
)

View File

@ -0,0 +1,63 @@
import sys
import textwrap
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
from cpl_core.console import Console
from cpl_core.utils import String
class Command(GenerateSchematicABC):
def __init__(self, name: str, schematic: str, path: str):
GenerateSchematicABC.__init__(self, name, schematic, path)
event = None
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
for event_type in DiscordEventTypesEnum:
event_name = event_type.value.__name__.replace("ABC", '')
if event_name in name:
name = name.replace(event_name, "")
event = event_name
break
if event is None:
Console.error(f'No valid event found in name {name}')
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)}'
def get_code(self) -> str:
code = """\
from cpl_core.logging import LoggerABC
from cpl_discord.events import $EventClass
from cpl_discord.service import DiscordBotServiceABC
class $Name($EventClass):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC,
):
OnReadyABC.__init__(self)
self._logger = logger
self._bot = bot
async def on_ready(self):
pass
"""
return self.build_code_str(code, Name=self._class_name, EventClass=self._event_class)
@classmethod
def register(cls):
GenerateSchematicABC.register(
cls,
'event',
[]
)

View File

@ -9,4 +9,4 @@ class OnGroupJoinABC(ABC):
@abstractmethod @abstractmethod
async def on_group_join( async def on_group_join(
self, chhanel: discord.GroupChannel, user: discord.User): pass self, channel: discord.GroupChannel, user: discord.User): pass

View File

@ -0,0 +1 @@
# imports