Skip to content

fix(cli): fix parsing CLI objects to classnames #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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