diff --git a/gitlab/cli.py b/gitlab/cli.py index a0efeec6c..0ee599449 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -21,11 +21,18 @@ import functools import re import sys -from typing import Any, Callable, cast, Dict, Optional, Tuple, TypeVar, Union +from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, TypeVar, Union -import gitlab.config # noqa: F401 +from requests.structures import CaseInsensitiveDict -camel_re = re.compile("(.)([A-Z])") +import gitlab.config +from gitlab.base import RESTObject + + +# This regex is based on: +# https://github.com/jpvanhal/inflection/blob/master/inflection/__init__.py +camel_upperlower_regex = re.compile(r"([A-Z]+)([A-Z][a-z])") +camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])") # custom_actions = { # cls: { @@ -82,12 +89,17 @@ def die(msg: str, e: Optional[Exception] = None) -> None: sys.exit(1) -def what_to_cls(what: str) -> str: - return "".join([s.capitalize() for s in what.split("-")]) +def what_to_cls(what: str, namespace: Type) -> RESTObject: + classes = CaseInsensitiveDict(namespace.__dict__) + lowercase_class = what.replace("-", "") + + return classes[lowercase_class] -def cls_to_what(cls: Any) -> str: - return camel_re.sub(r"\1-\2", cls.__name__).lower() +def cls_to_what(cls: RESTObject) -> str: + dasherized_uppercase = camel_upperlower_regex.sub(r"\1-\2", cls.__name__) + dasherized_lowercase = camel_lowerupper_regex.sub(r"\1-\2", dasherized_uppercase) + return dasherized_lowercase.lower() def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser: diff --git a/gitlab/tests/test_cli.py b/gitlab/tests/test_cli.py index aed9fc41a..b7fd369dd 100644 --- a/gitlab/tests/test_cli.py +++ b/gitlab/tests/test_cli.py @@ -28,20 +28,43 @@ from gitlab import cli -def test_what_to_cls(): - assert "Foo" == cli.what_to_cls("foo") - assert "FooBar" == cli.what_to_cls("foo-bar") +@pytest.mark.parametrize( + "what,expected_class", + [ + ("class", "Class"), + ("test-class", "TestClass"), + ("test-longer-class", "TestLongerClass"), + ("current-user-gpg-key", "CurrentUserGPGKey"), + ("user-gpg-key", "UserGPGKey"), + ("ldap-group", "LDAPGroup"), + ], +) +def test_what_to_cls(what, expected_class): + def _namespace(): + pass + ExpectedClass = type(expected_class, (), {}) + _namespace.__dict__[expected_class] = ExpectedClass -def test_cls_to_what(): - class Class(object): - pass + assert cli.what_to_cls(what, _namespace) == ExpectedClass - class TestClass(object): - pass - assert "test-class" == cli.cls_to_what(TestClass) - assert "class" == cli.cls_to_what(Class) +@pytest.mark.parametrize( + "class_name,expected_what", + [ + ("Class", "class"), + ("TestClass", "test-class"), + ("TestUPPERCASEClass", "test-uppercase-class"), + ("UPPERCASETestClass", "uppercase-test-class"), + ("CurrentUserGPGKey", "current-user-gpg-key"), + ("UserGPGKey", "user-gpg-key"), + ("LDAPGroup", "ldap-group"), + ], +) +def test_cls_to_what(class_name, expected_what): + TestClass = type(class_name, (), {}) + + assert cli.cls_to_what(TestClass) == expected_what def test_die(): diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index 42b94aa0e..a84a6a910 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -28,8 +28,8 @@ class GitlabCLI(object): def __init__(self, gl, what, action, args): - self.cls_name = cli.what_to_cls(what) - self.cls = gitlab.v4.objects.__dict__[self.cls_name] + self.cls = cli.what_to_cls(what, namespace=gitlab.v4.objects) + self.cls_name = self.cls.__name__ self.what = what.replace("-", "_") self.action = action.lower() self.gl = gl