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
inv = InvalidValueInputFlags(InputFlag('test'))
inve = InvalidValueInputFlags(InputFlag('test'))
router = Router()
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())
@work_router.command(Command('run', 'Run All'))
@work_router.command('run')
def command_start_solving(response: Response):
print(response.status)
print(response.undefined_flags.get_flags())
+1 -1
View File
@@ -9,7 +9,7 @@ license = { text = "MIT" }
dependencies = [
"rich (>=14.0.0,<15.0.0)",
"art (>=6.4,<7.0)",
"pyreadline3 (>=3.5.4,<4.0.0)",
"pyreadline3>=3.5.4",
]
[dependency-groups]
+1 -1
View File
@@ -39,7 +39,7 @@ class Command(BaseCommand):
"""
super().__init__(trigger)
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 []
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.command.flags import (ValidInputFlags,
UndefinedInputFlags,
InvalidValueInputFlags)
class Response:
+25 -10
View File
@@ -6,7 +6,10 @@ from argenta.command import Command
from argenta.command.models import InputCommand
from argenta.response import Response, Status
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,
TooManyTransferredArgsException,
RequiredArgumentNotPassedException,
@@ -26,13 +29,15 @@ class Router:
self._ignore_command_register: bool = False
def command(self, command: Command) -> Callable:
def command(self, command: Command | str) -> Callable:
"""
Public. Registers handler
:param command: Registered command
:return: decorated handler as Callable
"""
self._validate_command(command)
if isinstance(command, str):
command = Command(command)
def command_decorator(func):
Router._validate_func_args(func)
@@ -128,21 +133,25 @@ class Router:
@staticmethod
def _validate_command(command: Command) -> None:
def _validate_command(command: Command | str) -> None:
"""
Private. Validates the command registered in handler
:param command: validated command
:return: None if command is valid else raise exception
"""
match type(command).__name__:
case 'Command':
command_name: str = command.get_trigger()
if command_name.find(' ') != -1:
raise TriggerContainSpacesException()
flags: Flags = command.get_registered_flags()
if flags:
flags_name: list = [x.get_string_entity().lower() for x in flags]
if len(set(flags_name)) < len(flags_name):
raise RepeatedFlagNameException()
case 'str':
if command.find(' ') != -1:
raise TriggerContainSpacesException()
@staticmethod
@@ -158,13 +167,19 @@ class Router:
elif len(transferred_args) == 0:
raise RequiredArgumentNotPassedException()
transferred_arg: str = transferred_args[0]
func_annotations: dict[str, Type] = get_annotations(func)
if func_annotations:
arg_annotation: Type = func_annotations[transferred_args[0]]
if not arg_annotation is Response:
Console().print(f'\n\nFile "{getsourcefile(func)}", line {getsourcelines(func)[1]+1}\n'
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],'
f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False)
if arg_annotation := func_annotations.get(transferred_arg):
if arg_annotation is Response:
pass
else:
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)