mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
0.3.1
This commit is contained in:
@@ -1,7 +1,3 @@
|
|||||||
from .entity import App
|
from .entity import App
|
||||||
from .exceptions import (HandlerForUnknownCommandsOnNonMainRouterException,
|
from .exceptions import (InvalidDescriptionMessagePatternException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidRouterInstanceException)
|
||||||
InvalidRouterInstanceException,
|
|
||||||
OnlyOneMainRouterIsAllowedException,
|
|
||||||
MissingMainRouterException,
|
|
||||||
MissingHandlerForUnknownCommandsException)
|
|
||||||
|
|||||||
+25
-39
@@ -6,13 +6,10 @@ from ..router.entity import Router
|
|||||||
from ..command.input_comand.entity import InputCommand
|
from ..command.input_comand.entity import InputCommand
|
||||||
from ..command.input_comand.exceptions import (IncorrectInputFlagException,
|
from ..command.input_comand.exceptions import (IncorrectInputFlagException,
|
||||||
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
||||||
IncorrectNumberArgsInvalidInputFlagsHandlerException)
|
IncorrectNumberArgsHandlerException,
|
||||||
|
UnknownCommandHandlerHasBeenAlreadyCreatedException)
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
OnlyOneMainRouterIsAllowedException,
|
|
||||||
MissingMainRouterException,
|
|
||||||
MissingHandlerForUnknownCommandsException,
|
|
||||||
HandlerForUnknownCommandsOnNonMainRouterException,
|
|
||||||
NoRegisteredRoutersException,
|
NoRegisteredRoutersException,
|
||||||
NoRegisteredHandlersException,
|
NoRegisteredHandlersException,
|
||||||
RepeatedCommandInDifferentRoutersException)
|
RepeatedCommandInDifferentRoutersException)
|
||||||
@@ -49,6 +46,7 @@ class App:
|
|||||||
|
|
||||||
self._routers: list[Router] = []
|
self._routers: list[Router] = []
|
||||||
self._invalid_input_flags_handler: Callable[[str], None] | None = None
|
self._invalid_input_flags_handler: Callable[[str], None] | None = None
|
||||||
|
self._unknown_command_handler: Callable[[Command], None] | None = None
|
||||||
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
|
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
|
||||||
self._app_main_router: Router | None = None
|
self._app_main_router: Router | None = None
|
||||||
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
||||||
@@ -57,7 +55,6 @@ class App:
|
|||||||
def start_polling(self) -> None:
|
def start_polling(self) -> None:
|
||||||
self._validate_number_of_routers()
|
self._validate_number_of_routers()
|
||||||
self._validate_included_routers()
|
self._validate_included_routers()
|
||||||
self._validate_main_router()
|
|
||||||
self._validate_all_router_commands()
|
self._validate_all_router_commands()
|
||||||
|
|
||||||
self.print_func(self.initial_message)
|
self.print_func(self.initial_message)
|
||||||
@@ -89,7 +86,8 @@ class App:
|
|||||||
self._checking_command_for_exit_command(input_command.get_string_entity())
|
self._checking_command_for_exit_command(input_command.get_string_entity())
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
|
|
||||||
is_unknown_command: bool = self._check_is_command_unknown(input_command.get_string_entity())
|
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
||||||
|
|
||||||
if is_unknown_command:
|
if is_unknown_command:
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
@@ -127,13 +125,20 @@ class App:
|
|||||||
else:
|
else:
|
||||||
args = getfullargspec(handler).args
|
args = getfullargspec(handler).args
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
raise IncorrectNumberArgsInvalidInputFlagsHandlerException()
|
raise IncorrectNumberArgsHandlerException()
|
||||||
else:
|
else:
|
||||||
self._invalid_input_flags_handler = handler
|
self._invalid_input_flags_handler = handler
|
||||||
|
|
||||||
|
|
||||||
def get_main_router(self) -> Router:
|
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
||||||
return self._app_main_router
|
if self._unknown_command_handler:
|
||||||
|
raise UnknownCommandHandlerHasBeenAlreadyCreatedException()
|
||||||
|
else:
|
||||||
|
args = getfullargspec(handler).args
|
||||||
|
if len(args) != 1:
|
||||||
|
raise IncorrectNumberArgsHandlerException()
|
||||||
|
else:
|
||||||
|
self._unknown_command_handler = handler
|
||||||
|
|
||||||
|
|
||||||
def get_all_app_commands(self) -> list[str]:
|
def get_all_app_commands(self) -> list[str]:
|
||||||
@@ -144,17 +149,10 @@ class App:
|
|||||||
return all_commands
|
return all_commands
|
||||||
|
|
||||||
|
|
||||||
def include_router(self, router: Router, is_main: True | False = False) -> None:
|
def include_router(self, router: Router) -> None:
|
||||||
if not isinstance(router, Router):
|
if not isinstance(router, Router):
|
||||||
raise InvalidRouterInstanceException()
|
raise InvalidRouterInstanceException()
|
||||||
|
|
||||||
if is_main:
|
|
||||||
if not self._app_main_router:
|
|
||||||
self._app_main_router = router
|
|
||||||
router.set_router_as_main()
|
|
||||||
else:
|
|
||||||
raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name())
|
|
||||||
|
|
||||||
router.set_ignore_command_register(self.ignore_command_register)
|
router.set_ignore_command_register(self.ignore_command_register)
|
||||||
self._routers.append(router)
|
self._routers.append(router)
|
||||||
|
|
||||||
@@ -176,23 +174,6 @@ class App:
|
|||||||
raise NoRegisteredHandlersException(router.get_name())
|
raise NoRegisteredHandlersException(router.get_name())
|
||||||
|
|
||||||
|
|
||||||
def _validate_main_router(self):
|
|
||||||
if not self._app_main_router:
|
|
||||||
if len(self._routers) > 1:
|
|
||||||
raise MissingMainRouterException()
|
|
||||||
else:
|
|
||||||
router = self._routers[0]
|
|
||||||
router.set_router_as_main()
|
|
||||||
self._app_main_router = router
|
|
||||||
|
|
||||||
if not self._app_main_router.get_unknown_command_func():
|
|
||||||
raise MissingHandlerForUnknownCommandsException()
|
|
||||||
|
|
||||||
for router in self._routers:
|
|
||||||
if router.get_unknown_command_func() and not (self._app_main_router is router):
|
|
||||||
raise HandlerForUnknownCommandsOnNonMainRouterException()
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_all_router_commands(self) -> None:
|
def _validate_all_router_commands(self) -> None:
|
||||||
for idx in range(len(self._registered_router_entities)):
|
for idx in range(len(self._registered_router_entities)):
|
||||||
current_router: Router = self._registered_router_entities[idx]['entity']
|
current_router: Router = self._registered_router_entities[idx]['entity']
|
||||||
@@ -220,17 +201,22 @@ class App:
|
|||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
def _check_is_command_unknown(self, command: str):
|
def _check_is_command_unknown(self, command: Command):
|
||||||
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = self._registered_router_entities
|
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = self._registered_router_entities
|
||||||
for router_entity in registered_router_entities:
|
for router_entity in registered_router_entities:
|
||||||
for command_entity in router_entity['commands']:
|
for command_entity in router_entity['commands']:
|
||||||
if command_entity['command'].get_string_entity().lower() == command.lower():
|
if command_entity['command'].get_string_entity().lower() == command.get_string_entity().lower():
|
||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if command_entity['command'].get_string_entity() == command:
|
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
||||||
return False
|
return False
|
||||||
self._app_main_router.unknown_command_handler(command)
|
|
||||||
|
if self._unknown_command_handler:
|
||||||
|
self._unknown_command_handler(command)
|
||||||
|
else:
|
||||||
|
print(f"Unknown command: {command.get_string_entity()}")
|
||||||
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
self.print_func(self.command_group_description_separate)
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -13,32 +13,6 @@ class InvalidDescriptionMessagePatternException(Exception):
|
|||||||
f"Your pattern: {self.pattern}")
|
f"Your pattern: {self.pattern}")
|
||||||
|
|
||||||
|
|
||||||
class OnlyOneMainRouterIsAllowedException(Exception):
|
|
||||||
def __init__(self, existing_main_router):
|
|
||||||
self.existing_main_router = existing_main_router
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return ("Only One Main Router Allowed\n"
|
|
||||||
f"Existing main router is: {self.existing_main_router}")
|
|
||||||
|
|
||||||
|
|
||||||
class MissingMainRouterException(Exception):
|
|
||||||
def __str__(self):
|
|
||||||
return ("Missing Main Router\n"
|
|
||||||
"One of the registered routers must be the main one")
|
|
||||||
|
|
||||||
|
|
||||||
class MissingHandlerForUnknownCommandsException(Exception):
|
|
||||||
def __str__(self):
|
|
||||||
return ("Missing Handlers For Unknown Commands On The Main Router\n"
|
|
||||||
"The main router must have a declared handler for unknown commands")
|
|
||||||
|
|
||||||
|
|
||||||
class HandlerForUnknownCommandsOnNonMainRouterException(Exception):
|
|
||||||
def __str__(self):
|
|
||||||
return '\nThe handler for unknown commands can only be declared for the main router'
|
|
||||||
|
|
||||||
|
|
||||||
class NoRegisteredRoutersException(Exception):
|
class NoRegisteredRoutersException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "No Registered Router Found"
|
return "No Registered Router Found"
|
||||||
|
|||||||
@@ -26,7 +26,12 @@ class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
|
|||||||
return "Invalid Input Flags Handler has already been created"
|
return "Invalid Input Flags Handler has already been created"
|
||||||
|
|
||||||
|
|
||||||
class IncorrectNumberArgsInvalidInputFlagsHandlerException(Exception):
|
class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Unknown Command Handler has already been created"
|
||||||
|
|
||||||
|
|
||||||
|
class IncorrectNumberArgsHandlerException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Incorrect Input Flags Handler has incorrect number of arguments"
|
return "Incorrect Input Flags Handler has incorrect number of arguments"
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
from .entity import Router
|
from .entity import Router
|
||||||
from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
from .exceptions import InvalidDescriptionInstanceException
|
||||||
InvalidDescriptionInstanceException)
|
|
||||||
@@ -4,8 +4,7 @@ from ..command.entity import Command
|
|||||||
from ..command.input_comand.entity import InputCommand
|
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 (RepeatedCommandException, RepeatedFlagNameException,
|
||||||
RepeatedCommandException, RepeatedFlagNameException,
|
|
||||||
CurrentCommandDoesNotProcessFlagsException,
|
CurrentCommandDoesNotProcessFlagsException,
|
||||||
TooManyTransferredArgsException,
|
TooManyTransferredArgsException,
|
||||||
RequiredArgumentNotPassedException)
|
RequiredArgumentNotPassedException)
|
||||||
@@ -20,7 +19,6 @@ class Router:
|
|||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
||||||
self._is_main_router: bool = False
|
|
||||||
self._ignore_command_register: bool = False
|
self._ignore_command_register: bool = False
|
||||||
|
|
||||||
self._unknown_command_handler: Callable[[str], None] | None = None
|
self._unknown_command_handler: Callable[[str], None] | None = None
|
||||||
@@ -42,17 +40,6 @@ class Router:
|
|||||||
return command_decorator
|
return command_decorator
|
||||||
|
|
||||||
|
|
||||||
def unknown_command(self, func):
|
|
||||||
if self._unknown_command_handler is not None:
|
|
||||||
raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
|
|
||||||
|
|
||||||
self._unknown_command_handler: Callable = func
|
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def input_command_handler(self, input_command: InputCommand):
|
def input_command_handler(self, input_command: InputCommand):
|
||||||
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:
|
||||||
@@ -73,14 +60,6 @@ class Router:
|
|||||||
return command_entity['handler_func']()
|
return command_entity['handler_func']()
|
||||||
|
|
||||||
|
|
||||||
def get_unknown_command_func(self):
|
|
||||||
return self._unknown_command_handler
|
|
||||||
|
|
||||||
|
|
||||||
def unknown_command_handler(self, unknown_command):
|
|
||||||
self._unknown_command_handler(unknown_command)
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_command(self, command: Command):
|
def _validate_command(self, command: Command):
|
||||||
command_name: str = command.get_string_entity()
|
command_name: str = command.get_string_entity()
|
||||||
if command_name in self.get_all_commands():
|
if command_name in self.get_all_commands():
|
||||||
@@ -109,13 +88,6 @@ class Router:
|
|||||||
raise TooManyTransferredArgsException()
|
raise TooManyTransferredArgsException()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set_router_as_main(self):
|
|
||||||
if self.name == 'subordinate':
|
|
||||||
self.name = 'main'
|
|
||||||
self._is_main_router = True
|
|
||||||
|
|
||||||
|
|
||||||
def set_ignore_command_register(self, ignore_command_register: bool):
|
def set_ignore_command_register(self, ignore_command_register: bool):
|
||||||
self._ignore_command_register = ignore_command_register
|
self._ignore_command_register = ignore_command_register
|
||||||
|
|
||||||
@@ -139,8 +111,7 @@ class Router:
|
|||||||
'ignore_command_register': self._ignore_command_register,
|
'ignore_command_register': self._ignore_command_register,
|
||||||
'attributes': {
|
'attributes': {
|
||||||
'command_entities': self._command_entities,
|
'command_entities': self._command_entities,
|
||||||
'unknown_command_func': self._unknown_command_handler,
|
'unknown_command_func': self._unknown_command_handler
|
||||||
'is_main_router': self._is_main_router
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,6 @@ class InvalidDescriptionInstanceException(Exception):
|
|||||||
return "Invalid Description Instance"
|
return "Invalid Description Instance"
|
||||||
|
|
||||||
|
|
||||||
class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception):
|
|
||||||
def __str__(self):
|
|
||||||
return "Only one unknown command handler can be declared"
|
|
||||||
|
|
||||||
|
|
||||||
class RepeatedCommandException(Exception):
|
class RepeatedCommandException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Commands in handler cannot be repeated"
|
return "Commands in handler cannot be repeated"
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "argenta"
|
name = "argenta"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
description = "python library for creating custom shells"
|
description = "python library for creating custom shells"
|
||||||
authors = [
|
authors = [
|
||||||
{name = "kolo",email = "kolo.is.main@gmail.com"}
|
{name = "kolo",email = "kolo.is.main@gmail.com"}
|
||||||
|
|||||||
@@ -42,8 +42,3 @@ def command_start_solving(argrrtrts: FlagsGroup | None):
|
|||||||
def command_update():
|
def command_update():
|
||||||
print('uefi')
|
print('uefi')
|
||||||
# upgrade_command()
|
# upgrade_command()
|
||||||
|
|
||||||
|
|
||||||
@work_router.unknown_command
|
|
||||||
def command_unknown_command(command):
|
|
||||||
console.print(f'[bold red]Unknown command: [/bold red]{command}')
|
|
||||||
|
|||||||
@@ -18,12 +18,13 @@ def main():
|
|||||||
ascii_goodbye_message: str = text2art('GoodBye', font='small')
|
ascii_goodbye_message: str = text2art('GoodBye', font='small')
|
||||||
goodbye_message: str = f'[bold red]\n{ascii_goodbye_message}{' '*12}made by kolo\n'
|
goodbye_message: str = f'[bold red]\n{ascii_goodbye_message}{' '*12}made by kolo\n'
|
||||||
|
|
||||||
app.include_router(work_router, is_main=True)
|
app.include_router(work_router)
|
||||||
app.include_router(settings_router)
|
app.include_router(settings_router)
|
||||||
|
|
||||||
app.set_initial_message(initial_greeting)
|
app.set_initial_message(initial_greeting)
|
||||||
app.set_farewell_message(goodbye_message)
|
app.set_farewell_message(goodbye_message)
|
||||||
app.set_invalid_input_flags_handler(lambda raw_command: print("Custom iif handler"))
|
|
||||||
|
app.set_invalid_input_flags_handler(lambda raw_command: print(f"Invalid input flags: {raw_command}"))
|
||||||
|
|
||||||
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')
|
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user