mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
fix
This commit is contained in:
@@ -5,7 +5,10 @@ from ..command.input_comand.entity import InputCommand
|
|||||||
from ..command.input_comand.exceptions import InvalidInputFlagException
|
from ..command.input_comand.exceptions import InvalidInputFlagException
|
||||||
from ..command.params.flag.flags_group.entity import FlagsGroup
|
from ..command.params.flag.flags_group.entity import FlagsGroup
|
||||||
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||||
RepeatedCommandException, RepeatedFlagNameException)
|
RepeatedCommandException, RepeatedFlagNameException,
|
||||||
|
CurrentCommandDoesNotProcessFlagsException,
|
||||||
|
TooManyTransferredArgsException,
|
||||||
|
RequiredArgumentNotPassedException)
|
||||||
|
|
||||||
|
|
||||||
class Router:
|
class Router:
|
||||||
@@ -27,6 +30,7 @@ class Router:
|
|||||||
self._validate_command(command)
|
self._validate_command(command)
|
||||||
|
|
||||||
def command_decorator(func):
|
def command_decorator(func):
|
||||||
|
Router._validate_func_args(command, func)
|
||||||
self._command_entities.append({'handler_func': func,
|
self._command_entities.append({'handler_func': func,
|
||||||
'command': command})
|
'command': command})
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
@@ -51,16 +55,20 @@ class Router:
|
|||||||
input_command_name: str = input_command.get_string_entity()
|
input_command_name: str = input_command.get_string_entity()
|
||||||
for command_entity in self._command_entities:
|
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_string_entity().lower():
|
||||||
if input_command_name == command_entity['command'].get_string_entity():
|
if command_entity['command'].get_flags():
|
||||||
if input_command.get_input_flags():
|
if input_command.get_input_flags():
|
||||||
for flag in input_command.get_input_flags():
|
for flag in input_command.get_input_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:
|
||||||
raise InvalidInputFlagException(flag)
|
raise InvalidInputFlagException(flag)
|
||||||
return command_entity['handler_func'](input_command.get_input_flags())
|
return command_entity['handler_func'](args=input_command.get_input_flags())
|
||||||
else:
|
else:
|
||||||
print(getfullargspec(command_entity['handler_func']))
|
return command_entity['handler_func'](args=FlagsGroup(None))
|
||||||
return command_entity['handler_func'](None)
|
else:
|
||||||
|
if input_command.get_input_flags():
|
||||||
|
raise CurrentCommandDoesNotProcessFlagsException()
|
||||||
|
else:
|
||||||
|
return command_entity['handler_func']()
|
||||||
|
|
||||||
|
|
||||||
def get_unknown_command_func(self):
|
def get_unknown_command_func(self):
|
||||||
@@ -86,6 +94,20 @@ class Router:
|
|||||||
raise RepeatedFlagNameException()
|
raise RepeatedFlagNameException()
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_func_args(command: Command, func: Callable):
|
||||||
|
registered_args = command.get_flags()
|
||||||
|
transferred_args = getfullargspec(func).args
|
||||||
|
if registered_args and transferred_args:
|
||||||
|
if len(transferred_args) != 1:
|
||||||
|
raise TooManyTransferredArgsException()
|
||||||
|
elif registered_args and not transferred_args:
|
||||||
|
raise RequiredArgumentNotPassedException()
|
||||||
|
elif not registered_args and transferred_args:
|
||||||
|
raise TooManyTransferredArgsException()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set_router_as_main(self):
|
def set_router_as_main(self):
|
||||||
if self.name == 'subordinate':
|
if self.name == 'subordinate':
|
||||||
self.name = 'main'
|
self.name = 'main'
|
||||||
|
|||||||
@@ -16,3 +16,18 @@ class RepeatedCommandException(Exception):
|
|||||||
class RepeatedFlagNameException(Exception):
|
class RepeatedFlagNameException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Repeated flag name in register command"
|
return "Repeated flag name in register command"
|
||||||
|
|
||||||
|
|
||||||
|
class CurrentCommandDoesNotProcessFlagsException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Current command does not process flags"
|
||||||
|
|
||||||
|
|
||||||
|
class TooManyTransferredArgsException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Too many transferred arguments"
|
||||||
|
|
||||||
|
|
||||||
|
class RequiredArgumentNotPassedException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Required argument not passed"
|
||||||
|
|||||||
@@ -20,12 +20,12 @@ flagi = FlagsGroup(flags=[
|
|||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
@work_router.command(Command(command='0', description='Get Help', flags=flagi))
|
@work_router.command(Command(command='0', description='Get Help'))
|
||||||
def command_help(args: FlagsGroup | None):
|
def command_help():
|
||||||
print('Help command')
|
print('Help command')
|
||||||
flags = args.get_flags()
|
'''flags = args.get_flags()
|
||||||
for flag in flags:
|
for flag in flags:
|
||||||
print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"')
|
print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"')'''
|
||||||
#help_command()
|
#help_command()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user