From 4256d67789960625a0ab54aeaf691baa263aa9c1 Mon Sep 17 00:00:00 2001 From: kolo Date: Thu, 27 Mar 2025 00:17:40 +0300 Subject: [PATCH] v0.4.1 --- argenta/app/entity.py | 48 +++++++++++++++++++------------ argenta/router/defaults.py | 5 ++++ argenta/router/entity.py | 4 +++ mock/mock_app/handlers/routers.py | 5 ++-- mock/mock_app/main.py | 1 + pyproject.toml | 2 +- 6 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 argenta/router/defaults.py diff --git a/argenta/app/entity.py b/argenta/app/entity.py index 301615c..e3bbd01 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -2,9 +2,10 @@ from typing import Callable from inspect import getfullargspec import re -from ..command.entity import Command -from ..router.entity import Router -from ..command.exceptions import (UnprocessedInputFlagException, +from argenta.command import Command +from argenta.router import Router +from argenta.router.defaults import system_router +from argenta.command.exceptions import (UnprocessedInputFlagException, RepeatedInputFlagsException, EmptyInputCommandException) from .exceptions import (InvalidRouterInstanceException, @@ -53,9 +54,11 @@ class App: 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_trigger()}") + self._exit_command_handler: Callable[[], None] = lambda: print_func(self.farewell_message) def start_polling(self) -> None: + self._setup_system_router() self._validate_number_of_routers() self._validate_included_routers() self._validate_all_router_commands() @@ -105,7 +108,7 @@ class App: self.print_func(self.prompt) continue - is_exit = self._is_exit_command(input_command.get_trigger()) + is_exit = self._is_exit_command(input_command) if is_exit: return @@ -178,6 +181,14 @@ class App: self._empty_input_command_handler = handler + def set_exit_command_handler(self, handler: Callable[[], None]) -> None: + args = getfullargspec(handler).args + if len(args) != 0: + raise IncorrectNumberOfHandlerArgsException() + else: + self._exit_command_handler = handler + + def add_message_on_startup(self, message: str) -> None: self.messages_on_startup.append(message) @@ -223,14 +234,24 @@ class App: raise RepeatedCommandInDifferentRoutersException() - def _is_exit_command(self, command: str): - if command.lower() == self.exit_command.lower(): + def _setup_system_router(self): + system_router.set_title(self.system_points_title) + @system_router.command(Command(self.exit_command, self.exit_command_description)) + def exit_command(): + self._exit_command_handler() + + if system_router not in [router['entity'] for router in self._registered_router_entities]: + self.include_router(system_router) + + + def _is_exit_command(self, command: Command): + if command.get_trigger().lower() == self.exit_command.lower(): if self.ignore_exit_command_register: - self.print_func(self.farewell_message) + system_router.input_command_handler(command) return True else: - if command == self.exit_command: - self.print_func(self.farewell_message) + if command.get_trigger() == self.exit_command: + system_router.input_command_handler(command) return True return False @@ -259,12 +280,3 @@ class App: ) ) self.print_func(self.command_group_description_separate) - - self.print_func(self.system_points_title) - self.print_func(self._description_message_pattern.format( - command=self.exit_command, - description=self.exit_command_description - ) - ) - self.print_func(self.command_group_description_separate) - diff --git a/argenta/router/defaults.py b/argenta/router/defaults.py new file mode 100644 index 0000000..cd9b999 --- /dev/null +++ b/argenta/router/defaults.py @@ -0,0 +1,5 @@ +from argenta.router import Router + + +system_router = Router(title='System points:', + name='System') diff --git a/argenta/router/entity.py b/argenta/router/entity.py index 8569dd6..2b2f36f 100644 --- a/argenta/router/entity.py +++ b/argenta/router/entity.py @@ -116,6 +116,10 @@ class Router: return self._title + def set_title(self, title: str): + self._title = title + + def get_all_commands(self) -> list[str]: all_commands: list[str] = [] for command_entity in self._command_entities: diff --git a/mock/mock_app/handlers/routers.py b/mock/mock_app/handlers/routers.py index a934e95..e9e23ac 100644 --- a/mock/mock_app/handlers/routers.py +++ b/mock/mock_app/handlers/routers.py @@ -1,11 +1,11 @@ -import re from pprint import pprint from rich.console import Console from argenta.command import Command -from argenta.command.flag import Flag, FlagsGroup +from argenta.command.flag import FlagsGroup from argenta.command.flag.defaults import DefaultFlags from argenta.router import Router +from argenta.router.defaults import system_router from .handlers_implementation.help_command import help_command @@ -36,3 +36,4 @@ def command_update(): + diff --git a/mock/mock_app/main.py b/mock/mock_app/main.py index fc85661..759aea5 100644 --- a/mock/mock_app/main.py +++ b/mock/mock_app/main.py @@ -4,6 +4,7 @@ from rich.console import Console from argenta.app import App + app: App = App(prompt='[italic white bold]What do you want to do(enter number of action)?', line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n', print_func=Console().print, diff --git a/pyproject.toml b/pyproject.toml index 4195168..99cc662 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "argenta" -version = "0.4.0" +version = "0.4.1" description = "python library for creating custom shells" authors = [ {name = "kolo", email = "kolo.is.main@gmail.com"}