| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,11 +21,18 @@ | |||
| 21 | 21 | import functools | |
| 22 | 22 | import re | |
| 23 | 23 | import sys | |
| 24 | - from typing import Any, Callable, cast, Dict, Optional, Tuple, TypeVar, Union | ||
| 24 | + from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, TypeVar, Union | ||
| 25 | 25 | ||
| 26 | - import gitlab.config # noqa: F401 | ||
| 26 | + from requests.structures import CaseInsensitiveDict | ||
| 27 | 27 | ||
| 28 | - camel_re = re.compile("(.)([A-Z])") | ||
| 28 | + import gitlab.config | ||
| 29 | + from gitlab.base import RESTObject | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + # This regex is based on: | ||
| 33 | + # https://github.com/jpvanhal/inflection/blob/master/inflection/__init__.py | ||
| 34 | + camel_upperlower_regex = re.compile(r"([A-Z]+)([A-Z][a-z])") | ||
| 35 | + camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])") | ||
| 29 | 36 | ||
| 30 | 37 | # custom_actions = { | |
| 31 | 38 | # cls: { | |
@@ -82,12 +89,17 @@ def die(msg: str, e: Optional[Exception] = None) -> None: | |||
| 82 | 89 | sys.exit(1) | |
| 83 | 90 | ||
| 84 | 91 | ||
| 85 | - def what_to_cls(what: str) -> str: | ||
| 86 | - return "".join([s.capitalize() for s in what.split("-")]) | ||
| 92 | + def what_to_cls(what: str, namespace: Type) -> RESTObject: | ||
| 93 | + classes = CaseInsensitiveDict(namespace.__dict__) | ||
| 94 | + lowercase_class = what.replace("-", "") | ||
| 95 | + | ||
| 96 | + return classes[lowercase_class] | ||
| 87 | 97 | ||
| 88 | 98 | ||
| 89 | - def cls_to_what(cls: Any) -> str: | ||
| 90 | - return camel_re.sub(r"\1-\2", cls.__name__).lower() | ||
| 99 | + def cls_to_what(cls: RESTObject) -> str: | ||
| 100 | + dasherized_uppercase = camel_upperlower_regex.sub(r"\1-\2", cls.__name__) | ||
| 101 | + dasherized_lowercase = camel_lowerupper_regex.sub(r"\1-\2", dasherized_uppercase) | ||
| 102 | + return dasherized_lowercase.lower() | ||
| 91 | 103 | ||
| 92 | 104 | ||
| 93 | 105 | def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,20 +28,43 @@ | |||
| 28 | 28 | from gitlab import cli | |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | - def test_what_to_cls(): | ||
| 32 | - assert "Foo" == cli.what_to_cls("foo") | ||
| 33 | - assert "FooBar" == cli.what_to_cls("foo-bar") | ||
| 31 | + @pytest.mark.parametrize( | ||
| 32 | + "what,expected_class", | ||
| 33 | + [ | ||
| 34 | + ("class", "Class"), | ||
| 35 | + ("test-class", "TestClass"), | ||
| 36 | + ("test-longer-class", "TestLongerClass"), | ||
| 37 | + ("current-user-gpg-key", "CurrentUserGPGKey"), | ||
| 38 | + ("user-gpg-key", "UserGPGKey"), | ||
| 39 | + ("ldap-group", "LDAPGroup"), | ||
| 40 | + ], | ||
| 41 | + ) | ||
| 42 | + def test_what_to_cls(what, expected_class): | ||
| 43 | + def _namespace(): | ||
| 44 | + pass | ||
| 34 | 45 | ||
| 46 | + ExpectedClass = type(expected_class, (), {}) | ||
| 47 | + _namespace.__dict__[expected_class] = ExpectedClass | ||
| 35 | 48 | ||
| 36 | - def test_cls_to_what(): | ||
| 37 | - class Class(object): | ||
| 38 | - pass | ||
| 49 | + assert cli.what_to_cls(what, _namespace) == ExpectedClass | ||
| 39 | 50 | ||
| 40 | - class TestClass(object): | ||
| 41 | - pass | ||
| 42 | 51 | ||
| 43 | - assert "test-class" == cli.cls_to_what(TestClass) | ||
| 44 | - assert "class" == cli.cls_to_what(Class) | ||
| 52 | + @pytest.mark.parametrize( | ||
| 53 | + "class_name,expected_what", | ||
| 54 | + [ | ||
| 55 | + ("Class", "class"), | ||
| 56 | + ("TestClass", "test-class"), | ||
| 57 | + ("TestUPPERCASEClass", "test-uppercase-class"), | ||
| 58 | + ("UPPERCASETestClass", "uppercase-test-class"), | ||
| 59 | + ("CurrentUserGPGKey", "current-user-gpg-key"), | ||
| 60 | + ("UserGPGKey", "user-gpg-key"), | ||
| 61 | + ("LDAPGroup", "ldap-group"), | ||
| 62 | + ], | ||
| 63 | + ) | ||
| 64 | + def test_cls_to_what(class_name, expected_what): | ||
| 65 | + TestClass = type(class_name, (), {}) | ||
| 66 | + | ||
| 67 | + assert cli.cls_to_what(TestClass) == expected_what | ||
| 45 | 68 | ||
| 46 | 69 | ||
| 47 | 70 | def test_die(): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,8 +28,8 @@ | |||
| 28 | 28 | ||
| 29 | 29 | class GitlabCLI(object): | |
| 30 | 30 | def __init__(self, gl, what, action, args): | |
| 31 | - self.cls_name = cli.what_to_cls(what) | ||
| 32 | - self.cls = gitlab.v4.objects.__dict__[self.cls_name] | ||
| 31 | + self.cls = cli.what_to_cls(what, namespace=gitlab.v4.objects) | ||
| 32 | + self.cls_name = self.cls.__name__ | ||
| 33 | 33 | self.what = what.replace("-", "_") | |
| 34 | 34 | self.action = action.lower() | |
| 35 | 35 | self.gl = gl | |
| Back | FazBrowse Home | New Git URL |
0 commit comments