mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
work on v0.3.0
This commit is contained in:
+23
-9
@@ -1,5 +1,8 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
from ..command.entity import Command
|
||||||
|
from argenta.command.parse_input_command.entity import ParseInputCommand
|
||||||
from ..router.entity import Router
|
from ..router.entity import Router
|
||||||
|
from ..command.parse_input_command.exceptions import InvalidInputFlagsException
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
OnlyOneMainRouterIsAllowedException,
|
OnlyOneMainRouterIsAllowedException,
|
||||||
@@ -16,6 +19,7 @@ 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',
|
||||||
exit_command_title: str = 'System points:',
|
exit_command_title: str = 'System points:',
|
||||||
@@ -33,13 +37,14 @@ class App:
|
|||||||
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 = repeat_command_groups
|
self.repeat_command_groups = repeat_command_groups
|
||||||
|
|
||||||
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] | Command]] | Router]] = []
|
||||||
self._app_main_router: Router | None = None
|
self._app_main_router: Router | None = None
|
||||||
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
||||||
|
|
||||||
@@ -61,12 +66,21 @@ class App:
|
|||||||
self._print_command_group_description()
|
self._print_command_group_description()
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
|
|
||||||
command: str = input()
|
raw_command: str = input()
|
||||||
|
try:
|
||||||
|
command: Command = ParseInputCommand(raw_command=raw_command)
|
||||||
|
except InvalidInputFlagsException:
|
||||||
|
self.print_func(self.line_separate)
|
||||||
|
self.print_func(self.command_group_description_separate)
|
||||||
|
if not self.repeat_command_groups:
|
||||||
|
self.print_func(self.prompt)
|
||||||
|
continue
|
||||||
|
|
||||||
self._checking_command_for_exit_command(command)
|
|
||||||
|
self._checking_command_for_exit_command(command.get_string_entity())
|
||||||
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.get_string_entity())
|
||||||
if is_unknown_command:
|
if is_unknown_command:
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
@@ -124,7 +138,7 @@ class App:
|
|||||||
router.set_ignore_command_register(self.ignore_command_register)
|
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] | Command]] = 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(),
|
||||||
'entity': router,
|
'entity': router,
|
||||||
@@ -187,10 +201,10 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def _check_is_command_unknown(self, command: str):
|
def _check_is_command_unknown(self, command: str):
|
||||||
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = self._registered_router_entities
|
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = self._registered_router_entities
|
||||||
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'].get_string_entity().lower() == command.lower():
|
||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
@@ -207,8 +221,8 @@ class App:
|
|||||||
self.print_func(router_entity['title'])
|
self.print_func(router_entity['title'])
|
||||||
for command_entity in router_entity['commands']:
|
for command_entity in router_entity['commands']:
|
||||||
self.print_func(self._description_message_pattern.format(
|
self.print_func(self._description_message_pattern.format(
|
||||||
command=command_entity['command'],
|
command=command_entity['command'].get_string_entity(),
|
||||||
description=command_entity['description']
|
description=command_entity['command'].get_description()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.print_func(self.command_group_description_separate)
|
self.print_func(self.command_group_description_separate)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -26,11 +26,14 @@ class Command:
|
|||||||
def get_flags(self):
|
def get_flags(self):
|
||||||
return self._flags
|
return self._flags
|
||||||
|
|
||||||
|
def set_command(self, command: str):
|
||||||
|
self._command = command
|
||||||
|
|
||||||
def validate_commands_params(self):
|
def validate_commands_params(self):
|
||||||
if not isinstance(self._command, str):
|
if not isinstance(self._command, str):
|
||||||
raise InvalidCommandInstanceException(self._command)
|
raise InvalidCommandInstanceException(self._command)
|
||||||
if isinstance(self._description, str):
|
if not isinstance(self._description, str):
|
||||||
raise InvalidDescriptionInstanceException()
|
raise InvalidDescriptionInstanceException()
|
||||||
if isinstance(self._flags, Flag) or isinstance(self._flags, FlagsGroup):
|
if not (isinstance(self._flags, Flag) or isinstance(self._flags, FlagsGroup)):
|
||||||
raise InvalidFlagsInstanceException
|
raise InvalidFlagsInstanceException
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -11,6 +11,8 @@ class Flag:
|
|||||||
self.possible_flag_values = possible_flag_values
|
self.possible_flag_values = possible_flag_values
|
||||||
self.ignore_flag_value_register = ignore_flag_value_register
|
self.ignore_flag_value_register = ignore_flag_value_register
|
||||||
|
|
||||||
|
self._value = None
|
||||||
|
|
||||||
def get_string_entity(self):
|
def get_string_entity(self):
|
||||||
if self.ignore_flag_value_register:
|
if self.ignore_flag_value_register:
|
||||||
string_entity: str = self.flag_prefix + self.flag_name.lower()
|
string_entity: str = self.flag_prefix + self.flag_name.lower()
|
||||||
@@ -18,6 +20,12 @@ class Flag:
|
|||||||
string_entity: str = self.flag_prefix + self.flag_name
|
string_entity: str = self.flag_prefix + self.flag_name
|
||||||
return string_entity
|
return string_entity
|
||||||
|
|
||||||
|
def get_value(self):
|
||||||
|
return self._value
|
||||||
|
|
||||||
|
def set_value(self, value):
|
||||||
|
self._value = value
|
||||||
|
|
||||||
def validate_input_flag_value(self, input_flag_value: str):
|
def validate_input_flag_value(self, input_flag_value: str):
|
||||||
if self.possible_flag_values:
|
if self.possible_flag_values:
|
||||||
if self.ignore_flag_value_register:
|
if self.ignore_flag_value_register:
|
||||||
@@ -32,9 +40,3 @@ class Flag:
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
c = Flag('s', flag_prefix='---kinn', ignore_flag_value_register=False, possible_flag_values=['abc', 'ASW', 'eBc'])
|
|
||||||
print(c.get_string_entity())
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -1,3 +1,6 @@
|
|||||||
|
from argenta.command.params.flag.entity import Flag
|
||||||
|
|
||||||
|
|
||||||
class FlagsGroup:
|
class FlagsGroup:
|
||||||
def __init__(self, flags):
|
def __init__(self, flags: list[Flag]):
|
||||||
self.flags = flags
|
self.flags = flags
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,38 @@
|
|||||||
|
from typing import Literal, LiteralString
|
||||||
|
from argenta.command.entity import Command
|
||||||
|
from argenta.command.params.flag.entity import Flag
|
||||||
|
from argenta.command.params.flags_group.entity import FlagsGroup
|
||||||
|
from .exceptions import InvalidInputFlagsException
|
||||||
|
|
||||||
|
|
||||||
|
class ParseInputCommand:
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
raw_command = kwargs['raw_command']
|
||||||
|
return ParseInputCommand.parse(raw_command)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse(raw_command: str) -> Command:
|
||||||
|
list_of__ = raw_command.split()
|
||||||
|
command = list_of__[0]
|
||||||
|
list_of__.pop(0)
|
||||||
|
|
||||||
|
flags = []
|
||||||
|
for k, _ in enumerate(list_of__):
|
||||||
|
if not _.startswith('-') or len( _[:len(_.lstrip('-'))]) > 3:
|
||||||
|
raise InvalidInputFlagsException()
|
||||||
|
else:
|
||||||
|
flag_name: str = _.lstrip('-')
|
||||||
|
flag_prefix = _[:len(flag_name)]
|
||||||
|
|
||||||
|
parse_flag = Flag(flag_name=flag_name,
|
||||||
|
flag_prefix=flag_prefix)
|
||||||
|
|
||||||
|
flags.append(parse_flag)
|
||||||
|
|
||||||
|
if len(flags) == 0:
|
||||||
|
return Command(command=command)
|
||||||
|
elif len(flags) == 1:
|
||||||
|
return Command(command=command, flags=flags[0])
|
||||||
|
else:
|
||||||
|
flags = FlagsGroup(flags=flags)
|
||||||
|
return Command(command=command, flags=flags)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class InvalidInputFlagsException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Invalid Input Flags"
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
class InputCommand:
|
|
||||||
pass
|
|
||||||
|
|
||||||
+13
-10
@@ -1,4 +1,5 @@
|
|||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
|
from ..command.entity import Command
|
||||||
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||||
RepeatedCommandException)
|
RepeatedCommandException)
|
||||||
|
|
||||||
@@ -11,19 +12,19 @@ class Router:
|
|||||||
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] | Command]] = []
|
||||||
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
|
self.ignore_command_register: bool = False
|
||||||
|
|
||||||
|
|
||||||
def command(self, command: str, description: str = None) -> Callable[[Any], Any]:
|
def command(self, command: Command) -> Callable[[Any], Any]:
|
||||||
|
command.validate_commands_params()
|
||||||
self._validate_command(command)
|
self._validate_command(command)
|
||||||
|
|
||||||
def command_decorator(func):
|
def command_decorator(func):
|
||||||
self._command_entities.append({'handler_func': func,
|
self._command_entities.append({'handler_func': func,
|
||||||
'command': command,
|
'command': command})
|
||||||
'description': description})
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
@@ -42,9 +43,10 @@ class Router:
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def input_command_handler(self, input_command):
|
def input_command_handler(self, input_command: Command):
|
||||||
|
input_command_name: str = input_command.get_string_entity()
|
||||||
for command_entity in self._command_entities:
|
for command_entity in self._command_entities:
|
||||||
if input_command.lower() == command_entity['command'].lower():
|
if input_command_name.lower() == command_entity['command'].get_string_entity():
|
||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
return command_entity['handler_func']()
|
return command_entity['handler_func']()
|
||||||
else:
|
else:
|
||||||
@@ -56,11 +58,12 @@ class Router:
|
|||||||
self.unknown_command_func(unknown_command)
|
self.unknown_command_func(unknown_command)
|
||||||
|
|
||||||
|
|
||||||
def _validate_command(self, command: str):
|
def _validate_command(self, command: Command):
|
||||||
|
command_name: str = command.get_string_entity()
|
||||||
if command in self.get_all_commands():
|
if command in self.get_all_commands():
|
||||||
raise RepeatedCommandException()
|
raise RepeatedCommandException()
|
||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
if command.lower() in [x.lower() for x in self.get_all_commands()]:
|
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
||||||
raise RepeatedCommandException()
|
raise RepeatedCommandException()
|
||||||
|
|
||||||
|
|
||||||
@@ -74,7 +77,7 @@ class Router:
|
|||||||
self.ignore_command_register = ignore_command_register
|
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] | Command]]:
|
||||||
return self._command_entities
|
return self._command_entities
|
||||||
|
|
||||||
|
|
||||||
@@ -103,6 +106,6 @@ class Router:
|
|||||||
def get_all_commands(self) -> list[str]:
|
def get_all_commands(self) -> list[str]:
|
||||||
all_commands: list[str] = []
|
all_commands: list[str] = []
|
||||||
for command_entity in self._command_entities:
|
for command_entity in self._command_entities:
|
||||||
all_commands.append(command_entity['command'])
|
all_commands.append(command_entity['command'].get_string_entity())
|
||||||
|
|
||||||
return all_commands
|
return all_commands
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
|
from argenta.command.entity import Command
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
|
|
||||||
from ..handlers.handlers_implementation.help_command import help_command
|
from ..handlers.handlers_implementation.help_command import help_command
|
||||||
@@ -12,19 +14,19 @@ settings_router: Router = Router(title='Settings points:')
|
|||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
|
|
||||||
@work_router.command(command='0', description='Get Help')
|
@work_router.command(command=Command(command='0', description='Get Help'))
|
||||||
def command_help():
|
def command_help():
|
||||||
help_command()
|
help_command()
|
||||||
|
|
||||||
|
|
||||||
@work_router.command(command='1', description='Start Solving')
|
'''@work_router.command(command='1', description='Start Solving')
|
||||||
def command_start_solving():
|
def command_start_solving():
|
||||||
start_solving_command()
|
start_solving_command()
|
||||||
|
|
||||||
|
|
||||||
@settings_router.command(command='U', description='Update WordMath')
|
@settings_router.command(command='U', description='Update WordMath')
|
||||||
def command_update():
|
def command_update():
|
||||||
upgrade_command()
|
upgrade_command()'''
|
||||||
|
|
||||||
|
|
||||||
@work_router.unknown_command
|
@work_router.unknown_command
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ def main():
|
|||||||
goodbye_message: str = f'[bold red]\n{ascii_goodbye_message}{' '*12}made by kolo\n'
|
goodbye_message: str = f'[bold red]\n{ascii_goodbye_message}{' '*12}made by kolo\n'
|
||||||
|
|
||||||
app.include_router(work_router, is_main=True)
|
app.include_router(work_router, is_main=True)
|
||||||
app.include_router(settings_router)
|
#app.include_router(settings_router)
|
||||||
|
|
||||||
app.set_initial_message(initial_greeting)
|
app.set_initial_message(initial_greeting)
|
||||||
app.set_farewell_message(goodbye_message)
|
app.set_farewell_message(goodbye_message)
|
||||||
|
|
||||||
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')
|
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')
|
||||||
|
|
||||||
#app.start_polling()
|
app.start_polling()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user