mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
v0.3.5
This commit is contained in:
@@ -130,14 +130,13 @@ class App:
|
||||
|
||||
|
||||
def set_description_message_pattern(self, pattern: str) -> None:
|
||||
first_check = re.match(r'.*{commmand}.*', pattern)
|
||||
first_check = re.match(r'.*{command}.*', 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
|
||||
else:
|
||||
raise InvalidDescriptionMessagePatternException(pattern)
|
||||
|
||||
|
||||
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||
|
||||
@@ -15,10 +15,10 @@ T = TypeVar('T')
|
||||
|
||||
class Command(Generic[T]):
|
||||
def __init__(self, command: str,
|
||||
description: str | None = None,
|
||||
description: str = None,
|
||||
flags: Flag | FlagsGroup | None = None):
|
||||
self._command = command
|
||||
self._description = description
|
||||
self._description = f'description for "{self._command}" 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
|
||||
@@ -29,11 +29,7 @@ class Command(Generic[T]):
|
||||
|
||||
|
||||
def get_description(self):
|
||||
if not self._description:
|
||||
description = f'description for "{self._command}" command'
|
||||
return description
|
||||
else:
|
||||
return self._description
|
||||
return self._description
|
||||
|
||||
|
||||
def get_registered_flags(self):
|
||||
|
||||
@@ -48,5 +48,4 @@ class Flag:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return True
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import re
|
||||
|
||||
|
||||
def set_description_message_pattern(pattern: str) -> None:
|
||||
first_check = re.match(r'.*command.*', pattern)
|
||||
second_check = re.match(r'.*{description}.*', pattern)
|
||||
|
||||
|
||||
set_description_message_pattern('Invalid des{ommand}cription pattern')
|
||||
@@ -1,29 +0,0 @@
|
||||
from pprint import pprint
|
||||
from rich.console import Console
|
||||
from argenta.router import Router
|
||||
|
||||
|
||||
work_router: Router = Router(name='work')
|
||||
settings_router: Router = Router(name='settings')
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
@work_router.command(command='a')
|
||||
def command_help():
|
||||
console.print('[bold red]command help [/bold red]')
|
||||
|
||||
|
||||
@work_router.command(command='B', description='tester')
|
||||
def command_start_solving():
|
||||
console.print('[bold red]command start [/bold red]')
|
||||
|
||||
|
||||
@settings_router.command(command='b')
|
||||
def command_settings():
|
||||
console.print('[bold red]command settings [/bold red]')
|
||||
|
||||
|
||||
@work_router.unknown_command
|
||||
def command_unknown_command(command):
|
||||
console.print(f'[bold red]Unknown command: [/bold red]{command}')
|
||||
@@ -1,17 +0,0 @@
|
||||
from pprint import pprint
|
||||
from tests.mock_default_app.handlers.routers import work_router, settings_router
|
||||
from argenta.app.entity import App
|
||||
|
||||
|
||||
app: App = App(ignore_command_register=False,
|
||||
line_separate='\n-------------------------------\n')
|
||||
|
||||
|
||||
def main():
|
||||
app.include_router(work_router, is_main=True)
|
||||
app.include_router(settings_router)
|
||||
|
||||
app.start_polling()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,11 +0,0 @@
|
||||
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')
|
||||
+5
-1
@@ -8,7 +8,11 @@ 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')
|
||||
App().set_description_message_pattern('Invalid description pattern')
|
||||
|
||||
def test_set_invalid_description_message_pattern2(self):
|
||||
with self.assertRaises(InvalidDescriptionMessagePatternException):
|
||||
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
|
||||
|
||||
def test_no_registered_router(self):
|
||||
with self.assertRaises(NoRegisteredRoutersException):
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
from argenta.command.params.flag import Flag
|
||||
|
||||
import unittest
|
||||
import re
|
||||
|
||||
|
||||
class TestFlag(unittest.TestCase):
|
||||
def test_get_string_entity(self):
|
||||
self.assertEqual(Flag(flag_name='test').get_string_entity(),
|
||||
'-test')
|
||||
|
||||
def test_get_string_entity2(self):
|
||||
self.assertEqual(Flag(flag_name='test',
|
||||
flag_prefix='---').get_string_entity(),
|
||||
'---test')
|
||||
|
||||
def test_get_flag_name(self):
|
||||
self.assertEqual(Flag(flag_name='test').get_flag_name(),
|
||||
'test')
|
||||
|
||||
def test_get_flag_prefix(self):
|
||||
self.assertEqual(Flag(flag_name='test').get_flag_prefix(),
|
||||
'-')
|
||||
|
||||
def test_get_flag_prefix2(self):
|
||||
self.assertEqual(Flag(flag_name='test',
|
||||
flag_prefix='--').get_flag_prefix(),
|
||||
'--')
|
||||
|
||||
def test_get_flag_value_without_set(self):
|
||||
self.assertEqual(Flag(flag_name='test').get_value(),
|
||||
None)
|
||||
|
||||
def test_get_flag_value_with_set(self):
|
||||
flag = Flag(flag_name='test')
|
||||
flag.set_value('example')
|
||||
self.assertEqual(flag.get_value(), 'example')
|
||||
|
||||
def test_validate_incorrect_flag_value_with_list_of_possible_flag_values(self):
|
||||
flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3'])
|
||||
self.assertEqual(flag.validate_input_flag_value('bad value'), False)
|
||||
|
||||
def test_validate_correct_flag_value_with_list_of_possible_flag_values(self):
|
||||
flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3'])
|
||||
self.assertEqual(flag.validate_input_flag_value('1'), True)
|
||||
|
||||
def test_validate_incorrect_flag_value_with_pattern_of_possible_flag_values(self):
|
||||
flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+'))
|
||||
self.assertEqual(flag.validate_input_flag_value('152.123.9.8'), False)
|
||||
|
||||
def test_validate_correct_flag_value_with_pattern_of_possible_flag_values(self):
|
||||
flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+'))
|
||||
self.assertEqual(flag.validate_input_flag_value('192.168.9.8'), True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
from argenta.command.params.flag import Flag, FlagsGroup
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class TestFlagsGroup(unittest.TestCase):
|
||||
def test_get_flags(self):
|
||||
flags = FlagsGroup()
|
||||
list_of_flags = [
|
||||
Flag('test1'),
|
||||
Flag('test2'),
|
||||
Flag('test3'),
|
||||
]
|
||||
flags.add_flags(list_of_flags)
|
||||
self.assertEqual(flags.get_flags(),
|
||||
list_of_flags)
|
||||
|
||||
def test_add_flag(self):
|
||||
flags = FlagsGroup()
|
||||
flags.add_flag(Flag('test'))
|
||||
self.assertEqual(len(flags.get_flags()), 1)
|
||||
|
||||
def test_add_flags(self):
|
||||
flags = FlagsGroup()
|
||||
flags.add_flags([Flag('test'), Flag('test2')])
|
||||
self.assertEqual(len(flags.get_flags()), 2)
|
||||
@@ -0,0 +1,34 @@
|
||||
from argenta.command.params.flag import FlagsGroup, Flag
|
||||
from argenta.router import Router
|
||||
from argenta.command import Command
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class TestRouter(unittest.TestCase):
|
||||
def test_get_router_name(self):
|
||||
self.assertEqual(Router(name='test name').get_name(), 'test name')
|
||||
|
||||
def test_get_router_title(self):
|
||||
self.assertEqual(Router(title='test title').get_title(), 'test title')
|
||||
|
||||
def test_input_correct_command(self):
|
||||
router = Router()
|
||||
@router.command(Command(command='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
self.assertEqual(router.input_command_handler(Command(command='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(command='test'))
|
||||
def test():
|
||||
return 'correct result'
|
||||
|
||||
input_command = Command(command='test')
|
||||
input_command.set_input_flags(FlagsGroup([Flag('host')]))
|
||||
|
||||
self.assertEqual(router.input_command_handler(input_command), None)
|
||||
Reference in New Issue
Block a user