diff options
| author | Nejc Habjan <hab.nejc@gmail.com> | 2021-05-15 12:10:45 +0200 |
|---|---|---|
| committer | Nejc Habjan <hab.nejc@gmail.com> | 2021-05-15 12:10:45 +0200 |
| commit | 42520705a97289ac895a6b110d34d6c115e45500 (patch) | |
| tree | 7304a61559b4e309acf3d5eede4e2eadb9c7e745 | |
| parent | f35c73e50918e4d55b70323669f394e52e75cde9 (diff) | |
| download | gitlab-42520705a97289ac895a6b110d34d6c115e45500.tar.gz | |
fix(cli): fix parsing CLI objects to classnames
| -rw-r--r-- | gitlab/cli.py | 26 | ||||
| -rw-r--r-- | gitlab/tests/test_cli.py | 37 | ||||
| -rw-r--r-- | gitlab/v4/cli.py | 4 |
3 files changed, 48 insertions, 19 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index a0efeec..0ee5994 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -21,11 +21,18 @@ import argparse 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 aed9fc4..6f67e8f 100644 --- a/gitlab/tests/test_cli.py +++ b/gitlab/tests/test_cli.py @@ -28,20 +28,37 @@ import pytest 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"), + ], +) +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"), + ], +) +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 42b94aa..a84a6a9 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -28,8 +28,8 @@ import gitlab.v4.objects 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 |
