Commit 61d86792 authored by John L. Villalovos's avatar John L. Villalovos Committed by John Villalovos
Browse files

chore: create a CustomAction dataclass

parent 74db84ca
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
import argparse
import dataclasses
import functools
import os
import pathlib
@@ -29,12 +30,20 @@ from gitlab.base import RESTObject
camel_upperlower_regex = re.compile(r"([A-Z]+)([A-Z][a-z])")
camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])")


@dataclasses.dataclass
class CustomAction:
    required: Tuple[str, ...]
    optional: Tuple[str, ...]
    in_object: bool


# custom_actions = {
#    cls: {
#        action: (mandatory_args, optional_args, in_obj),
#    },
# }
custom_actions: Dict[str, Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...], bool]]] = {}
custom_actions: Dict[str, Dict[str, CustomAction]] = {}


# For an explanation of how these type-hints work see:
@@ -99,7 +108,9 @@ def register_custom_action(
                custom_actions[final_name] = {}

            action = custom_action or f.__name__.replace("_", "-")
            custom_actions[final_name][action] = (required, optional, in_obj)
            custom_actions[final_name][action] = CustomAction(
                required=required, optional=optional, in_object=in_obj
            )

        return cast(__F, wrapped_f)

+7 −7
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ class GitlabCLI:

    def do_custom(self) -> Any:
        class_instance: Union[gitlab.base.RESTManager, gitlab.base.RESTObject]
        in_obj = cli.custom_actions[self.cls_name][self.resource_action][2]
        in_obj = cli.custom_actions[self.cls_name][self.resource_action].in_object

        # Get the object (lazy), then act
        if in_obj:
@@ -321,13 +321,13 @@ def _populate_sub_parser_by_class(
                    id_attr = cls._id_attr.replace("_", "-")
                    sub_parser_action.add_argument(f"--{id_attr}", required=True)

            required, optional, dummy = cli.custom_actions[name][action_name]
            for x in required:
            custom_action = cli.custom_actions[name][action_name]
            for x in custom_action.required:
                if x != cls._id_attr:
                    sub_parser_action.add_argument(
                        f"--{x.replace('_', '-')}", required=True
                    )
            for x in optional:
            for x in custom_action.optional:
                if x != cls._id_attr:
                    sub_parser_action.add_argument(
                        f"--{x.replace('_', '-')}", required=False
@@ -350,13 +350,13 @@ def _populate_sub_parser_by_class(
                    )
                sub_parser_action.add_argument("--sudo", required=False)

            required, optional, dummy = cli.custom_actions[name][action_name]
            for x in required:
            custom_action = cli.custom_actions[name][action_name]
            for x in custom_action.required:
                if x != cls._id_attr:
                    sub_parser_action.add_argument(
                        f"--{x.replace('_', '-')}", required=True
                    )
            for x in optional:
            for x in custom_action.optional:
                if x != cls._id_attr:
                    sub_parser_action.add_argument(
                        f"--{x.replace('_', '-')}", required=False