|
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: |
|
0 commit comments