- Notifications
You must be signed in to change notification settings - Fork673
chore: add type-hints to gitlab/v4/cli.py#1483
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion.mypy.ini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
159 changes: 120 additions & 39 deletionsgitlab/v4/cli.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -16,8 +16,10 @@ | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
import argparse | ||
import operator | ||
import sys | ||
from typing import Any, Dict, List, Optional, Type, TYPE_CHECKING, Union | ||
import gitlab | ||
import gitlab.base | ||
@@ -26,18 +28,31 @@ | ||
class GitlabCLI(object): | ||
def __init__( | ||
self, gl: gitlab.Gitlab, what: str, action: str, args: Dict[str, str] | ||
) -> None: | ||
self.cls: Type[gitlab.base.RESTObject] = 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 | ||
self.args = args | ||
self.mgr_cls: Union[ | ||
Type[gitlab.mixins.CreateMixin], | ||
Type[gitlab.mixins.DeleteMixin], | ||
Type[gitlab.mixins.GetMixin], | ||
Type[gitlab.mixins.GetWithoutIdMixin], | ||
Type[gitlab.mixins.ListMixin], | ||
Type[gitlab.mixins.UpdateMixin], | ||
] = getattr(gitlab.v4.objects, self.cls.__name__ + "Manager") | ||
# We could do something smart, like splitting the manager name to find | ||
# parents, build the chain of managers to get to the final object. | ||
# Instead we do something ugly and efficient: interpolate variables in | ||
# the class _path attribute, and replace the value with the result. | ||
if TYPE_CHECKING: | ||
assert self.mgr_cls._path is not None | ||
self.mgr_cls._path = self.mgr_cls._path % self.args | ||
self.mgr = self.mgr_cls(gl) | ||
@@ -48,7 +63,7 @@ def __init__(self, gl, what, action, args): | ||
obj.set_from_cli(self.args[attr_name]) | ||
self.args[attr_name] = obj.get() | ||
def __call__(self) -> Any: | ||
# Check for a method that matches object + action | ||
method = "do_%s_%s" % (self.what, self.action) | ||
if hasattr(self, method): | ||
@@ -62,7 +77,7 @@ def __call__(self): | ||
# Finally try to find custom methods | ||
return self.do_custom() | ||
def do_custom(self) -> Any: | ||
in_obj = cli.custom_actions[self.cls_name][self.action][2] | ||
# Get the object (lazy), then act | ||
@@ -72,14 +87,16 @@ def do_custom(self): | ||
for k in self.mgr._from_parent_attrs: | ||
data[k] = self.args[k] | ||
if not issubclass(self.cls, gitlab.mixins.GetWithoutIdMixin): | ||
if TYPE_CHECKING: | ||
assert isinstance(self.cls._id_attr, str) | ||
data[self.cls._id_attr] = self.args.pop(self.cls._id_attr) | ||
obj = self.cls(self.mgr, data) | ||
method_name = self.action.replace("-", "_") | ||
return getattr(obj, method_name)(**self.args) | ||
else: | ||
return getattr(self.mgr, self.action)(**self.args) | ||
def do_project_export_download(self) -> None: | ||
try: | ||
project = self.gl.projects.get(int(self.args["project_id"]), lazy=True) | ||
data = project.exports.get().download() | ||
@@ -88,46 +105,75 @@ def do_project_export_download(self): | ||
except Exception as e: | ||
cli.die("Impossible to download the export", e) | ||
def do_create(self) -> gitlab.base.RESTObject: | ||
if TYPE_CHECKING: | ||
assert isinstance(self.mgr, gitlab.mixins.CreateMixin) | ||
try: | ||
result = self.mgr.create(self.args) | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
except Exception as e: | ||
cli.die("Impossible to create object", e) | ||
return result | ||
def do_list( | ||
self, | ||
) -> Union[gitlab.base.RESTObjectList, List[gitlab.base.RESTObject]]: | ||
if TYPE_CHECKING: | ||
assert isinstance(self.mgr, gitlab.mixins.ListMixin) | ||
try: | ||
result = self.mgr.list(**self.args) | ||
except Exception as e: | ||
cli.die("Impossible to list objects", e) | ||
return result | ||
def do_get(self) -> Optional[gitlab.base.RESTObject]: | ||
if isinstance(self.mgr, gitlab.mixins.GetWithoutIdMixin): | ||
try: | ||
result = self.mgr.get(id=None, **self.args) | ||
except Exception as e: | ||
cli.die("Impossible to get object", e) | ||
return result | ||
if TYPE_CHECKING: | ||
assert isinstance(self.mgr, gitlab.mixins.GetMixin) | ||
assert isinstance(self.cls._id_attr, str) | ||
id = self.args.pop(self.cls._id_attr) | ||
try: | ||
result =self.mgr.get(id, lazy=False, **self.args) | ||
except Exception as e: | ||
cli.die("Impossible to get object", e) | ||
return result | ||
def do_delete(self) -> None: | ||
if TYPE_CHECKING: | ||
assert isinstance(self.mgr, gitlab.mixins.DeleteMixin) | ||
assert isinstance(self.cls._id_attr, str) | ||
id = self.args.pop(self.cls._id_attr) | ||
try: | ||
self.mgr.delete(id, **self.args) | ||
except Exception as e: | ||
cli.die("Impossible to destroy object", e) | ||
def do_update(self) -> Dict[str, Any]: | ||
if TYPE_CHECKING: | ||
assert isinstance(self.mgr, gitlab.mixins.UpdateMixin) | ||
if issubclass(self.mgr_cls, gitlab.mixins.GetWithoutIdMixin): | ||
id = None | ||
else: | ||
if TYPE_CHECKING: | ||
assert isinstance(self.cls._id_attr, str) | ||
id = self.args.pop(self.cls._id_attr) | ||
try: | ||
result = self.mgr.update(id, self.args) | ||
except Exception as e: | ||
cli.die("Impossible to update object", e) | ||
return result | ||
def _populate_sub_parser_by_class( | ||
cls: Type[gitlab.base.RESTObject], sub_parser: argparse._SubParsersAction | ||
) -> None: | ||
mgr_cls_name = cls.__name__ + "Manager" | ||
mgr_cls = getattr(gitlab.v4.objects, mgr_cls_name) | ||
@@ -258,7 +304,7 @@ def _populate_sub_parser_by_class(cls, sub_parser): | ||
] | ||
def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: | ||
subparsers = parser.add_subparsers( | ||
title="object", dest="what", help="Object to manipulate." | ||
) | ||
@@ -287,7 +333,9 @@ def extend_parser(parser): | ||
return parser | ||
def get_dict( | ||
obj: Union[str, gitlab.base.RESTObject], fields: List[str] | ||
) -> Union[str, Dict[str, Any]]: | ||
if isinstance(obj, str): | ||
return obj | ||
@@ -297,19 +345,24 @@ def get_dict(obj, fields): | ||
class JSONPrinter(object): | ||
def display(self, d: Union[str, Dict[str, Any]],**kwargs: Any) -> None: | ||
import json # noqa | ||
print(json.dumps(d)) | ||
def display_list( | ||
self, | ||
data: List[Union[str, gitlab.base.RESTObject]], | ||
fields: List[str], | ||
**kwargs: Any | ||
) -> None: | ||
import json # noqa | ||
print(json.dumps([get_dict(obj, fields) for obj in data])) | ||
class YAMLPrinter(object): | ||
def display(self, d: Union[str, Dict[str, Any]],**kwargs: Any) -> None: | ||
try: | ||
import yaml # noqa | ||
@@ -321,7 +374,12 @@ def display(self, d, **kwargs): | ||
"to use the yaml output feature" | ||
) | ||
def display_list( | ||
self, | ||
data: List[Union[str, gitlab.base.RESTObject]], | ||
fields: List[str], | ||
**kwargs: Any | ||
) -> None: | ||
try: | ||
import yaml # noqa | ||
@@ -339,12 +397,14 @@ def display_list(self, data, fields, **kwargs): | ||
class LegacyPrinter(object): | ||
def display(self, d: Union[str, Dict[str, Any]],**kwargs: Any) -> None: | ||
verbose = kwargs.get("verbose", False) | ||
padding = kwargs.get("padding", 0) | ||
obj: Optional[Union[Dict[str, Any], gitlab.base.RESTObject]] = kwargs.get("obj") | ||
if TYPE_CHECKING: | ||
assert obj is not None | ||
def display_dict(d: Dict[str, Any],padding: int) -> None: | ||
for k in sorted(d.keys()): | ||
v = d[k] | ||
if isinstance(v, dict): | ||
@@ -369,6 +429,8 @@ def display_dict(d, padding): | ||
display_dict(attrs, padding) | ||
else: | ||
if TYPE_CHECKING: | ||
assert isinstance(obj, gitlab.base.RESTObject) | ||
if obj._id_attr: | ||
id = getattr(obj, obj._id_attr) | ||
print("%s: %s" % (obj._id_attr.replace("_", "-"), id)) | ||
@@ -383,7 +445,12 @@ def display_dict(d, padding): | ||
line = line[:76] + "..." | ||
print(line) | ||
def display_list( | ||
self, | ||
data: List[Union[str, gitlab.base.RESTObject]], | ||
fields: List[str], | ||
**kwargs: Any | ||
) -> None: | ||
verbose = kwargs.get("verbose", False) | ||
for obj in data: | ||
if isinstance(obj, gitlab.base.RESTObject): | ||
@@ -393,14 +460,28 @@ def display_list(self, data, fields, **kwargs): | ||
print("") | ||
PRINTERS: Dict[ | ||
str, Union[Type[JSONPrinter], Type[LegacyPrinter], Type[YAMLPrinter]] | ||
] = { | ||
"json": JSONPrinter, | ||
"legacy": LegacyPrinter, | ||
"yaml": YAMLPrinter, | ||
} | ||
def run( | ||
gl: gitlab.Gitlab, | ||
what: str, | ||
action: str, | ||
args: Dict[str, Any], | ||
verbose: bool, | ||
output: str, | ||
fields: List[str], | ||
) -> None: | ||
g_cli = GitlabCLI(gl=gl, what=what, action=action, args=args) | ||
data = g_cli() | ||
printer: Union[JSONPrinter, LegacyPrinter, YAMLPrinter] = PRINTERS[output]() | ||
if isinstance(data, dict): | ||
printer.display(data, verbose=True, obj=data) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.