mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
better perf
This commit is contained in:
@@ -48,7 +48,7 @@ class AutoCompleter:
|
||||
else:
|
||||
return None
|
||||
|
||||
def initial_setup(self, all_commands: list[str]) -> None:
|
||||
def initial_setup(self, all_commands: set[str]) -> None:
|
||||
"""
|
||||
Private. Initial setup function
|
||||
:param all_commands: Registered commands for adding them to the autocomplete history
|
||||
@@ -69,7 +69,7 @@ class AutoCompleter:
|
||||
readline.set_completer_delims(readline.get_completer_delims().replace(" ", ""))
|
||||
readline.parse_and_bind(f"{self.autocomplete_button}: complete")
|
||||
|
||||
def exit_setup(self, all_commands: list[str], ignore_command_register: bool) -> None:
|
||||
def exit_setup(self, all_commands: set[str]) -> None:
|
||||
"""
|
||||
Private. Exit setup function
|
||||
:return: None
|
||||
@@ -80,22 +80,12 @@ class AutoCompleter:
|
||||
raw_history = history_file.read()
|
||||
pretty_history: list[str] = []
|
||||
for line in set(raw_history.strip().split("\n")):
|
||||
if _is_command_exist(
|
||||
line.split()[0], all_commands, ignore_command_register
|
||||
):
|
||||
if line.split()[0] in all_commands:
|
||||
pretty_history.append(line)
|
||||
with open(self.history_filename, "w") as history_file:
|
||||
_ = history_file.write("\n".join(pretty_history))
|
||||
|
||||
|
||||
def _is_command_exist(
|
||||
command: str, existing_commands: list[str], ignore_command_register: bool
|
||||
) -> bool:
|
||||
if ignore_command_register:
|
||||
return command.lower() in existing_commands
|
||||
return command in existing_commands
|
||||
|
||||
|
||||
def _get_history_items() -> list[str] | list[Never]:
|
||||
"""
|
||||
Private. Returns a list of all commands entered by the user
|
||||
|
||||
+15
-68
@@ -40,7 +40,6 @@ class BaseApp:
|
||||
farewell_message: str,
|
||||
exit_command: Command,
|
||||
system_router_title: str,
|
||||
ignore_command_register: bool,
|
||||
dividing_line: StaticDividingLine | DynamicDividingLine,
|
||||
repeat_command_groups_printing: bool,
|
||||
override_system_messages: bool,
|
||||
@@ -51,8 +50,7 @@ class BaseApp:
|
||||
self._print_func: Printer = print_func
|
||||
self._exit_command: Command = exit_command
|
||||
self._dividing_line: StaticDividingLine | DynamicDividingLine = dividing_line
|
||||
self._ignore_command_register: bool = ignore_command_register
|
||||
self._repeat_command_groups_printing_description: bool = repeat_command_groups_printing
|
||||
self._repeat_command_groups_printing: bool = repeat_command_groups_printing
|
||||
self._override_system_messages: bool = override_system_messages
|
||||
self._autocompleter: AutoCompleter = autocompleter
|
||||
self.system_router: Router = Router(title=system_router_title)
|
||||
@@ -66,15 +64,6 @@ class BaseApp:
|
||||
self.registered_routers: RegisteredRouters = RegisteredRouters()
|
||||
self._messages_on_startup: list[str] = []
|
||||
|
||||
self._matching_lower_triggers_with_routers: dict[str, Router] = {}
|
||||
self._matching_default_triggers_with_routers: dict[str, Router] = {}
|
||||
|
||||
self._current_matching_triggers_with_routers: dict[str, Router] = (
|
||||
self._matching_lower_triggers_with_routers
|
||||
if self._ignore_command_register
|
||||
else self._matching_default_triggers_with_routers
|
||||
)
|
||||
|
||||
self._incorrect_input_syntax_handler: NonStandardBehaviorHandler[str] = (
|
||||
lambda _: print_func(f"Incorrect flag syntax: {_}")
|
||||
)
|
||||
@@ -217,37 +206,12 @@ class BaseApp:
|
||||
"""
|
||||
trigger = command.trigger
|
||||
exit_trigger = self._exit_command.trigger
|
||||
if self._ignore_command_register:
|
||||
if trigger.lower() == exit_trigger.lower():
|
||||
return True
|
||||
elif trigger.lower() in [x.lower() for x in self._exit_command.aliases]:
|
||||
return True
|
||||
else:
|
||||
if trigger == exit_trigger:
|
||||
return True
|
||||
elif trigger in self._exit_command.aliases:
|
||||
return True
|
||||
if trigger.lower() == exit_trigger.lower():
|
||||
return True
|
||||
elif trigger.lower() in [x.lower() for x in self._exit_command.aliases]:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _is_unknown_command(self, command: InputCommand) -> bool:
|
||||
"""
|
||||
Private. Checks if the given command is an unknown command
|
||||
:param command: command to check
|
||||
:return: is it an unknown command or not as bool
|
||||
"""
|
||||
input_command_trigger = command.trigger
|
||||
if self._ignore_command_register:
|
||||
if input_command_trigger.lower() in list(
|
||||
self._current_matching_triggers_with_routers.keys()
|
||||
):
|
||||
return False
|
||||
else:
|
||||
if input_command_trigger in list(
|
||||
self._current_matching_triggers_with_routers.keys()
|
||||
):
|
||||
return False
|
||||
return True
|
||||
|
||||
def _error_handler(self, error: InputCommandException, raw_command: str) -> None:
|
||||
"""
|
||||
Private. Handles parsing errors of the entered command
|
||||
@@ -297,7 +261,7 @@ class BaseApp:
|
||||
all_aliases.update(router_entity.aliases)
|
||||
|
||||
def _most_similar_command(self, unknown_command: str) -> str | None:
|
||||
all_commands = list(self._current_matching_triggers_with_routers.keys())
|
||||
all_commands = self.registered_routers.get_triggers()
|
||||
|
||||
matches_startswith_unknown_command: Matches = sorted(
|
||||
cmd for cmd in all_commands if cmd.startswith(unknown_command)
|
||||
@@ -368,20 +332,7 @@ class BaseApp:
|
||||
self._setup_system_router()
|
||||
self._validate_routers_for_collisions()
|
||||
|
||||
for router_entity in self.registered_routers:
|
||||
router_triggers = router_entity.triggers
|
||||
router_aliases = router_entity.aliases
|
||||
combined = router_triggers | router_aliases
|
||||
|
||||
for trigger in combined:
|
||||
self._matching_default_triggers_with_routers[trigger] = router_entity
|
||||
self._matching_lower_triggers_with_routers[trigger.lower()] = (
|
||||
router_entity
|
||||
)
|
||||
|
||||
self._autocompleter.initial_setup(
|
||||
list(self._current_matching_triggers_with_routers.keys())
|
||||
)
|
||||
self._autocompleter.initial_setup(self.registered_routers.get_triggers())
|
||||
|
||||
if not self._override_system_messages:
|
||||
self._setup_default_view()
|
||||
@@ -392,13 +343,14 @@ class BaseApp:
|
||||
self._print_func(message)
|
||||
if self._messages_on_startup:
|
||||
print("\n")
|
||||
if not self._repeat_command_groups_printing_description:
|
||||
if not self._repeat_command_groups_printing:
|
||||
self._print_command_group_description()
|
||||
|
||||
def _process_exist_and_valid_command(self, input_command: InputCommand) -> None:
|
||||
processing_router = self._current_matching_triggers_with_routers[
|
||||
input_command.trigger.lower()
|
||||
]
|
||||
processing_router = self.registered_routers.get_router_by_trigger(input_command.trigger.lower())
|
||||
|
||||
if not processing_router:
|
||||
raise RuntimeError(f"Router for '{input_command.trigger}' not found. Panic!")
|
||||
|
||||
if processing_router.disable_redirect_stdout:
|
||||
dividing_line_unit_part: str = self._dividing_line.get_unit_part()
|
||||
@@ -452,7 +404,6 @@ class App(BaseApp):
|
||||
:param farewell_message: displayed at the end of the app
|
||||
:param exit_command: the entity of the command that will be terminated when entered
|
||||
:param system_router_title: system router title
|
||||
:param ignore_command_register: whether to ignore the case of the entered commands
|
||||
:param dividing_line: the entity of the dividing line
|
||||
:param repeat_command_groups_printing: whether to repeat the available commands and their description
|
||||
:param override_system_messages: whether to redefine the default formatting of system messages
|
||||
@@ -466,7 +417,6 @@ class App(BaseApp):
|
||||
farewell_message=farewell_message,
|
||||
exit_command=exit_command,
|
||||
system_router_title=system_router_title,
|
||||
ignore_command_register=ignore_command_register,
|
||||
dividing_line=dividing_line,
|
||||
repeat_command_groups_printing=repeat_command_groups_printing,
|
||||
override_system_messages=override_system_messages,
|
||||
@@ -481,7 +431,7 @@ class App(BaseApp):
|
||||
"""
|
||||
self._pre_cycle_setup()
|
||||
while True:
|
||||
if self._repeat_command_groups_printing_description:
|
||||
if self._repeat_command_groups_printing:
|
||||
self._print_command_group_description()
|
||||
|
||||
raw_command: str = Console().input(self._prompt)
|
||||
@@ -497,13 +447,10 @@ class App(BaseApp):
|
||||
|
||||
if self._is_exit_command(input_command):
|
||||
self.system_router.finds_appropriate_handler(input_command)
|
||||
self._autocompleter.exit_setup(
|
||||
list(self._current_matching_triggers_with_routers.keys()),
|
||||
self._ignore_command_register,
|
||||
)
|
||||
self._autocompleter.exit_setup(self.registered_routers.get_triggers())
|
||||
return
|
||||
|
||||
if self._is_unknown_command(input_command):
|
||||
if self.registered_routers.get_router_by_trigger(input_command.trigger.lower()):
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
self._unknown_command_handler(input_command)
|
||||
stdout_res: str = stdout.getvalue()
|
||||
|
||||
@@ -6,15 +6,14 @@ from argenta.router import Router
|
||||
|
||||
|
||||
class RegisteredRouters:
|
||||
def __init__(self, registered_routers: list[Router] | None = None) -> None:
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Private. Combines registered routers
|
||||
:param registered_routers: list of the registered routers
|
||||
:return: None
|
||||
"""
|
||||
self.registered_routers: list[Router] = registered_routers if registered_routers else []
|
||||
|
||||
self._matching_lower_triggers_with_routers
|
||||
self.registered_routers: list[Router] = []
|
||||
self._paired_trigger_router: dict[str, Router] = {}
|
||||
|
||||
def add_registered_router(self, router: Router, /) -> None:
|
||||
"""
|
||||
@@ -23,6 +22,14 @@ class RegisteredRouters:
|
||||
:return: None
|
||||
"""
|
||||
self.registered_routers.append(router)
|
||||
|
||||
for trigger in (router.aliases | router.triggers):
|
||||
self._paired_trigger_router[trigger] = router
|
||||
|
||||
def get_router_by_trigger(self, trigger: str) -> Router | None:
|
||||
return self._paired_trigger_router.get(trigger)
|
||||
|
||||
def get_triggers(self) -> set[str]:
|
||||
return set(self._paired_trigger_router.keys())
|
||||
|
||||
def __iter__(self) -> Iterator[Router]:
|
||||
return iter(self.registered_routers)
|
||||
|
||||
@@ -104,7 +104,7 @@ class Router:
|
||||
command_handler = self.command_handlers.get_command_handler_by_trigger(input_command_name)
|
||||
|
||||
if not command_handler:
|
||||
raise RuntimeError(f"Handler for '{input_command.trigger}' command not found!")
|
||||
raise RuntimeError(f"Handler for '{input_command.trigger}' command not found. Panic!")
|
||||
else:
|
||||
self.process_input_command(input_command_flags, command_handler)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user