mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work
This commit is contained in:
+47
-50
@@ -22,7 +22,6 @@ class App:
|
|||||||
prompt: str = 'Enter a command',
|
prompt: str = 'Enter a command',
|
||||||
initial_message: str = '\nHello, I am Argenta\n',
|
initial_message: str = '\nHello, I am Argenta\n',
|
||||||
farewell_message: str = '\nGoodBye\n',
|
farewell_message: str = '\nGoodBye\n',
|
||||||
invalid_input_flags_message: str = 'Invalid input flags',
|
|
||||||
exit_command: str = 'Q',
|
exit_command: str = 'Q',
|
||||||
exit_command_description: str = 'Exit command',
|
exit_command_description: str = 'Exit command',
|
||||||
system_points_title: str = 'System points:',
|
system_points_title: str = 'System points:',
|
||||||
@@ -31,22 +30,19 @@ class App:
|
|||||||
line_separate: str = '',
|
line_separate: str = '',
|
||||||
command_group_description_separate: str = '',
|
command_group_description_separate: str = '',
|
||||||
repeat_command_groups: bool = True,
|
repeat_command_groups: bool = True,
|
||||||
messages_on_startup: list[str] = None,
|
|
||||||
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
|
||||||
self.exit_command = exit_command
|
self._exit_command = exit_command
|
||||||
self.exit_command_description = exit_command_description
|
self._exit_command_description = exit_command_description
|
||||||
self.system_points_title = system_points_title
|
self._system_points_title = system_points_title
|
||||||
self.ignore_exit_command_register = ignore_exit_command_register
|
self._ignore_exit_command_register = ignore_exit_command_register
|
||||||
self.farewell_message = farewell_message
|
self._farewell_message = farewell_message
|
||||||
self.initial_message = initial_message
|
self._initial_message = initial_message
|
||||||
self.invalid_input_flags_message = invalid_input_flags_message
|
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.ignore_command_register = ignore_command_register
|
self._repeat_command_groups_description = repeat_command_groups
|
||||||
self.repeat_command_groups = repeat_command_groups
|
|
||||||
self.messages_on_startup = messages_on_startup if messages_on_startup else []
|
|
||||||
|
|
||||||
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
||||||
self._registered_routers: RegisteredRouters = RegisteredRouters()
|
self._registered_routers: RegisteredRouters = RegisteredRouters()
|
||||||
@@ -54,7 +50,8 @@ class App:
|
|||||||
self._repeated_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Repeated input flags: "{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('Empty input command')
|
self._empty_input_command_handler: Callable[[], None] = lambda: print_func('Empty input command')
|
||||||
self._unknown_command_handler: Callable[[InputCommand], None] = lambda command: print_func(f"Unknown command: {command.get_trigger()}")
|
self._unknown_command_handler: Callable[[InputCommand], None] = lambda command: print_func(f"Unknown command: {command.get_trigger()}")
|
||||||
self._exit_command_handler: Callable[[], None] = lambda: print_func(self.farewell_message)
|
self._exit_command_handler: Callable[[], None] = lambda: print_func(self._farewell_message)
|
||||||
|
self._messages_on_startup = []
|
||||||
|
|
||||||
|
|
||||||
def start_polling(self) -> None:
|
def start_polling(self) -> None:
|
||||||
@@ -62,62 +59,60 @@ class App:
|
|||||||
self._validate_number_of_routers()
|
self._validate_number_of_routers()
|
||||||
self._validate_included_routers()
|
self._validate_included_routers()
|
||||||
|
|
||||||
self.print_func(self.initial_message)
|
self._print_func(self._initial_message)
|
||||||
|
|
||||||
for message in self.messages_on_startup:
|
for message in self._messages_on_startup:
|
||||||
self.print_func(message)
|
self._print_func(message)
|
||||||
|
|
||||||
if not self.repeat_command_groups:
|
if not self._repeat_command_groups_description:
|
||||||
self._print_command_group_description()
|
self._print_command_group_description()
|
||||||
self.print_func(self.prompt)
|
self._print_func(self._prompt)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if self.repeat_command_groups:
|
if self._repeat_command_groups_description:
|
||||||
self._print_command_group_description()
|
self._print_command_group_description()
|
||||||
self.print_func(self.prompt)
|
self._print_func(self._prompt)
|
||||||
|
|
||||||
raw_command: str = input()
|
raw_command: str = input()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
|
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
|
||||||
except BaseInputCommandException as error:
|
except BaseInputCommandException as error:
|
||||||
self.print_func(self.line_separate)
|
self._print_func(self._line_separate)
|
||||||
self._error_handler(error, raw_command)
|
self._error_handler(error, raw_command)
|
||||||
self.print_func(self.line_separate)
|
self._print_func(self._line_separate)
|
||||||
|
|
||||||
if not self.repeat_command_groups:
|
if not self._repeat_command_groups_description:
|
||||||
self.print_func(self.prompt)
|
self._print_func(self._prompt)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
is_exit = self._is_exit_command(input_command)
|
is_exit = self._is_exit_command(input_command)
|
||||||
if is_exit:
|
if is_exit:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.print_func(self.line_separate)
|
self._print_func(self._line_separate)
|
||||||
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
||||||
|
|
||||||
if is_unknown_command:
|
if is_unknown_command:
|
||||||
self.print_func(self.line_separate)
|
self._print_func(self._line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
if not self._repeat_command_groups_description:
|
||||||
if not self.repeat_command_groups:
|
self._print_func(self._prompt)
|
||||||
self.print_func(self.prompt)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for registered_router in self._registered_routers:
|
for registered_router in self._registered_routers:
|
||||||
registered_router.input_command_handler(input_command)
|
registered_router.input_command_handler(input_command)
|
||||||
|
|
||||||
self.print_func(self.line_separate)
|
self._print_func(self._line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
if not self._repeat_command_groups_description:
|
||||||
if not self.repeat_command_groups:
|
self._print_func(self._prompt)
|
||||||
self.print_func(self.prompt)
|
|
||||||
|
|
||||||
|
|
||||||
def set_initial_message(self, message: str) -> None:
|
def set_initial_message(self, message: str) -> None:
|
||||||
self.initial_message: str = message
|
self._initial_message: str = message
|
||||||
|
|
||||||
|
|
||||||
def set_farewell_message(self, message: str) -> None:
|
def set_farewell_message(self, message: str) -> None:
|
||||||
self.farewell_message: str = message
|
self._farewell_message: str = message
|
||||||
|
|
||||||
|
|
||||||
def set_description_message_pattern(self, pattern: str) -> None:
|
def set_description_message_pattern(self, pattern: str) -> None:
|
||||||
@@ -171,14 +166,14 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def add_message_on_startup(self, message: str) -> None:
|
def add_message_on_startup(self, message: str) -> None:
|
||||||
self.messages_on_startup.append(message)
|
self._messages_on_startup.append(message)
|
||||||
|
|
||||||
|
|
||||||
def include_router(self, router: Router) -> None:
|
def include_router(self, router: Router) -> None:
|
||||||
if not isinstance(router, Router):
|
if not isinstance(router, Router):
|
||||||
raise InvalidRouterInstanceException()
|
raise InvalidRouterInstanceException()
|
||||||
|
|
||||||
router.set_ignore_command_register(self.ignore_command_register)
|
router.set_ignore_command_register(self._ignore_command_register)
|
||||||
self._registered_routers.add_registered_router(router)
|
self._registered_routers.add_registered_router(router)
|
||||||
|
|
||||||
|
|
||||||
@@ -199,21 +194,23 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def _setup_system_router(self):
|
def _setup_system_router(self):
|
||||||
system_router.set_title(self.system_points_title)
|
system_router.set_title(self._system_points_title)
|
||||||
@system_router.command(Command(self.exit_command, self.exit_command_description))
|
@system_router.command(Command(trigger=self._exit_command,
|
||||||
|
description=self._exit_command_description))
|
||||||
def exit_command():
|
def exit_command():
|
||||||
self._exit_command_handler()
|
self._exit_command_handler()
|
||||||
|
|
||||||
if system_router not in self._registered_routers.get_registered_routers():
|
if system_router not in self._registered_routers.get_registered_routers():
|
||||||
self.include_router(system_router)
|
self.include_router(system_router)
|
||||||
|
|
||||||
|
|
||||||
def _is_exit_command(self, command: InputCommand):
|
def _is_exit_command(self, command: InputCommand):
|
||||||
if command.get_trigger().lower() == self.exit_command.lower():
|
if command.get_trigger().lower() == self._exit_command.lower():
|
||||||
if self.ignore_exit_command_register:
|
if self._ignore_exit_command_register:
|
||||||
system_router.input_command_handler(command)
|
system_router.input_command_handler(command)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
if command.get_trigger() == self.exit_command:
|
if command.get_trigger() == self._exit_command:
|
||||||
system_router.input_command_handler(command)
|
system_router.input_command_handler(command)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
@@ -224,7 +221,7 @@ class App:
|
|||||||
for command_handler in router_entity.get_command_handlers():
|
for command_handler in router_entity.get_command_handlers():
|
||||||
handled_command_trigger = command_handler.get_handled_command().get_trigger()
|
handled_command_trigger = command_handler.get_handled_command().get_trigger()
|
||||||
if handled_command_trigger.lower() == command.get_trigger().lower():
|
if handled_command_trigger.lower() == command.get_trigger().lower():
|
||||||
if self.ignore_command_register:
|
if self._ignore_command_register:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if handled_command_trigger == command.get_trigger():
|
if handled_command_trigger == command.get_trigger():
|
||||||
@@ -235,12 +232,12 @@ class App:
|
|||||||
|
|
||||||
def _print_command_group_description(self):
|
def _print_command_group_description(self):
|
||||||
for registered_router in self._registered_routers:
|
for registered_router in self._registered_routers:
|
||||||
self.print_func(registered_router.get_title())
|
self._print_func(registered_router.get_title())
|
||||||
for command_handler in registered_router.get_command_handlers():
|
for command_handler in registered_router.get_command_handlers():
|
||||||
self.print_func(self._description_message_pattern.format(
|
self._print_func(self._description_message_pattern.format(
|
||||||
command=command_handler.get_handled_command().get_trigger(),
|
command=command_handler.get_handled_command().get_trigger(),
|
||||||
description=command_handler.get_handled_command().get_description()))
|
description=command_handler.get_handled_command().get_description()))
|
||||||
self.print_func(self.command_group_description_separate)
|
self._print_func(self._command_group_description_separate)
|
||||||
|
|
||||||
|
|
||||||
def _error_handler(self, error: BaseInputCommandException, raw_command: str) -> None:
|
def _error_handler(self, error: BaseInputCommandException, raw_command: str) -> None:
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ class CommandHandler:
|
|||||||
self.handler = handler
|
self.handler = handler
|
||||||
self.handled_command = handled_command
|
self.handled_command = handled_command
|
||||||
|
|
||||||
def handling(self, input_flags: InputFlags):
|
def handling(self, input_flags: InputFlags = None):
|
||||||
if input_flags.get_flags():
|
if input_flags is not None:
|
||||||
self.handler(input_flags)
|
self.handler(input_flags)
|
||||||
else:
|
else:
|
||||||
self.handler()
|
self.handler()
|
||||||
|
|||||||
+17
-19
@@ -51,23 +51,20 @@ class Router:
|
|||||||
def input_command_handler(self, input_command: InputCommand):
|
def input_command_handler(self, input_command: InputCommand):
|
||||||
input_command_name: str = input_command.get_trigger()
|
input_command_name: str = input_command.get_trigger()
|
||||||
input_command_flags: InputFlags = input_command.get_input_flags()
|
input_command_flags: InputFlags = input_command.get_input_flags()
|
||||||
|
|
||||||
for command_handler in self._command_handlers:
|
for command_handler in self._command_handlers:
|
||||||
handle_command = command_handler.get_handled_command()
|
handle_command = command_handler.get_handled_command()
|
||||||
if input_command_name.lower() == handle_command.get_trigger().lower():
|
if input_command_name.lower() == handle_command.get_trigger().lower():
|
||||||
if handle_command.get_registered_flags():
|
if handle_command.get_registered_flags().get_flags():
|
||||||
if input_command_flags:
|
if input_command_flags.get_flags():
|
||||||
for flag in input_command_flags:
|
if self._validate_input_flags(handle_command, input_command_flags):
|
||||||
is_valid = handle_command.validate_input_flag(flag)
|
command_handler.handling(input_command_flags)
|
||||||
if not is_valid:
|
return
|
||||||
self._not_valid_flag_handler(flag)
|
else:
|
||||||
return
|
|
||||||
command_handler.handling(input_command_flags)
|
command_handler.handling(input_command_flags)
|
||||||
return
|
return
|
||||||
else:
|
|
||||||
command_handler.handling({})
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
if input_command_flags:
|
if input_command_flags.get_flags():
|
||||||
self._not_valid_flag_handler(input_command_flags[0])
|
self._not_valid_flag_handler(input_command_flags[0])
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
@@ -75,6 +72,15 @@ class Router:
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_input_flags(self, handle_command: Command, input_flags: InputFlags):
|
||||||
|
for flag in input_flags:
|
||||||
|
is_valid = handle_command.validate_input_flag(flag)
|
||||||
|
if not is_valid:
|
||||||
|
self._not_valid_flag_handler(flag)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_command(command: Command):
|
def _validate_command(command: Command):
|
||||||
command_name: str = command.get_trigger()
|
command_name: str = command.get_trigger()
|
||||||
@@ -119,11 +125,3 @@ class Router:
|
|||||||
|
|
||||||
def set_title(self, title: str):
|
def set_title(self, title: str):
|
||||||
self._title = title
|
self._title = title
|
||||||
|
|
||||||
|
|
||||||
def get_all_commands(self) -> list[str]:
|
|
||||||
all_commands: list[str] = []
|
|
||||||
for command_handler in self._command_handlers:
|
|
||||||
all_commands.append(command_handler.get_handled_command().get_trigger())
|
|
||||||
|
|
||||||
return all_commands
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ def command_help():
|
|||||||
|
|
||||||
@work_router.command(Command(trigger='S', description='Start Solving', flags=Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT)))
|
@work_router.command(Command(trigger='S', description='Start Solving', flags=Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT)))
|
||||||
def command_start_solving(args: InputFlags):
|
def command_start_solving(args: InputFlags):
|
||||||
pass
|
print(args.get_flags())
|
||||||
|
|
||||||
|
|
||||||
@settings_router.command(Command(trigger='U', description='Update WordMath'))
|
@settings_router.command(Command(trigger='U', description='Update WordMath'))
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ from argenta.app import App
|
|||||||
app: App = App(prompt='[italic white bold]What do you want to do(enter number of action)?',
|
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',
|
line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n',
|
||||||
print_func=Console().print,
|
print_func=Console().print,
|
||||||
command_group_description_separate='',
|
command_group_description_separate='ISIISISISIISISISISISISISISIISISI',
|
||||||
repeat_command_groups=False)
|
repeat_command_groups=True)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user