mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
extend arguments
This commit is contained in:
+3
-10
@@ -1,10 +1,3 @@
|
||||
__all__ = [
|
||||
'App',
|
||||
'Orchestrator',
|
||||
'Router',
|
||||
]
|
||||
|
||||
|
||||
from argenta.app import App
|
||||
from argenta.orchestrator import Orchestrator
|
||||
from argenta.router import Router
|
||||
from argenta.app.models import App as App
|
||||
from argenta.orchestrator.entity import Orchestrator as Orchestrator
|
||||
from argenta.router.entity import Router as Router
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
__all__ = [
|
||||
"Orchestrator",
|
||||
"ArgParser"
|
||||
"ArgParser",
|
||||
]
|
||||
|
||||
|
||||
from argenta.orchestrator.entity import Orchestrator
|
||||
from argenta.orchestrator.argparser.entity import ArgParser
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
__all__ = [
|
||||
"ArgParser",
|
||||
"RequiredArgument",
|
||||
"ValueArgument",
|
||||
"BooleanArgument"
|
||||
"BooleanArgument",
|
||||
"ValueArgument"
|
||||
]
|
||||
|
||||
|
||||
from argenta.orchestrator.argparser.entity import ArgParser
|
||||
from argenta.orchestrator.argparser.arguments import (BooleanArgument,
|
||||
RequiredArgument,
|
||||
ValueArgument)
|
||||
from argenta.orchestrator.argparser.arguments import BooleanArgument, ValueArgument
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
__all__ = ["BooleanArgument", "RequiredArgument", "ValueArgument"]
|
||||
__all__ = ["BooleanArgument", "ValueArgument", "InputArgument"]
|
||||
|
||||
|
||||
from argenta.orchestrator.argparser.arguments.models import (
|
||||
BooleanArgument,
|
||||
RequiredArgument,
|
||||
ValueArgument,
|
||||
InputArgument
|
||||
)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from typing import Literal
|
||||
|
||||
|
||||
class BaseArgument:
|
||||
"""
|
||||
Private. Base class for all arguments
|
||||
"""
|
||||
def __init__(self, name: str, *,
|
||||
help: str,
|
||||
is_required: bool,
|
||||
is_deprecated: bool):
|
||||
is_deprecated: bool,
|
||||
prefix: Literal["-", "--", "---"]):
|
||||
"""
|
||||
Public. Boolean argument, does not require a value
|
||||
:param name: name of the argument
|
||||
@@ -15,42 +18,26 @@ class BaseArgument:
|
||||
"""
|
||||
self.name: str = name
|
||||
self.help: str = help
|
||||
self.is_required: bool = is_required
|
||||
self.is_deprecated: bool = is_deprecated
|
||||
|
||||
|
||||
class RequiredArgument(BaseArgument):
|
||||
def __init__(self, name: str, *,
|
||||
help: str = "Help for required argument",
|
||||
default: str | None = None,
|
||||
possible_values: list[str] | None = None,
|
||||
is_required: bool = True,
|
||||
is_deprecated: bool = False):
|
||||
"""
|
||||
Public. Required argument at startup
|
||||
:param name: name of the argument, must not start with minus (-)
|
||||
:param help: help message for the argument
|
||||
:param default: default value for the argument
|
||||
:param possible_values: list of possible values for the argument
|
||||
:param is_required: whether the argument is required
|
||||
:param is_deprecated: whether the argument is deprecated
|
||||
"""
|
||||
self.default: str | None = default
|
||||
self.possible_values: list[str] | None = possible_values
|
||||
self.action: str = "store"
|
||||
super().__init__(name, help=help, is_required=is_required, is_deprecated=is_deprecated)
|
||||
self.prefix: Literal["-", "--", "---"] = prefix
|
||||
|
||||
@property
|
||||
def string_entity(self) -> str:
|
||||
return self.prefix + self.name
|
||||
|
||||
|
||||
class ValueArgument(BaseArgument):
|
||||
def __init__(self, name: str, *,
|
||||
help: str = "Help message for the value argument",
|
||||
possible_values: list[str] | None = None,
|
||||
default: str | None = None,
|
||||
is_required: bool = False,
|
||||
is_deprecated: bool = False):
|
||||
prefix: Literal["-", "--", "---"] = "--",
|
||||
help: str = "Help message for the value argument",
|
||||
possible_values: list[str] | None = None,
|
||||
default: str | None = None,
|
||||
is_required: bool = False,
|
||||
is_deprecated: bool = False):
|
||||
"""
|
||||
Public. Value argument, must have the value
|
||||
:param name: name of the argument
|
||||
:param prefix: prefix for the argument
|
||||
:param help: help message for the argument
|
||||
:param possible_values: list of possible values for the argument
|
||||
:param default: default value for the argument
|
||||
@@ -59,15 +46,16 @@ class ValueArgument(BaseArgument):
|
||||
"""
|
||||
self.default: str | None = default
|
||||
self.possible_values: list[str] | None = possible_values
|
||||
self.is_required: bool = is_required
|
||||
self.action: str = "store"
|
||||
super().__init__(name, help=help, is_required=is_required, is_deprecated=is_deprecated)
|
||||
super().__init__(name, prefix=prefix, help=help, is_deprecated=is_deprecated)
|
||||
|
||||
|
||||
class BooleanArgument(BaseArgument):
|
||||
def __init__(self, name: str, *,
|
||||
help: str = "Help message for the boolean argument",
|
||||
is_required: bool = False,
|
||||
is_deprecated: bool = False):
|
||||
prefix: Literal["-", "--", "---"] = "--",
|
||||
help: str = "Help message for the boolean argument",
|
||||
is_deprecated: bool = False):
|
||||
"""
|
||||
Public. Boolean argument, does not require a value
|
||||
:param name: name of the argument
|
||||
@@ -76,7 +64,7 @@ class BooleanArgument(BaseArgument):
|
||||
:param is_deprecated: whether the argument is deprecated
|
||||
"""
|
||||
self.action: str = "store_true"
|
||||
super().__init__(name, help=help, is_required=is_required, is_deprecated=is_deprecated)
|
||||
super().__init__(name, prefix=prefix, help=help, is_deprecated=is_deprecated)
|
||||
|
||||
|
||||
class InputArgument:
|
||||
@@ -88,4 +76,7 @@ class InputArgument:
|
||||
self.founder_class: type[BaseArgument] = founder_class
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.name}={self.value}"
|
||||
return f"InputArgument({self.name}={self.value})"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"InputArgument<name={self.name}, value={self.value}, founder_class={self.founder_class.__name__}>"
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
from argparse import ArgumentParser, Namespace
|
||||
from typing import Self
|
||||
from typing import Never, Self
|
||||
|
||||
from argenta.orchestrator.argparser.arguments.models import (
|
||||
BaseArgument,
|
||||
BooleanArgument,
|
||||
InputArgument,
|
||||
ValueArgument,
|
||||
RequiredArgument,
|
||||
ValueArgument
|
||||
)
|
||||
|
||||
|
||||
@@ -16,19 +15,30 @@ class ArgSpace:
|
||||
|
||||
@classmethod
|
||||
def from_namespace(cls, namespace: Namespace,
|
||||
processed_args: list[RequiredArgument | ValueArgument | BooleanArgument]) -> Self:
|
||||
processed_args: list[ValueArgument | BooleanArgument]) -> Self:
|
||||
name_type_paired_args: dict[str, type[BaseArgument]] = {
|
||||
arg.name: type(arg)
|
||||
for arg in processed_args
|
||||
}
|
||||
return cls([InputArgument(name, value, name_type_paired_args[name])
|
||||
return cls([InputArgument(name=name,
|
||||
value=value,
|
||||
founder_class=name_type_paired_args[name])
|
||||
for name, value in vars(namespace).items()])
|
||||
|
||||
|
||||
def get_by_name(self, name: str) -> InputArgument | None:
|
||||
for arg in self.all_arguments:
|
||||
if arg.name == name:
|
||||
return arg
|
||||
return None
|
||||
|
||||
def get_by_type(self, arg_type: type[BaseArgument]) -> list[InputArgument] | list[Never]:
|
||||
return [arg for arg in self.all_arguments if arg.founder_class is arg_type]
|
||||
|
||||
|
||||
class ArgParser:
|
||||
def __init__(
|
||||
self,
|
||||
processed_args: list[RequiredArgument | ValueArgument | BooleanArgument], *,
|
||||
processed_args: list[ValueArgument | BooleanArgument], *,
|
||||
name: str = "Argenta",
|
||||
description: str = "Argenta available arguments",
|
||||
epilog: str = "github.com/koloideal/Argenta | made by kolo",
|
||||
@@ -40,22 +50,21 @@ class ArgParser:
|
||||
:param epilog: the epilog of the ArgParse instance
|
||||
:param processed_args: registered and processed arguments
|
||||
"""
|
||||
self._name: str = name
|
||||
self._description: str = description
|
||||
self._epilog: str = epilog
|
||||
self.name: str = name
|
||||
self.description: str = description
|
||||
self.epilog: str = epilog
|
||||
self.processed_args: list[ValueArgument | BooleanArgument] = processed_args
|
||||
|
||||
self._entity: ArgumentParser = ArgumentParser(prog=name, description=description, epilog=epilog)
|
||||
self._processed_args: list[RequiredArgument | ValueArgument | BooleanArgument] = processed_args
|
||||
self._core: ArgumentParser = ArgumentParser(prog=name, description=description, epilog=epilog)
|
||||
|
||||
for arg in processed_args:
|
||||
if isinstance(arg, BooleanArgument):
|
||||
_ = self._entity.add_argument(arg.name,
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
action=arg.action,
|
||||
help=arg.help,
|
||||
required=arg.is_required,
|
||||
deprecated=arg.is_deprecated)
|
||||
else:
|
||||
_ = self._entity.add_argument(arg.name,
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
action=arg.action,
|
||||
help=arg.help,
|
||||
default=arg.default,
|
||||
@@ -64,6 +73,6 @@ class ArgParser:
|
||||
deprecated=arg.is_deprecated)
|
||||
|
||||
def parse_args(self) -> ArgSpace:
|
||||
return ArgSpace.from_namespace(namespace=self._entity.parse_args(),
|
||||
processed_args=self._processed_args)
|
||||
return ArgSpace.from_namespace(namespace=self._core.parse_args(),
|
||||
processed_args=self.processed_args)
|
||||
|
||||
@@ -3,14 +3,17 @@ from argenta.orchestrator.argparser import ArgParser
|
||||
from argenta.orchestrator.argparser.entity import ArgSpace
|
||||
|
||||
|
||||
DEFAULT_ARGPARSER: ArgParser = ArgParser(processed_args=[])
|
||||
|
||||
|
||||
class Orchestrator:
|
||||
def __init__(self, arg_parser: ArgParser | None = None):
|
||||
def __init__(self, arg_parser: ArgParser = DEFAULT_ARGPARSER):
|
||||
"""
|
||||
Public. An orchestrator and configurator that defines the behavior of an integrated system, one level higher than the App
|
||||
:param arg_parser: Cmd argument parser and configurator at startup
|
||||
:return: None
|
||||
"""
|
||||
self._arg_parser: ArgParser | None = arg_parser
|
||||
self._arg_parser: ArgParser = arg_parser
|
||||
|
||||
def start_polling(self, app: App) -> None:
|
||||
"""
|
||||
@@ -18,8 +21,5 @@ class Orchestrator:
|
||||
:param app: a running application
|
||||
:return: None
|
||||
"""
|
||||
if self._arg_parser is not None:
|
||||
parsed_argspace: ArgSpace = self._arg_parser.parse_args()
|
||||
app.run_polling(argspace=parsed_argspace)
|
||||
else:
|
||||
app.run_polling(argspace=None)
|
||||
parsed_argspace: ArgSpace = self._arg_parser.parse_args()
|
||||
app.run_polling(argspace=parsed_argspace)
|
||||
|
||||
Reference in New Issue
Block a user