Skip to content

Commit c86e471

Browse files
chore: rename what to gitlab_resource
Naming a variable `what` makes it difficult to understand what it is used for. Rename it to `gitlab_resource` as that is what is being stored. The Gitlab documentation talks about them being resources: https://docs.gitlab.com/ee/api/api_resources.html This will improve code readability.
1 parent 6189437 commit c86e471

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

gitlab/cli.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ def die(msg: str, e: Optional[Exception] = None) -> None:
9191
sys.exit(1)
9292

9393

94-
def what_to_cls(what: str, namespace: ModuleType) -> Type[RESTObject]:
94+
def gitlab_resource_to_cls(
95+
gitlab_resource: str, namespace: ModuleType
96+
) -> Type[RESTObject]:
9597
classes = CaseInsensitiveDict(namespace.__dict__)
96-
lowercase_class = what.replace("-", "")
98+
lowercase_class = gitlab_resource.replace("-", "")
9799

98100
return classes[lowercase_class]
99101

100102

101-
def cls_to_what(cls: RESTObject) -> str:
103+
def cls_to_gitlab_resource(cls: RESTObject) -> str:
102104
dasherized_uppercase = camel_upperlower_regex.sub(r"\1-\2", cls.__name__)
103105
dasherized_lowercase = camel_lowerupper_regex.sub(r"\1-\2", dasherized_uppercase)
104106
return dasherized_lowercase.lower()
@@ -322,7 +324,7 @@ def main() -> None:
322324
fields = [x.strip() for x in args.fields.split(",")]
323325
debug = args.debug
324326
action = args.whaction
325-
what = args.what
327+
gitlab_resource = args.gitlab_resource
326328

327329
args_dict = vars(args)
328330
# Remove CLI behavior-related args
@@ -331,7 +333,7 @@ def main() -> None:
331333
"config_file",
332334
"verbose",
333335
"debug",
334-
"what",
336+
"gitlab_resource",
335337
"whaction",
336338
"version",
337339
"output",
@@ -359,4 +361,4 @@ def main() -> None:
359361
if debug:
360362
gl.enable_debug()
361363

362-
gitlab.v4.cli.run(gl, what, action, args_dict, verbose, output, fields)
364+
gitlab.v4.cli.run(gl, gitlab_resource, action, args_dict, verbose, output, fields)

gitlab/v4/cli.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929

3030
class GitlabCLI:
3131
def __init__(
32-
self, gl: gitlab.Gitlab, what: str, action: str, args: Dict[str, str]
32+
self, gl: gitlab.Gitlab, gitlab_resource: str, action: str, args: Dict[str, str]
3333
) -> None:
34-
self.cls: Type[gitlab.base.RESTObject] = cli.what_to_cls(
35-
what, namespace=gitlab.v4.objects
34+
self.cls: Type[gitlab.base.RESTObject] = cli.gitlab_resource_to_cls(
35+
gitlab_resource, namespace=gitlab.v4.objects
3636
)
3737
self.cls_name = self.cls.__name__
38-
self.what = what.replace("-", "_")
38+
self.gitlab_resource = gitlab_resource.replace("-", "_")
3939
self.action = action.lower()
4040
self.gl = gl
4141
self.args = args
@@ -81,7 +81,7 @@ def _process_from_parent_attrs(self) -> None:
8181

8282
def run(self) -> Any:
8383
# Check for a method that matches object + action
84-
method = f"do_{self.what}_{self.action}"
84+
method = f"do_{self.gitlab_resource}_{self.action}"
8585
if hasattr(self, method):
8686
return getattr(self, method)()
8787

@@ -333,7 +333,7 @@ def _populate_sub_parser_by_class(
333333

334334
def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
335335
subparsers = parser.add_subparsers(
336-
title="object", dest="what", help="Object to manipulate."
336+
title="object", dest="gitlab_resource", help="Object to manipulate."
337337
)
338338
subparsers.required = True
339339

@@ -347,7 +347,7 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
347347
classes.add(cls._obj_cls)
348348

349349
for cls in sorted(classes, key=operator.attrgetter("__name__")):
350-
arg_name = cli.cls_to_what(cls)
350+
arg_name = cli.cls_to_gitlab_resource(cls)
351351
object_group = subparsers.add_parser(arg_name)
352352

353353
object_subparsers = object_group.add_subparsers(
@@ -497,14 +497,14 @@ def display_list(
497497

498498
def run(
499499
gl: gitlab.Gitlab,
500-
what: str,
500+
gitlab_resource: str,
501501
action: str,
502502
args: Dict[str, Any],
503503
verbose: bool,
504504
output: str,
505505
fields: List[str],
506506
) -> None:
507-
g_cli = GitlabCLI(gl=gl, what=what, action=action, args=args)
507+
g_cli = GitlabCLI(gl=gl, gitlab_resource=gitlab_resource, action=action, args=args)
508508
data = g_cli.run()
509509

510510
printer: Union[JSONPrinter, LegacyPrinter, YAMLPrinter] = PRINTERS[output]()

tests/unit/test_cli.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
@pytest.mark.parametrize(
32-
"what,expected_class",
32+
"gitlab_resource,expected_class",
3333
[
3434
("class", "Class"),
3535
("test-class", "TestClass"),
@@ -39,18 +39,18 @@
3939
("ldap-group", "LDAPGroup"),
4040
],
4141
)
42-
def test_what_to_cls(what, expected_class):
42+
def test_gitlab_resource_to_cls(gitlab_resource, expected_class):
4343
def _namespace():
4444
pass
4545

4646
ExpectedClass = type(expected_class, (), {})
4747
_namespace.__dict__[expected_class] = ExpectedClass
4848

49-
assert cli.what_to_cls(what, _namespace) == ExpectedClass
49+
assert cli.gitlab_resource_to_cls(gitlab_resource, _namespace) == ExpectedClass
5050

5151

5252
@pytest.mark.parametrize(
53-
"class_name,expected_what",
53+
"class_name,expected_gitlab_resource",
5454
[
5555
("Class", "class"),
5656
("TestClass", "test-class"),
@@ -61,10 +61,10 @@ def _namespace():
6161
("LDAPGroup", "ldap-group"),
6262
],
6363
)
64-
def test_cls_to_what(class_name, expected_what):
64+
def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource):
6565
TestClass = type(class_name, (), {})
6666

67-
assert cli.cls_to_what(TestClass) == expected_what
67+
assert cli.cls_to_gitlab_resource(TestClass) == expected_gitlab_resource
6868

6969

7070
@pytest.mark.parametrize(
@@ -125,7 +125,7 @@ def test_base_parser():
125125
def test_v4_parse_args():
126126
parser = cli._get_parser()
127127
args = parser.parse_args(["project", "list"])
128-
assert args.what == "project"
128+
assert args.gitlab_resource == "project"
129129
assert args.whaction == "list"
130130

131131

0 commit comments

Comments
 (0)