mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
refactor, new model e.t.c.
This commit is contained in:
@@ -56,7 +56,7 @@ if __name__ == '__main__':
|
|||||||
import re
|
import re
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.command.flag import FlagsGroup, Flag
|
from argenta.command.flag.registered_flag import FlagsGroup, Flag
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from argenta.app.exceptions import (InvalidRouterInstanceException,
|
|||||||
NoRegisteredRoutersException,
|
NoRegisteredRoutersException,
|
||||||
NoRegisteredHandlersException,
|
NoRegisteredHandlersException,
|
||||||
IncorrectNumberOfHandlerArgsException)
|
IncorrectNumberOfHandlerArgsException)
|
||||||
from argenta.app.models import RegisteredRouters
|
from argenta.app.registered_routers.entity import RegisteredRouters
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
@@ -219,7 +219,6 @@ class App:
|
|||||||
@system_router.command(Command(self.exit_command, self.exit_command_description))
|
@system_router.command(Command(self.exit_command, self.exit_command_description))
|
||||||
def exit_command():
|
def exit_command():
|
||||||
self._exit_command_handler()
|
self._exit_command_handler()
|
||||||
|
|
||||||
if system_router not in self._registered_routers.get_registered_routers():
|
if system_router not in self._registered_routers.get_registered_routers():
|
||||||
self.include_router(system_router)
|
self.include_router(system_router)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from argenta.command.flag.entity import Flag
|
from argenta.command.flag.registered_flag.entity import Flag
|
||||||
|
from argenta.command.flag.input_flag.entity import InputFlag
|
||||||
from argenta.command.flag.flags_group import FlagsGroup
|
from argenta.command.flag.flags_group import FlagsGroup
|
||||||
from .exceptions import (UnprocessedInputFlagException,
|
from .exceptions import (UnprocessedInputFlagException,
|
||||||
RepeatedInputFlagsException,
|
RepeatedInputFlagsException,
|
||||||
@@ -32,7 +33,7 @@ class Command(Generic[CommandType]):
|
|||||||
return self._registered_flags
|
return self._registered_flags
|
||||||
|
|
||||||
|
|
||||||
def validate_input_flag(self, flag: Flag):
|
def validate_input_flag(self, flag: InputFlag):
|
||||||
registered_flags: FlagsGroup | None = self.get_registered_flags()
|
registered_flags: FlagsGroup | None = self.get_registered_flags()
|
||||||
if registered_flags:
|
if registered_flags:
|
||||||
if isinstance(registered_flags, Flag):
|
if isinstance(registered_flags, Flag):
|
||||||
@@ -85,7 +86,7 @@ class Command(Generic[CommandType]):
|
|||||||
flag_prefix_last_symbol_index = current_flag_name.rfind('-')
|
flag_prefix_last_symbol_index = current_flag_name.rfind('-')
|
||||||
flag_prefix = current_flag_name[:flag_prefix_last_symbol_index+1]
|
flag_prefix = current_flag_name[:flag_prefix_last_symbol_index+1]
|
||||||
flag_name = current_flag_name[flag_prefix_last_symbol_index+1:]
|
flag_name = current_flag_name[flag_prefix_last_symbol_index+1:]
|
||||||
input_flag = Flag(flag_name=flag_name,
|
input_flag = InputFlag(flag_name=flag_name,
|
||||||
flag_prefix=cast(Literal['-', '--', '---'], flag_prefix))
|
flag_prefix=cast(Literal['-', '--', '---'], flag_prefix))
|
||||||
input_flag.set_value(current_flag_value)
|
input_flag.set_value(current_flag_value)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from argenta.command.flag.entity import Flag
|
from argenta.command.flag.input_flag.entity import InputFlag
|
||||||
|
from argenta.command.flag.registered_flag.entity import Flag
|
||||||
|
|
||||||
|
|
||||||
class UnprocessedInputFlagException(Exception):
|
class UnprocessedInputFlagException(Exception):
|
||||||
@@ -7,7 +8,7 @@ class UnprocessedInputFlagException(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class RepeatedInputFlagsException(Exception):
|
class RepeatedInputFlagsException(Exception):
|
||||||
def __init__(self, flag: Flag):
|
def __init__(self, flag: Flag | InputFlag):
|
||||||
self.flag = flag
|
self.flag = flag
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ("Repeated Input Flags\n"
|
return ("Repeated Input Flags\n"
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
__all__ = ["Flag", "FlagsGroup"]
|
|
||||||
|
|
||||||
|
|
||||||
from .entity import Flag
|
|
||||||
from .flags_group.entity import FlagsGroup
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from typing import Literal, Pattern
|
from typing import Literal, Pattern
|
||||||
|
|
||||||
|
|
||||||
class Flag:
|
class BaseFlag:
|
||||||
def __init__(self, flag_name: str,
|
def __init__(self, flag_name: str,
|
||||||
flag_prefix: Literal['-', '--', '---'] = '--',
|
flag_prefix: Literal['-', '--', '---'] = '--',
|
||||||
possible_flag_values: list[str] | Pattern[str] | False = True):
|
possible_flag_values: list[str] | Pattern[str] | False = True):
|
||||||
@@ -9,8 +9,6 @@ class Flag:
|
|||||||
self._flag_prefix = flag_prefix
|
self._flag_prefix = flag_prefix
|
||||||
self.possible_flag_values = possible_flag_values
|
self.possible_flag_values = possible_flag_values
|
||||||
|
|
||||||
self._flag_value = None
|
|
||||||
|
|
||||||
def get_string_entity(self):
|
def get_string_entity(self):
|
||||||
string_entity: str = self._flag_prefix + self._flag_name
|
string_entity: str = self._flag_prefix + self._flag_name
|
||||||
return string_entity
|
return string_entity
|
||||||
@@ -21,12 +19,6 @@ class Flag:
|
|||||||
def get_flag_prefix(self):
|
def get_flag_prefix(self):
|
||||||
return self._flag_prefix
|
return self._flag_prefix
|
||||||
|
|
||||||
def get_value(self):
|
|
||||||
return self._flag_value
|
|
||||||
|
|
||||||
def set_value(self, value):
|
|
||||||
self._flag_value = value
|
|
||||||
|
|
||||||
def validate_input_flag_value(self, input_flag_value: str | None):
|
def validate_input_flag_value(self, input_flag_value: str | None):
|
||||||
if self.possible_flag_values is False:
|
if self.possible_flag_values is False:
|
||||||
if input_flag_value is None:
|
if input_flag_value is None:
|
||||||
@@ -1,22 +1,23 @@
|
|||||||
from argenta.command.flag import Flag
|
from argenta.command.flag.input_flag.entity import InputFlag
|
||||||
|
from argenta.command.flag.registered_flag import Flag
|
||||||
|
|
||||||
|
|
||||||
class FlagsGroup:
|
class FlagsGroup:
|
||||||
def __init__(self, *flags: Flag):
|
def __init__(self, *flags: Flag | InputFlag):
|
||||||
self._flags: list[Flag] = [] if not flags else flags
|
self._flags: list[Flag | InputFlag] = [] if not flags else flags
|
||||||
|
|
||||||
def get_flags(self) -> list[Flag]:
|
def get_flags(self) -> list[Flag | InputFlag]:
|
||||||
return self._flags
|
return self._flags
|
||||||
|
|
||||||
def add_flag(self, flag: Flag):
|
def add_flag(self, flag: Flag | InputFlag):
|
||||||
self._flags.append(flag)
|
self._flags.append(flag)
|
||||||
|
|
||||||
def add_flags(self, flags: list[Flag]):
|
def add_flags(self, flags: list[Flag | InputFlag]):
|
||||||
self._flags.extend(flags)
|
self._flags.extend(flags)
|
||||||
|
|
||||||
def unparse_to_dict(self):
|
def unparse_to_dict(self):
|
||||||
result_dict: dict[str, dict] = {}
|
result_dict: dict[str, dict] = {}
|
||||||
for flag in self._flags:
|
for flag in self.get_flags():
|
||||||
result_dict[flag.get_flag_name()] = {
|
result_dict[flag.get_flag_name()] = {
|
||||||
'name': flag.get_flag_name(),
|
'name': flag.get_flag_name(),
|
||||||
'string_entity': flag.get_string_entity(),
|
'string_entity': flag.get_string_entity(),
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
from re import Pattern
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
from argenta.command.flag.base_flag.entity import BaseFlag
|
||||||
|
|
||||||
|
|
||||||
|
class InputFlag(BaseFlag):
|
||||||
|
def __init__(self, flag_name: str,
|
||||||
|
flag_prefix: Literal['-', '--', '---'] = '--',
|
||||||
|
possible_flag_values: list[str] | Pattern[str] | False = True):
|
||||||
|
super().__init__(flag_name, flag_prefix, possible_flag_values)
|
||||||
|
self._flag_value = None
|
||||||
|
|
||||||
|
def get_value(self):
|
||||||
|
return self._flag_value
|
||||||
|
|
||||||
|
def set_value(self, value):
|
||||||
|
self._flag_value = value
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
__all__ = ["Flag", "FlagsGroup"]
|
||||||
|
|
||||||
|
|
||||||
|
from .entity import Flag
|
||||||
|
from argenta.command.flag.flags_group import FlagsGroup
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from argenta.command.flag import Flag
|
from argenta.command.flag.registered_flag import Flag
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from argenta.command.flag.base_flag.entity import BaseFlag
|
||||||
|
|
||||||
|
|
||||||
|
class Flag(BaseFlag):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
from typing import Callable
|
||||||
|
from argenta.command import Command
|
||||||
|
|
||||||
|
|
||||||
|
class CommandHandler:
|
||||||
|
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
||||||
|
self.handler = handler
|
||||||
|
self.handled_command = handled_command
|
||||||
|
|
||||||
|
def handling(self, input_flags: dict = None):
|
||||||
|
if input_flags is not None:
|
||||||
|
self.handler(input_flags)
|
||||||
|
else:
|
||||||
|
self.handler()
|
||||||
|
|
||||||
|
def get_handler(self):
|
||||||
|
return self.handler
|
||||||
|
|
||||||
|
def get_handled_command(self):
|
||||||
|
return self.handled_command
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
from argenta.router.command_handler.entity import CommandHandler
|
||||||
|
|
||||||
|
|
||||||
|
class CommandHandlers:
|
||||||
|
def __init__(self, command_handlers: list[CommandHandler] = None):
|
||||||
|
self.command_handlers = command_handlers if command_handlers else []
|
||||||
|
|
||||||
|
def get_command_handlers(self) -> list[CommandHandler]:
|
||||||
|
return self.command_handlers
|
||||||
|
|
||||||
|
def add_command_handler(self, command_handler: CommandHandler):
|
||||||
|
self.command_handlers.append(command_handler)
|
||||||
|
|
||||||
|
def add_command_handlers(self, *command_handlers: CommandHandler):
|
||||||
|
self.command_handlers.extend(command_handlers)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.command_handlers)
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
return next(iter(self.command_handlers))
|
||||||
+12
-13
@@ -2,10 +2,10 @@ from typing import Callable, Any
|
|||||||
from inspect import getfullargspec
|
from inspect import getfullargspec
|
||||||
|
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.router.models import CommandHandler, CommandHandlers
|
from argenta.router.command_handlers.entity import CommandHandlers
|
||||||
from argenta.command.flag import Flag, FlagsGroup
|
from argenta.router.command_handler.entity import CommandHandler
|
||||||
from argenta.router.exceptions import (RepeatedCommandException,
|
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||||
RepeatedFlagNameException,
|
from argenta.router.exceptions import (RepeatedFlagNameException,
|
||||||
TooManyTransferredArgsException,
|
TooManyTransferredArgsException,
|
||||||
RequiredArgumentNotPassedException,
|
RequiredArgumentNotPassedException,
|
||||||
IncorrectNumberOfHandlerArgsException,
|
IncorrectNumberOfHandlerArgsException,
|
||||||
@@ -61,26 +61,25 @@ class Router:
|
|||||||
if not is_valid:
|
if not is_valid:
|
||||||
self._not_valid_flag_handler(flag)
|
self._not_valid_flag_handler(flag)
|
||||||
return
|
return
|
||||||
return command_handler.handling(input_command_flags.unparse_to_dict())
|
command_handler.handling(input_command_flags.unparse_to_dict())
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
return command_handler.handling({})
|
command_handler.handling({})
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
if input_command_flags:
|
if input_command_flags:
|
||||||
self._not_valid_flag_handler(input_command_flags[0])
|
self._not_valid_flag_handler(input_command_flags[0])
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
return command_handler.handling()
|
command_handler.handling()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def _validate_command(self, command: Command):
|
@staticmethod
|
||||||
|
def _validate_command(command: Command):
|
||||||
command_name: str = command.get_trigger()
|
command_name: str = command.get_trigger()
|
||||||
if command_name.find(' ') != -1:
|
if command_name.find(' ') != -1:
|
||||||
raise TriggerCannotContainSpacesException()
|
raise TriggerCannotContainSpacesException()
|
||||||
if command_name in self.get_all_commands():
|
|
||||||
raise RepeatedCommandException()
|
|
||||||
if self._ignore_command_register:
|
|
||||||
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
|
||||||
raise RepeatedCommandException()
|
|
||||||
|
|
||||||
flags: FlagsGroup = command.get_registered_flags()
|
flags: FlagsGroup = command.get_registered_flags()
|
||||||
if flags:
|
if flags:
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
class RepeatedCommandException(Exception):
|
|
||||||
def __str__(self):
|
|
||||||
return "Commands in handler cannot be repeated"
|
|
||||||
|
|
||||||
|
|
||||||
class RepeatedFlagNameException(Exception):
|
class RepeatedFlagNameException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Repeated flag name in register command"
|
return "Repeated registered_flag name in register command"
|
||||||
|
|
||||||
|
|
||||||
class TooManyTransferredArgsException(Exception):
|
class TooManyTransferredArgsException(Exception):
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
from typing import Callable
|
|
||||||
from argenta.command import Command
|
|
||||||
|
|
||||||
|
|
||||||
class CommandHandler:
|
|
||||||
def __init__(self, handler: Callable[[], None] | Callable[[dict], None], handled_command: Command):
|
|
||||||
self.handler = handler
|
|
||||||
self.handled_command = handled_command
|
|
||||||
|
|
||||||
def handling(self, input_flags: dict = None):
|
|
||||||
if input_flags is not None:
|
|
||||||
self.handler(input_flags)
|
|
||||||
else:
|
|
||||||
self.handler()
|
|
||||||
|
|
||||||
def get_handler(self):
|
|
||||||
return self.handler
|
|
||||||
|
|
||||||
def get_handled_command(self):
|
|
||||||
return self.handled_command
|
|
||||||
|
|
||||||
|
|
||||||
class CommandHandlers:
|
|
||||||
def __init__(self, command_handlers: list[CommandHandler] = None):
|
|
||||||
self.command_handlers = command_handlers if command_handlers else []
|
|
||||||
|
|
||||||
def get_command_handlers(self) -> list[CommandHandler]:
|
|
||||||
return self.command_handlers
|
|
||||||
|
|
||||||
def add_command_handler(self, command_handler: CommandHandler):
|
|
||||||
self.command_handlers.append(command_handler)
|
|
||||||
|
|
||||||
def add_command_handlers(self, *command_handlers: CommandHandler):
|
|
||||||
self.command_handlers.extend(command_handlers)
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return iter(self.command_handlers)
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
return next(iter(self.command_handlers))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+15
-4
@@ -1,7 +1,18 @@
|
|||||||
import re
|
from argenta.app import App
|
||||||
|
from argenta.command import Command
|
||||||
|
from argenta.router import Router
|
||||||
|
|
||||||
|
|
||||||
def test(string):
|
router = Router()
|
||||||
return bool(re.match(r'\ntest command\n(.|\s)*\nsome command\n', string))
|
|
||||||
|
|
||||||
print(test('test command tpgm4tigm4tigmt\n i0hhmi6h some command'))
|
@router.command(Command('test'))
|
||||||
|
def test():
|
||||||
|
print(f'test command')
|
||||||
|
|
||||||
|
@router.command(Command('some'))
|
||||||
|
def test2():
|
||||||
|
print(f'some command')
|
||||||
|
|
||||||
|
app = App()
|
||||||
|
app.include_router(router)
|
||||||
|
app.start_polling()
|
||||||
@@ -2,10 +2,9 @@ from pprint import pprint
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.command.flag import FlagsGroup
|
from argenta.command.flag.registered_flag import FlagsGroup
|
||||||
from argenta.command.flag.defaults import DefaultFlags
|
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.router.defaults import system_router
|
|
||||||
from .handlers_implementation.help_command import help_command
|
from .handlers_implementation.help_command import help_command
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import re
|
|||||||
from argenta.app import App
|
from argenta.app import App
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.command.flag import FlagsGroup
|
from argenta.command.flag.registered_flag import FlagsGroup
|
||||||
from argenta.command.flag.defaults import DefaultFlags
|
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nUndefined or incorrect input flag: --help\n', output)
|
self.assertIn('\nUndefined or incorrect input registered_flag: --help\n', output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
||||||
@@ -82,14 +82,14 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nUndefined or incorrect input flag: --port 22\n', output)
|
self.assertIn('\nUndefined or incorrect input registered_flag: --port 22\n', output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test --host 192.168.32.1 --port 132", "q"])
|
@patch("builtins.input", side_effect=["test --host 192.168.32.1 --port 132", "q"])
|
||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
flags = FlagsGroup(DefaultFlags.host_flag)
|
flags = FlagsGroup(DefaultFlags.HOST)
|
||||||
|
|
||||||
@router.command(Command('test', flags=flags))
|
@router.command(Command('test', flags=flags))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
@@ -101,7 +101,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nUndefined or incorrect input flag: --port 132\n', output)
|
self.assertIn('\nUndefined or incorrect input registered_flag: --port 132\n', output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test", "some", "q"])
|
@patch("builtins.input", side_effect=["test", "some", "q"])
|
||||||
@@ -159,7 +159,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn("\nIncorrect flag syntax: \"test 535 --port\"\n", output)
|
self.assertIn("\nIncorrect registered_flag syntax: \"test 535 --port\"\n", output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["", "q"])
|
@patch("builtins.input", side_effect=["", "q"])
|
||||||
@@ -185,7 +185,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
def test_input_correct_command_with_repeated_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_repeated_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
@router.command(Command('test', flags=DefaultFlags.port_flag))
|
@router.command(Command('test', flags=DefaultFlags.PORT))
|
||||||
def test(args):
|
def test(args):
|
||||||
print('test command')
|
print('test command')
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import re
|
|||||||
from argenta.app import App
|
from argenta.app import App
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.command.flag import Flag, FlagsGroup
|
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||||
from argenta.command.flag.defaults import DefaultFlags
|
from argenta.command.flag.registered_flag.defaults import DefaultFlags
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
@router.command(Command('test', flags=flag))
|
@router.command(Command('test', flags=flag))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
print(f'\nhelp for {args['help']['name']} flag\n')
|
print(f'\nhelp for {args['help']['name']} registered_flag\n')
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
@@ -65,7 +65,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nhelp for help flag\n', output)
|
self.assertIn('\nhelp for help registered_flag\n', output)
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
@patch("builtins.input", side_effect=["test --port 22", "q"])
|
||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
@@ -75,7 +75,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
@router.command(Command('test', flags=flag))
|
@router.command(Command('test', flags=flag))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
print(f'flag value for {args['port']['name']} flag : {args["port"]["value"]}')
|
print(f'registered_flag value for {args['port']['name']} registered_flag : {args["port"]["value"]}')
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
@@ -83,18 +83,18 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nflag value for port flag : 22\n', output)
|
self.assertIn('\nregistered_flag value for port registered_flag : 22\n', output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test -h", "q"])
|
@patch("builtins.input", side_effect=["test -h", "q"])
|
||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
def test_input_correct_command_with_default_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_default_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
flag = DefaultFlags.short_help_flag
|
flag = DefaultFlags.SHORT_HELP
|
||||||
|
|
||||||
@router.command(Command('test', flags=flag))
|
@router.command(Command('test', flags=flag))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
print(f'help for {args['h']['name']} flag')
|
print(f'help for {args['h']['name']} registered_flag')
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
@@ -102,14 +102,14 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
|
|
||||||
output = mock_stdout.getvalue()
|
output = mock_stdout.getvalue()
|
||||||
|
|
||||||
self.assertIn('\nhelp for h flag\n', output)
|
self.assertIn('\nhelp for h registered_flag\n', output)
|
||||||
|
|
||||||
|
|
||||||
@patch("builtins.input", side_effect=["test --info", "q"])
|
@patch("builtins.input", side_effect=["test --info", "q"])
|
||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
def test_input_correct_command_with_default_flag2(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_default_flag2(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
flag = DefaultFlags.info_flag
|
flag = DefaultFlags.INFO
|
||||||
|
|
||||||
@router.command(Command('test', flags=flag))
|
@router.command(Command('test', flags=flag))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
@@ -129,7 +129,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
def test_input_correct_command_with_default_flag3(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_default_flag3(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
flag = DefaultFlags.host_flag
|
flag = DefaultFlags.HOST
|
||||||
|
|
||||||
@router.command(Command('test', flags=flag))
|
@router.command(Command('test', flags=flag))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
@@ -148,7 +148,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
@patch("sys.stdout", new_callable=io.StringIO)
|
@patch("sys.stdout", new_callable=io.StringIO)
|
||||||
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
|
||||||
router = Router()
|
router = Router()
|
||||||
flags = FlagsGroup(DefaultFlags.host_flag, DefaultFlags.port_flag)
|
flags = FlagsGroup(DefaultFlags.HOST, DefaultFlags.PORT)
|
||||||
|
|
||||||
@router.command(Command('test', flags=flags))
|
@router.command(Command('test', flags=flags))
|
||||||
def test(args: dict):
|
def test(args: dict):
|
||||||
@@ -173,7 +173,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
|
|||||||
print(f'test command')
|
print(f'test command')
|
||||||
|
|
||||||
@router.command(Command('some'))
|
@router.command(Command('some'))
|
||||||
def test():
|
def test2():
|
||||||
print(f'some command')
|
print(f'some command')
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from argenta.app import App
|
from argenta.app import App
|
||||||
from argenta.app.exceptions import (InvalidDescriptionMessagePatternException,
|
from argenta.app.exceptions import InvalidDescriptionMessagePatternException
|
||||||
NoRegisteredRoutersException)
|
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@@ -14,7 +13,3 @@ class TestApp(unittest.TestCase):
|
|||||||
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||||
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
|
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
|
||||||
|
|
||||||
def test_no_registered_router(self):
|
|
||||||
with self.assertRaises(NoRegisteredRoutersException):
|
|
||||||
App()._validate_number_of_routers()
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from argenta.command.flag import Flag
|
from argenta.command.flag.registered_flag import Flag
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import re
|
import re
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from argenta.command.flag import Flag, FlagsGroup
|
from argenta.command.flag.registered_flag import Flag, FlagsGroup
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
from argenta.command.flag import FlagsGroup, Flag
|
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.command import Command
|
from argenta.command import Command
|
||||||
from argenta.router.exceptions import RepeatedCommandException, TriggerCannotContainSpacesException
|
from argenta.router.exceptions import TriggerCannotContainSpacesException
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@@ -13,102 +12,6 @@ class TestRouter(unittest.TestCase):
|
|||||||
def test_get_router_title(self):
|
def test_get_router_title(self):
|
||||||
self.assertEqual(Router(title='test title').get_title(), 'test title')
|
self.assertEqual(Router(title='test title').get_title(), 'test title')
|
||||||
|
|
||||||
def test_input_correct_command(self):
|
|
||||||
router = Router()
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
|
|
||||||
|
|
||||||
def test_input_command_with_invalid_flag(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_invalid_input_flag_handler(lambda x: x)
|
|
||||||
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
input_command = Command(trigger='test')
|
|
||||||
input_command._set_input_flags(FlagsGroup([Flag('host')]))
|
|
||||||
|
|
||||||
self.assertEqual(router.input_command_handler(input_command), None)
|
|
||||||
|
|
||||||
def test_input_correct_command_with_one_register_and_ignore_command_register(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_ignore_command_register(True)
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
|
|
||||||
|
|
||||||
def test_input_correct_command_with_different_register_and_ignore_command_register(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_ignore_command_register(True)
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
self.assertEqual(router.input_command_handler(Command(trigger='TeSt')), 'correct result')
|
|
||||||
|
|
||||||
def test_input_incorrect_command_with_ignore_command_register(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_ignore_command_register(True)
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
self.assertEqual(router.input_command_handler(Command(trigger='Test2')), None)
|
|
||||||
|
|
||||||
def test_register_repeated_commands_with_one_register(self):
|
|
||||||
router = Router()
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
with self.assertRaises(RepeatedCommandException):
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
def test_register_commands_with_different_register(self):
|
|
||||||
router = Router()
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
try:
|
|
||||||
@router.command(Command(trigger='Test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
except RepeatedCommandException:
|
|
||||||
self.fail('RepeatedCommandException should not have been thrown')
|
|
||||||
|
|
||||||
def test_register_repeated_commands_with_one_register_and_set_ignore_command_register(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_ignore_command_register(True)
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
with self.assertRaises(RepeatedCommandException):
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
def test_register_repeated_commands_with_different_register_and_set_ignore_command_register(self):
|
|
||||||
router = Router()
|
|
||||||
router.set_ignore_command_register(True)
|
|
||||||
@router.command(Command(trigger='test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
with self.assertRaises(RepeatedCommandException):
|
|
||||||
@router.command(Command(trigger='Test'))
|
|
||||||
def test():
|
|
||||||
return 'correct result'
|
|
||||||
|
|
||||||
def test_register_command_with_spaces_in_trigger(self):
|
def test_register_command_with_spaces_in_trigger(self):
|
||||||
router = Router()
|
router = Router()
|
||||||
with self.assertRaises(TriggerCannotContainSpacesException):
|
with self.assertRaises(TriggerCannotContainSpacesException):
|
||||||
|
|||||||
Reference in New Issue
Block a user