diff --git a/mock/local_test.py b/mock/local_test.py index 741b5eb..fc09521 100644 --- a/mock/local_test.py +++ b/mock/local_test.py @@ -10,22 +10,13 @@ from argenta.orchestrator import Orchestrator router = Router() -@router.command(Command('case are')) + +@router.command(Command("case are")) def handler(response: Response): print(response.status) - app = App(repeat_command_groups=False) app.include_router(router) app.run_polling() - - - - - - - - - diff --git a/mock/mock_app/routers.py b/mock/mock_app/routers.py index 6f83e99..86fbc97 100644 --- a/mock/mock_app/routers.py +++ b/mock/mock_app/routers.py @@ -21,7 +21,7 @@ console = Console() ) ) def command_help(response: Response): - case = input('test > ') + case = input("test > ") print(case) print(response.status) print(response.undefined_flags.get_flags()) diff --git a/src/argenta/app/defaults.py b/src/argenta/app/defaults.py index 2908cef..6e819de 100644 --- a/src/argenta/app/defaults.py +++ b/src/argenta/app/defaults.py @@ -5,6 +5,7 @@ class PredefinedMessages(StrEnum): """ Public. A dataclass with predetermined messages for quick use """ + USAGE = "[b dim]Usage[/b dim]: [i] <[green]flags[/green]>[/i]" HELP = "[b dim]Help[/b dim]: [i][/i] [b red]--help[/b red]" AUTOCOMPLETE = "[b dim]Autocomplete[/b dim]: [i][/i] [bold]" diff --git a/src/argenta/app/models.py b/src/argenta/app/models.py index 7d15d03..4af13ff 100644 --- a/src/argenta/app/models.py +++ b/src/argenta/app/models.py @@ -22,18 +22,20 @@ from argenta.response import Response class BaseApp: - def __init__(self, - prompt: str, - initial_message: str, - farewell_message: str, - exit_command: Command, - system_router_title: str | None, - ignore_command_register: bool, - dividing_line: StaticDividingLine | DynamicDividingLine, - repeat_command_groups: bool, - override_system_messages: bool, - autocompleter: AutoCompleter, - print_func: Callable[[str], None]) -> None: + def __init__( + self, + prompt: str, + initial_message: str, + farewell_message: str, + exit_command: Command, + system_router_title: str | None, + ignore_command_register: bool, + dividing_line: StaticDividingLine | DynamicDividingLine, + repeat_command_groups: bool, + override_system_messages: bool, + autocompleter: AutoCompleter, + print_func: Callable[[str], None], + ) -> None: self._prompt = prompt self._print_func = print_func self._exit_command = exit_command @@ -47,18 +49,30 @@ class BaseApp: self._farewell_message = farewell_message self._initial_message = initial_message - self._description_message_gen: Callable[[str, str], str] = (lambda command, description: f"[{command}] *=*=* {description}") + self._description_message_gen: Callable[[str, str], str] = ( + lambda command, description: f"[{command}] *=*=* {description}" + ) self._registered_routers: RegisteredRouters = RegisteredRouters() self._messages_on_startup: list[str] = [] self._all_registered_triggers_in_lower_case: list[str] = [] self._all_registered_triggers_in_default_case: list[str] = [] - self._incorrect_input_syntax_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("Empty input command") - self._unknown_command_handler: Callable[[InputCommand], None] = (lambda command: print_func(f"Unknown command: {command.get_trigger()}")) - self._exit_command_handler: Callable[[Response], None] = (lambda response: print_func(self._farewell_message)) + self._incorrect_input_syntax_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( + "Empty input command" + ) + self._unknown_command_handler: Callable[[InputCommand], None] = ( + lambda command: print_func(f"Unknown command: {command.get_trigger()}") + ) + self._exit_command_handler: Callable[[Response], None] = ( + lambda response: print_func(self._farewell_message) + ) def set_description_message_pattern(self, _: Callable[[str, str], str]) -> None: """ @@ -194,14 +208,19 @@ class BaseApp: """ input_command_trigger = command.get_trigger() if self._ignore_command_register: - if input_command_trigger.lower() in self._all_registered_triggers_in_lower_case: + if ( + input_command_trigger.lower() + in self._all_registered_triggers_in_lower_case + ): return False else: if input_command_trigger in self._all_registered_triggers_in_default_case: return False return True - def _error_handler(self, error: BaseInputCommandException, raw_command: str) -> None: + def _error_handler( + self, error: BaseInputCommandException, raw_command: str + ) -> None: """ Private. Handles parsing errors of the entered command :param error: error being handled @@ -257,10 +276,12 @@ class BaseApp: """ self._prompt = "[italic dim bold]What do you want to do?\n" self._initial_message = ( - "\n"+f"[bold red]{text2art(self._initial_message, font='tarty1')}"+"\n" + "\n" + f"[bold red]{text2art(self._initial_message, font='tarty1')}" + "\n" ) self._farewell_message = ( - "[bold red]\n\n"+text2art(self._farewell_message, font='chanky')+"\n[/bold red]\n" + "[bold red]\n\n" + + text2art(self._farewell_message, font="chanky") + + "\n[/bold red]\n" "[red i]github.com/koloideal/Argenta[/red i] | [red bold i]made by kolo[/red bold i]\n" ) self._description_message_gen = lambda command, description: ( @@ -307,7 +328,9 @@ class BaseApp: self._all_registered_triggers_in_default_case.extend(combined) - self._all_registered_triggers_in_lower_case.extend(x.lower() for x in combined) + self._all_registered_triggers_in_lower_case.extend( + x.lower() for x in combined + ) self._autocompleter.initial_setup(self._all_registered_triggers_in_lower_case) @@ -315,14 +338,18 @@ class BaseApp: seen = {} for item in self._all_registered_triggers_in_lower_case: if item in seen: - Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{item}[/b blue]") + Console().print( + f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{item}[/b blue]" + ) else: seen[item] = True else: seen = {} for item in self._all_registered_triggers_in_default_case: if item in seen: - Console().print(f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{item}[/b blue]") + Console().print( + f"\n[b red]WARNING:[/b red] Overlapping trigger or alias: [b blue]{item}[/b blue]" + ) else: seen[item] = True @@ -340,18 +367,20 @@ class BaseApp: class App(BaseApp): - def __init__(self, - prompt: str = "What do you want to do?\n", - initial_message: str = "Argenta\n", - farewell_message: str = "\nSee you\n", - exit_command: Command = Command("Q", "Exit command"), - system_router_title: str | None = "System points:", - ignore_command_register: bool = True, - dividing_line: StaticDividingLine | DynamicDividingLine = StaticDividingLine(), - repeat_command_groups: bool = True, - override_system_messages: bool = False, - autocompleter: AutoCompleter = AutoCompleter(), - print_func: Callable[[str], None] = Console().print) -> None: + def __init__( + self, + prompt: str = "What do you want to do?\n", + initial_message: str = "Argenta\n", + farewell_message: str = "\nSee you\n", + exit_command: Command = Command("Q", "Exit command"), + system_router_title: str | None = "System points:", + ignore_command_register: bool = True, + dividing_line: StaticDividingLine | DynamicDividingLine = StaticDividingLine(), + repeat_command_groups: bool = True, + override_system_messages: bool = False, + autocompleter: AutoCompleter = AutoCompleter(), + print_func: Callable[[str], None] = Console().print, + ) -> None: """ Public. The essence of the application itself. Configures and manages all aspects of the behavior and presentation of the user interacting with the user @@ -368,17 +397,19 @@ class App(BaseApp): :param print_func: system messages text output function :return: None """ - super().__init__(prompt=prompt, - initial_message=initial_message, - farewell_message=farewell_message, - exit_command=exit_command, - system_router_title=system_router_title, - ignore_command_register=ignore_command_register, - dividing_line=dividing_line, - repeat_command_groups=repeat_command_groups, - override_system_messages=override_system_messages, - autocompleter=autocompleter, - print_func=print_func) + super().__init__( + prompt=prompt, + initial_message=initial_message, + farewell_message=farewell_message, + exit_command=exit_command, + system_router_title=system_router_title, + ignore_command_register=ignore_command_register, + dividing_line=dividing_line, + repeat_command_groups=repeat_command_groups, + override_system_messages=override_system_messages, + autocompleter=autocompleter, + print_func=print_func, + ) def run_polling(self) -> None: """ @@ -425,13 +456,29 @@ class App(BaseApp): for registered_router in self._registered_routers: if registered_router.disable_redirect_stdout: if isinstance(self._dividing_line, StaticDividingLine): - self._print_func(self._dividing_line.get_full_static_line(self._override_system_messages)) + self._print_func( + self._dividing_line.get_full_static_line( + self._override_system_messages + ) + ) registered_router.finds_appropriate_handler(input_command) - self._print_func(self._dividing_line.get_full_static_line(self._override_system_messages)) + self._print_func( + self._dividing_line.get_full_static_line( + self._override_system_messages + ) + ) else: - self._print_func(StaticDividingLine(self._dividing_line.get_unit_part()).get_full_static_line(self._override_system_messages)) + self._print_func( + StaticDividingLine( + self._dividing_line.get_unit_part() + ).get_full_static_line(self._override_system_messages) + ) registered_router.finds_appropriate_handler(input_command) - self._print_func(StaticDividingLine(self._dividing_line.get_unit_part()).get_full_static_line(self._override_system_messages)) + self._print_func( + StaticDividingLine( + self._dividing_line.get_unit_part() + ).get_full_static_line(self._override_system_messages) + ) else: with redirect_stdout(io.StringIO()) as f: registered_router.finds_appropriate_handler(input_command) diff --git a/src/argenta/command/flag/__init__.py b/src/argenta/command/flag/__init__.py index fc02a90..ba35e39 100644 --- a/src/argenta/command/flag/__init__.py +++ b/src/argenta/command/flag/__init__.py @@ -1,7 +1,17 @@ -__all__ = ["Flag", "InputFlag", "UndefinedInputFlags", "ValidInputFlags", "InvalidValueInputFlags", "Flags"] +__all__ = [ + "Flag", + "InputFlag", + "UndefinedInputFlags", + "ValidInputFlags", + "InvalidValueInputFlags", + "Flags", +] from argenta.command.flag.models import Flag, InputFlag -from argenta.command.flag.flags.models import (UndefinedInputFlags, - ValidInputFlags, Flags, - InvalidValueInputFlags) +from argenta.command.flag.flags.models import ( + UndefinedInputFlags, + ValidInputFlags, + Flags, + InvalidValueInputFlags, +) diff --git a/src/argenta/command/flag/models.py b/src/argenta/command/flag/models.py index 7e1546f..c533292 100644 --- a/src/argenta/command/flag/models.py +++ b/src/argenta/command/flag/models.py @@ -87,7 +87,10 @@ class Flag(BaseFlag): class InputFlag(BaseFlag): def __init__( - self, name: str, prefix: Literal["-", "--", "---"] = "--", value: str | None = None + self, + name: str, + prefix: Literal["-", "--", "---"] = "--", + value: str | None = None, ): """ Public. The entity of the flag of the entered command diff --git a/src/argenta/command/models.py b/src/argenta/command/models.py index 1e83c82..ab33f93 100644 --- a/src/argenta/command/models.py +++ b/src/argenta/command/models.py @@ -139,7 +139,7 @@ class InputCommand(BaseCommand): return self._input_flags @staticmethod - def parse(raw_command: str) -> 'InputCommand': + def parse(raw_command: str) -> "InputCommand": """ Private. Parse the raw input command :param raw_command: raw input command diff --git a/src/argenta/metrics/main.py b/src/argenta/metrics/main.py index 182354b..bcff06b 100644 --- a/src/argenta/metrics/main.py +++ b/src/argenta/metrics/main.py @@ -16,11 +16,3 @@ def get_time_of_pre_cycle_setup(app: App) -> float: app.pre_cycle_setup() end = time() return end - start - - - - - - - - diff --git a/src/argenta/router/entity.py b/src/argenta/router/entity.py index 54d8596..199915d 100644 --- a/src/argenta/router/entity.py +++ b/src/argenta/router/entity.py @@ -22,7 +22,9 @@ from argenta.router.exceptions import ( class Router: - def __init__(self, title: str | None = "Awesome title", disable_redirect_stdout: bool = False): + def __init__( + self, title: str | None = "Awesome title", disable_redirect_stdout: bool = False + ): """ Public. Directly configures and manages handlers :param title: the title of the router, displayed when displaying the available commands @@ -196,10 +198,12 @@ class Router: file_path: str | None = getsourcefile(func) source_line: int = getsourcelines(func)[1] fprint = Console().print - fprint(f'\nFile "{file_path}", line {source_line}\n[b red]WARNING:[/b red] [i]The typehint ' + fprint( + f'\nFile "{file_path}", line {source_line}\n[b red]WARNING:[/b red] [i]The typehint ' f"of argument([green]{transferred_arg}[/green]) passed to the handler is [/i][bold blue]{Response}[/bold blue]," f" [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]", - highlight=False) + highlight=False, + ) def set_command_register_ignore(self, _: bool) -> None: """