8000 chore: create a CustomAction dataclass · python-gitlab/python-gitlab@61d8679 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61d8679

Browse files
chore: create a CustomAction dataclass
1 parent 74db84c commit 61d8679

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

gitlab/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import dataclasses
23
import functools
34
import os
45
import pathlib
@@ -29,12 +30,20 @@
2930
camel_upperlower_regex = re.compile(r"([A-Z]+)([A-Z][a-z])")
3031
camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])")
3132

33+
34+
@dataclasses.dataclass
35+
class CustomAction:
36+
required: Tuple[str, ...]
37+
optional: Tuple[str, ...]
38+
in_object: bool
39+
40+
3241
# custom_actions = {
3342
# cls: {
3443
# action: (mandatory_args, optional_args, in_obj),
3544
# },
3645
# }
37-
custom_actions: Dict[str, Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...], bool]]] = {}
46+
custom_actions: Dict[str, Dict[str, CustomAction]] = {}
3847

3948

4049
# For an explanation of how these type-hints work see:
@@ -99,7 +108,9 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any:
99108
custom_actions[final_name] = {}
100109

101110
action = custom_action or f.__name__.replace("_", "-")
102-
custom_actions[final_name][action] = (required, optional, in_obj)
111+
custom_actions[final_name][action] = CustomAction(
112+
required=required, optional=optional, in_object=in_obj
113+
)
103114

104115
return cast(__F, wrapped_f)
105116

gitlab/v4/cli.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run(self) -> Any:
8282

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

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

324-
required, optional, dummy = cli.custom_actions[name][action_name]
325-
for x in required:
324+
custom_action = cli.custom_actions[name][action_name]
325+
for x in custom_action.required:
326326
if x != cls._id_attr:
327327
sub_parser_action.add_argument(
328328
f"--{x.replace('_', '-')}", required=True
329329
)
330-
for x in optional:
330+
for x in custom_action.optional:
331331
if x != cls._id_attr:
332332
sub_parser_action.add_argument(
333333
f"--{x.replace('_', '-')}", required=False
@@ -350,13 +350,13 @@ def _populate_sub_parser_by_class(
350350
)
351351
sub_parser_action.add_argument("--sudo", required=False)
352352

353-
required, optional, dummy = cli.custom_actions[name][action_name]
354-
for x in required:
353+
custom_action = cli.custom_actions[name][action_name]
354+
for x in custom_action.required:
355355
if x != cls._id_attr:
356356
sub_parser_action.add_argument(
357357
f"--{x.replace('_', '-')}", required=True
358358
)
359-
for x in optional:
359+
for x in custom_action.optional:
360360
if x != cls._id_attr:
361361
sub_parser_action.add_argument(
362362
f"--{x.replace('_', '-')}", required=False

0 commit comments

Comments
 (0)
0