This commit is contained in:
2025-02-09 13:41:11 +03:00
parent 8a3f742636
commit dfc3c472ce
4 changed files with 23 additions and 19 deletions
+11 -11
View File
@@ -17,9 +17,10 @@ class App:
exit_command: str = 'q', exit_command: str = 'q',
ignore_exit_command_register: bool = True, ignore_exit_command_register: bool = True,
initial_greeting: str = '\nHello, I am Argenta\n', initial_greeting: str = '\nHello, I am Argenta\n',
farewell_message: str = 'GoodBye', farewell_message: str = '\nGoodBye\n',
line_separate: str = '', line_separate: str = '',
command_group_description_separate: str = '', command_group_description_separate: str = '',
ignore_command_register: bool = False,
print_func: Callable[[str], None] = print) -> None: print_func: Callable[[str], None] = print) -> None:
self.prompt = prompt self.prompt = prompt
self.print_func = print_func self.print_func = print_func
@@ -29,6 +30,7 @@ class App:
self.initial_greeting = initial_greeting self.initial_greeting = initial_greeting
self.line_separate = line_separate self.line_separate = line_separate
self.command_group_description_separate = command_group_description_separate self.command_group_description_separate = command_group_description_separate
self.ignore_command_register = ignore_command_register
self._routers: list[Router] = [] self._routers: list[Router] = []
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = [] self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
@@ -54,12 +56,12 @@ class App:
self.print_func(self.line_separate) self.print_func(self.line_separate)
is_unknown_command: bool = self._check_is_command_unknown(command) is_unknown_command: bool = self._check_is_command_unknown(command)
if is_unknown_command: if is_unknown_command:
continue continue
for router in self._routers: for router in self._routers:
router.input_command_handler(command) router.input_command_handler(command)
self.print_func(self.line_separate) self.print_func(self.line_separate)
self.print_func(self.command_group_description_separate) self.print_func(self.command_group_description_separate)
@@ -123,23 +125,20 @@ class App:
def _validate_all_router_commands(self) -> None: def _validate_all_router_commands(self) -> None:
for idx in range(len(self._registered_router_entities)): for idx in range(len(self._registered_router_entities)):
current_router: Router = self._registered_router_entities[idx]['router'] current_router: Router = self._registered_router_entities[idx]['entity']
routers_without_current_router = self._registered_router_entities.copy() routers_without_current_router = self._registered_router_entities.copy()
routers_without_current_router.pop(idx) routers_without_current_router.pop(idx)
current_router_ignore_commands_registered: bool = current_router.ignore_command_register
current_router_all_commands: list[str] = current_router.get_all_commands() current_router_all_commands: list[str] = current_router.get_all_commands()
for router_entity in routers_without_current_router: for router_entity in routers_without_current_router:
if len(set(current_router_all_commands).intersection(set(router_entity['router'].get_all_commands()))) > 0: if len(set(current_router_all_commands).intersection(set(router_entity['entity'].get_all_commands()))) > 0:
raise RepeatedCommandInDifferentRoutersException() raise RepeatedCommandInDifferentRoutersException()
if current_router_ignore_commands_registered: if self.ignore_command_register:
if len(set([x.lower() for x in current_router_all_commands]).intersection(set([x.lower() for x in router_entity['router'].get_all_commands()]))) > 0: if len(set([x.lower() for x in current_router_all_commands]).intersection(set([x.lower() for x in router_entity['entity'].get_all_commands()]))) > 0:
raise RepeatedCommandInDifferentRoutersException() raise RepeatedCommandInDifferentRoutersException()
def _checking_command_for_exit_command(self, command: str): def _checking_command_for_exit_command(self, command: str):
if command.lower() == self.exit_command.lower(): if command.lower() == self.exit_command.lower():
if self.ignore_exit_command_register: if self.ignore_exit_command_register:
@@ -156,7 +155,7 @@ class App:
for router_entity in registered_router_entities: for router_entity in registered_router_entities:
for command_entity in router_entity['commands']: for command_entity in router_entity['commands']:
if command_entity['command'].lower() == command.lower(): if command_entity['command'].lower() == command.lower():
if router_entity['router'].ignore_command_register: if self.ignore_command_register:
return False return False
else: else:
if command_entity['command'] == command: if command_entity['command'] == command:
@@ -190,11 +189,12 @@ class App:
else: else:
raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name()) raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name())
router.set_ignore_command_register(self.ignore_command_register)
self._routers.append(router) self._routers.append(router)
command_entities: list[dict[str, Callable[[], None] | str]] = router.get_command_entities() command_entities: list[dict[str, Callable[[], None] | str]] = router.get_command_entities()
self._registered_router_entities.append({'name': router.get_name(), self._registered_router_entities.append({'name': router.get_name(),
'title': router.get_title(), 'title': router.get_title(),
'router': router, 'entity': router,
'commands': command_entities}) 'commands': command_entities})
+6 -3
View File
@@ -8,16 +8,15 @@ from ..router.exceptions import (InvalidCommandInstanceException,
class Router: class Router:
def __init__(self, def __init__(self,
title: str = 'Commands group title:', title: str = 'Commands group title:',
name: str = 'subordinate', name: str = 'subordinate'):
ignore_command_register: bool = False):
self.ignore_command_register = ignore_command_register
self.title = title self.title = title
self.name = name self.name = name
self._command_entities: list[dict[str, Callable[[], None] | str]] = [] self._command_entities: list[dict[str, Callable[[], None] | str]] = []
self.unknown_command_func: Callable[[str], None] | None = None 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
def command(self, command: str, description: str = None) -> Callable[[Any], Any]: def command(self, command: str, description: str = None) -> Callable[[Any], Any]:
@@ -86,6 +85,10 @@ class Router:
self._is_main_router = True self._is_main_router = True
def set_ignore_command_register(self, ignore_command_register: bool):
self.ignore_command_register = ignore_command_register
def get_command_entities(self) -> list[dict[str, Callable[[], None] | str]]: def get_command_entities(self) -> list[dict[str, Callable[[], None] | str]]:
return self._command_entities return self._command_entities
+4 -3
View File
@@ -1,3 +1,4 @@
from pprint import pprint
from rich.console import Console from rich.console import Console
from argenta.router import Router from argenta.router import Router
@@ -8,17 +9,17 @@ settings_router: Router = Router(name='settings')
console = Console() console = Console()
@work_router.command(command='2') @work_router.command(command='a')
def command_help(): def command_help():
console.print('[bold red]command help [/bold red]') console.print('[bold red]command help [/bold red]')
@work_router.command(command='2', description='txiyxykkkkkkkkkkkk') @work_router.command(command='B', description='tester')
def command_start_solving(): def command_start_solving():
console.print('[bold red]command start [/bold red]') console.print('[bold red]command start [/bold red]')
@settings_router.command(command='2') @settings_router.command(command='b')
def command_settings(): def command_settings():
console.print('[bold red]command settings [/bold red]') console.print('[bold red]command settings [/bold red]')
+2 -2
View File
@@ -1,11 +1,11 @@
from pprint import pprint from pprint import pprint
from tests.mock_default_app.handlers.routers import work_router, settings_router from tests.mock_default_app.handlers.routers import work_router, settings_router
from argenta.app.entity import App from argenta.app.entity import App
from art import text2art from art import text2art
app: App = App() app: App = App(ignore_command_register=False,
line_separate='\n-------------------------------\n')
def main(): def main():