Skip to content

Commit

Permalink
Merge pull request #1290 from python-gitlab/fix/parse-cli-objects-cam…
Browse files Browse the repository at this point in the history
…elcase

fix(cli): fix parsing CLI objects to classnames
  • Loading branch information
JohnVillalovos committed May 18, 2021
2 parents ce0e642 + 8cf5031 commit 1508eb7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
26 changes: 19 additions & 7 deletions gitlab/cli.py
Expand Up @@ -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: {
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 33 additions & 10 deletions gitlab/tests/test_cli.py
Expand Up @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions gitlab/v4/cli.py
Expand Up @@ -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
Expand Down

0 comments on commit 1508eb7

Please sign in to comment.