mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
start make tests
This commit is contained in:
@@ -1,17 +1,18 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
from inspect import getfullargspec
|
from inspect import getfullargspec
|
||||||
|
import re
|
||||||
|
|
||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
from ..router.entity import Router
|
from ..router.entity import Router
|
||||||
from ..command.exceptions import (UnprocessedInputFlagException,
|
from ..command.exceptions import (UnprocessedInputFlagException,
|
||||||
IncorrectNumberOfHandlerArgsException,
|
|
||||||
RepeatedInputFlagsException,
|
RepeatedInputFlagsException,
|
||||||
EmptyInputCommandException)
|
EmptyInputCommandException)
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
NoRegisteredRoutersException,
|
NoRegisteredRoutersException,
|
||||||
NoRegisteredHandlersException,
|
NoRegisteredHandlersException,
|
||||||
RepeatedCommandInDifferentRoutersException)
|
RepeatedCommandInDifferentRoutersException,
|
||||||
|
IncorrectNumberOfHandlerArgsException)
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
@@ -129,9 +130,11 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def set_description_message_pattern(self, pattern: str) -> None:
|
def set_description_message_pattern(self, pattern: str) -> None:
|
||||||
try:
|
first_check = re.match(r'.*{commmand}.*', pattern)
|
||||||
pattern.format(command='command', description='description')
|
second_check = re.match(r'.*{description}.*', pattern)
|
||||||
except KeyError:
|
print(first_check)
|
||||||
|
print(second_check)
|
||||||
|
if bool(first_check) and bool(second_check):
|
||||||
raise InvalidDescriptionMessagePatternException(pattern)
|
raise InvalidDescriptionMessagePatternException(pattern)
|
||||||
else:
|
else:
|
||||||
self._description_message_pattern: str = pattern
|
self._description_message_pattern: str = pattern
|
||||||
|
|||||||
@@ -28,3 +28,8 @@ class NoRegisteredHandlersException(Exception):
|
|||||||
class RepeatedCommandInDifferentRoutersException(Exception):
|
class RepeatedCommandInDifferentRoutersException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Commands in different handlers cannot be repeated"
|
return "Commands in different handlers cannot be repeated"
|
||||||
|
|
||||||
|
|
||||||
|
class IncorrectNumberOfHandlerArgsException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Incorrect Input Flags Handler has incorrect number of arguments"
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
from .entity import Command
|
||||||
@@ -19,7 +19,7 @@ class Command(Generic[T]):
|
|||||||
flags: Flag | FlagsGroup | None = None):
|
flags: Flag | FlagsGroup | None = None):
|
||||||
self._command = command
|
self._command = command
|
||||||
self._description = description
|
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
|
self._input_flags: FlagsGroup | None = None
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ class Command(Generic[T]):
|
|||||||
|
|
||||||
|
|
||||||
def get_registered_flags(self):
|
def get_registered_flags(self):
|
||||||
return self._flags
|
return self._registered_flags
|
||||||
|
|
||||||
|
|
||||||
def validate_commands_params(self):
|
def validate_commands_params(self):
|
||||||
@@ -45,7 +45,7 @@ class Command(Generic[T]):
|
|||||||
raise InvalidCommandInstanceException(self._command)
|
raise InvalidCommandInstanceException(self._command)
|
||||||
if not isinstance(self._description, str):
|
if not isinstance(self._description, str):
|
||||||
raise InvalidDescriptionInstanceException()
|
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
|
raise InvalidFlagsInstanceException
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,6 @@ class RepeatedInputFlagsException(Exception):
|
|||||||
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
|
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):
|
class EmptyInputCommandException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Input Command is empty"
|
return "Input Command is empty"
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
from .entity import Flag
|
||||||
|
from .flags_group.entity import FlagsGroup
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
from inspect import getfullargspec
|
from inspect import getfullargspec
|
||||||
|
|
||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
|
from ..command.params.flag.entity import Flag
|
||||||
from ..command.params.flag.flags_group.entity import FlagsGroup
|
from ..command.params.flag.flags_group.entity import FlagsGroup
|
||||||
from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
|
from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
|
||||||
TooManyTransferredArgsException,
|
TooManyTransferredArgsException,
|
||||||
RequiredArgumentNotPassedException,
|
RequiredArgumentNotPassedException,
|
||||||
NotValidInputFlagHandlerHasBeenAlreadyCreatedException,
|
|
||||||
IncorrectNumberOfHandlerArgsException)
|
IncorrectNumberOfHandlerArgsException)
|
||||||
|
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ class Router:
|
|||||||
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
||||||
self._ignore_command_register: bool = False
|
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]:
|
def command(self, command: Command) -> Callable[[Any], Any]:
|
||||||
@@ -37,19 +38,12 @@ class Router:
|
|||||||
|
|
||||||
return command_decorator
|
return command_decorator
|
||||||
|
|
||||||
def not_valid_input_flag(self, func):
|
def set_invalid_input_flag_handler(self, func):
|
||||||
if self._not_valid_flag_handler:
|
|
||||||
raise NotValidInputFlagHandlerHasBeenAlreadyCreatedException()
|
|
||||||
else:
|
|
||||||
processed_args = getfullargspec(func).args
|
processed_args = getfullargspec(func).args
|
||||||
if len(processed_args) != 1:
|
if len(processed_args) != 1:
|
||||||
raise IncorrectNumberOfHandlerArgsException()
|
raise IncorrectNumberOfHandlerArgsException()
|
||||||
else:
|
else:
|
||||||
self._not_valid_flag_handler = func
|
self._not_valid_flag_handler = func
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def input_command_handler(self, input_command: Command):
|
def input_command_handler(self, input_command: Command):
|
||||||
@@ -62,20 +56,14 @@ class Router:
|
|||||||
for flag in input_command_flags:
|
for flag in input_command_flags:
|
||||||
is_valid = command_entity['command'].validate_input_flag(flag)
|
is_valid = command_entity['command'].validate_input_flag(flag)
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
if self._not_valid_flag_handler:
|
self._not_valid_flag_handler(flag)
|
||||||
self._not_valid_flag_handler(input_command)
|
|
||||||
else:
|
|
||||||
print(f"Undefined or incorrect input flag: '{flag.get_string_entity()} {flag.get_value()}'")
|
|
||||||
return
|
return
|
||||||
return command_entity['handler_func'](input_command_flags)
|
return command_entity['handler_func'](input_command_flags)
|
||||||
else:
|
else:
|
||||||
return command_entity['handler_func'](FlagsGroup(None))
|
return command_entity['handler_func'](FlagsGroup(None))
|
||||||
else:
|
else:
|
||||||
if input_command_flags:
|
if input_command_flags:
|
||||||
if self._not_valid_flag_handler:
|
self._not_valid_flag_handler(input_command_flags[0])
|
||||||
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()}'")
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
return command_entity['handler_func']()
|
return command_entity['handler_func']()
|
||||||
@@ -125,28 +113,9 @@ class Router:
|
|||||||
return self.title
|
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]:
|
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'].get_string_entity())
|
all_commands.append(command_entity['command'].get_string_entity())
|
||||||
|
|
||||||
return all_commands
|
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
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from argenta.command.params.flag.flags_group.entity import FlagsGroup
|
|||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
|
|
||||||
|
|
||||||
work_router: Router = Router(title='Work points:')
|
work_router: Router = Router(title='Work nts:')
|
||||||
settings_router: Router = Router(title='Settings points:')
|
settings_router: Router = Router(title='Settings points:')
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
@@ -46,9 +46,11 @@ def command_update():
|
|||||||
# upgrade_command()
|
# upgrade_command()
|
||||||
|
|
||||||
|
|
||||||
@work_router.not_valid_input_flag
|
def invalid_input_flag(flag: Flag):
|
||||||
def invalid_input_flag(command: Command):
|
print(f'Invalid inpuuuuuuuuuuuuuuuuuuuuuuuut flag: "{flag.get_string_entity()} {flag.get_value()}"')
|
||||||
print(f'Invalid inpuuuuuuuuuuuuuuuuuuuuuuuut flag: "{command.get_input_flags()[0].get_value()}"')
|
|
||||||
|
|
||||||
|
work_router.set_invalid_input_flag_handler(invalid_input_flag)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
from tests.mock_app.handlers.routers import work_router, settings_router
|
from tests.mock_app.handlers.routers import work_router, settings_router
|
||||||
from argenta.app.entity import App
|
|
||||||
from art import text2art
|
from art import text2art
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
|
from argenta.app import App
|
||||||
|
from argenta.router import Router
|
||||||
|
from argenta.command import Command
|
||||||
|
from argenta.command.params.flag import Flag, FlagsGroup
|
||||||
|
|
||||||
|
|
||||||
app: App = App(prompt='[italic white bold]What do you want to do(enter number of action)?',
|
app: App = App(prompt='[italic white bold]What do you want to do(enter number of action)?',
|
||||||
line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n',
|
line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n',
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def set_description_message_pattern(pattern: str) -> None:
|
||||||
|
first_check = re.fullmatch(r'.*{commmand}.*', pattern)
|
||||||
|
second_check = re.fullmatch(r'.*{description}.*', pattern)
|
||||||
|
print(bool(first_check))
|
||||||
|
print(second_check)
|
||||||
|
|
||||||
|
|
||||||
|
set_description_message_pattern('Invalid des{command}cription pattern')
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
class Entity:
|
|
||||||
pass
|
|
||||||
|
|
||||||
class Person(Entity):
|
|
||||||
pass
|
|
||||||
|
|
||||||
a: Entity = Entity()
|
|
||||||
print(a)
|
|
||||||
a = Person()
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
from argenta.app import App
|
||||||
|
from argenta.app.exceptions import (InvalidDescriptionMessagePatternException,
|
||||||
|
NoRegisteredRoutersException)
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class TestApp(unittest.TestCase):
|
||||||
|
def test_set_invalid_description_message_pattern(self):
|
||||||
|
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||||
|
App().set_description_message_pattern('Invalid des{}cription pattern')
|
||||||
|
|
||||||
|
def test_no_registered_router(self):
|
||||||
|
with self.assertRaises(NoRegisteredRoutersException):
|
||||||
|
App()._validate_number_of_routers()
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
from argenta.command import Command
|
||||||
|
from argenta.command.params.flag import Flag, FlagsGroup
|
||||||
|
from argenta.command.exceptions import (UnprocessedInputFlagException,
|
||||||
|
RepeatedInputFlagsException,
|
||||||
|
EmptyInputCommandException)
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class TestCommand(unittest.TestCase):
|
||||||
|
def test_parse_correct_raw_command(self):
|
||||||
|
self.assertEqual(Command.parse_input_command('ssh --host 192.168.0.3').get_string_entity(), 'ssh')
|
||||||
|
|
||||||
|
def test_parse_raw_command_with_flag_name_without_value(self):
|
||||||
|
with self.assertRaises(UnprocessedInputFlagException):
|
||||||
|
Command.parse_input_command('ssh --host')
|
||||||
|
|
||||||
|
def test_parse_raw_command_without_flag_name_with_value(self):
|
||||||
|
with self.assertRaises(UnprocessedInputFlagException):
|
||||||
|
Command.parse_input_command('ssh 192.168.0.3')
|
||||||
|
|
||||||
|
def test_parse_raw_command_with_repeated_flag_name(self):
|
||||||
|
with self.assertRaises(RepeatedInputFlagsException):
|
||||||
|
Command.parse_input_command('ssh --host 192.168.0.3 --host 172.198.0.43')
|
||||||
|
|
||||||
|
def test_parse_empty_raw_command(self):
|
||||||
|
with self.assertRaises(EmptyInputCommandException):
|
||||||
|
Command.parse_input_command('')
|
||||||
|
|
||||||
|
def test_get_command_description(self):
|
||||||
|
self.assertEqual(Command(command='test', description='test description').get_description(), 'test description')
|
||||||
|
|
||||||
Reference in New Issue
Block a user