This commit is contained in:
2025-02-27 01:16:39 +03:00
parent 158e5eb75a
commit b72fcc6a11
5 changed files with 49 additions and 16 deletions
+26 -6
View File
@@ -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()
@@ -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"
+10 -8
View File
@@ -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
}