work on v0.3.0

This commit is contained in:
2025-02-13 23:26:01 +03:00
parent 250704fc88
commit ebfd5a80b3
16 changed files with 101 additions and 29 deletions
View File
+36
View File
@@ -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
+13
View File
@@ -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"
View File
+40
View File
@@ -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())
@@ -0,0 +1,3 @@
class FlagsGroup:
def __init__(self, flags):
self.flags = flags
View File
+3
View File
@@ -0,0 +1,3 @@
class InputCommand:
pass
+1 -2
View File
@@ -1,4 +1,3 @@
from .entity import Router from .entity import Router
from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException, from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
InvalidDescriptionInstanceException, InvalidDescriptionInstanceException)
InvalidCommandInstanceException)
+2 -17
View File
@@ -1,7 +1,5 @@
from typing import Callable, Any from typing import Callable, Any
from ..router.exceptions import (InvalidCommandInstanceException, from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
UnknownCommandHandlerHasAlreadyBeenCreatedException,
InvalidDescriptionInstanceException,
RepeatedCommandException) RepeatedCommandException)
@@ -20,13 +18,12 @@ class Router:
def command(self, command: str, description: str = None) -> Callable[[Any], Any]: def command(self, command: str, description: str = None) -> Callable[[Any], Any]:
processed_description = Router._validate_description(command, description)
self._validate_command(command) self._validate_command(command)
def command_decorator(func): def command_decorator(func):
self._command_entities.append({'handler_func': func, self._command_entities.append({'handler_func': func,
'command': command, 'command': command,
'description': processed_description}) 'description': description})
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return func(*args, **kwargs) return func(*args, **kwargs)
return wrapper return wrapper
@@ -60,8 +57,6 @@ class Router:
def _validate_command(self, command: str): def _validate_command(self, command: str):
if not isinstance(command, str):
raise InvalidCommandInstanceException()
if command in self.get_all_commands(): if command in self.get_all_commands():
raise RepeatedCommandException() raise RepeatedCommandException()
if self.ignore_command_register: if self.ignore_command_register:
@@ -69,16 +64,6 @@ class Router:
raise RepeatedCommandException() 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): def set_router_as_main(self):
if self.name == 'subordinate': if self.name == 'subordinate':
self.name = 'main' self.name = 'main'
-5
View File
@@ -1,8 +1,3 @@
class InvalidCommandInstanceException(Exception):
def __str__(self):
return "Invalid Command Instance"
class InvalidDescriptionInstanceException(Exception): class InvalidDescriptionInstanceException(Exception):
def __str__(self): def __str__(self):
return "Invalid Description Instance" return "Invalid Description Instance"
+2 -3
View File
@@ -1,15 +1,14 @@
[project] [project]
name = "argenta" name = "argenta"
version = "0.2.2" version = "0.2.2"
description = "python library for creating cli apps" 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"}
] ]
license = {text = "MIT"} license = {text = "MIT"}
readme = "README.md" readme = "README.md"
requires-python = ">=3.11" requires-python = ">=3.11"
dependencies = [ dependencies = [] # no dependencies
]
[build-system] [build-system]
+1 -1
View File
@@ -26,7 +26,7 @@ def main():
app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}') 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__": if __name__ == "__main__":
main() main()
-1
View File
@@ -1,7 +1,6 @@
from pprint import pprint from pprint import pprint
from tests.mock_default_app.handlers.routers import work_router, settings_router from tests.mock_default_app.handlers.routers import work_router, settings_router
from argenta.app.entity import App from argenta.app.entity import App
from art import text2art
app: App = App(ignore_command_register=False, app: App = App(ignore_command_register=False,