mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
better perf
This commit is contained in:
@@ -7,14 +7,17 @@ from argenta.command import Command
|
||||
from argenta.response import Response
|
||||
|
||||
|
||||
HandlerFunc = Callable[..., None]
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler_as_func: Callable[..., None], handled_command: Command):
|
||||
def __init__(self, handler_as_func: HandlerFunc, handled_command: Command):
|
||||
"""
|
||||
Private. Entity of the model linking the handler and the command being processed
|
||||
:param handler: the handler being called
|
||||
:param handled_command: the command being processed
|
||||
"""
|
||||
self.handler_as_func: Callable[..., None] = handler_as_func
|
||||
self.handler_as_func: HandlerFunc = handler_as_func
|
||||
self.handled_command: Command = handled_command
|
||||
|
||||
def handling(self, response: Response) -> None:
|
||||
@@ -27,12 +30,13 @@ class CommandHandler:
|
||||
|
||||
|
||||
class CommandHandlers:
|
||||
def __init__(self, command_handlers: list[CommandHandler] | None = None):
|
||||
def __init__(self, command_handlers: tuple[CommandHandler] = tuple()):
|
||||
"""
|
||||
Private. The model that unites all CommandHandler of the routers
|
||||
:param command_handlers: list of CommandHandlers for register
|
||||
"""
|
||||
self.command_handlers: list[CommandHandler] = command_handlers if command_handlers else []
|
||||
self.command_handlers: list[CommandHandler] = list(command_handlers) if command_handlers else []
|
||||
self.paired_command_handler_trigger: dict[str, CommandHandler] = {x.handled_command.trigger: x for x in command_handlers}
|
||||
|
||||
def add_handler(self, command_handler: CommandHandler) -> None:
|
||||
"""
|
||||
@@ -41,6 +45,13 @@ class CommandHandlers:
|
||||
:return: None
|
||||
"""
|
||||
self.command_handlers.append(command_handler)
|
||||
self.paired_command_handler_trigger[command_handler.handled_command.trigger.lower()] = command_handler
|
||||
for alias in command_handler.handled_command.aliases:
|
||||
self.paired_command_handler_trigger[alias.lower()] = command_handler
|
||||
|
||||
def get_command_handler_by_trigger(self, trigger: str):
|
||||
print(self.paired_command_handler_trigger)
|
||||
return self.paired_command_handler_trigger.get(trigger)
|
||||
|
||||
def __iter__(self) -> Iterator[CommandHandler]:
|
||||
return iter(self.command_handlers)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
__all__ = ["system_router"]
|
||||
|
||||
from argenta.router import Router
|
||||
|
||||
system_router = Router(title="System points:")
|
||||
@@ -11,7 +11,9 @@ from argenta.command.flag.flags import Flags, InputFlags
|
||||
from argenta.response import Response, ResponseStatus
|
||||
from argenta.router.command_handler.entity import CommandHandler, CommandHandlers
|
||||
from argenta.router.exceptions import (
|
||||
RepeatedAliasNameException,
|
||||
RepeatedFlagNameException,
|
||||
RepeatedTriggerNameException,
|
||||
RequiredArgumentNotPassedException,
|
||||
TriggerContainSpacesException,
|
||||
)
|
||||
@@ -57,13 +59,8 @@ class Router:
|
||||
redefined_command = command
|
||||
|
||||
self._validate_command(redefined_command)
|
||||
|
||||
if overlapping := (self.aliases | self.triggers) & redefined_command.aliases:
|
||||
Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{overlapping}[/b blue]")
|
||||
self._update_routing_keys(redefined_command)
|
||||
|
||||
self.aliases.update(redefined_command.aliases)
|
||||
self.triggers.add(redefined_command.trigger)
|
||||
|
||||
def decorator(func: HandlerFunc) -> HandlerFunc:
|
||||
_validate_func_args(func)
|
||||
self.command_handlers.add_handler(CommandHandler(func, redefined_command))
|
||||
@@ -80,10 +77,22 @@ class Router:
|
||||
command_name: str = command.trigger
|
||||
if command_name.find(" ") != -1:
|
||||
raise TriggerContainSpacesException()
|
||||
|
||||
if command_name.lower() in self.triggers:
|
||||
raise RepeatedTriggerNameException()
|
||||
|
||||
if overlapping := (self.aliases | self.triggers) & set(map(lambda x: x.lower(), command.aliases)):
|
||||
raise RepeatedAliasNameException(overlapping)
|
||||
|
||||
flags: Flags = command.registered_flags
|
||||
flags_name: list[str] = [flag.string_entity.lower() for flag in flags]
|
||||
if len(set(flags_name)) < len(flags_name):
|
||||
raise RepeatedFlagNameException()
|
||||
|
||||
def _update_routing_keys(self, registered_command: Command):
|
||||
redefined_command_aliases_in_lower = set(map(lambda x: x.lower(), registered_command.aliases))
|
||||
self.aliases.update(redefined_command_aliases_in_lower)
|
||||
self.triggers.add(registered_command.trigger.lower())
|
||||
|
||||
def finds_appropriate_handler(self, input_command: InputCommand) -> None:
|
||||
"""
|
||||
@@ -91,15 +100,15 @@ class Router:
|
||||
:param input_command: input command as InputCommand
|
||||
:return: None
|
||||
"""
|
||||
input_command_name: str = input_command.trigger
|
||||
input_command_name: str = input_command.trigger.lower()
|
||||
input_command_flags: InputFlags = input_command.input_flags
|
||||
|
||||
for command_handler in self.command_handlers:
|
||||
handle_command = command_handler.handled_command
|
||||
if input_command_name.lower() == handle_command.trigger.lower():
|
||||
self.process_input_command(input_command_flags, command_handler)
|
||||
if input_command_name.lower() in handle_command.aliases:
|
||||
self.process_input_command(input_command_flags, command_handler)
|
||||
command_handler = self.command_handlers.get_command_handler_by_trigger(input_command_name)
|
||||
|
||||
if not command_handler:
|
||||
raise RuntimeError(f"Handler for '{input_command.trigger}' command not found!")
|
||||
else:
|
||||
self.process_input_command(input_command_flags, command_handler)
|
||||
|
||||
def process_input_command(self, input_command_flags: InputFlags, command_handler: CommandHandler) -> None:
|
||||
"""
|
||||
@@ -147,13 +156,14 @@ def _structuring_input_flags(handled_command: Command, input_flags: InputFlags)
|
||||
undefined_flags = True
|
||||
|
||||
status = ResponseStatus.from_flags(
|
||||
has_invalid_value_flags=invalid_value_flags, has_undefined_flags=undefined_flags
|
||||
has_invalid_value_flags=invalid_value_flags,
|
||||
has_undefined_flags=undefined_flags
|
||||
)
|
||||
|
||||
return Response(status=status, input_flags=input_flags)
|
||||
|
||||
|
||||
def _validate_func_args(func: Callable[..., None]) -> None:
|
||||
def _validate_func_args(func: HandlerFunc) -> None:
|
||||
"""
|
||||
Private. Validates the arguments of the handler
|
||||
:param func: entity of the handler func
|
||||
|
||||
@@ -11,7 +11,31 @@ class RepeatedFlagNameException(Exception):
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return "Repeated registered flag names in register command"
|
||||
|
||||
|
||||
class RepeatedTriggerNameException(Exception):
|
||||
"""
|
||||
Private. Raised when a repeated trigger name is registered
|
||||
"""
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return "Repeated trigger name in registered commands"
|
||||
|
||||
|
||||
class RepeatedAliasNameException(Exception):
|
||||
"""
|
||||
Private. Raised when a repeated alias name is registered
|
||||
"""
|
||||
@override
|
||||
def __init__(self, repeated_aliases: set[str]) -> None:
|
||||
self.repeated_aliases = repeated_aliases
|
||||
super().__init__()
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return f"Repeated aliases names: {self.repeated_aliases}"
|
||||
|
||||
|
||||
class RequiredArgumentNotPassedException(Exception):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user