ruff format

This commit is contained in:
2025-05-09 23:25:21 +03:00
parent 9a78aa9263
commit 13f7e33db1
29 changed files with 471 additions and 298 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
__all__ = ["Command"]
from argenta.command.models import Command
from argenta.command.models import Command
+9 -2
View File
@@ -5,6 +5,7 @@ class BaseInputCommandException(Exception):
"""
Private. Base exception class for all exceptions raised when parse input command
"""
pass
@@ -12,6 +13,7 @@ class UnprocessedInputFlagException(BaseInputCommandException):
"""
Private. Raised when an unprocessed input flag is detected
"""
def __str__(self):
return "Unprocessed Input Flags"
@@ -20,16 +22,21 @@ class RepeatedInputFlagsException(BaseInputCommandException):
"""
Private. Raised when repeated input flags are detected
"""
def __init__(self, flag: Flag | InputFlag):
self.flag = flag
def __str__(self):
return ("Repeated Input Flags\n"
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
return (
"Repeated Input Flags\n"
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'"
)
class EmptyInputCommandException(BaseInputCommandException):
"""
Private. Raised when an empty input command is detected
"""
def __str__(self):
return "Input Command is empty"
+17 -10
View File
@@ -8,17 +8,24 @@ class PredefinedFlags:
"""
Public. A dataclass with predefined flags and most frequently used flags for quick use
"""
HELP = Flag(name='help', possible_values=False)
SHORT_HELP = Flag(name='H', prefix='-', possible_values=False)
INFO = Flag(name='info', possible_values=False)
SHORT_INFO = Flag(name='I', prefix='-', possible_values=False)
HELP = Flag(name="help", possible_values=False)
SHORT_HELP = Flag(name="H", prefix="-", possible_values=False)
ALL = Flag(name='all', possible_values=False)
SHORT_ALL = Flag(name='A', prefix='-', possible_values=False)
INFO = Flag(name="info", possible_values=False)
SHORT_INFO = Flag(name="I", prefix="-", possible_values=False)
HOST = Flag(name='host', possible_values=re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'))
SHORT_HOST = Flag(name='H', prefix='-', possible_values=re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'))
ALL = Flag(name="all", possible_values=False)
SHORT_ALL = Flag(name="A", prefix="-", possible_values=False)
PORT = Flag(name='port', possible_values=re.compile(r'^\d{1,5}$'))
SHORT_PORT = Flag(name='P', prefix='-', possible_values=re.compile(r'^\d{1,5}$'))
HOST = Flag(
name="host", possible_values=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
)
SHORT_HOST = Flag(
name="H",
prefix="-",
possible_values=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"),
)
PORT = Flag(name="port", possible_values=re.compile(r"^\d{1,5}$"))
SHORT_PORT = Flag(name="P", prefix="-", possible_values=re.compile(r"^\d{1,5}$"))
+14 -11
View File
@@ -1,10 +1,8 @@
from typing import Literal, Pattern
class BaseFlag:
def __init__(self, name: str,
prefix: Literal['-', '--', '---'] = '--') -> None:
def __init__(self, name: str, prefix: Literal["-", "--", "---"] = "--") -> None:
"""
Private. Base class for flags
:param name: the name of the flag
@@ -41,9 +39,12 @@ class BaseFlag:
class Flag(BaseFlag):
def __init__(self, name: str,
prefix: Literal['-', '--', '---'] = '--',
possible_values: list[str] | Pattern[str] | False = True) -> None:
def __init__(
self,
name: str,
prefix: Literal["-", "--", "---"] = "--",
possible_values: list[str] | Pattern[str] | False = True,
) -> None:
"""
Public. The entity of the flag being registered for subsequent processing
:param name: The name of the flag
@@ -85,9 +86,9 @@ class Flag(BaseFlag):
class InputFlag(BaseFlag):
def __init__(self, name: str,
prefix: Literal['-', '--', '---'] = '--',
value: str = None):
def __init__(
self, name: str, prefix: Literal["-", "--", "---"] = "--", value: str = None
):
"""
Public. The entity of the flag of the entered command
:param name: the name of the input flag
@@ -114,5 +115,7 @@ class InputFlag(BaseFlag):
self._flag_value = value
def __eq__(self, other) -> bool:
return self.get_string_entity() == other.get_string_entity() and self.get_value() == other.get_value()
return (
self.get_string_entity() == other.get_string_entity()
and self.get_value() == other.get_value()
)
+14 -8
View File
@@ -1,10 +1,16 @@
__all__ = ["Flags", "InputFlags",
"UndefinedInputFlags",
"InvalidValueInputFlags",
"ValidInputFlags"]
__all__ = [
"Flags",
"InputFlags",
"UndefinedInputFlags",
"InvalidValueInputFlags",
"ValidInputFlags",
]
from argenta.command.flags.models import (Flags, InputFlags,
UndefinedInputFlags,
InvalidValueInputFlags,
ValidInputFlags)
from argenta.command.flags.models import (
Flags,
InputFlags,
UndefinedInputFlags,
InvalidValueInputFlags,
ValidInputFlags,
)
+1 -3
View File
@@ -2,8 +2,7 @@ from argenta.command.flag.models import InputFlag, Flag
from typing import Generic, TypeVar
FlagType = TypeVar('FlagType')
FlagType = TypeVar("FlagType")
class BaseFlags(Generic[FlagType]):
@@ -89,4 +88,3 @@ class UndefinedInputFlags(InputFlags):
class InvalidValueInputFlags(InputFlags):
pass
+60 -35
View File
@@ -1,12 +1,14 @@
from argenta.command.flag.models import Flag, InputFlag
from argenta.command.flags.models import InputFlags, Flags
from argenta.command.exceptions import (UnprocessedInputFlagException,
RepeatedInputFlagsException,
EmptyInputCommandException)
from argenta.command.exceptions import (
UnprocessedInputFlagException,
RepeatedInputFlagsException,
EmptyInputCommandException,
)
from typing import Generic, TypeVar, cast, Literal
InputCommandType = TypeVar('InputCommandType')
InputCommandType = TypeVar("InputCommandType")
class BaseCommand:
@@ -26,10 +28,13 @@ class BaseCommand:
class Command(BaseCommand):
def __init__(self, trigger: str,
description: str = None,
flags: Flag | Flags = None,
aliases: list[str] = None):
def __init__(
self,
trigger: str,
description: str = None,
flags: Flag | Flags = None,
aliases: list[str] = None,
):
"""
Public. The command that can and should be registered in the Router
:param trigger: A string trigger, which, when entered by the user, indicates that the input corresponds to the command
@@ -38,8 +43,14 @@ class Command(BaseCommand):
:param aliases: string synonyms for the main trigger
"""
super().__init__(trigger)
self._registered_flags: Flags = flags if isinstance(flags, Flags) else Flags(flags) if isinstance(flags, Flag) else Flags()
self._description = 'Very useful command' if not description else description
self._registered_flags: Flags = (
flags
if isinstance(flags, Flags)
else Flags(flags)
if isinstance(flags, Flag)
else Flags()
)
self._description = "Very useful command" if not description else description
self._aliases = aliases if isinstance(aliases, list) else []
def get_registered_flags(self) -> Flags:
@@ -56,7 +67,9 @@ class Command(BaseCommand):
"""
return self._aliases
def validate_input_flag(self, flag: InputFlag) -> Literal['Undefined', 'Valid', 'Invalid']:
def validate_input_flag(
self, flag: InputFlag
) -> Literal["Undefined", "Valid", "Invalid"]:
"""
Private. Validates the input flag
:param flag: input flag for validation
@@ -66,23 +79,27 @@ class Command(BaseCommand):
if registered_flags:
if isinstance(registered_flags, Flag):
if registered_flags.get_string_entity() == flag.get_string_entity():
is_valid = registered_flags.validate_input_flag_value(flag.get_value())
is_valid = registered_flags.validate_input_flag_value(
flag.get_value()
)
if is_valid:
return 'Valid'
return "Valid"
else:
return 'Invalid'
return "Invalid"
else:
return 'Undefined'
return "Undefined"
else:
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.get_value())
is_valid = registered_flag.validate_input_flag_value(
flag.get_value()
)
if is_valid:
return 'Valid'
return "Valid"
else:
return 'Invalid'
return 'Undefined'
return 'Undefined'
return "Invalid"
return "Undefined"
return "Undefined"
def get_description(self) -> str:
"""
@@ -92,10 +109,8 @@ class Command(BaseCommand):
return self._description
class InputCommand(BaseCommand, Generic[InputCommandType]):
def __init__(self, trigger: str,
input_flags: InputFlag | InputFlags = None):
def __init__(self, trigger: str, input_flags: InputFlag | InputFlags = None):
"""
Private. The model of the input command, after parsing
:param trigger:the trigger of the command
@@ -103,7 +118,13 @@ class InputCommand(BaseCommand, Generic[InputCommandType]):
:return: None
"""
super().__init__(trigger)
self._input_flags: InputFlags = input_flags if isinstance(input_flags, InputFlags) else InputFlags(input_flags) if isinstance(input_flags, InputFlag) else InputFlags()
self._input_flags: InputFlags = (
input_flags
if isinstance(input_flags, InputFlags)
else InputFlags(input_flags)
if isinstance(input_flags, InputFlag)
else InputFlags()
)
def _set_input_flags(self, input_flags: InputFlags) -> None:
"""
@@ -120,7 +141,6 @@ class InputCommand(BaseCommand, Generic[InputCommandType]):
"""
return self._input_flags
@staticmethod
def parse(raw_command: str) -> InputCommandType:
"""
@@ -138,8 +158,8 @@ class InputCommand(BaseCommand, Generic[InputCommandType]):
current_flag_name, current_flag_value = None, None
for k, _ in enumerate(list_of_tokens):
if _.startswith('-'):
if len(_) < 2 or len(_[:_.rfind('-')]) > 3:
if _.startswith("-"):
if len(_) < 2 or len(_[: _.rfind("-")]) > 3:
raise UnprocessedInputFlagException()
current_flag_name = _
else:
@@ -148,16 +168,22 @@ class InputCommand(BaseCommand, Generic[InputCommandType]):
current_flag_value = _
if current_flag_name:
if not len(list_of_tokens) == k+1:
if not list_of_tokens[k+1].startswith('-'):
if not len(list_of_tokens) == k + 1:
if not list_of_tokens[k + 1].startswith("-"):
continue
input_flag = InputFlag(name=current_flag_name[current_flag_name.rfind('-') + 1:],
prefix=cast(Literal['-', '--', '---'],
current_flag_name[:current_flag_name.rfind('-')+1]),
value=current_flag_value)
input_flag = InputFlag(
name=current_flag_name[current_flag_name.rfind("-") + 1 :],
prefix=cast(
Literal["-", "--", "---"],
current_flag_name[: current_flag_name.rfind("-") + 1],
),
value=current_flag_value,
)
all_flags = [flag.get_string_entity() for flag in input_flags.get_flags()]
all_flags = [
flag.get_string_entity() for flag in input_flags.get_flags()
]
if input_flag.get_string_entity() not in all_flags:
input_flags.add_flag(input_flag)
else:
@@ -169,4 +195,3 @@ class InputCommand(BaseCommand, Generic[InputCommandType]):
raise UnprocessedInputFlagException()
else:
return InputCommand(trigger=command, input_flags=input_flags)