This commit is contained in:
2025-05-07 02:15:42 +03:00
parent 61ef6a6466
commit c4b3aa7db8
6 changed files with 44 additions and 25 deletions
+5 -3
View File
@@ -14,8 +14,10 @@ from argenta.command.models import InputCommand
import inspect import inspect
inv = InvalidValueInputFlags(InputFlag('test')) router = Router()
inve = InvalidValueInputFlags(InputFlag('test'))
print(inv == inve) @router.command(Command('some'))
def handler(res: Response) -> Response:
pass
+1 -1
View File
@@ -20,7 +20,7 @@ def command_help(response: Response):
print(response.invalid_value_flags.get_flags()) print(response.invalid_value_flags.get_flags())
@work_router.command(Command('run', 'Run All')) @work_router.command('run')
def command_start_solving(response: Response): def command_start_solving(response: Response):
print(response.status) print(response.status)
print(response.undefined_flags.get_flags()) print(response.undefined_flags.get_flags())
+1 -1
View File
@@ -9,7 +9,7 @@ license = { text = "MIT" }
dependencies = [ dependencies = [
"rich (>=14.0.0,<15.0.0)", "rich (>=14.0.0,<15.0.0)",
"art (>=6.4,<7.0)", "art (>=6.4,<7.0)",
"pyreadline3 (>=3.5.4,<4.0.0)", "pyreadline3>=3.5.4",
] ]
[dependency-groups] [dependency-groups]
+1 -1
View File
@@ -39,7 +39,7 @@ class Command(BaseCommand):
""" """
super().__init__(trigger) super().__init__(trigger)
self._registered_flags: Flags = flags if isinstance(flags, Flags) else Flags(flags) if isinstance(flags, Flag) else Flags() self._registered_flags: Flags = flags if isinstance(flags, Flags) else Flags(flags) if isinstance(flags, Flag) else Flags()
self._description = f'Description for "{self._trigger}" command' if not description else description self._description = f'Very useful command' if not description else description
self._aliases = aliases if isinstance(aliases, list) else [] self._aliases = aliases if isinstance(aliases, list) else []
def get_registered_flags(self) -> Flags: def get_registered_flags(self) -> Flags:
+3 -1
View File
@@ -1,5 +1,7 @@
from argenta.command.flags.models import ValidInputFlags, UndefinedInputFlags, InvalidValueInputFlags
from argenta.response.status import Status from argenta.response.status import Status
from argenta.command.flags import (ValidInputFlags,
UndefinedInputFlags,
InvalidValueInputFlags)
class Response: class Response:
+25 -10
View File
@@ -6,7 +6,10 @@ from argenta.command import Command
from argenta.command.models import InputCommand from argenta.command.models import InputCommand
from argenta.response import Response, Status from argenta.response import Response, Status
from argenta.router.command_handler.entity import CommandHandlers, CommandHandler from argenta.router.command_handler.entity import CommandHandlers, CommandHandler
from argenta.command.flags.models import Flags, InputFlags, UndefinedInputFlags, ValidInputFlags, InvalidValueInputFlags from argenta.command.flags.models import (Flags, InputFlags,
UndefinedInputFlags,
ValidInputFlags,
InvalidValueInputFlags)
from argenta.router.exceptions import (RepeatedFlagNameException, from argenta.router.exceptions import (RepeatedFlagNameException,
TooManyTransferredArgsException, TooManyTransferredArgsException,
RequiredArgumentNotPassedException, RequiredArgumentNotPassedException,
@@ -26,13 +29,15 @@ class Router:
self._ignore_command_register: bool = False self._ignore_command_register: bool = False
def command(self, command: Command) -> Callable: def command(self, command: Command | str) -> Callable:
""" """
Public. Registers handler Public. Registers handler
:param command: Registered command :param command: Registered command
:return: decorated handler as Callable :return: decorated handler as Callable
""" """
self._validate_command(command) self._validate_command(command)
if isinstance(command, str):
command = Command(command)
def command_decorator(func): def command_decorator(func):
Router._validate_func_args(func) Router._validate_func_args(func)
@@ -128,21 +133,25 @@ class Router:
@staticmethod @staticmethod
def _validate_command(command: Command) -> None: def _validate_command(command: Command | str) -> None:
""" """
Private. Validates the command registered in handler Private. Validates the command registered in handler
:param command: validated command :param command: validated command
:return: None if command is valid else raise exception :return: None if command is valid else raise exception
""" """
match type(command).__name__:
case 'Command':
command_name: str = command.get_trigger() command_name: str = command.get_trigger()
if command_name.find(' ') != -1: if command_name.find(' ') != -1:
raise TriggerContainSpacesException() raise TriggerContainSpacesException()
flags: Flags = command.get_registered_flags() flags: Flags = command.get_registered_flags()
if flags: if flags:
flags_name: list = [x.get_string_entity().lower() for x in flags] flags_name: list = [x.get_string_entity().lower() for x in flags]
if len(set(flags_name)) < len(flags_name): if len(set(flags_name)) < len(flags_name):
raise RepeatedFlagNameException() raise RepeatedFlagNameException()
case 'str':
if command.find(' ') != -1:
raise TriggerContainSpacesException()
@staticmethod @staticmethod
@@ -158,13 +167,19 @@ class Router:
elif len(transferred_args) == 0: elif len(transferred_args) == 0:
raise RequiredArgumentNotPassedException() raise RequiredArgumentNotPassedException()
transferred_arg: str = transferred_args[0]
func_annotations: dict[str, Type] = get_annotations(func) func_annotations: dict[str, Type] = get_annotations(func)
if func_annotations:
arg_annotation: Type = func_annotations[transferred_args[0]] if arg_annotation := func_annotations.get(transferred_arg):
if not arg_annotation is Response: if arg_annotation is Response:
Console().print(f'\n\nFile "{getsourcefile(func)}", line {getsourcelines(func)[1]+1}\n' pass
f'[b red]WARNING:[/b red] [i]The typehint of argument([green]{transferred_args[0]}[/green]) passed to the handler is [/i][blue]{Response}[/blue],' else:
f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False) file_path: str = getsourcefile(func)
source_line: int = getsourcelines(func)[1]+1
fprint = Console().print
fprint(f'\nFile "{file_path}", line {source_line}\n[b red]WARNING:[/b red] [i]The typehint '
f'of argument([green]{transferred_arg}[/green]) passed to the handler is [/i][bold blue]{Response}[/bold blue],'
f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]\n', highlight=False)