| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,9 +23,14 @@ | |||
| 23 | 23 | import re | |
| 24 | 24 | import sys | |
| 25 | 25 | ||
| 26 | + from requests.structures import CaseInsensitiveDict | ||
| 27 | + | ||
| 26 | 28 | import gitlab.config | |
| 27 | 29 | ||
| 28 | - camel_re = re.compile("(.)([A-Z])") | ||
| 30 | + # Full credit for this regex goes to: | ||
| 31 | + # https://github.com/jpvanhal/inflection/blob/master/inflection/__init__.py | ||
| 32 | + camel_upperlower_regex = re.compile(r"([A-Z]+)([A-Z][a-z])") | ||
| 33 | + camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])") | ||
| 29 | 34 | ||
| 30 | 35 | # custom_actions = { | |
| 31 | 36 | # cls: { | |
@@ -70,12 +75,20 @@ def die(msg, e=None): | |||
| 70 | 75 | sys.exit(1) | |
| 71 | 76 | ||
| 72 | 77 | ||
| 73 | - def what_to_cls(what): | ||
| 74 | - return "".join([s.capitalize() for s in what.split("-")]) | ||
| 78 | + def what_to_cls(object, what): | ||
| 79 | + """Convert a kebab-case argument to the corresponding attribute of an object | ||
| 80 | + with a case-insensitive lookup.""" | ||
| 81 | + attributes = CaseInsensitiveDict(object.__dict__) | ||
| 82 | + lowercase_attribute = what.replace("-", "") | ||
| 83 | + | ||
| 84 | + return attributes[lowercase_attribute] | ||
| 75 | 85 | ||
| 76 | 86 | ||
| 77 | 87 | def cls_to_what(cls): | |
| 78 | - return camel_re.sub(r"\1-\2", cls.__name__).lower() | ||
| 88 | + """Convert CamelCase class names to kebab-case in two steps, to ensure names | ||
| 89 | + with whole upper-case words are correctly dash-separated as well.""" | ||
| 90 | + dash_upper = camel_upperlower_regex.sub(r"\1-\2", cls.__name__) | ||
| 91 | + return camel_lowerupper_regex.sub(r"\1-\2", dash_upper).lower() | ||
| 79 | 92 | ||
| 80 | 93 | ||
| 81 | 94 | def _get_base_parser(add_help=True): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,20 +29,34 @@ | |||
| 29 | 29 | import gitlab.v4.cli | |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | - def test_what_to_cls(): | ||
| 33 | - assert "Foo" == cli.what_to_cls("foo") | ||
| 34 | - assert "FooBar" == cli.what_to_cls("foo-bar") | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - def test_cls_to_what(): | ||
| 38 | - class Class(object): | ||
| 39 | - pass | ||
| 40 | - | ||
| 41 | - class TestClass(object): | ||
| 42 | - pass | ||
| 43 | - | ||
| 44 | - assert "test-class" == cli.cls_to_what(TestClass) | ||
| 45 | - assert "class" == cli.cls_to_what(Class) | ||
| 32 | + @pytest.mark.parametrize( | ||
| 33 | + "what,attribute_class", | ||
| 34 | + [ | ||
| 35 | + ("class", "Class"), | ||
| 36 | + ("test-class", "TestClass"), | ||
| 37 | + ("test-longer-class", "TestLongerClass"), | ||
| 38 | + ], | ||
| 39 | + ) | ||
| 40 | + def test_what_to_cls(what, attribute_class): | ||
| 41 | + AttributeClass = type(attribute_class, (), {}) | ||
| 42 | + GitlabCLI = type("GitlabCLI", (), {attribute_class: AttributeClass}) | ||
| 43 | + | ||
| 44 | + assert cli.what_to_cls(GitlabCLI, what) == AttributeClass | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + @pytest.mark.parametrize( | ||
| 48 | + "class_name,expected_what", | ||
| 49 | + [ | ||
| 50 | + ("Class", "class"), | ||
| 51 | + ("TestClass", "test-class"), | ||
| 52 | + ("TestUPPERCASEClass", "test-uppercase-class"), | ||
| 53 | + ("UPPERCASETestClass", "uppercase-test-class"), | ||
| 54 | + ], | ||
| 55 | + ) | ||
| 56 | + def test_cls_to_what(class_name, expected_what): | ||
| 57 | + TestClass = type(class_name, (), {}) | ||
| 58 | + | ||
| 59 | + assert cli.cls_to_what(TestClass) == expected_what | ||
| 46 | 60 | ||
| 47 | 61 | ||
| 48 | 62 | 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(self, what) | ||
| 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