mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
work on v0.3.0
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -0,0 +1,3 @@
|
||||
class InputCommand:
|
||||
pass
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from .entity import Router
|
||||
from .exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||
InvalidDescriptionInstanceException,
|
||||
InvalidCommandInstanceException)
|
||||
InvalidDescriptionInstanceException)
|
||||
@@ -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'
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
class InvalidCommandInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Command Instance"
|
||||
|
||||
|
||||
class InvalidDescriptionInstanceException(Exception):
|
||||
def __str__(self):
|
||||
return "Invalid Description Instance"
|
||||
|
||||
+2
-3
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user