duit.arguments.Argument

 1from typing import Optional, TypeVar
 2
 3from duit.annotation.Annotation import Annotation
 4from duit.arguments import ARGUMENT_ANNOTATION_ATTRIBUTE_NAME
 5from duit.model.DataField import DataField
 6
 7M = TypeVar("M", bound=DataField)
 8
 9
10class Argument(Annotation):
11    """
12    Annotation class for defining command-line arguments.
13
14    Args:
15        dest (Optional[str]): The destination name for the argument (optional).
16        group (Optional[str]): The argument group name (optional).
17        auto_params (bool): Whether to automatically infer type and default values from the DataField (default is True).
18        *args: Variable positional arguments.
19        **kwargs: Variable keyword arguments.
20
21    Attributes:
22        dest (Optional[str]): The destination name for the argument.
23        group (Optional[str]): The argument group name.
24        args: Variable positional arguments.
25        kwargs: Variable keyword arguments.
26        auto_params (bool): Whether to automatically infer type and default values from the DataField.
27    """
28
29    def __init__(self, dest: Optional[str] = None, *args, group: Optional[str] = None,
30                 auto_params: bool = True, auto_default: bool = False, allow_none: bool = False, **kwargs):
31        """
32        Initialize an Argument instance.
33
34        Args:
35            dest (Optional[str]): The destination name for the argument (optional).
36            group (Optional[str]): The argument group name (optional).
37            auto_params (bool): Whether to automatically infer type and other values from the DataField (default is True).
38            auto_default (bool): Whether to automatically infer default value from the DataField (default is False).
39            allow_none (bool): Whether to allow None as valid value. Otherwise, it is evaluated as not set.
40            *args: Variable positional arguments.
41            **kwargs: Variable keyword arguments.
42        """
43        self.dest = dest
44        self.group = group
45        self.args = args
46        self.kwargs = kwargs
47        self.auto_params = auto_params
48        self.auto_default = auto_default
49        self.allow_none = allow_none
50
51    def _apply_annotation(self, model: M) -> M:
52        """
53        Apply the Argument annotation to a DataField model.
54
55        Args:
56            model (M): The DataField model to apply the Argument to.
57
58        Returns:
59            M: The modified DataField model with the Argument applied.
60        """
61        if not isinstance(model, DataField):
62            raise Exception(f"Argument can not be applied to {type(model).__name__}")
63
64        # read model parameter
65        if self.auto_params:
66            if "type" not in self.kwargs:
67                self.kwargs["type"] = type(model.value)
68
69        if self.auto_default:
70            if "default" not in self.kwargs:
71                self.kwargs["default"] = model.value
72
73        # add ui attribute to data model
74        model.__setattr__(ARGUMENT_ANNOTATION_ATTRIBUTE_NAME, self)
75        return model
76
77    @staticmethod
78    def _get_annotation_attribute_name() -> str:
79        """
80        Get the name of the annotation attribute.
81
82        Returns:
83            str: The name of the annotation attribute (used for Argument).
84        """
85        return ARGUMENT_ANNOTATION_ATTRIBUTE_NAME
class Argument(duit.annotation.Annotation.Annotation):
11class Argument(Annotation):
12    """
13    Annotation class for defining command-line arguments.
14
15    Args:
16        dest (Optional[str]): The destination name for the argument (optional).
17        group (Optional[str]): The argument group name (optional).
18        auto_params (bool): Whether to automatically infer type and default values from the DataField (default is True).
19        *args: Variable positional arguments.
20        **kwargs: Variable keyword arguments.
21
22    Attributes:
23        dest (Optional[str]): The destination name for the argument.
24        group (Optional[str]): The argument group name.
25        args: Variable positional arguments.
26        kwargs: Variable keyword arguments.
27        auto_params (bool): Whether to automatically infer type and default values from the DataField.
28    """
29
30    def __init__(self, dest: Optional[str] = None, *args, group: Optional[str] = None,
31                 auto_params: bool = True, auto_default: bool = False, allow_none: bool = False, **kwargs):
32        """
33        Initialize an Argument instance.
34
35        Args:
36            dest (Optional[str]): The destination name for the argument (optional).
37            group (Optional[str]): The argument group name (optional).
38            auto_params (bool): Whether to automatically infer type and other values from the DataField (default is True).
39            auto_default (bool): Whether to automatically infer default value from the DataField (default is False).
40            allow_none (bool): Whether to allow None as valid value. Otherwise, it is evaluated as not set.
41            *args: Variable positional arguments.
42            **kwargs: Variable keyword arguments.
43        """
44        self.dest = dest
45        self.group = group
46        self.args = args
47        self.kwargs = kwargs
48        self.auto_params = auto_params
49        self.auto_default = auto_default
50        self.allow_none = allow_none
51
52    def _apply_annotation(self, model: M) -> M:
53        """
54        Apply the Argument annotation to a DataField model.
55
56        Args:
57            model (M): The DataField model to apply the Argument to.
58
59        Returns:
60            M: The modified DataField model with the Argument applied.
61        """
62        if not isinstance(model, DataField):
63            raise Exception(f"Argument can not be applied to {type(model).__name__}")
64
65        # read model parameter
66        if self.auto_params:
67            if "type" not in self.kwargs:
68                self.kwargs["type"] = type(model.value)
69
70        if self.auto_default:
71            if "default" not in self.kwargs:
72                self.kwargs["default"] = model.value
73
74        # add ui attribute to data model
75        model.__setattr__(ARGUMENT_ANNOTATION_ATTRIBUTE_NAME, self)
76        return model
77
78    @staticmethod
79    def _get_annotation_attribute_name() -> str:
80        """
81        Get the name of the annotation attribute.
82
83        Returns:
84            str: The name of the annotation attribute (used for Argument).
85        """
86        return ARGUMENT_ANNOTATION_ATTRIBUTE_NAME

Annotation class for defining command-line arguments.

Args: dest (Optional[str]): The destination name for the argument (optional). group (Optional[str]): The argument group name (optional). auto_params (bool): Whether to automatically infer type and default values from the DataField (default is True). args: Variable positional arguments. *kwargs: Variable keyword arguments.

Attributes: dest (Optional[str]): The destination name for the argument. group (Optional[str]): The argument group name. args: Variable positional arguments. kwargs: Variable keyword arguments. auto_params (bool): Whether to automatically infer type and default values from the DataField.

Argument( dest: Optional[str] = None, *args, group: Optional[str] = None, auto_params: bool = True, auto_default: bool = False, allow_none: bool = False, **kwargs)
30    def __init__(self, dest: Optional[str] = None, *args, group: Optional[str] = None,
31                 auto_params: bool = True, auto_default: bool = False, allow_none: bool = False, **kwargs):
32        """
33        Initialize an Argument instance.
34
35        Args:
36            dest (Optional[str]): The destination name for the argument (optional).
37            group (Optional[str]): The argument group name (optional).
38            auto_params (bool): Whether to automatically infer type and other values from the DataField (default is True).
39            auto_default (bool): Whether to automatically infer default value from the DataField (default is False).
40            allow_none (bool): Whether to allow None as valid value. Otherwise, it is evaluated as not set.
41            *args: Variable positional arguments.
42            **kwargs: Variable keyword arguments.
43        """
44        self.dest = dest
45        self.group = group
46        self.args = args
47        self.kwargs = kwargs
48        self.auto_params = auto_params
49        self.auto_default = auto_default
50        self.allow_none = allow_none

Initialize an Argument instance.

Args: dest (Optional[str]): The destination name for the argument (optional). group (Optional[str]): The argument group name (optional). auto_params (bool): Whether to automatically infer type and other values from the DataField (default is True). auto_default (bool): Whether to automatically infer default value from the DataField (default is False). allow_none (bool): Whether to allow None as valid value. Otherwise, it is evaluated as not set. args: Variable positional arguments. *kwargs: Variable keyword arguments.

dest
group
args
kwargs
auto_params
auto_default
allow_none