From 3f7c577c295d904d320a1d278325153ddf7c4355 Mon Sep 17 00:00:00 2001 From: kolo Date: Fri, 28 Feb 2025 16:19:22 +0300 Subject: [PATCH] 0.3.1 --- argenta/app/__init__.py | 8 +-- argenta/app/entity.py | 64 +++++++++------------- argenta/app/exceptions.py | 26 --------- argenta/command/input_comand/exceptions.py | 7 ++- argenta/router/__init__.py | 3 +- argenta/router/entity.py | 33 +---------- argenta/router/exceptions.py | 5 -- pyproject.toml | 2 +- tests/mock_app/handlers/routers.py | 5 -- tests/mock_app/main.py | 5 +- 10 files changed, 40 insertions(+), 118 deletions(-) diff --git a/argenta/app/__init__.py b/argenta/app/__init__.py index 9f315a8..f3d0744 100644 --- a/argenta/app/__init__.py +++ b/argenta/app/__init__.py @@ -1,7 +1,3 @@ from .entity import App -from .exceptions import (HandlerForUnknownCommandsOnNonMainRouterException, - InvalidDescriptionMessagePatternException, - InvalidRouterInstanceException, - OnlyOneMainRouterIsAllowedException, - MissingMainRouterException, - MissingHandlerForUnknownCommandsException) +from .exceptions import (InvalidDescriptionMessagePatternException, + InvalidRouterInstanceException) diff --git a/argenta/app/entity.py b/argenta/app/entity.py index b4ec13b..e44781c 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -6,13 +6,10 @@ from ..router.entity import Router from ..command.input_comand.entity import InputCommand from ..command.input_comand.exceptions import (IncorrectInputFlagException, InvalidInputFlagsHandlerHasBeenAlreadyCreatedException, - IncorrectNumberArgsInvalidInputFlagsHandlerException) + IncorrectNumberArgsHandlerException, + UnknownCommandHandlerHasBeenAlreadyCreatedException) from .exceptions import (InvalidRouterInstanceException, InvalidDescriptionMessagePatternException, - OnlyOneMainRouterIsAllowedException, - MissingMainRouterException, - MissingHandlerForUnknownCommandsException, - HandlerForUnknownCommandsOnNonMainRouterException, NoRegisteredRoutersException, NoRegisteredHandlersException, RepeatedCommandInDifferentRoutersException) @@ -49,6 +46,7 @@ class App: self._routers: list[Router] = [] self._invalid_input_flags_handler: Callable[[str], None] | None = None + self._unknown_command_handler: Callable[[Command], 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}' @@ -57,7 +55,6 @@ class App: def start_polling(self) -> None: self._validate_number_of_routers() self._validate_included_routers() - self._validate_main_router() self._validate_all_router_commands() self.print_func(self.initial_message) @@ -89,7 +86,8 @@ class App: self._checking_command_for_exit_command(input_command.get_string_entity()) self.print_func(self.line_separate) - is_unknown_command: bool = self._check_is_command_unknown(input_command.get_string_entity()) + is_unknown_command: bool = self._check_is_command_unknown(input_command) + if is_unknown_command: if not self.repeat_command_groups: self.print_func(self.prompt) @@ -127,13 +125,20 @@ class App: else: args = getfullargspec(handler).args if len(args) != 1: - raise IncorrectNumberArgsInvalidInputFlagsHandlerException() + raise IncorrectNumberArgsHandlerException() else: self._invalid_input_flags_handler = handler - def get_main_router(self) -> Router: - return self._app_main_router + def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None: + if self._unknown_command_handler: + raise UnknownCommandHandlerHasBeenAlreadyCreatedException() + else: + args = getfullargspec(handler).args + if len(args) != 1: + raise IncorrectNumberArgsHandlerException() + else: + self._unknown_command_handler = handler def get_all_app_commands(self) -> list[str]: @@ -144,17 +149,10 @@ class App: return all_commands - def include_router(self, router: Router, is_main: True | False = False) -> None: + def include_router(self, router: Router) -> None: if not isinstance(router, Router): raise InvalidRouterInstanceException() - if is_main: - if not self._app_main_router: - self._app_main_router = router - router.set_router_as_main() - else: - raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name()) - router.set_ignore_command_register(self.ignore_command_register) self._routers.append(router) @@ -176,23 +174,6 @@ class App: raise NoRegisteredHandlersException(router.get_name()) - def _validate_main_router(self): - if not self._app_main_router: - if len(self._routers) > 1: - raise MissingMainRouterException() - else: - router = self._routers[0] - router.set_router_as_main() - self._app_main_router = router - - if not self._app_main_router.get_unknown_command_func(): - raise MissingHandlerForUnknownCommandsException() - - for router in self._routers: - if router.get_unknown_command_func() and not (self._app_main_router is router): - raise HandlerForUnknownCommandsOnNonMainRouterException() - - def _validate_all_router_commands(self) -> None: for idx in range(len(self._registered_router_entities)): current_router: Router = self._registered_router_entities[idx]['entity'] @@ -220,17 +201,22 @@ class App: exit(0) - def _check_is_command_unknown(self, command: str): + def _check_is_command_unknown(self, command: Command): registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = self._registered_router_entities for router_entity in registered_router_entities: for command_entity in router_entity['commands']: - if command_entity['command'].get_string_entity().lower() == command.lower(): + if command_entity['command'].get_string_entity().lower() == command.get_string_entity().lower(): if self.ignore_command_register: return False else: - if command_entity['command'].get_string_entity() == command: + if command_entity['command'].get_string_entity() == command.get_string_entity(): return False - self._app_main_router.unknown_command_handler(command) + + if self._unknown_command_handler: + self._unknown_command_handler(command) + else: + print(f"Unknown command: {command.get_string_entity()}") + self.print_func(self.line_separate) self.print_func(self.command_group_description_separate) return True diff --git a/argenta/app/exceptions.py b/argenta/app/exceptions.py index 7e626bc..5b9dba1 100644 --- a/argenta/app/exceptions.py +++ b/argenta/app/exceptions.py @@ -13,32 +13,6 @@ class InvalidDescriptionMessagePatternException(Exception): f"Your pattern: {self.pattern}") -class OnlyOneMainRouterIsAllowedException(Exception): - def __init__(self, existing_main_router): - self.existing_main_router = existing_main_router - - def __str__(self): - return ("Only One Main Router Allowed\n" - f"Existing main router is: {self.existing_main_router}") - - -class MissingMainRouterException(Exception): - def __str__(self): - return ("Missing Main Router\n" - "One of the registered routers must be the main one") - - -class MissingHandlerForUnknownCommandsException(Exception): - def __str__(self): - return ("Missing Handlers For Unknown Commands On The Main Router\n" - "The main router must have a declared handler for unknown commands") - - -class HandlerForUnknownCommandsOnNonMainRouterException(Exception): - def __str__(self): - return '\nThe handler for unknown commands can only be declared for the main router' - - class NoRegisteredRoutersException(Exception): def __str__(self): return "No Registered Router Found" diff --git a/argenta/command/input_comand/exceptions.py b/argenta/command/input_comand/exceptions.py index 700267c..3cbdb2b 100644 --- a/argenta/command/input_comand/exceptions.py +++ b/argenta/command/input_comand/exceptions.py @@ -26,7 +26,12 @@ class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception): return "Invalid Input Flags Handler has already been created" -class IncorrectNumberArgsInvalidInputFlagsHandlerException(Exception): +class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception): + def __str__(self): + return "Unknown Command Handler has already been created" + + +class IncorrectNumberArgsHandlerException(Exception): def __str__(self): return "Incorrect Input Flags Handler has incorrect number of arguments" diff --git a/argenta/router/__init__.py b/argenta/router/__init__.py index 3387795..29a41b5 100644 --- a/argenta/router/__init__.py +++ b/argenta/router/__init__.py @@ -1,3 +1,2 @@ from .entity import Router -from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException, - InvalidDescriptionInstanceException) \ No newline at end of file +from .exceptions import InvalidDescriptionInstanceException \ No newline at end of file diff --git a/argenta/router/entity.py b/argenta/router/entity.py index 161ab9e..0e6522d 100644 --- a/argenta/router/entity.py +++ b/argenta/router/entity.py @@ -4,8 +4,7 @@ from ..command.entity import Command from ..command.input_comand.entity import InputCommand from ..command.input_comand.exceptions import InvalidInputFlagException from ..command.params.flag.flags_group.entity import FlagsGroup -from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException, - RepeatedCommandException, RepeatedFlagNameException, +from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException, CurrentCommandDoesNotProcessFlagsException, TooManyTransferredArgsException, RequiredArgumentNotPassedException) @@ -20,7 +19,6 @@ class Router: self.name = name self._command_entities: list[dict[str, Callable[[], None] | Command]] = [] - self._is_main_router: bool = False self._ignore_command_register: bool = False self._unknown_command_handler: Callable[[str], None] | None = None @@ -42,17 +40,6 @@ class Router: return command_decorator - def unknown_command(self, func): - if self._unknown_command_handler is not None: - raise UnknownCommandHandlerHasAlreadyBeenCreatedException() - - self._unknown_command_handler: Callable = func - - def wrapper(*args, **kwargs): - return func(*args, **kwargs) - return wrapper - - def input_command_handler(self, input_command: InputCommand): input_command_name: str = input_command.get_string_entity() for command_entity in self._command_entities: @@ -73,14 +60,6 @@ class Router: return command_entity['handler_func']() - def get_unknown_command_func(self): - return self._unknown_command_handler - - - def unknown_command_handler(self, unknown_command): - self._unknown_command_handler(unknown_command) - - def _validate_command(self, command: Command): command_name: str = command.get_string_entity() if command_name in self.get_all_commands(): @@ -109,13 +88,6 @@ class Router: raise TooManyTransferredArgsException() - - def set_router_as_main(self): - if self.name == 'subordinate': - self.name = 'main' - self._is_main_router = True - - def set_ignore_command_register(self, ignore_command_register: bool): self._ignore_command_register = ignore_command_register @@ -139,8 +111,7 @@ class Router: 'ignore_command_register': self._ignore_command_register, 'attributes': { 'command_entities': self._command_entities, - 'unknown_command_func': self._unknown_command_handler, - 'is_main_router': self._is_main_router + 'unknown_command_func': self._unknown_command_handler } } diff --git a/argenta/router/exceptions.py b/argenta/router/exceptions.py index 62f5157..18493b9 100644 --- a/argenta/router/exceptions.py +++ b/argenta/router/exceptions.py @@ -3,11 +3,6 @@ class InvalidDescriptionInstanceException(Exception): return "Invalid Description Instance" -class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception): - def __str__(self): - return "Only one unknown command handler can be declared" - - class RepeatedCommandException(Exception): def __str__(self): return "Commands in handler cannot be repeated" diff --git a/pyproject.toml b/pyproject.toml index 68ad7ec..90ac497 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "argenta" -version = "0.3.0" +version = "0.3.1" description = "python library for creating custom shells" authors = [ {name = "kolo",email = "kolo.is.main@gmail.com"} diff --git a/tests/mock_app/handlers/routers.py b/tests/mock_app/handlers/routers.py index e9e28cd..77c94e0 100644 --- a/tests/mock_app/handlers/routers.py +++ b/tests/mock_app/handlers/routers.py @@ -42,8 +42,3 @@ def command_start_solving(argrrtrts: FlagsGroup | None): def command_update(): print('uefi') # upgrade_command() - - -@work_router.unknown_command -def command_unknown_command(command): - console.print(f'[bold red]Unknown command: [/bold red]{command}') diff --git a/tests/mock_app/main.py b/tests/mock_app/main.py index e673162..70c1e68 100644 --- a/tests/mock_app/main.py +++ b/tests/mock_app/main.py @@ -18,12 +18,13 @@ def main(): ascii_goodbye_message: str = text2art('GoodBye', font='small') goodbye_message: str = f'[bold red]\n{ascii_goodbye_message}{' '*12}made by kolo\n' - app.include_router(work_router, is_main=True) + app.include_router(work_router) app.include_router(settings_router) 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_invalid_input_flags_handler(lambda raw_command: print(f"Invalid input flags: {raw_command}")) app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')