mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
fix
This commit is contained in:
+26
-6
@@ -1,8 +1,12 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
from inspect import getfullargspec
|
||||||
|
|
||||||
from ..command.entity import Command
|
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 ..router.entity import Router
|
||||||
|
from ..command.input_comand.entity import InputCommand
|
||||||
|
from ..command.input_comand.exceptions import (IncorrectInputFlagException,
|
||||||
|
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
||||||
|
IncorrectNumberArgsInvalidInputFlagsHandlerException)
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
OnlyOneMainRouterIsAllowedException,
|
OnlyOneMainRouterIsAllowedException,
|
||||||
@@ -44,6 +48,7 @@ class App:
|
|||||||
self.repeat_command_groups = repeat_command_groups
|
self.repeat_command_groups = repeat_command_groups
|
||||||
|
|
||||||
self._routers: list[Router] = []
|
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._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
|
||||||
self._app_main_router: Router | None = None
|
self._app_main_router: Router | None = None
|
||||||
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
||||||
@@ -67,11 +72,16 @@ class App:
|
|||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
|
|
||||||
raw_command: str = input()
|
raw_command: str = input()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
|
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.line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
continue
|
continue
|
||||||
@@ -86,7 +96,6 @@ class App:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
for router in self._routers:
|
for router in self._routers:
|
||||||
|
|
||||||
router.input_command_handler(input_command)
|
router.input_command_handler(input_command)
|
||||||
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
@@ -112,6 +121,17 @@ class App:
|
|||||||
self._description_message_pattern: str = pattern
|
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:
|
def get_main_router(self) -> Router:
|
||||||
return self._app_main_router
|
return self._app_main_router
|
||||||
|
|
||||||
@@ -169,7 +189,7 @@ class App:
|
|||||||
raise MissingHandlerForUnknownCommandsException()
|
raise MissingHandlerForUnknownCommandsException()
|
||||||
|
|
||||||
for router in self._routers:
|
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()
|
raise HandlerForUnknownCommandsOnNonMainRouterException()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,4 +21,14 @@ class RepeatedInputFlagsException(Exception):
|
|||||||
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
|
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"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,12 @@ class Router:
|
|||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
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._is_main_router: bool = False
|
||||||
self._ignore_command_register: 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]:
|
def command(self, command: Command) -> Callable[[Any], Any]:
|
||||||
command.validate_commands_params()
|
command.validate_commands_params()
|
||||||
@@ -41,10 +43,10 @@ class Router:
|
|||||||
|
|
||||||
|
|
||||||
def unknown_command(self, func):
|
def unknown_command(self, func):
|
||||||
if self._unknown_command_func is not None:
|
if self._unknown_command_handler is not None:
|
||||||
raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
|
raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
|
||||||
|
|
||||||
self._unknown_command_func: Callable = func
|
self._unknown_command_handler: Callable = func
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
@@ -61,9 +63,9 @@ class Router:
|
|||||||
is_valid = command_entity['command'].validate_input_flag(flag)
|
is_valid = command_entity['command'].validate_input_flag(flag)
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
raise InvalidInputFlagException(flag)
|
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:
|
else:
|
||||||
return command_entity['handler_func'](args=FlagsGroup(None))
|
return command_entity['handler_func'](FlagsGroup(None))
|
||||||
else:
|
else:
|
||||||
if input_command.get_input_flags():
|
if input_command.get_input_flags():
|
||||||
raise CurrentCommandDoesNotProcessFlagsException()
|
raise CurrentCommandDoesNotProcessFlagsException()
|
||||||
@@ -72,11 +74,11 @@ class Router:
|
|||||||
|
|
||||||
|
|
||||||
def get_unknown_command_func(self):
|
def get_unknown_command_func(self):
|
||||||
return self._unknown_command_func
|
return self._unknown_command_handler
|
||||||
|
|
||||||
|
|
||||||
def unknown_command_handler(self, unknown_command):
|
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):
|
def _validate_command(self, command: Command):
|
||||||
@@ -137,7 +139,7 @@ class Router:
|
|||||||
'ignore_command_register': self._ignore_command_register,
|
'ignore_command_register': self._ignore_command_register,
|
||||||
'attributes': {
|
'attributes': {
|
||||||
'command_entities': self._command_entities,
|
'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
|
'is_main_router': self._is_main_router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ def command_help():
|
|||||||
|
|
||||||
|
|
||||||
@work_router.command(Command(command='P', description='Start Solving', flags=flagi))
|
@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...')
|
print('Solving...')
|
||||||
flags = args.get_flags()
|
flags = argrrtrts.get_flags()
|
||||||
for flag in flags:
|
for flag in flags:
|
||||||
print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"')
|
print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"')
|
||||||
#start_solving_command()
|
#start_solving_command()
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ def main():
|
|||||||
|
|
||||||
app.set_initial_message(initial_greeting)
|
app.set_initial_message(initial_greeting)
|
||||||
app.set_farewell_message(goodbye_message)
|
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}')
|
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user