mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work on docs, tests and some fix
This commit is contained in:
@@ -100,7 +100,7 @@ class App:
|
||||
self.print_func(self.prompt)
|
||||
continue
|
||||
|
||||
self._check_command_for_exit_command(input_command.get_string_entity())
|
||||
self._check_command_for_exit_command(input_command.get_trigger())
|
||||
|
||||
self.print_func(self.line_separate)
|
||||
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
||||
@@ -227,11 +227,11 @@ class App:
|
||||
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 command_entity in router_entity['commands']:
|
||||
if command_entity['command'].get_string_entity().lower() == command.get_string_entity().lower():
|
||||
if command_entity['command'].get_trigger().lower() == command.get_trigger().lower():
|
||||
if self.ignore_command_register:
|
||||
return False
|
||||
else:
|
||||
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
||||
if command_entity['command'].get_trigger() == command.get_trigger():
|
||||
return False
|
||||
self._unknown_command_handler(command)
|
||||
return True
|
||||
@@ -242,7 +242,7 @@ class App:
|
||||
self.print_func(router_entity['title'])
|
||||
for command_entity in router_entity['commands']:
|
||||
self.print_func(self._description_message_pattern.format(
|
||||
command=command_entity['command'].get_string_entity(),
|
||||
command=command_entity['command'].get_trigger(),
|
||||
description=command_entity['command'].get_description()
|
||||
)
|
||||
)
|
||||
|
||||
+13
-25
@@ -1,9 +1,6 @@
|
||||
from .params.flag.entity import Flag
|
||||
from .params.flag.flags_group.entity import FlagsGroup
|
||||
from .exceptions import (InvalidCommandInstanceException,
|
||||
InvalidDescriptionInstanceException,
|
||||
InvalidFlagsInstanceException,
|
||||
UnprocessedInputFlagException,
|
||||
from .exceptions import (UnprocessedInputFlagException,
|
||||
RepeatedInputFlagsException,
|
||||
EmptyInputCommandException)
|
||||
|
||||
@@ -14,37 +11,28 @@ T = TypeVar('T')
|
||||
|
||||
|
||||
class Command(Generic[T]):
|
||||
def __init__(self, command: str,
|
||||
def __init__(self, trigger: str,
|
||||
description: str = None,
|
||||
flags: Flag | FlagsGroup | None = None):
|
||||
self._command = command
|
||||
self._description = f'description for "{self._command}" command' if not description else description
|
||||
flags: Flag | FlagsGroup = None):
|
||||
self._trigger = trigger
|
||||
self._description = f'description for "{self._trigger}" command' if not description else description
|
||||
self._registered_flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
|
||||
|
||||
self._input_flags: FlagsGroup | None = None
|
||||
|
||||
|
||||
def get_string_entity(self):
|
||||
return self._command
|
||||
def get_trigger(self) -> str:
|
||||
return self._trigger
|
||||
|
||||
|
||||
def get_description(self):
|
||||
def get_description(self) -> str:
|
||||
return self._description
|
||||
|
||||
|
||||
def get_registered_flags(self):
|
||||
def get_registered_flags(self) -> FlagsGroup | None:
|
||||
return self._registered_flags
|
||||
|
||||
|
||||
def validate_commands_params(self):
|
||||
if not isinstance(self._command, str):
|
||||
raise InvalidCommandInstanceException(self._command)
|
||||
if not isinstance(self._description, str):
|
||||
raise InvalidDescriptionInstanceException()
|
||||
if not any([(isinstance(self._registered_flags, FlagsGroup)), not self._registered_flags]):
|
||||
raise InvalidFlagsInstanceException
|
||||
|
||||
|
||||
def validate_input_flag(self, flag: Flag):
|
||||
registered_flags: FlagsGroup | None = self.get_registered_flags()
|
||||
if registered_flags:
|
||||
@@ -62,7 +50,7 @@ class Command(Generic[T]):
|
||||
return False
|
||||
|
||||
|
||||
def set_input_flags(self, input_flags: FlagsGroup):
|
||||
def _set_input_flags(self, input_flags: FlagsGroup):
|
||||
self._input_flags = input_flags
|
||||
|
||||
def get_input_flags(self) -> FlagsGroup:
|
||||
@@ -110,10 +98,10 @@ class Command(Generic[T]):
|
||||
if any([current_flag_name, current_flag_value]):
|
||||
raise UnprocessedInputFlagException()
|
||||
if len(flags.get_flags()) == 0:
|
||||
return Command(command=command)
|
||||
return Command(trigger=command)
|
||||
else:
|
||||
input_command = Command(command=command)
|
||||
input_command.set_input_flags(flags)
|
||||
input_command = Command(trigger=command)
|
||||
input_command._set_input_flags(flags)
|
||||
return input_command
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,6 @@
|
||||
from .params.flag.entity import Flag
|
||||
|
||||
|
||||
class InvalidCommandInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Command Instance"
|
||||
|
||||
|
||||
class InvalidDescriptionInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Description Instance"
|
||||
|
||||
|
||||
class InvalidFlagsInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Flags Instance"
|
||||
|
||||
|
||||
class UnprocessedInputFlagException(Exception):
|
||||
def __str__(self):
|
||||
return "Unprocessed Input Flags"
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import Literal, Pattern
|
||||
|
||||
class Flag:
|
||||
def __init__(self, flag_name: str,
|
||||
flag_prefix: Literal['-', '--', '---'] = '-',
|
||||
flag_prefix: Literal['-', '--', '---'] = '--',
|
||||
ignore_flag_value_register: bool = False,
|
||||
possible_flag_values: list[str] | Pattern[str] = False):
|
||||
self._flag_name = flag_name
|
||||
|
||||
@@ -5,7 +5,7 @@ class FlagsGroup:
|
||||
def __init__(self, flags: list[Flag] = None):
|
||||
self._flags: list[Flag] = [] if not flags else flags
|
||||
|
||||
def get_flags(self):
|
||||
def get_flags(self) -> list[Flag]:
|
||||
return self._flags
|
||||
|
||||
def add_flag(self, flag: Flag):
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
from .entity import Router
|
||||
from .exceptions import InvalidDescriptionInstanceException
|
||||
from .entity import Router
|
||||
@@ -4,7 +4,8 @@ from inspect import getfullargspec
|
||||
from ..command.entity import Command
|
||||
from ..command.params.flag.entity import Flag
|
||||
from ..command.params.flag.flags_group.entity import FlagsGroup
|
||||
from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
|
||||
from ..router.exceptions import (RepeatedCommandException,
|
||||
RepeatedFlagNameException,
|
||||
TooManyTransferredArgsException,
|
||||
RequiredArgumentNotPassedException,
|
||||
IncorrectNumberOfHandlerArgsException)
|
||||
@@ -25,7 +26,6 @@ class Router:
|
||||
|
||||
|
||||
def command(self, command: Command) -> Callable[[Any], Any]:
|
||||
command.validate_commands_params()
|
||||
self._validate_command(command)
|
||||
|
||||
def command_decorator(func):
|
||||
@@ -47,10 +47,10 @@ class Router:
|
||||
|
||||
|
||||
def input_command_handler(self, input_command: Command):
|
||||
input_command_name: str = input_command.get_string_entity()
|
||||
input_command_name: str = input_command.get_trigger()
|
||||
input_command_flags: FlagsGroup = input_command.get_input_flags()
|
||||
for command_entity in self._command_entities:
|
||||
if input_command_name.lower() == command_entity['command'].get_string_entity().lower():
|
||||
if input_command_name.lower() == command_entity['command'].get_trigger().lower():
|
||||
if command_entity['command'].get_registered_flags():
|
||||
if input_command_flags:
|
||||
for flag in input_command_flags:
|
||||
@@ -70,7 +70,7 @@ class Router:
|
||||
|
||||
|
||||
def _validate_command(self, command: Command):
|
||||
command_name: str = command.get_string_entity()
|
||||
command_name: str = command.get_trigger()
|
||||
if command_name in self.get_all_commands():
|
||||
raise RepeatedCommandException()
|
||||
if self._ignore_command_register:
|
||||
@@ -116,6 +116,6 @@ class Router:
|
||||
def get_all_commands(self) -> list[str]:
|
||||
all_commands: list[str] = []
|
||||
for command_entity in self._command_entities:
|
||||
all_commands.append(command_entity['command'].get_string_entity())
|
||||
all_commands.append(command_entity['command'].get_trigger())
|
||||
|
||||
return all_commands
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
class InvalidDescriptionInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Description Instance"
|
||||
|
||||
|
||||
class RepeatedCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Commands in handler cannot be repeated"
|
||||
|
||||
Reference in New Issue
Block a user