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:
+24
-42
@@ -4,11 +4,9 @@ from inspect import getfullargspec
|
|||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
from ..router.entity import Router
|
from ..router.entity import Router
|
||||||
from ..command.exceptions import (UnprocessedInputFlagException,
|
from ..command.exceptions import (UnprocessedInputFlagException,
|
||||||
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
|
|
||||||
IncorrectNumberOfHandlerArgsException,
|
IncorrectNumberOfHandlerArgsException,
|
||||||
UnknownCommandHandlerHasBeenAlreadyCreatedException,
|
|
||||||
RepeatedInputFlagsException,
|
RepeatedInputFlagsException,
|
||||||
RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException)
|
EmptyInputCommandException)
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
NoRegisteredRoutersException,
|
NoRegisteredRoutersException,
|
||||||
@@ -46,12 +44,12 @@ class App:
|
|||||||
self.repeat_command_groups = repeat_command_groups
|
self.repeat_command_groups = repeat_command_groups
|
||||||
|
|
||||||
self._routers: list[Router] = []
|
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._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:
|
def start_polling(self) -> None:
|
||||||
@@ -76,31 +74,39 @@ class App:
|
|||||||
input_command: Command = Command.parse_input_command(raw_command=raw_command)
|
input_command: Command = Command.parse_input_command(raw_command=raw_command)
|
||||||
except UnprocessedInputFlagException:
|
except UnprocessedInputFlagException:
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
if self._invalid_input_flags_handler:
|
|
||||||
self._invalid_input_flags_handler(raw_command)
|
self._invalid_input_flags_handler(raw_command)
|
||||||
else:
|
|
||||||
self.print_func(f'Incorrect flag syntax: "{raw_command}"')
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
|
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
except RepeatedInputFlagsException:
|
except RepeatedInputFlagsException:
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
if self._repeated_input_flags_handler:
|
|
||||||
self._repeated_input_flags_handler(raw_command)
|
self._repeated_input_flags_handler(raw_command)
|
||||||
else:
|
|
||||||
self.print_func(f'Repeated input flags: "{raw_command}"')
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
|
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._checking_command_for_exit_command(input_command.get_string_entity())
|
except EmptyInputCommandException:
|
||||||
|
self.print_func(self.line_separate)
|
||||||
|
self._empty_input_command_handler()
|
||||||
self.print_func(self.line_separate)
|
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)
|
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
||||||
|
|
||||||
if is_unknown_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:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
continue
|
continue
|
||||||
@@ -124,17 +130,14 @@ class App:
|
|||||||
|
|
||||||
def set_description_message_pattern(self, pattern: str) -> None:
|
def set_description_message_pattern(self, pattern: str) -> None:
|
||||||
try:
|
try:
|
||||||
pattern.format(command='command',
|
pattern.format(command='command', description='description')
|
||||||
description='description')
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise InvalidDescriptionMessagePatternException(pattern)
|
raise InvalidDescriptionMessagePatternException(pattern)
|
||||||
|
else:
|
||||||
self._description_message_pattern: str = pattern
|
self._description_message_pattern: str = pattern
|
||||||
|
|
||||||
|
|
||||||
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||||
if self._invalid_input_flags_handler:
|
|
||||||
raise InvalidInputFlagsHandlerHasBeenAlreadyCreatedException()
|
|
||||||
else:
|
|
||||||
args = getfullargspec(handler).args
|
args = getfullargspec(handler).args
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
raise IncorrectNumberOfHandlerArgsException()
|
raise IncorrectNumberOfHandlerArgsException()
|
||||||
@@ -143,9 +146,6 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
||||||
if self._repeated_input_flags_handler:
|
|
||||||
raise RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException()
|
|
||||||
else:
|
|
||||||
args = getfullargspec(handler).args
|
args = getfullargspec(handler).args
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
raise IncorrectNumberOfHandlerArgsException()
|
raise IncorrectNumberOfHandlerArgsException()
|
||||||
@@ -154,9 +154,6 @@ class App:
|
|||||||
|
|
||||||
|
|
||||||
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
||||||
if self._unknown_command_handler:
|
|
||||||
raise UnknownCommandHandlerHasBeenAlreadyCreatedException()
|
|
||||||
else:
|
|
||||||
args = getfullargspec(handler).args
|
args = getfullargspec(handler).args
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
raise IncorrectNumberOfHandlerArgsException()
|
raise IncorrectNumberOfHandlerArgsException()
|
||||||
@@ -164,14 +161,6 @@ class App:
|
|||||||
self._unknown_command_handler = handler
|
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
|
|
||||||
|
|
||||||
|
|
||||||
def include_router(self, router: Router) -> None:
|
def include_router(self, router: Router) -> None:
|
||||||
if not isinstance(router, Router):
|
if not isinstance(router, Router):
|
||||||
raise InvalidRouterInstanceException()
|
raise InvalidRouterInstanceException()
|
||||||
@@ -213,7 +202,7 @@ class App:
|
|||||||
raise RepeatedCommandInDifferentRoutersException()
|
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 command.lower() == self.exit_command.lower():
|
||||||
if self.ignore_exit_command_register:
|
if self.ignore_exit_command_register:
|
||||||
self.print_func(self.farewell_message)
|
self.print_func(self.farewell_message)
|
||||||
@@ -234,14 +223,7 @@ class App:
|
|||||||
else:
|
else:
|
||||||
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
if command_entity['command'].get_string_entity() == command.get_string_entity():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self._unknown_command_handler:
|
|
||||||
self._unknown_command_handler(command)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ from .params.flag.entity import Flag
|
|||||||
from .params.flag.flags_group.entity import FlagsGroup
|
from .params.flag.flags_group.entity import FlagsGroup
|
||||||
from .exceptions import (InvalidCommandInstanceException,
|
from .exceptions import (InvalidCommandInstanceException,
|
||||||
InvalidDescriptionInstanceException,
|
InvalidDescriptionInstanceException,
|
||||||
InvalidFlagsInstanceException, UnprocessedInputFlagException, RepeatedInputFlagsException)
|
InvalidFlagsInstanceException,
|
||||||
|
UnprocessedInputFlagException,
|
||||||
|
RepeatedInputFlagsException,
|
||||||
|
EmptyInputCommandException)
|
||||||
|
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
@@ -71,6 +74,8 @@ class Command(Generic[T]):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_input_command(raw_command: str) -> 'Command[T]':
|
def parse_input_command(raw_command: str) -> 'Command[T]':
|
||||||
|
if not raw_command:
|
||||||
|
raise EmptyInputCommandException()
|
||||||
list_of_tokens = raw_command.split()
|
list_of_tokens = raw_command.split()
|
||||||
command = list_of_tokens[0]
|
command = list_of_tokens[0]
|
||||||
list_of_tokens.pop(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()}'")
|
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):
|
class IncorrectNumberOfHandlerArgsException(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"
|
||||||
|
|
||||||
|
|
||||||
|
class EmptyInputCommandException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Input Command is empty"
|
||||||
@@ -36,6 +36,7 @@ class Flag:
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if isinstance(self.possible_flag_values, list):
|
if isinstance(self.possible_flag_values, list):
|
||||||
if self.ignore_flag_value_register:
|
if self.ignore_flag_value_register:
|
||||||
if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]:
|
if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]:
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "argenta"
|
name = "argenta"
|
||||||
version = "0.3.3"
|
version = "0.3.4"
|
||||||
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"}
|
||||||
|
|||||||
@@ -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',
|
line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n',
|
||||||
print_func=Console().print,
|
print_func=Console().print,
|
||||||
command_group_description_separate='',
|
command_group_description_separate='',
|
||||||
repeat_command_groups=False)
|
repeat_command_groups=True)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user