mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work on v0.3.0
This commit is contained in:
+2
-2
@@ -6,8 +6,8 @@ argenta/__pycache__/
|
|||||||
argenta/command/__pycache__
|
argenta/command/__pycache__
|
||||||
argenta/command/params/__pycache__
|
argenta/command/params/__pycache__
|
||||||
argenta/command/params/flag/__pycache__
|
argenta/command/params/flag/__pycache__
|
||||||
argenta/command/params/flags_group/__pycache__
|
argenta/command/params/flag/flags_group/__pycache__
|
||||||
argenta/command/params/input_flag/__pycache__
|
argenta/command/params/flag/input_flag/__pycache__
|
||||||
dist
|
dist
|
||||||
poetry.lock
|
poetry.lock
|
||||||
tests/__pycache__
|
tests/__pycache__
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
from argenta.command.input_comand.entity import InputCommand
|
from argenta.command.input_comand.entity import InputCommand
|
||||||
from argenta.command.input_comand.exceptions import InvalidInputFlagsException
|
from argenta.command.input_comand.exceptions import InvalidInputFlagException
|
||||||
from ..router.entity import Router
|
from ..router.entity import Router
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
@@ -68,26 +68,25 @@ class App:
|
|||||||
|
|
||||||
raw_command: str = input()
|
raw_command: str = input()
|
||||||
try:
|
try:
|
||||||
command: Command = InputCommand.parse(raw_command=raw_command)
|
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
|
||||||
except InvalidInputFlagsException:
|
except InvalidInputFlagException:
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
self.print_func(self.command_group_description_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
|
||||||
|
|
||||||
|
self._checking_command_for_exit_command(input_command.get_string_entity())
|
||||||
self._checking_command_for_exit_command(command.get_string_entity())
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
|
|
||||||
is_unknown_command: bool = self._check_is_command_unknown(command.get_string_entity())
|
is_unknown_command: bool = self._check_is_command_unknown(input_command.get_string_entity())
|
||||||
if is_unknown_command:
|
if is_unknown_command:
|
||||||
if not self.repeat_command_groups:
|
if not self.repeat_command_groups:
|
||||||
self.print_func(self.prompt)
|
self.print_func(self.prompt)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for router in self._routers:
|
for router in self._routers:
|
||||||
router.input_command_handler(command)
|
router.input_command_handler(input_command)
|
||||||
|
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
self.print_func(self.command_group_description_separate)
|
||||||
@@ -208,7 +207,7 @@ class App:
|
|||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if command_entity['command'] == command:
|
if command_entity['command'].get_string_entity() == command:
|
||||||
return False
|
return False
|
||||||
self._app_main_router.unknown_command_handler(command)
|
self._app_main_router.unknown_command_handler(command)
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from .params.flag.entity import Flag
|
from .params.flag.entity import Flag
|
||||||
from .params.flags_group.entity import FlagsGroup
|
from .params.flag.flags_group.entity import FlagsGroup
|
||||||
from .exceptions import (InvalidCommandInstanceException,
|
from .exceptions import (InvalidCommandInstanceException,
|
||||||
InvalidDescriptionInstanceException,
|
InvalidDescriptionInstanceException,
|
||||||
InvalidFlagsInstanceException)
|
InvalidFlagsInstanceException)
|
||||||
|
from .params.flag.input_flag.entity import InputFlag
|
||||||
|
|
||||||
|
|
||||||
class Command:
|
class Command:
|
||||||
@@ -13,7 +14,7 @@ class Command:
|
|||||||
self._description = description
|
self._description = description
|
||||||
self._flags = flags
|
self._flags = flags
|
||||||
|
|
||||||
self._input_flags = None
|
self._input_flags: FlagsGroup | None = None
|
||||||
|
|
||||||
def get_string_entity(self):
|
def get_string_entity(self):
|
||||||
return self._command
|
return self._command
|
||||||
@@ -39,3 +40,15 @@ class Command:
|
|||||||
if not (isinstance(self._flags, Flag) or not isinstance(self._flags, FlagsGroup)) or not self._flags is None:
|
if not (isinstance(self._flags, Flag) or not isinstance(self._flags, FlagsGroup)) or not self._flags is None:
|
||||||
raise InvalidFlagsInstanceException
|
raise InvalidFlagsInstanceException
|
||||||
|
|
||||||
|
def validate_input_flag(self, flag: InputFlag):
|
||||||
|
registered_flags: FlagsGroup = self._flags
|
||||||
|
if registered_flags:
|
||||||
|
for registered_flag in registered_flags:
|
||||||
|
if registered_flag.get_string_entity() == flag.get_string_entity():
|
||||||
|
is_valid = registered_flag.validate_input_flag_value(flag)
|
||||||
|
if is_valid:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,35 +1,40 @@
|
|||||||
from argenta.command.input_comand.exceptions import InvalidInputFlagsException
|
from ..input_comand.exceptions import IncorrectInputFlagException
|
||||||
from ..entity import Command
|
from ..entity import Command
|
||||||
from ..params.flags_group.entity import FlagsGroup
|
from ..params.flag.flags_group.entity import FlagsGroup
|
||||||
from ..params.input_flag.entity import InputFlag
|
from ..params.flag.input_flag.entity import InputFlag
|
||||||
|
|
||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
|
||||||
class InputCommand(Command):
|
T = TypeVar('T')
|
||||||
def set_input_flags(self, input_flags: list[InputFlag]):
|
|
||||||
|
|
||||||
|
class InputCommand(Command, Generic[T]):
|
||||||
|
def set_input_flags(self, input_flags: FlagsGroup):
|
||||||
self._input_flags = input_flags
|
self._input_flags = input_flags
|
||||||
|
|
||||||
def get_input_flags(self) -> list[InputFlag]:
|
def get_input_flags(self) -> FlagsGroup:
|
||||||
return self._input_flags
|
return self._input_flags
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse(raw_command: str) -> Command:
|
def parse(raw_command: str) -> 'InputCommand[T]':
|
||||||
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)
|
||||||
|
|
||||||
flags = []
|
flags: FlagsGroup = FlagsGroup()
|
||||||
current_flag_name = None
|
current_flag_name = None
|
||||||
current_flag_value = None
|
current_flag_value = None
|
||||||
for _ in list_of_tokens:
|
for _ in list_of_tokens:
|
||||||
flag_prefix_last_symbol_index = _.rfind('-')
|
flag_prefix_last_symbol_index = _.rfind('-')
|
||||||
if _.startswith('-'):
|
if _.startswith('-'):
|
||||||
if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
|
if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
|
||||||
raise InvalidInputFlagsException
|
raise IncorrectInputFlagException()
|
||||||
else:
|
else:
|
||||||
current_flag_name = _
|
current_flag_name = _
|
||||||
else:
|
else:
|
||||||
if not current_flag_name:
|
if not current_flag_name:
|
||||||
raise InvalidInputFlagsException
|
raise IncorrectInputFlagException()
|
||||||
else:
|
else:
|
||||||
current_flag_value = _
|
current_flag_value = _
|
||||||
if current_flag_name and current_flag_value:
|
if current_flag_name and current_flag_value:
|
||||||
@@ -40,15 +45,15 @@ class InputCommand(Command):
|
|||||||
flag_prefix=flag_prefix)
|
flag_prefix=flag_prefix)
|
||||||
input_flag.set_value(current_flag_value)
|
input_flag.set_value(current_flag_value)
|
||||||
|
|
||||||
flags.append(input_flag)
|
flags.add_flag(input_flag)
|
||||||
|
|
||||||
current_flag_name = None
|
current_flag_name = None
|
||||||
current_flag_value = None
|
current_flag_value = None
|
||||||
|
if len(flags.get_flags()) == 0:
|
||||||
if len(flags) == 0:
|
return InputCommand(command=command)
|
||||||
return Command(command=command)
|
|
||||||
else:
|
else:
|
||||||
flags = FlagsGroup(flags=flags)
|
input_command = InputCommand(command=command)
|
||||||
return Command(command=command, flags=flags)
|
input_command.set_input_flags(flags)
|
||||||
|
return input_command
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,16 @@
|
|||||||
class InvalidInputFlagsException(Exception):
|
from ..params.flag.input_flag.entity import InputFlag
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidInputFlagException(Exception):
|
||||||
|
def __init__(self, flag: InputFlag):
|
||||||
|
self.flag = flag
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Invalid Input Flags"
|
return ("Invalid Input Flags\n"
|
||||||
|
f"Unknown or invalid input flag: '{self.flag.get_string_entity()} {self.flag.get_value()}'")
|
||||||
|
|
||||||
|
class IncorrectInputFlagException(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
return "Incorrect Input Flags"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class Flag:
|
|||||||
string_entity: str = self.flag_prefix + self.flag_name.lower()
|
string_entity: str = self.flag_prefix + self.flag_name.lower()
|
||||||
else:
|
else:
|
||||||
string_entity: str = self.flag_prefix + self.flag_name
|
string_entity: str = self.flag_prefix + self.flag_name
|
||||||
|
print(string_entity)
|
||||||
return string_entity
|
return string_entity
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
from argenta.command.params.flag.entity import Flag
|
||||||
|
from argenta.command.params.flag.input_flag.entity import InputFlag
|
||||||
|
|
||||||
|
|
||||||
|
class FlagsGroup:
|
||||||
|
def __init__(self, flags: list[Flag | InputFlag] = None):
|
||||||
|
self._flags: list[Flag | InputFlag] = [] if not flags else flags
|
||||||
|
|
||||||
|
def get_flags(self):
|
||||||
|
return self._flags
|
||||||
|
|
||||||
|
def add_flag(self, flag: Flag | InputFlag):
|
||||||
|
self._flags.append(flag)
|
||||||
|
|
||||||
|
def add_flags(self, flags: list[Flag | InputFlag]):
|
||||||
|
self._flags.extend(flags)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._flags)
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
return next(iter(self))
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
from ...params.flag.entity import Flag
|
from ...flag.entity import Flag
|
||||||
|
|
||||||
|
|
||||||
class InputFlag(Flag):
|
class InputFlag(Flag):
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
from argenta.command.params.flag.entity import Flag
|
|
||||||
|
|
||||||
|
|
||||||
class FlagsGroup:
|
|
||||||
def __init__(self, flags: list[Flag]):
|
|
||||||
self.flags = flags
|
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
|
from ..command.input_comand.entity import InputCommand
|
||||||
|
from ..command.input_comand.exceptions import InvalidInputFlagException
|
||||||
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||||
RepeatedCommandException)
|
RepeatedCommandException)
|
||||||
|
|
||||||
@@ -43,14 +45,28 @@ class Router:
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def input_command_handler(self, input_command: Command):
|
def input_command_handler(self, input_command: InputCommand):
|
||||||
input_command_name: str = input_command.get_string_entity()
|
input_command_name: str = input_command.get_string_entity()
|
||||||
for command_entity in self._command_entities:
|
for command_entity in self._command_entities:
|
||||||
if input_command_name.lower() == command_entity['command'].get_string_entity():
|
if input_command_name.lower() == command_entity['command'].get_string_entity().lower():
|
||||||
if self.ignore_command_register:
|
if self.ignore_command_register:
|
||||||
|
if input_command.get_input_flags():
|
||||||
|
for flag in input_command.get_input_flags():
|
||||||
|
is_valid = command_entity['command'].validate_input_flag(flag)
|
||||||
|
if not is_valid:
|
||||||
|
raise InvalidInputFlagException(flag)
|
||||||
|
return command_entity['handler_func'](input_command.get_input_flags())
|
||||||
|
else:
|
||||||
return command_entity['handler_func']()
|
return command_entity['handler_func']()
|
||||||
else:
|
else:
|
||||||
if input_command == command_entity['command']:
|
if input_command_name == command_entity['command'].get_string_entity():
|
||||||
|
if input_command.get_input_flags():
|
||||||
|
for flag in input_command.get_input_flags():
|
||||||
|
is_valid = command_entity['command'].validate_input_flag(flag)
|
||||||
|
if not is_valid:
|
||||||
|
raise InvalidInputFlagException(flag)
|
||||||
|
return command_entity['handler_func'](input_command.get_input_flags())
|
||||||
|
else:
|
||||||
return command_entity['handler_func']()
|
return command_entity['handler_func']()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
from argenta.command.entity import Command
|
from argenta.command.entity import Command
|
||||||
|
from argenta.command.params.flag.flags_group.entity import FlagsGroup
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
|
|
||||||
from ..handlers.handlers_implementation.help_command import help_command
|
from ..handlers.handlers_implementation.help_command import help_command
|
||||||
from ..handlers.handlers_implementation.solving_command import start_solving_command
|
|
||||||
from ..handlers.handlers_implementation.upgrade_command import upgrade_command
|
|
||||||
|
|
||||||
|
|
||||||
work_router: Router = Router(title='Work points:')
|
work_router: Router = Router(title='Work points:')
|
||||||
settings_router: Router = Router(title='Settings points:')
|
settings_router: Router = Router(title='Settings points:')
|
||||||
@@ -15,7 +13,9 @@ console = Console()
|
|||||||
|
|
||||||
|
|
||||||
@work_router.command(command=Command(command='0', description='Get Help'))
|
@work_router.command(command=Command(command='0', description='Get Help'))
|
||||||
def command_help():
|
def command_help(args: FlagsGroup):
|
||||||
|
print(args.get_flags())
|
||||||
|
print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
|
||||||
help_command()
|
help_command()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+8
-5
@@ -1,6 +1,9 @@
|
|||||||
from typing import Literal, LiteralString
|
class Entity:
|
||||||
|
pass
|
||||||
|
|
||||||
x: LiteralString = '---'
|
class Person(Entity):
|
||||||
m: LiteralString = input()
|
pass
|
||||||
if m == '-':
|
|
||||||
x = m
|
a: Entity = Entity()
|
||||||
|
print(a)
|
||||||
|
a = Person()
|
||||||
Reference in New Issue
Block a user