-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a767a4b
commit 6536bdb
Showing
13 changed files
with
169 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
|
||
import inspect | ||
import logging | ||
from discord.ext.commands import Bot | ||
from pyramid.connector.discord.commands.api.abc import AbstractCommand | ||
from pyramid.connector.discord.commands.api.parameters import ParametersCommand | ||
|
||
COMMANDS_AUTOREGISTRED: dict[type[AbstractCommand], ParametersCommand] = {} | ||
COMMANDS_TO_REGISTER: dict[type[AbstractCommand], ParametersCommand] = {} | ||
|
||
def register_commands(bot: Bot, logger: logging.Logger, command_prefix: str | None = None): | ||
for cls, parameters in COMMANDS_AUTOREGISTRED.items(): | ||
def register_commands(services: dict[str, object], bot: Bot, logger: logging.Logger, command_prefix: str | None = None): | ||
for cls, parameters in COMMANDS_TO_REGISTER.items(): | ||
class_instance = cls(parameters, bot, logger) | ||
class_instance.register(command_prefix) | ||
logger.info("%s - %s" % (vars(cls), vars(parameters))) | ||
# logger.info("%s - %s" % (vars(cls), vars(parameters))) | ||
# logger.info("services %s" % ", ".join(services.keys())) | ||
|
||
signature = inspect.signature(class_instance.injectService) | ||
params = list(signature.parameters.values()) | ||
# for param in params: | ||
# logger.info("param %s" % param.annotation) | ||
|
||
# logger.info("params %s" % ", ".join(params)) | ||
dependencies = [services[param.annotation.__name__] for param in params] | ||
# logger.info("dependencies %s" % (vars(dependencies))) | ||
class_instance.injectService(*dependencies) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pyramid.connector.discord.services.api.register import SERVICE_TO_REGISTER | ||
|
||
|
||
def pyramid_service(): | ||
def decorator(cls): | ||
# if not issubclass(cls, AbstractService): | ||
# raise TypeError(f"Class {cls.__name__} must extend from AbstractListener") | ||
|
||
class_name = cls.__name__ | ||
SERVICE_TO_REGISTER[class_name] = cls | ||
return cls | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
import logging | ||
from abc import ABC, abstractmethod | ||
from discord.ext.commands import Bot | ||
|
||
class ServiceInjector(ABC): | ||
|
||
def __init__(self, bot: Bot, logger: logging.Logger): | ||
self.bot = bot | ||
self.logger = logger | ||
|
||
def injectService(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import logging | ||
from discord.ext.commands import Bot | ||
from pyramid.connector.discord.services.api.injector import ServiceInjector | ||
|
||
|
||
SERVICE_TO_REGISTER: dict[str, type[object]] = {} | ||
SERVICE_REGISTRED: dict[str, object] = {} | ||
|
||
def register_services(bot: Bot, logger: logging.Logger): | ||
for name, cls in SERVICE_TO_REGISTER.items(): | ||
if issubclass(cls, ServiceInjector): | ||
class_instance = cls(bot, logger) | ||
else: | ||
class_instance = cls() | ||
SERVICE_REGISTRED[name] = class_instance | ||
logger.info("SERVICE_REGISTRED %s" % name) | ||
|
||
def get_service(name: str): | ||
return SERVICE_REGISTRED[name] | ||
|
||
def define_bot(bot: Bot): | ||
for _, instance in SERVICE_REGISTRED.items(): | ||
if isinstance(instance, ServiceInjector): | ||
instance.bot = bot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
from pyramid.connector.discord.services.api.annotation import pyramid_service | ||
from pyramid.data.environment import Environment | ||
|
||
|
||
@pyramid_service() | ||
class EnvironmentService: | ||
|
||
def __init__(self): | ||
self.__type: Environment = Environment.PRODUCTION | ||
|
||
def get_type(self): | ||
return self.__type | ||
|
||
def get_type_name(self): | ||
return self.__type.name.capitalize() | ||
|
||
def set_type(self, environnement: Environment): | ||
self.__type = environnement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
|
||
from pyramid.connector.discord.services.api.annotation import pyramid_service | ||
|
||
|
||
@pyramid_service() | ||
class LoggerService: | ||
|
||
def __init__(self): | ||
self.__logger = logging.getLogger() | ||
|
||
def critical(self, msg, *args, **kwargs): | ||
self.__logger.critical(msg, *args, **kwargs) | ||
|
||
def error(self, msg, *args, **kwargs): | ||
self.__logger.error(msg, *args, **kwargs) | ||
|
||
def warning(self, msg, *args, **kwargs): | ||
self.__logger.warning(msg, *args, **kwargs) | ||
|
||
def info(self, msg, *args, **kwargs): | ||
self.__logger.info(msg, *args, **kwargs) | ||
|
||
def debug(self, msg, *args, **kwargs): | ||
self.__logger.debug(msg, *args, **kwargs) | ||
|
||
def log(self, level, msg, *args, **kwargs): | ||
self.__logger.log(msg, level, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters