From ebfd5a80b37342e930833cc4e96d7953ab666962 Mon Sep 17 00:00:00 2001 From: kolo Date: Thu, 13 Feb 2025 23:26:01 +0300 Subject: [PATCH] work on v0.3.0 --- argenta/command/__init__.py | 0 argenta/command/entity.py | 36 +++++++++++++++++ argenta/command/exceptions.py | 13 ++++++ argenta/command/params/__init__.py | 0 argenta/command/params/flag/__init__.py | 0 argenta/command/params/flag/entity.py | 40 +++++++++++++++++++ .../command/params/flags_group/__init__.py | 0 argenta/command/params/flags_group/entity.py | 3 ++ argenta/input_command/__init__.py | 0 argenta/input_command/entity.py | 3 ++ argenta/router/__init__.py | 3 +- argenta/router/entity.py | 19 +-------- argenta/router/exceptions.py | 5 --- pyproject.toml | 5 +-- tests/mock_app/main.py | 2 +- tests/mock_default_app/main.py | 1 - 16 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 argenta/command/__init__.py create mode 100644 argenta/command/entity.py create mode 100644 argenta/command/exceptions.py create mode 100644 argenta/command/params/__init__.py create mode 100644 argenta/command/params/flag/__init__.py create mode 100644 argenta/command/params/flag/entity.py create mode 100644 argenta/command/params/flags_group/__init__.py create mode 100644 argenta/command/params/flags_group/entity.py create mode 100644 argenta/input_command/__init__.py create mode 100644 argenta/input_command/entity.py diff --git a/argenta/command/__init__.py b/argenta/command/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/command/entity.py b/argenta/command/entity.py new file mode 100644 index 0000000..fa7fd8b --- /dev/null +++ b/argenta/command/entity.py @@ -0,0 +1,36 @@ +from .params.flag.entity import Flag +from .params.flags_group.entity import FlagsGroup +from .exceptions import (InvalidCommandInstanceException, + InvalidDescriptionInstanceException, + InvalidFlagsInstanceException) + + +class Command: + def __init__(self, command: str, + description: str | None = None, + flags: Flag | FlagsGroup | None = None): + self._command = command + self._description = description + self._flags = flags + + def get_string_entity(self): + return self._command + + def get_description(self): + if not self._description: + description = f'description for "{self._command}" command' + return description + else: + return self._description + + def get_flags(self): + return self._flags + + def validate_commands_params(self): + if not isinstance(self._command, str): + raise InvalidCommandInstanceException(self._command) + if isinstance(self._description, str): + raise InvalidDescriptionInstanceException() + if isinstance(self._flags, Flag) or isinstance(self._flags, FlagsGroup): + raise InvalidFlagsInstanceException + diff --git a/argenta/command/exceptions.py b/argenta/command/exceptions.py new file mode 100644 index 0000000..0c513be --- /dev/null +++ b/argenta/command/exceptions.py @@ -0,0 +1,13 @@ +class InvalidCommandInstanceException(Exception): + def __str__(self): + return "Invalid Command Instance" + + +class InvalidDescriptionInstanceException(Exception): + def __str__(self): + return "Invalid Description Instance" + + +class InvalidFlagsInstanceException(Exception): + def __str__(self): + return "Invalid Flags Instance" diff --git a/argenta/command/params/__init__.py b/argenta/command/params/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/command/params/flag/__init__.py b/argenta/command/params/flag/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/command/params/flag/entity.py b/argenta/command/params/flag/entity.py new file mode 100644 index 0000000..ca45eca --- /dev/null +++ b/argenta/command/params/flag/entity.py @@ -0,0 +1,40 @@ +from typing import Literal + + +class Flag: + def __init__(self, flag_name: str, + flag_prefix: Literal['-', '--', '---'] = '-', + ignore_flag_value_register: bool = False, + possible_flag_values: list[str] = False): + self.flag_name = flag_name + self.flag_prefix = flag_prefix + self.possible_flag_values = possible_flag_values + self.ignore_flag_value_register = ignore_flag_value_register + + def get_string_entity(self): + if self.ignore_flag_value_register: + string_entity: str = self.flag_prefix + self.flag_name.lower() + else: + string_entity: str = self.flag_prefix + self.flag_name + return string_entity + + def validate_input_flag_value(self, input_flag_value: str): + if self.possible_flag_values: + if self.ignore_flag_value_register: + if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]: + return True + else: + return False + else: + if input_flag_value in self.possible_flag_values: + return True + else: + return False + else: + return True + + + + +c = Flag('s', flag_prefix='---kinn', ignore_flag_value_register=False, possible_flag_values=['abc', 'ASW', 'eBc']) +print(c.get_string_entity()) diff --git a/argenta/command/params/flags_group/__init__.py b/argenta/command/params/flags_group/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/command/params/flags_group/entity.py b/argenta/command/params/flags_group/entity.py new file mode 100644 index 0000000..e8c798d --- /dev/null +++ b/argenta/command/params/flags_group/entity.py @@ -0,0 +1,3 @@ +class FlagsGroup: + def __init__(self, flags): + self.flags = flags diff --git a/argenta/input_command/__init__.py b/argenta/input_command/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/input_command/entity.py b/argenta/input_command/entity.py new file mode 100644 index 0000000..5b90617 --- /dev/null +++ b/argenta/input_command/entity.py @@ -0,0 +1,3 @@ +class InputCommand: + pass + diff --git a/argenta/router/__init__.py b/argenta/router/__init__.py index c622906..3387795 100644 --- a/argenta/router/__init__.py +++ b/argenta/router/__init__.py @@ -1,4 +1,3 @@ from .entity import Router from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException, - InvalidDescriptionInstanceException, - InvalidCommandInstanceException) \ No newline at end of file + InvalidDescriptionInstanceException) \ No newline at end of file diff --git a/argenta/router/entity.py b/argenta/router/entity.py index c9bcab0..88515d5 100644 --- a/argenta/router/entity.py +++ b/argenta/router/entity.py @@ -1,7 +1,5 @@ from typing import Callable, Any -from ..router.exceptions import (InvalidCommandInstanceException, - UnknownCommandHandlerHasAlreadyBeenCreatedException, - InvalidDescriptionInstanceException, +from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException, RepeatedCommandException) @@ -20,13 +18,12 @@ class Router: def command(self, command: str, description: str = None) -> Callable[[Any], Any]: - processed_description = Router._validate_description(command, description) self._validate_command(command) def command_decorator(func): self._command_entities.append({'handler_func': func, 'command': command, - 'description': processed_description}) + 'description': description}) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @@ -60,8 +57,6 @@ class Router: def _validate_command(self, command: str): - if not isinstance(command, str): - raise InvalidCommandInstanceException() if command in self.get_all_commands(): raise RepeatedCommandException() if self.ignore_command_register: @@ -69,16 +64,6 @@ class Router: raise RepeatedCommandException() - @staticmethod - def _validate_description(command: str, description: str): - if not isinstance(description, str): - if description is None: - description = f'description for "{command}" command' - else: - raise InvalidDescriptionInstanceException() - return description - - def set_router_as_main(self): if self.name == 'subordinate': self.name = 'main' diff --git a/argenta/router/exceptions.py b/argenta/router/exceptions.py index 117960a..edfee7f 100644 --- a/argenta/router/exceptions.py +++ b/argenta/router/exceptions.py @@ -1,8 +1,3 @@ -class InvalidCommandInstanceException(Exception): - def __str__(self): - return "Invalid Command Instance" - - class InvalidDescriptionInstanceException(Exception): def __str__(self): return "Invalid Description Instance" diff --git a/pyproject.toml b/pyproject.toml index 524a3cc..489c418 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,15 +1,14 @@ [project] name = "argenta" version = "0.2.2" -description = "python library for creating cli apps" +description = "python library for creating custom shells" authors = [ {name = "kolo",email = "kolo.is.main@gmail.com"} ] license = {text = "MIT"} readme = "README.md" requires-python = ">=3.11" -dependencies = [ -] +dependencies = [] # no dependencies [build-system] diff --git a/tests/mock_app/main.py b/tests/mock_app/main.py index da14dac..32937fd 100644 --- a/tests/mock_app/main.py +++ b/tests/mock_app/main.py @@ -26,7 +26,7 @@ def main(): app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}') - app.start_polling() + #app.start_polling() if __name__ == "__main__": main() diff --git a/tests/mock_default_app/main.py b/tests/mock_default_app/main.py index 75ff26e..f61b843 100644 --- a/tests/mock_default_app/main.py +++ b/tests/mock_default_app/main.py @@ -1,7 +1,6 @@ from pprint import pprint from tests.mock_default_app.handlers.routers import work_router, settings_router from argenta.app.entity import App -from art import text2art app: App = App(ignore_command_register=False,