From 5e6cdc342ebdbc8a0c02fdbf32494512d249ff53 Mon Sep 17 00:00:00 2001 From: kolo Date: Sun, 2 Mar 2025 00:54:34 +0300 Subject: [PATCH] v0.3.4 --- argenta/app/entity.py | 100 +++++++++++--------------- argenta/command/entity.py | 7 +- argenta/command/exceptions.py | 20 ++---- argenta/command/params/flag/entity.py | 1 + pyproject.toml | 2 +- tests/mock_app/main.py | 2 +- 6 files changed, 55 insertions(+), 77 deletions(-) diff --git a/argenta/app/entity.py b/argenta/app/entity.py index 500243e..6efcf0c 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -4,11 +4,9 @@ from inspect import getfullargspec from ..command.entity import Command from ..router.entity import Router from ..command.exceptions import (UnprocessedInputFlagException, - InvalidInputFlagsHandlerHasBeenAlreadyCreatedException, IncorrectNumberOfHandlerArgsException, - UnknownCommandHandlerHasBeenAlreadyCreatedException, RepeatedInputFlagsException, - RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException) + EmptyInputCommandException) from .exceptions import (InvalidRouterInstanceException, InvalidDescriptionMessagePatternException, NoRegisteredRoutersException, @@ -46,12 +44,12 @@ class App: self.repeat_command_groups = repeat_command_groups self._routers: list[Router] = [] - self._invalid_input_flags_handler: Callable[[str], None] | None = None - self._repeated_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}' + self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = [] + self._invalid_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Incorrect flag syntax: "{raw_command}"') + self._repeated_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Repeated input flags: "{raw_command}"') + self._empty_input_command_handler: Callable[[], None] = lambda: print_func(f'Empty input command') + self._unknown_command_handler: Callable[[Command], None] = lambda command: print_func(f"Unknown command: {command.get_string_entity()}") def start_polling(self) -> None: @@ -76,31 +74,39 @@ class App: input_command: Command = Command.parse_input_command(raw_command=raw_command) except UnprocessedInputFlagException: 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._invalid_input_flags_handler(raw_command) self.print_func(self.line_separate) + if not self.repeat_command_groups: self.print_func(self.prompt) continue + except RepeatedInputFlagsException: self.print_func(self.line_separate) - if self._repeated_input_flags_handler: - self._repeated_input_flags_handler(raw_command) - else: - self.print_func(f'Repeated input flags: "{raw_command}"') + self._repeated_input_flags_handler(raw_command) self.print_func(self.line_separate) + if not self.repeat_command_groups: self.print_func(self.prompt) continue - self._checking_command_for_exit_command(input_command.get_string_entity()) - self.print_func(self.line_separate) + except EmptyInputCommandException: + self.print_func(self.line_separate) + self._empty_input_command_handler() + self.print_func(self.line_separate) + if not self.repeat_command_groups: + self.print_func(self.prompt) + continue + + self._check_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) if is_unknown_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 @@ -124,52 +130,35 @@ class App: def set_description_message_pattern(self, pattern: str) -> None: try: - pattern.format(command='command', - description='description') + pattern.format(command='command', description='description') except KeyError: raise InvalidDescriptionMessagePatternException(pattern) - self._description_message_pattern: str = pattern + else: + 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() + args = getfullargspec(handler).args + if len(args) != 1: + raise IncorrectNumberOfHandlerArgsException() else: - args = getfullargspec(handler).args - if len(args) != 1: - raise IncorrectNumberOfHandlerArgsException() - else: - self._invalid_input_flags_handler = handler + self._invalid_input_flags_handler = handler def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None: - if self._repeated_input_flags_handler: - raise RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException() + args = getfullargspec(handler).args + if len(args) != 1: + raise IncorrectNumberOfHandlerArgsException() else: - args = getfullargspec(handler).args - if len(args) != 1: - raise IncorrectNumberOfHandlerArgsException() - else: - self._repeated_input_flags_handler = handler + self._repeated_input_flags_handler = handler def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None: - if self._unknown_command_handler: - raise UnknownCommandHandlerHasBeenAlreadyCreatedException() + args = getfullargspec(handler).args + if len(args) != 1: + raise IncorrectNumberOfHandlerArgsException() else: - args = getfullargspec(handler).args - if len(args) != 1: - raise IncorrectNumberOfHandlerArgsException() - else: - self._unknown_command_handler = handler - - - def get_all_app_commands(self) -> list[str]: - all_commands: list[str] = [] - for router in self._routers: - all_commands.extend(router.get_all_commands()) - - return all_commands + self._unknown_command_handler = handler def include_router(self, router: Router) -> None: @@ -213,7 +202,7 @@ class App: raise RepeatedCommandInDifferentRoutersException() - def _checking_command_for_exit_command(self, command: str): + def _check_command_for_exit_command(self, command: str): if command.lower() == self.exit_command.lower(): if self.ignore_exit_command_register: self.print_func(self.farewell_message) @@ -234,14 +223,7 @@ class App: else: if command_entity['command'].get_string_entity() == command.get_string_entity(): return False - - 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) + self._unknown_command_handler(command) return True diff --git a/argenta/command/entity.py b/argenta/command/entity.py index b9c5fe2..c0abbc6 100644 --- a/argenta/command/entity.py +++ b/argenta/command/entity.py @@ -2,7 +2,10 @@ from .params.flag.entity import Flag from .params.flag.flags_group.entity import FlagsGroup from .exceptions import (InvalidCommandInstanceException, InvalidDescriptionInstanceException, - InvalidFlagsInstanceException, UnprocessedInputFlagException, RepeatedInputFlagsException) + InvalidFlagsInstanceException, + UnprocessedInputFlagException, + RepeatedInputFlagsException, + EmptyInputCommandException) from typing import Generic, TypeVar @@ -71,6 +74,8 @@ class Command(Generic[T]): @staticmethod def parse_input_command(raw_command: str) -> 'Command[T]': + if not raw_command: + raise EmptyInputCommandException() list_of_tokens = raw_command.split() command = list_of_tokens[0] list_of_tokens.pop(0) diff --git a/argenta/command/exceptions.py b/argenta/command/exceptions.py index c617ecd..2586982 100644 --- a/argenta/command/exceptions.py +++ b/argenta/command/exceptions.py @@ -29,21 +29,11 @@ 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 RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException(Exception): - def __str__(self): - return "Repeated Input Flags Handler has already been created" - - -class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception): - def __str__(self): - return "Unknown Command Handler has already been created" - - class IncorrectNumberOfHandlerArgsException(Exception): def __str__(self): return "Incorrect Input Flags Handler has incorrect number of arguments" + + +class EmptyInputCommandException(Exception): + def __str__(self): + return "Input Command is empty" \ No newline at end of file diff --git a/argenta/command/params/flag/entity.py b/argenta/command/params/flag/entity.py index 21e7499..c3cba34 100644 --- a/argenta/command/params/flag/entity.py +++ b/argenta/command/params/flag/entity.py @@ -36,6 +36,7 @@ class Flag: return True else: return False + if isinstance(self.possible_flag_values, list): if self.ignore_flag_value_register: if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]: diff --git a/pyproject.toml b/pyproject.toml index 98241f0..5839ac3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "argenta" -version = "0.3.3" +version = "0.3.4" description = "python library for creating custom shells" authors = [ {name = "kolo",email = "kolo.is.main@gmail.com"} diff --git a/tests/mock_app/main.py b/tests/mock_app/main.py index 5f42cb4..db9f5c6 100644 --- a/tests/mock_app/main.py +++ b/tests/mock_app/main.py @@ -8,7 +8,7 @@ app: App = App(prompt='[italic white bold]What do you want to do(enter number of line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n', print_func=Console().print, command_group_description_separate='', - repeat_command_groups=False) + repeat_command_groups=True) def main():