This commit is contained in:
2025-02-08 00:57:21 +03:00
parent 3f88c5278e
commit c78609caca
4 changed files with 67 additions and 44 deletions
+35 -35
View File
@@ -13,56 +13,56 @@ class App:
prompt: str = 'Enter a command',
exit_command: str = 'q',
ignore_exit_command_register: bool = True,
initial_greeting: str = 'Hello',
goodbye_message: str = 'GoodBye',
line_separate: str = '\n',
command_group_description_separate: str = '\n',
initial_greeting: str = '\nHello, I am Argenta\n',
farewell_message: str = 'GoodBye',
line_separate: str = '',
command_group_description_separate: str = '',
print_func: Callable[[str], None] = print) -> None:
self.prompt = prompt
self.print_func = print_func
self.exit_command = exit_command
self.ignore_exit_command_register = ignore_exit_command_register
self.goodbye_message = goodbye_message
self.farewell_message = farewell_message
self.initial_greeting = initial_greeting
self.line_separate = line_separate
self.command_group_description_separate = command_group_description_separate
self.routers: list[Router] = []
self.registered_commands: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
self.main_app_router: Router | None = None
self._description_message_pattern = '[{command}] *=*=* {description}'
self._routers: list[Router] = []
self._registered_commands: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
self._main_app_router: Router | None = None
self._description_message_pattern: str = '[{command}] *=*=* {description}'
def start_polling(self) -> None:
self.print_func(self.initial_greeting)
self.validate_main_router()
self._validate_main_router()
while True:
self.print_command_group_description()
self._print_command_group_description()
self.print_func(self.prompt)
command: str = input()
self.checking_command_for_exit_command(command)
self._checking_command_for_exit_command(command)
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:
continue
for router in self.routers:
for router in self._routers:
router.input_command_handler(command)
self.print_func(self.line_separate)
self.print_func(self.command_group_description_separate)
def set_initial_greeting(self, greeting: str) -> None:
self.initial_greeting = greeting
self.initial_greeting: str = greeting
def set_goodbye_message(self, message: str) -> None:
self.goodbye_message = message
def set_farewell_message(self, message: str) -> None:
self.farewell_message: str = message
def set_description_message_pattern(self, pattern: str) -> None:
@@ -71,34 +71,34 @@ class App:
description='description')
except KeyError:
raise InvalidDescriptionMessagePatternException(pattern)
self._description_message_pattern = pattern
self._description_message_pattern: str = pattern
def validate_main_router(self):
if not self.main_app_router:
def _validate_main_router(self):
if not self._main_app_router:
raise MissingMainRouterException()
if not self.main_app_router.unknown_command_func:
if not self._main_app_router.unknown_command_func:
raise MissingHandlersForUnknownCommandsOnMainRouterException()
for router in self.routers:
if router.unknown_command_func and self.main_app_router is not router:
for router in self._routers:
if router.unknown_command_func and self._main_app_router is not router:
raise HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException()
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 self.ignore_exit_command_register:
self.print_func(self.goodbye_message)
self.print_func(self.farewell_message)
exit(0)
else:
if command == self.exit_command:
self.print_func(self.goodbye_message)
self.print_func(self.farewell_message)
exit(0)
def check_is_command_unknown(self, command: str):
registered_commands = self.registered_commands
def _check_is_command_unknown(self, command: str):
registered_commands: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = self._registered_commands
for router in registered_commands:
for command_entity in router['commands']:
if command_entity['command'].lower() == command.lower():
@@ -107,14 +107,14 @@ class App:
else:
if command_entity['command'] == command:
return False
self.main_app_router.unknown_command_handler(command)
self._main_app_router.unknown_command_handler(command)
self.print_func(self.line_separate)
self.print_func(self.command_group_description_separate)
return True
def print_command_group_description(self):
for router in self.registered_commands:
def _print_command_group_description(self):
for router in self._registered_commands:
self.print_func(router['name'])
for command_entity in router['commands']:
self.print_func(self._description_message_pattern.format(
@@ -130,16 +130,16 @@ class App:
raise InvalidRouterInstanceException()
if is_main:
if not self.main_app_router:
self.main_app_router = router
if not self._main_app_router:
self._main_app_router = router
router.set_router_as_main()
else:
raise OnlyOneMainRouterIsAllowedException(router)
self.routers.append(router)
self._routers.append(router)
registered_commands: list[dict[str, Callable[[], None] | str]] = router.get_registered_commands()
self.registered_commands.append({'name': router.get_name(),
self._registered_commands.append({'name': router.get_name(),
'router': router,
'commands': registered_commands})