mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
0.3.1
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
from .entity import App
|
||||
from .exceptions import (HandlerForUnknownCommandsOnNonMainRouterException,
|
||||
InvalidDescriptionMessagePatternException,
|
||||
InvalidRouterInstanceException,
|
||||
OnlyOneMainRouterIsAllowedException,
|
||||
MissingMainRouterException,
|
||||
MissingHandlerForUnknownCommandsException)
|
||||
from .exceptions import (InvalidDescriptionMessagePatternException,
|
||||
InvalidRouterInstanceException)
|
||||
|
||||
+25
-39
@@ -6,13 +6,10 @@ from ..router.entity import Router
|
||||
from ..command.input_comand.entity import InputCommand
|
||||
from ..command.input_comand.exceptions import (IncorrectInputFlagException,
|
||||
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
||||
IncorrectNumberArgsInvalidInputFlagsHandlerException)
|
||||
IncorrectNumberArgsHandlerException,
|
||||
UnknownCommandHandlerHasBeenAlreadyCreatedException)
|
||||
from .exceptions import (InvalidRouterInstanceException,
|
||||
InvalidDescriptionMessagePatternException,
|
||||
OnlyOneMainRouterIsAllowedException,
|
||||
MissingMainRouterException,
|
||||
MissingHandlerForUnknownCommandsException,
|
||||
HandlerForUnknownCommandsOnNonMainRouterException,
|
||||
NoRegisteredRoutersException,
|
||||
NoRegisteredHandlersException,
|
||||
RepeatedCommandInDifferentRoutersException)
|
||||
@@ -49,6 +46,7 @@ class App:
|
||||
|
||||
self._routers: list[Router] = []
|
||||
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._app_main_router: Router | None = None
|
||||
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
||||
@@ -57,7 +55,6 @@ class App:
|
||||
def start_polling(self) -> None:
|
||||
self._validate_number_of_routers()
|
||||
self._validate_included_routers()
|
||||
self._validate_main_router()
|
||||
self._validate_all_router_commands()
|
||||
|
||||
self.print_func(self.initial_message)
|
||||
@@ -89,7 +86,8 @@ class App:
|
||||
self._checking_command_for_exit_command(input_command.get_string_entity())
|
||||
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 not self.repeat_command_groups:
|
||||
self.print_func(self.prompt)
|
||||
@@ -127,13 +125,20 @@ class App:
|
||||
else:
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberArgsInvalidInputFlagsHandlerException()
|
||||
raise IncorrectNumberArgsHandlerException()
|
||||
else:
|
||||
self._invalid_input_flags_handler = handler
|
||||
|
||||
|
||||
def get_main_router(self) -> Router:
|
||||
return self._app_main_router
|
||||
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
||||
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]:
|
||||
@@ -144,17 +149,10 @@ class App:
|
||||
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):
|
||||
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)
|
||||
self._routers.append(router)
|
||||
|
||||
@@ -176,23 +174,6 @@ class App:
|
||||
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:
|
||||
for idx in range(len(self._registered_router_entities)):
|
||||
current_router: Router = self._registered_router_entities[idx]['entity']
|
||||
@@ -220,17 +201,22 @@ class App:
|
||||
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
|
||||
for router_entity in registered_router_entities:
|
||||
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:
|
||||
return False
|
||||
else:
|
||||
if command_entity['command'].get_string_entity() == command:
|
||||
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
||||
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.command_group_description_separate)
|
||||
return True
|
||||
|
||||
@@ -13,32 +13,6 @@ class InvalidDescriptionMessagePatternException(Exception):
|
||||
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):
|
||||
def __str__(self):
|
||||
return "No Registered Router Found"
|
||||
|
||||
@@ -26,7 +26,12 @@ class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
|
||||
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):
|
||||
return "Incorrect Input Flags Handler has incorrect number of arguments"
|
||||
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
from .entity import Router
|
||||
from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||
InvalidDescriptionInstanceException)
|
||||
from .exceptions import InvalidDescriptionInstanceException
|
||||
@@ -4,8 +4,7 @@ from ..command.entity import Command
|
||||
from ..command.input_comand.entity import InputCommand
|
||||
from ..command.input_comand.exceptions import InvalidInputFlagException
|
||||
from ..command.params.flag.flags_group.entity import FlagsGroup
|
||||
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||
RepeatedCommandException, RepeatedFlagNameException,
|
||||
from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
|
||||
CurrentCommandDoesNotProcessFlagsException,
|
||||
TooManyTransferredArgsException,
|
||||
RequiredArgumentNotPassedException)
|
||||
@@ -20,7 +19,6 @@ class Router:
|
||||
self.name = name
|
||||
|
||||
self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
|
||||
self._is_main_router: bool = False
|
||||
self._ignore_command_register: bool = False
|
||||
|
||||
self._unknown_command_handler: Callable[[str], None] | None = None
|
||||
@@ -42,17 +40,6 @@ class Router:
|
||||
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):
|
||||
input_command_name: str = input_command.get_string_entity()
|
||||
for command_entity in self._command_entities:
|
||||
@@ -73,14 +60,6 @@ class Router:
|
||||
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):
|
||||
command_name: str = command.get_string_entity()
|
||||
if command_name in self.get_all_commands():
|
||||
@@ -109,13 +88,6 @@ class Router:
|
||||
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):
|
||||
self._ignore_command_register = ignore_command_register
|
||||
|
||||
@@ -139,8 +111,7 @@ class Router:
|
||||
'ignore_command_register': self._ignore_command_register,
|
||||
'attributes': {
|
||||
'command_entities': self._command_entities,
|
||||
'unknown_command_func': self._unknown_command_handler,
|
||||
'is_main_router': self._is_main_router
|
||||
'unknown_command_func': self._unknown_command_handler
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,11 +3,6 @@ class InvalidDescriptionInstanceException(Exception):
|
||||
return "Invalid Description Instance"
|
||||
|
||||
|
||||
class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception):
|
||||
def __str__(self):
|
||||
return "Only one unknown command handler can be declared"
|
||||
|
||||
|
||||
class RepeatedCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Commands in handler cannot be repeated"
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "argenta"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
description = "python library for creating custom shells"
|
||||
authors = [
|
||||
{name = "kolo",email = "kolo.is.main@gmail.com"}
|
||||
|
||||
@@ -42,8 +42,3 @@ def command_start_solving(argrrtrts: FlagsGroup | None):
|
||||
def command_update():
|
||||
print('uefi')
|
||||
# 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')
|
||||
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.set_initial_message(initial_greeting)
|
||||
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}')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user