From b72fcc6a114f4826c7472d9495b2656b8a3b2c4f Mon Sep 17 00:00:00 2001 From: kolo Date: Thu, 27 Feb 2025 01:16:39 +0300 Subject: [PATCH] fix --- argenta/app/entity.py | 32 ++++++++++++++++++---- argenta/command/input_comand/exceptions.py | 10 +++++++ argenta/router/entity.py | 18 ++++++------ tests/mock_app/handlers/routers.py | 4 +-- tests/mock_app/main.py | 1 + 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/argenta/app/entity.py b/argenta/app/entity.py index 2499d66..b4ec13b 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -1,8 +1,12 @@ from typing import Callable +from inspect import getfullargspec + from ..command.entity import Command -from argenta.command.input_comand.entity import InputCommand -from argenta.command.input_comand.exceptions import InvalidInputFlagException from ..router.entity import Router +from ..command.input_comand.entity import InputCommand +from ..command.input_comand.exceptions import (IncorrectInputFlagException, + InvalidInputFlagsHandlerHasBeenAlreadyCreatedException, + IncorrectNumberArgsInvalidInputFlagsHandlerException) from .exceptions import (InvalidRouterInstanceException, InvalidDescriptionMessagePatternException, OnlyOneMainRouterIsAllowedException, @@ -44,6 +48,7 @@ class App: self.repeat_command_groups = repeat_command_groups self._routers: list[Router] = [] + self._invalid_input_flags_handler: Callable[[str], None] | None = None self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = [] self._app_main_router: Router | None = None self._description_message_pattern: str = '[{command}] *=*=* {description}' @@ -67,11 +72,16 @@ class App: self.print_func(self.prompt) raw_command: str = input() + try: input_command: InputCommand = InputCommand.parse(raw_command=raw_command) - except InvalidInputFlagException: + except IncorrectInputFlagException: + self.print_func(self.line_separate) + if self._invalid_input_flags_handler: + self._invalid_input_flags_handler(raw_command) + else: + self.print_func(f'Incorrect flag syntax: "{raw_command}"') self.print_func(self.line_separate) - self.print_func(self.command_group_description_separate) if not self.repeat_command_groups: self.print_func(self.prompt) continue @@ -86,7 +96,6 @@ class App: continue for router in self._routers: - router.input_command_handler(input_command) self.print_func(self.line_separate) @@ -112,6 +121,17 @@ class App: self._description_message_pattern: str = pattern + def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None: + if self._invalid_input_flags_handler: + raise InvalidInputFlagsHandlerHasBeenAlreadyCreatedException() + else: + args = getfullargspec(handler).args + if len(args) != 1: + raise IncorrectNumberArgsInvalidInputFlagsHandlerException() + else: + self._invalid_input_flags_handler = handler + + def get_main_router(self) -> Router: return self._app_main_router @@ -169,7 +189,7 @@ class App: raise MissingHandlerForUnknownCommandsException() for router in self._routers: - if router.get_unknown_command_func() and self._app_main_router is not router: + if router.get_unknown_command_func() and not (self._app_main_router is router): raise HandlerForUnknownCommandsOnNonMainRouterException() diff --git a/argenta/command/input_comand/exceptions.py b/argenta/command/input_comand/exceptions.py index d8a5125..700267c 100644 --- a/argenta/command/input_comand/exceptions.py +++ b/argenta/command/input_comand/exceptions.py @@ -21,4 +21,14 @@ class RepeatedInputFlagsException(Exception): f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'") +class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception): + def __str__(self): + return "Invalid Input Flags Handler has already been created" + + +class IncorrectNumberArgsInvalidInputFlagsHandlerException(Exception): + def __str__(self): + return "Incorrect Input Flags Handler has incorrect number of arguments" + + diff --git a/argenta/router/entity.py b/argenta/router/entity.py index 802de1a..161ab9e 100644 --- a/argenta/router/entity.py +++ b/argenta/router/entity.py @@ -20,10 +20,12 @@ class Router: self.name = name self._command_entities: list[dict[str, Callable[[], None] | Command]] = [] - self._unknown_command_func: Callable[[str], None] | None = None self._is_main_router: bool = False self._ignore_command_register: bool = False + self._unknown_command_handler: Callable[[str], None] | None = None + self._not_valid_flag_handler: Callable[[str], None] | None = None + def command(self, command: Command) -> Callable[[Any], Any]: command.validate_commands_params() @@ -41,10 +43,10 @@ class Router: def unknown_command(self, func): - if self._unknown_command_func is not None: + if self._unknown_command_handler is not None: raise UnknownCommandHandlerHasAlreadyBeenCreatedException() - self._unknown_command_func: Callable = func + self._unknown_command_handler: Callable = func def wrapper(*args, **kwargs): return func(*args, **kwargs) @@ -61,9 +63,9 @@ class Router: is_valid = command_entity['command'].validate_input_flag(flag) if not is_valid: raise InvalidInputFlagException(flag) - return command_entity['handler_func'](args=input_command.get_input_flags()) + return command_entity['handler_func'](input_command.get_input_flags()) else: - return command_entity['handler_func'](args=FlagsGroup(None)) + return command_entity['handler_func'](FlagsGroup(None)) else: if input_command.get_input_flags(): raise CurrentCommandDoesNotProcessFlagsException() @@ -72,11 +74,11 @@ class Router: def get_unknown_command_func(self): - return self._unknown_command_func + return self._unknown_command_handler def unknown_command_handler(self, unknown_command): - self._unknown_command_func(unknown_command) + self._unknown_command_handler(unknown_command) def _validate_command(self, command: Command): @@ -137,7 +139,7 @@ class Router: 'ignore_command_register': self._ignore_command_register, 'attributes': { 'command_entities': self._command_entities, - 'unknown_command_func': self._unknown_command_func, + 'unknown_command_func': self._unknown_command_handler, 'is_main_router': self._is_main_router } diff --git a/tests/mock_app/handlers/routers.py b/tests/mock_app/handlers/routers.py index cfd4100..e9e28cd 100644 --- a/tests/mock_app/handlers/routers.py +++ b/tests/mock_app/handlers/routers.py @@ -30,9 +30,9 @@ def command_help(): @work_router.command(Command(command='P', description='Start Solving', flags=flagi)) -def command_start_solving(args: FlagsGroup | None): +def command_start_solving(argrrtrts: FlagsGroup | None): print('Solving...') - flags = args.get_flags() + flags = argrrtrts.get_flags() for flag in flags: print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"') #start_solving_command() diff --git a/tests/mock_app/main.py b/tests/mock_app/main.py index da14dac..e673162 100644 --- a/tests/mock_app/main.py +++ b/tests/mock_app/main.py @@ -23,6 +23,7 @@ def main(): app.set_initial_message(initial_greeting) app.set_farewell_message(goodbye_message) + app.set_invalid_input_flags_handler(lambda raw_command: print("Custom iif handler")) app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')