mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
v0.3.4
This commit is contained in:
+41
-59
@@ -4,11 +4,9 @@ from inspect import getfullargspec
|
||||
from ..command.entity import Command
|
||||
from ..router.entity import Router
|
||||
from ..command.exceptions import (UnprocessedInputFlagException,
|
||||
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
||||
IncorrectNumberOfHandlerArgsException,
|
||||
UnknownCommandHandlerHasBeenAlreadyCreatedException,
|
||||
RepeatedInputFlagsException,
|
||||
RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException)
|
||||
EmptyInputCommandException)
|
||||
from .exceptions import (InvalidRouterInstanceException,
|
||||
InvalidDescriptionMessagePatternException,
|
||||
NoRegisteredRoutersException,
|
||||
@@ -46,12 +44,12 @@ class App:
|
||||
self.repeat_command_groups = repeat_command_groups
|
||||
|
||||
self._routers: list[Router] = []
|
||||
self._invalid_input_flags_handler: Callable[[str], None] | None = None
|
||||
self._repeated_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}'
|
||||
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
|
||||
self._invalid_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Incorrect flag syntax: "{raw_command}"')
|
||||
self._repeated_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Repeated input flags: "{raw_command}"')
|
||||
self._empty_input_command_handler: Callable[[], None] = lambda: print_func(f'Empty input command')
|
||||
self._unknown_command_handler: Callable[[Command], None] = lambda command: print_func(f"Unknown command: {command.get_string_entity()}")
|
||||
|
||||
|
||||
def start_polling(self) -> None:
|
||||
@@ -76,31 +74,39 @@ class App:
|
||||
input_command: Command = Command.parse_input_command(raw_command=raw_command)
|
||||
except UnprocessedInputFlagException:
|
||||
self.print_func(self.line_separate)
|
||||
if self._invalid_input_flags_handler:
|
||||
self._invalid_input_flags_handler(raw_command)
|
||||
else:
|
||||
self.print_func(f'Incorrect flag syntax: "{raw_command}"')
|
||||
self._invalid_input_flags_handler(raw_command)
|
||||
self.print_func(self.line_separate)
|
||||
|
||||
if not self.repeat_command_groups:
|
||||
self.print_func(self.prompt)
|
||||
continue
|
||||
|
||||
except RepeatedInputFlagsException:
|
||||
self.print_func(self.line_separate)
|
||||
if self._repeated_input_flags_handler:
|
||||
self._repeated_input_flags_handler(raw_command)
|
||||
else:
|
||||
self.print_func(f'Repeated input flags: "{raw_command}"')
|
||||
self._repeated_input_flags_handler(raw_command)
|
||||
self.print_func(self.line_separate)
|
||||
|
||||
if not self.repeat_command_groups:
|
||||
self.print_func(self.prompt)
|
||||
continue
|
||||
|
||||
self._checking_command_for_exit_command(input_command.get_string_entity())
|
||||
self.print_func(self.line_separate)
|
||||
except EmptyInputCommandException:
|
||||
self.print_func(self.line_separate)
|
||||
self._empty_input_command_handler()
|
||||
self.print_func(self.line_separate)
|
||||
|
||||
if not self.repeat_command_groups:
|
||||
self.print_func(self.prompt)
|
||||
continue
|
||||
|
||||
self._check_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)
|
||||
|
||||
if is_unknown_command:
|
||||
self.print_func(self.line_separate)
|
||||
self.print_func(self.command_group_description_separate)
|
||||
if not self.repeat_command_groups:
|
||||
self.print_func(self.prompt)
|
||||
continue
|
||||
@@ -124,52 +130,35 @@ class App:
|
||||
|
||||
def set_description_message_pattern(self, pattern: str) -> None:
|
||||
try:
|
||||
pattern.format(command='command',
|
||||
description='description')
|
||||
pattern.format(command='command', description='description')
|
||||
except KeyError:
|
||||
raise InvalidDescriptionMessagePatternException(pattern)
|
||||
self._description_message_pattern: str = pattern
|
||||
else:
|
||||
self._description_message_pattern: str = pattern
|
||||
|
||||
|
||||
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||
if self._invalid_input_flags_handler:
|
||||
raise InvalidInputFlagsHandlerHasBeenAlreadyCreatedException()
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
self._invalid_input_flags_handler = handler
|
||||
self._invalid_input_flags_handler = handler
|
||||
|
||||
|
||||
def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||
if self._repeated_input_flags_handler:
|
||||
raise RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException()
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
self._repeated_input_flags_handler = handler
|
||||
self._repeated_input_flags_handler = handler
|
||||
|
||||
|
||||
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
||||
if self._unknown_command_handler:
|
||||
raise UnknownCommandHandlerHasBeenAlreadyCreatedException()
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
args = getfullargspec(handler).args
|
||||
if len(args) != 1:
|
||||
raise IncorrectNumberOfHandlerArgsException()
|
||||
else:
|
||||
self._unknown_command_handler = handler
|
||||
|
||||
|
||||
def get_all_app_commands(self) -> list[str]:
|
||||
all_commands: list[str] = []
|
||||
for router in self._routers:
|
||||
all_commands.extend(router.get_all_commands())
|
||||
|
||||
return all_commands
|
||||
self._unknown_command_handler = handler
|
||||
|
||||
|
||||
def include_router(self, router: Router) -> None:
|
||||
@@ -213,7 +202,7 @@ class App:
|
||||
raise RepeatedCommandInDifferentRoutersException()
|
||||
|
||||
|
||||
def _checking_command_for_exit_command(self, command: str):
|
||||
def _check_command_for_exit_command(self, command: str):
|
||||
if command.lower() == self.exit_command.lower():
|
||||
if self.ignore_exit_command_register:
|
||||
self.print_func(self.farewell_message)
|
||||
@@ -234,14 +223,7 @@ class App:
|
||||
else:
|
||||
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
||||
return False
|
||||
|
||||
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)
|
||||
self._unknown_command_handler(command)
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ from .params.flag.entity import Flag
|
||||
from .params.flag.flags_group.entity import FlagsGroup
|
||||
from .exceptions import (InvalidCommandInstanceException,
|
||||
InvalidDescriptionInstanceException,
|
||||
InvalidFlagsInstanceException, UnprocessedInputFlagException, RepeatedInputFlagsException)
|
||||
InvalidFlagsInstanceException,
|
||||
UnprocessedInputFlagException,
|
||||
RepeatedInputFlagsException,
|
||||
EmptyInputCommandException)
|
||||
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
@@ -71,6 +74,8 @@ class Command(Generic[T]):
|
||||
|
||||
@staticmethod
|
||||
def parse_input_command(raw_command: str) -> 'Command[T]':
|
||||
if not raw_command:
|
||||
raise EmptyInputCommandException()
|
||||
list_of_tokens = raw_command.split()
|
||||
command = list_of_tokens[0]
|
||||
list_of_tokens.pop(0)
|
||||
|
||||
@@ -29,21 +29,11 @@ class RepeatedInputFlagsException(Exception):
|
||||
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
|
||||
|
||||
|
||||
class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Input Flags Handler has already been created"
|
||||
|
||||
|
||||
class RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
|
||||
def __str__(self):
|
||||
return "Repeated Input Flags Handler has already been created"
|
||||
|
||||
|
||||
class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception):
|
||||
def __str__(self):
|
||||
return "Unknown Command Handler has already been created"
|
||||
|
||||
|
||||
class IncorrectNumberOfHandlerArgsException(Exception):
|
||||
def __str__(self):
|
||||
return "Incorrect Input Flags Handler has incorrect number of arguments"
|
||||
|
||||
|
||||
class EmptyInputCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Input Command is empty"
|
||||
@@ -36,6 +36,7 @@ class Flag:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
if isinstance(self.possible_flag_values, list):
|
||||
if self.ignore_flag_value_register:
|
||||
if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]:
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "argenta"
|
||||
version = "0.3.3"
|
||||
version = "0.3.4"
|
||||
description = "python library for creating custom shells"
|
||||
authors = [
|
||||
{name = "kolo",email = "kolo.is.main@gmail.com"}
|
||||
|
||||
@@ -8,7 +8,7 @@ app: App = App(prompt='[italic white bold]What do you want to do(enter number of
|
||||
line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n',
|
||||
print_func=Console().print,
|
||||
command_group_description_separate='',
|
||||
repeat_command_groups=False)
|
||||
repeat_command_groups=True)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user