mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
fix public api
This commit is contained in:
@@ -1,4 +1,2 @@
|
||||
__all__ = ["ArgParser", "Orchestrator"]
|
||||
|
||||
from argenta.orchestrator.argparser.entity import ArgParser
|
||||
from argenta.orchestrator.entity import Orchestrator
|
||||
from argenta.orchestrator.argparser.entity import ArgParser as ArgParser
|
||||
from argenta.orchestrator.entity import Orchestrator as Orchestrator
|
||||
|
||||
@@ -1,9 +1,2 @@
|
||||
__all__ = [
|
||||
"ArgParser",
|
||||
"BooleanArgument",
|
||||
"ValueArgument"
|
||||
]
|
||||
|
||||
|
||||
from argenta.orchestrator.argparser.entity import ArgParser
|
||||
from argenta.orchestrator.argparser.arguments import BooleanArgument, ValueArgument
|
||||
from argenta.orchestrator.argparser.entity import ArgParser as ArgParser
|
||||
from argenta.orchestrator.argparser.arguments import BooleanArgument as BooleanArgument, ValueArgument as ValueArgument
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
__all__ = ["BooleanArgument", "ValueArgument", "InputArgument"]
|
||||
|
||||
|
||||
from argenta.orchestrator.argparser.arguments.models import (
|
||||
BooleanArgument,
|
||||
ValueArgument,
|
||||
InputArgument
|
||||
BooleanArgument as BooleanArgument,
|
||||
ValueArgument as ValueArgument,
|
||||
InputArgument as InputArgument
|
||||
)
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
__all__ = [
|
||||
'BooleanArgument',
|
||||
'ValueArgument',
|
||||
'InputArgument'
|
||||
]
|
||||
|
||||
from typing import Literal
|
||||
|
||||
|
||||
@@ -20,7 +26,7 @@ class BaseArgument:
|
||||
self.help: str = help
|
||||
self.is_deprecated: bool = is_deprecated
|
||||
self.prefix: Literal["-", "--", "---"] = prefix
|
||||
|
||||
|
||||
@property
|
||||
def string_entity(self) -> str:
|
||||
return self.prefix + self.name
|
||||
@@ -29,7 +35,7 @@ class BaseArgument:
|
||||
class ValueArgument(BaseArgument):
|
||||
def __init__(self, name: str, *,
|
||||
prefix: Literal["-", "--", "---"] = "--",
|
||||
help: str = "Help message for the value argument",
|
||||
help: str = "Help message for the value argument",
|
||||
possible_values: list[str] | None = None,
|
||||
default: str | None = None,
|
||||
is_required: bool = False,
|
||||
@@ -74,9 +80,9 @@ class InputArgument:
|
||||
self.name: str = name
|
||||
self.value: str | None = value
|
||||
self.founder_class: type[BaseArgument] = founder_class
|
||||
|
||||
|
||||
def __str__(self) -> str:
|
||||
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,3 +1,8 @@
|
||||
__all__ = [
|
||||
"ArgSpace",
|
||||
"ArgParser",
|
||||
]
|
||||
|
||||
from argparse import ArgumentParser, Namespace
|
||||
from typing import Never, Self
|
||||
|
||||
@@ -8,32 +13,32 @@ from argenta.orchestrator.argparser.arguments.models import (
|
||||
ValueArgument
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ArgSpace:
|
||||
def __init__(self, all_arguments: list[InputArgument]) -> None:
|
||||
self.all_arguments = all_arguments
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_namespace(cls, namespace: Namespace,
|
||||
def from_namespace(cls, namespace: Namespace,
|
||||
processed_args: list[ValueArgument | BooleanArgument]) -> Self:
|
||||
name_type_paired_args: dict[str, type[BaseArgument]] = {
|
||||
arg.name: type(arg)
|
||||
for arg in processed_args
|
||||
for arg in processed_args
|
||||
}
|
||||
return cls([InputArgument(name=name,
|
||||
value=value,
|
||||
founder_class=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__(
|
||||
@@ -56,15 +61,15 @@ class ArgParser:
|
||||
self.processed_args: list[ValueArgument | BooleanArgument] = processed_args
|
||||
|
||||
self._core: ArgumentParser = ArgumentParser(prog=name, description=description, epilog=epilog)
|
||||
|
||||
|
||||
for arg in processed_args:
|
||||
if isinstance(arg, BooleanArgument):
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
action=arg.action,
|
||||
help=arg.help,
|
||||
deprecated=arg.is_deprecated)
|
||||
else:
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
_ = self._core.add_argument(arg.string_entity,
|
||||
action=arg.action,
|
||||
help=arg.help,
|
||||
default=arg.default,
|
||||
@@ -75,4 +80,3 @@ class ArgParser:
|
||||
def parse_args(self) -> ArgSpace:
|
||||
return ArgSpace.from_namespace(namespace=self._core.parse_args(),
|
||||
processed_args=self.processed_args)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
__all__ = ["Orchestrator"]
|
||||
|
||||
from argenta.app import App
|
||||
from argenta.response import Response
|
||||
|
||||
from argenta.orchestrator.argparser import ArgParser
|
||||
from argenta.di.integration import setup_dishka
|
||||
@@ -31,7 +32,6 @@ class Orchestrator:
|
||||
:return: None
|
||||
"""
|
||||
container = make_container(SystemProvider(self._arg_parser), *self._custom_providers)
|
||||
Response.patch_by_container(container)
|
||||
setup_dishka(app, auto_inject=self._auto_inject_handlers)
|
||||
setup_dishka(app, container, auto_inject=self._auto_inject_handlers)
|
||||
|
||||
app.run_polling()
|
||||
|
||||
Reference in New Issue
Block a user