Skip to content

Commit 0edbcaf

Browse files
committed
fix(cli): fix parsing CLI objects to classnames
1 parent 9d6c188 commit 0edbcaf

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

gitlab/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import gitlab.config
2727

28-
camel_re = re.compile("(.)([A-Z])")
28+
# Full credit for this regex goes to:
29+
# https://github.com/jpvanhal/inflection/blob/master/inflection/__init__.py
30+
camel_re = re.compile(r"([A-Z]+)([A-Z][a-z])")
31+
camel_re2 = re.compile(r"([a-z\d])([A-Z])")
2932

3033
# custom_actions = {
3134
# cls: {
@@ -75,7 +78,8 @@ def what_to_cls(what):
7578

7679

7780
def cls_to_what(cls):
78-
return camel_re.sub(r"\1-\2", cls.__name__).lower()
81+
what = camel_re.sub(r"\1-\2", cls.__name__)
82+
return camel_re2.sub(r"\1-\2", what).lower()
7983

8084

8185
def _get_base_parser(add_help=True):

gitlab/tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import pytest
2727

2828
from gitlab import cli
29+
from gitlab.v4.objects import CurrentUserGPGKey
2930
import gitlab.v4.cli
3031

3132

@@ -34,15 +35,25 @@ def test_what_to_cls():
3435
assert "FooBar" == cli.what_to_cls("foo-bar")
3536

3637

38+
def test_what_to_cls_v4(gl):
39+
what = "current-user-gpg-key"
40+
g_cli = gitlab.v4.cli.GitlabCLI(gl, what, "list", [])
41+
assert CurrentUserGPGKey == g_cli.what_to_cls(what)
42+
43+
3744
def test_cls_to_what():
3845
class Class(object):
3946
pass
4047

4148
class TestClass(object):
4249
pass
4350

51+
class TestUPPERClass(object):
52+
pass
53+
4454
assert "test-class" == cli.cls_to_what(TestClass)
4555
assert "class" == cli.cls_to_what(Class)
56+
assert "test-upper-class" == cli.cls_to_what(TestUPPERClass)
4657

4758

4859
def test_die():

gitlab/v4/cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import operator
2121
import sys
2222

23+
from requests.structures import CaseInsensitiveDict
24+
2325
import gitlab
2426
import gitlab.base
2527
from gitlab import cli
@@ -28,8 +30,9 @@
2830

2931
class GitlabCLI(object):
3032
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]
33+
self.classes = gitlab.v4.objects.__dict__
34+
self.cls = self.what_to_cls(what)
35+
self.cls_name = self.cls.__name__
3336
self.what = what.replace("-", "_")
3437
self.action = action.lower()
3538
self.gl = gl
@@ -64,6 +67,12 @@ def __call__(self):
6467
# Finally try to find custom methods
6568
return self.do_custom()
6669

70+
def what_to_cls(self, what):
71+
"""Case-insensitive lookup of module class names from CLI args"""
72+
classes = CaseInsensitiveDict(self.classes)
73+
lowercase_class = what.replace("-", "")
74+
return classes[lowercase_class]
75+
6776
def do_custom(self):
6877
in_obj = cli.custom_actions[self.cls_name][self.action][2]
6978

0 commit comments

Comments
 (0)