start make tests

This commit is contained in:
2025-03-03 00:10:07 +03:00
parent 5e6cdc342e
commit d9c74310c3
33 changed files with 100 additions and 69 deletions
+8 -5
View File
@@ -1,17 +1,18 @@
from typing import Callable
from inspect import getfullargspec
import re
from ..command.entity import Command
from ..router.entity import Router
from ..command.exceptions import (UnprocessedInputFlagException,
IncorrectNumberOfHandlerArgsException,
RepeatedInputFlagsException,
EmptyInputCommandException)
from .exceptions import (InvalidRouterInstanceException,
InvalidDescriptionMessagePatternException,
NoRegisteredRoutersException,
NoRegisteredHandlersException,
RepeatedCommandInDifferentRoutersException)
RepeatedCommandInDifferentRoutersException,
IncorrectNumberOfHandlerArgsException)
class App:
@@ -129,9 +130,11 @@ class App:
def set_description_message_pattern(self, pattern: str) -> None:
try:
pattern.format(command='command', description='description')
except KeyError:
first_check = re.match(r'.*{commmand}.*', pattern)
second_check = re.match(r'.*{description}.*', pattern)
print(first_check)
print(second_check)
if bool(first_check) and bool(second_check):
raise InvalidDescriptionMessagePatternException(pattern)
else:
self._description_message_pattern: str = pattern
+5
View File
@@ -28,3 +28,8 @@ class NoRegisteredHandlersException(Exception):
class RepeatedCommandInDifferentRoutersException(Exception):
def __str__(self):
return "Commands in different handlers cannot be repeated"
class IncorrectNumberOfHandlerArgsException(Exception):
def __str__(self):
return "Incorrect Input Flags Handler has incorrect number of arguments"
+1
View File
@@ -0,0 +1 @@
from .entity import Command
+3 -3
View File
@@ -19,7 +19,7 @@ class Command(Generic[T]):
flags: Flag | FlagsGroup | None = None):
self._command = command
self._description = description
self._flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
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
@@ -37,7 +37,7 @@ class Command(Generic[T]):
def get_registered_flags(self):
return self._flags
return self._registered_flags
def validate_commands_params(self):
@@ -45,7 +45,7 @@ class Command(Generic[T]):
raise InvalidCommandInstanceException(self._command)
if not isinstance(self._description, str):
raise InvalidDescriptionInstanceException()
if not any([(isinstance(self._flags, FlagsGroup)), not self._flags]):
if not any([(isinstance(self._registered_flags, FlagsGroup)), not self._registered_flags]):
raise InvalidFlagsInstanceException
-5
View File
@@ -29,11 +29,6 @@ class RepeatedInputFlagsException(Exception):
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
class IncorrectNumberOfHandlerArgsException(Exception):
def __str__(self):
return "Incorrect Input Flags Handler has incorrect number of arguments"
class EmptyInputCommandException(Exception):
def __str__(self):
return "Input Command is empty"
+2
View File
@@ -0,0 +1,2 @@
from .entity import Flag
from .flags_group.entity import FlagsGroup
+10 -41
View File
@@ -1,11 +1,12 @@
from typing import Callable, Any
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,
TooManyTransferredArgsException,
RequiredArgumentNotPassedException,
NotValidInputFlagHandlerHasBeenAlreadyCreatedException,
IncorrectNumberOfHandlerArgsException)
@@ -20,7 +21,7 @@ class Router:
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
self._ignore_command_register: bool = False
self._not_valid_flag_handler: Callable[[Command], None] | None = None
self._not_valid_flag_handler: Callable[[Flag], None] = lambda flag: print(f"Undefined or incorrect input flag: '{flag.get_string_entity()} {flag.get_value()}'")
def command(self, command: Command) -> Callable[[Any], Any]:
@@ -37,19 +38,12 @@ class Router:
return command_decorator
def not_valid_input_flag(self, func):
if self._not_valid_flag_handler:
raise NotValidInputFlagHandlerHasBeenAlreadyCreatedException()
def set_invalid_input_flag_handler(self, func):
processed_args = getfullargspec(func).args
if len(processed_args) != 1:
raise IncorrectNumberOfHandlerArgsException()
else:
processed_args = getfullargspec(func).args
if len(processed_args) != 1:
raise IncorrectNumberOfHandlerArgsException()
else:
self._not_valid_flag_handler = func
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
self._not_valid_flag_handler = func
def input_command_handler(self, input_command: Command):
@@ -62,20 +56,14 @@ class Router:
for flag in input_command_flags:
is_valid = command_entity['command'].validate_input_flag(flag)
if not is_valid:
if self._not_valid_flag_handler:
self._not_valid_flag_handler(input_command)
else:
print(f"Undefined or incorrect input flag: '{flag.get_string_entity()} {flag.get_value()}'")
self._not_valid_flag_handler(flag)
return
return command_entity['handler_func'](input_command_flags)
else:
return command_entity['handler_func'](FlagsGroup(None))
else:
if input_command_flags:
if self._not_valid_flag_handler:
self._not_valid_flag_handler(input_command)
else:
print(f"Undefined or incorrect input flag: '{input_command_flags[0].get_string_entity()} {input_command_flags[0].get_value()}'")
self._not_valid_flag_handler(input_command_flags[0])
return
else:
return command_entity['handler_func']()
@@ -125,28 +113,9 @@ class Router:
return self.title
def get_router_info(self) -> dict:
return {
'title': self.title,
'name': self.name,
'ignore_command_register': self._ignore_command_register,
'attributes': {
'command_entities': self._command_entities,
}
}
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())
return all_commands
def get_all_flags(self) -> list[FlagsGroup]:
all_flags: list[FlagsGroup] = []
for command_entity in self._command_entities:
all_flags.append(command_entity['command'].get_registered_flags())
return all_flags