mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
final adding
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import readline
|
||||
import os
|
||||
|
||||
import readline
|
||||
|
||||
|
||||
class AutoCompleter:
|
||||
@@ -27,10 +27,14 @@ class AutoCompleter:
|
||||
else:
|
||||
return None
|
||||
|
||||
def initial_setup(self):
|
||||
def initial_setup(self, all_commands: list[str]):
|
||||
if self.history_filename:
|
||||
if os.path.exists(self.history_filename):
|
||||
readline.read_history_file(self.history_filename)
|
||||
else:
|
||||
for line in all_commands:
|
||||
readline.add_history(line)
|
||||
|
||||
readline.set_completer(self.complete)
|
||||
readline.set_completer_delims(readline.get_completer_delims().replace(' ', ''))
|
||||
readline.parse_and_bind(f'{self.autocomplete_button}: complete')
|
||||
@@ -42,4 +46,3 @@ class AutoCompleter:
|
||||
@staticmethod
|
||||
def get_history_items():
|
||||
return [readline.get_history_item(i) for i in range(1, readline.get_current_history_length() + 1)]
|
||||
|
||||
|
||||
@@ -3,16 +3,6 @@ class InvalidRouterInstanceException(Exception):
|
||||
return "Invalid Router Instance"
|
||||
|
||||
|
||||
class InvalidDescriptionMessagePatternException(Exception):
|
||||
def __init__(self, pattern: str):
|
||||
self.pattern = pattern
|
||||
def __str__(self):
|
||||
return ("Invalid Description Message Pattern\n"
|
||||
"Correct pattern example: [{command}] *=*=* {description}\n"
|
||||
"The pattern must contain two variables: `command` and `description` - description of the command\n"
|
||||
f"Your pattern: {self.pattern}")
|
||||
|
||||
|
||||
class NoRegisteredRoutersException(Exception):
|
||||
def __str__(self):
|
||||
return "No Registered Router Found"
|
||||
|
||||
+13
-14
@@ -1,5 +1,6 @@
|
||||
from typing import Callable
|
||||
from rich.console import Console
|
||||
from rich.markup import escape
|
||||
from art import text2art
|
||||
from contextlib import redirect_stdout
|
||||
import io
|
||||
@@ -15,7 +16,6 @@ from argenta.command.exceptions import (UnprocessedInputFlagException,
|
||||
EmptyInputCommandException,
|
||||
BaseInputCommandException)
|
||||
from argenta.app.exceptions import (InvalidRouterInstanceException,
|
||||
InvalidDescriptionMessagePatternException,
|
||||
NoRegisteredRoutersException,
|
||||
NoRegisteredHandlersException)
|
||||
from argenta.app.registered_routers.entity import RegisteredRouters
|
||||
@@ -50,7 +50,8 @@ class AppInit:
|
||||
self._farewell_message = farewell_message
|
||||
self._initial_message = initial_message
|
||||
|
||||
self._description_message_pattern: str = '[bold red][{command}][/bold red] [blue dim]*=*=*[/blue dim] [bold yellow italic]{description}'
|
||||
|
||||
self._description_message_gen: Callable[[str, str], str] = lambda command, description: f'[bold red]{escape('['+command+']')}[/bold red] [blue dim]*=*=*[/blue dim] [bold yellow italic]{escape(description)}'
|
||||
self._registered_routers: RegisteredRouters = RegisteredRouters()
|
||||
self._messages_on_startup = []
|
||||
|
||||
@@ -62,14 +63,8 @@ class AppInit:
|
||||
|
||||
|
||||
class AppSetters(AppInit):
|
||||
def set_description_message_pattern(self, pattern: str) -> None:
|
||||
first_check = re.match(r'.*{command}.*', pattern)
|
||||
second_check = re.match(r'.*{description}.*', pattern)
|
||||
|
||||
if bool(first_check) and bool(second_check):
|
||||
self._description_message_pattern: str = pattern
|
||||
else:
|
||||
raise InvalidDescriptionMessagePatternException(pattern)
|
||||
def set_description_message_pattern(self, pattern: Callable[[str, str], str]) -> None:
|
||||
self._description_message_gen: Callable[[str, str], str] = pattern
|
||||
|
||||
|
||||
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||
@@ -97,9 +92,9 @@ class AppPrinters(AppInit):
|
||||
for registered_router in self._registered_routers:
|
||||
self._print_func(registered_router.get_title())
|
||||
for command_handler in registered_router.get_command_handlers():
|
||||
self._print_func(self._description_message_pattern.format(
|
||||
command=command_handler.get_handled_command().get_trigger(),
|
||||
description=command_handler.get_handled_command().get_description()))
|
||||
self._print_func(self._description_message_gen(
|
||||
command_handler.get_handled_command().get_trigger(),
|
||||
command_handler.get_handled_command().get_description()))
|
||||
self._print_func('')
|
||||
|
||||
|
||||
@@ -190,7 +185,11 @@ class AppSetups(AppValidators, AppPrinters):
|
||||
self._setup_system_router()
|
||||
self._validate_number_of_routers()
|
||||
self._validate_included_routers()
|
||||
self._autocompleter.initial_setup()
|
||||
|
||||
all_triggers: list[str] = []
|
||||
for router_entity in self._registered_routers:
|
||||
all_triggers.extend(router_entity.get_triggers())
|
||||
self._autocompleter.initial_setup(all_triggers)
|
||||
|
||||
self._print_func(self._initial_message)
|
||||
|
||||
|
||||
@@ -5,17 +5,17 @@ from argenta.command.flag.models import InputFlags
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, handler: Callable[[], None] | Callable[[InputFlags], None], handled_command: Command):
|
||||
self.handler = handler
|
||||
self.handled_command = handled_command
|
||||
self._handler = handler
|
||||
self._handled_command = handled_command
|
||||
|
||||
def handling(self, input_flags: InputFlags = None):
|
||||
if input_flags is not None:
|
||||
self.handler(input_flags)
|
||||
self._handler(input_flags)
|
||||
else:
|
||||
self.handler()
|
||||
self._handler()
|
||||
|
||||
def get_handler(self):
|
||||
return self.handler
|
||||
return self._handler
|
||||
|
||||
def get_handled_command(self):
|
||||
return self.handled_command
|
||||
return self._handled_command
|
||||
@@ -110,6 +110,13 @@ class Router:
|
||||
self._ignore_command_register = ignore_command_register
|
||||
|
||||
|
||||
def get_triggers(self):
|
||||
all_triggers: list[str] = []
|
||||
for command_handler in self._command_handlers:
|
||||
all_triggers.append(command_handler.get_handled_command().get_trigger())
|
||||
return all_triggers
|
||||
|
||||
|
||||
def get_command_handlers(self) -> CommandHandlers:
|
||||
return self._command_handlers
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
test
|
||||
q
|
||||
P
|
||||
q
|
||||
S
|
||||
S --host 125
|
||||
q
|
||||
S --host 125.0123
|
||||
0
|
||||
U
|
||||
q
|
||||
+6
-30
@@ -1,32 +1,8 @@
|
||||
import readline
|
||||
from rich.console import Console
|
||||
from rich.markup import escape
|
||||
|
||||
|
||||
def completer(text, state):
|
||||
matches = sorted(cmd for cmd in get_history_items() if cmd.startswith(text))
|
||||
if len(matches) > 1:
|
||||
common_prefix = matches[0]
|
||||
for match in matches[1:]:
|
||||
i = 0
|
||||
while i < len(common_prefix) and i < len(match) and common_prefix[i] == match[i]:
|
||||
i += 1
|
||||
common_prefix = common_prefix[:i]
|
||||
if state == 0:
|
||||
readline.insert_text(common_prefix[len(text):])
|
||||
readline.redisplay()
|
||||
return None
|
||||
elif len(matches) == 1:
|
||||
return matches[0] if state == 0 else None
|
||||
else:
|
||||
return None
|
||||
|
||||
readline.set_completer(completer)
|
||||
readline.parse_and_bind("tab: complete")
|
||||
|
||||
def get_history_items():
|
||||
return [readline.get_history_item(i) for i in range(1, readline.get_current_history_length() + 1)]
|
||||
|
||||
while True:
|
||||
try:
|
||||
line = input('> ')
|
||||
except EOFError:
|
||||
break
|
||||
console = Console()
|
||||
text = lambda command, description: f'[bold red]{escape('['+command+']')}[/bold red] [blue dim]*=*=*[/blue dim] [bold yellow italic]{escape(description)}'
|
||||
print(text('start', 'command start'))
|
||||
console.print(text('start', 'command start'))
|
||||
|
||||
@@ -14,17 +14,17 @@ settings_router: Router = Router(title='Settings points:')
|
||||
console = Console()
|
||||
|
||||
|
||||
@work_router.command(Command('0', 'Get Help'))
|
||||
@work_router.command(Command('get', 'Get Help'))
|
||||
def command_help():
|
||||
help_command()
|
||||
|
||||
|
||||
@work_router.command(Command('S', 'Start Solving', Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT)))
|
||||
@work_router.command(Command('start', 'Start Solving', Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT)))
|
||||
def command_start_solving(args: InputFlags):
|
||||
print(args.get_flag('test'))
|
||||
|
||||
|
||||
@settings_router.command(Command('U', 'Update WordMath'))
|
||||
@settings_router.command(Command('update', 'Update WordMath'))
|
||||
def command_update():
|
||||
print('eeeeeee')
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from argenta.app.dividing_line import DynamicDividingLine
|
||||
from argenta.app.autocompleter import AutoCompleter
|
||||
|
||||
|
||||
autocompleter = AutoCompleter('./mock/.history')
|
||||
autocompleter = AutoCompleter()
|
||||
app: App = App(dividing_line=DynamicDividingLine(),
|
||||
autocompleter=autocompleter)
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
from argenta.app import App
|
||||
from argenta.app.exceptions import InvalidDescriptionMessagePatternException
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class TestApp(unittest.TestCase):
|
||||
def test_set_invalid_description_message_pattern(self):
|
||||
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||
App().set_description_message_pattern('Invalid description pattern')
|
||||
|
||||
def test_set_invalid_description_message_pattern2(self):
|
||||
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
|
||||
|
||||
Reference in New Issue
Block a user