mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
refactor, new model e.t.c.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
from typing import Callable
|
||||
from argenta.command import Command
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
||||
self.handler = handler
|
||||
self.handled_command = handled_command
|
||||
|
||||
def handling(self, input_flags: dict = None):
|
||||
if input_flags is not None:
|
||||
self.handler(input_flags)
|
||||
else:
|
||||
self.handler()
|
||||
|
||||
def get_handler(self):
|
||||
return self.handler
|
||||
|
||||
def get_handled_command(self):
|
||||
return self.handled_command
|
||||
@@ -0,0 +1,21 @@
|
||||
from argenta.router.command_handler.entity import CommandHandler
|
||||
|
||||
|
||||
class CommandHandlers:
|
||||
def __init__(self, command_handlers: list[CommandHandler] = None):
|
||||
self.command_handlers = command_handlers if command_handlers else []
|
||||
|
||||
def get_command_handlers(self) -> list[CommandHandler]:
|
||||
return self.command_handlers
|
||||
|
||||
def add_command_handler(self, command_handler: CommandHandler):
|
||||
self.command_handlers.append(command_handler)
|
||||
|
||||
def add_command_handlers(self, *command_handlers: CommandHandler):
|
||||
self.command_handlers.extend(command_handlers)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.command_handlers)
|
||||
|
||||
def __next__(self):
|
||||
return next(iter(self.command_handlers))
|
||||
+12
-13
@@ -2,10 +2,10 @@ from typing import Callable, Any
|
||||
from inspect import getfullargspec
|
||||
|
||||
from argenta.command import Command
|
||||
from argenta.router.models import CommandHandler, CommandHandlers
|
||||
from argenta.command.flag import Flag, FlagsGroup
|
||||
from argenta.router.exceptions import (RepeatedCommandException,
|
||||
RepeatedFlagNameException,
|
||||
from argenta.router.command_handlers.entity import CommandHandlers
|
||||
from argenta.router.command_handler.entity import CommandHandler
|
||||
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||
from argenta.router.exceptions import (RepeatedFlagNameException,
|
||||
TooManyTransferredArgsException,
|
||||
RequiredArgumentNotPassedException,
|
||||
IncorrectNumberOfHandlerArgsException,
|
||||
@@ -61,26 +61,25 @@ class Router:
|
||||
if not is_valid:
|
||||
self._not_valid_flag_handler(flag)
|
||||
return
|
||||
return command_handler.handling(input_command_flags.unparse_to_dict())
|
||||
command_handler.handling(input_command_flags.unparse_to_dict())
|
||||
return
|
||||
else:
|
||||
return command_handler.handling({})
|
||||
command_handler.handling({})
|
||||
return
|
||||
else:
|
||||
if input_command_flags:
|
||||
self._not_valid_flag_handler(input_command_flags[0])
|
||||
return
|
||||
else:
|
||||
return command_handler.handling()
|
||||
command_handler.handling()
|
||||
return
|
||||
|
||||
|
||||
def _validate_command(self, command: Command):
|
||||
@staticmethod
|
||||
def _validate_command(command: Command):
|
||||
command_name: str = command.get_trigger()
|
||||
if command_name.find(' ') != -1:
|
||||
raise TriggerCannotContainSpacesException()
|
||||
if command_name in self.get_all_commands():
|
||||
raise RepeatedCommandException()
|
||||
if self._ignore_command_register:
|
||||
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
||||
raise RepeatedCommandException()
|
||||
|
||||
flags: FlagsGroup = command.get_registered_flags()
|
||||
if flags:
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
class RepeatedCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Commands in handler cannot be repeated"
|
||||
|
||||
|
||||
class RepeatedFlagNameException(Exception):
|
||||
def __str__(self):
|
||||
return "Repeated flag name in register command"
|
||||
return "Repeated registered_flag name in register command"
|
||||
|
||||
|
||||
class TooManyTransferredArgsException(Exception):
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
from typing import Callable
|
||||
from argenta.command import Command
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
||||
self.handler = handler
|
||||
self.handled_command = handled_command
|
||||
|
||||
def handling(self, input_flags: dict = None):
|
||||
if input_flags is not None:
|
||||
self.handler(input_flags)
|
||||
else:
|
||||
self.handler()
|
||||
|
||||
def get_handler(self):
|
||||
return self.handler
|
||||
|
||||
def get_handled_command(self):
|
||||
return self.handled_command
|
||||
|
||||
|
||||
class CommandHandlers:
|
||||
def __init__(self, command_handlers: list[CommandHandler] = None):
|
||||
self.command_handlers = command_handlers if command_handlers else []
|
||||
|
||||
def get_command_handlers(self) -> list[CommandHandler]:
|
||||
return self.command_handlers
|
||||
|
||||
def add_command_handler(self, command_handler: CommandHandler):
|
||||
self.command_handlers.append(command_handler)
|
||||
|
||||
def add_command_handlers(self, *command_handlers: CommandHandler):
|
||||
self.command_handlers.extend(command_handlers)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.command_handlers)
|
||||
|
||||
def __next__(self):
|
||||
return next(iter(self.command_handlers))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user