summaryrefslogtreecommitdiff
path: root/gitlab/v4/cli.py
diff options
context:
space:
mode:
authorMax Wittig <max.wittig95@gmail.com>2019-05-16 18:00:34 +0200
committerMax Wittig <max.wittig95@gmail.com>2019-05-16 18:03:56 +0200
commit318d2770cbc90ae4d33170274e214b9d828bca43 (patch)
treec5b6d6d9b57f6642dca83ea33956a4d5ae15e5eb /gitlab/v4/cli.py
parentef32990347d0ab9145b8919d25269766dc2ce445 (diff)
downloadgitlab-refactor/black.tar.gz
refactor: format everything blackrefactor/black
Diffstat (limited to 'gitlab/v4/cli.py')
-rw-r--r--gitlab/v4/cli.py261
1 files changed, 153 insertions, 108 deletions
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py
index 242874d..f0ed199 100644
--- a/gitlab/v4/cli.py
+++ b/gitlab/v4/cli.py
@@ -33,12 +33,11 @@ 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.what = what.replace('-', '_')
+ self.what = what.replace("-", "_")
self.action = action.lower()
self.gl = gl
self.args = args
- self.mgr_cls = getattr(gitlab.v4.objects,
- self.cls.__name__ + 'Manager')
+ self.mgr_cls = 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
@@ -46,7 +45,7 @@ class GitlabCLI(object):
self.mgr_cls._path = self.mgr_cls._path % self.args
self.mgr = self.mgr_cls(gl)
- types = getattr(self.mgr_cls, '_types', {})
+ types = getattr(self.mgr_cls, "_types", {})
if types:
for attr_name, type_cls in types.items():
if attr_name in self.args.keys():
@@ -56,12 +55,12 @@ class GitlabCLI(object):
def __call__(self):
# Check for a method that matches object + action
- method = 'do_%s_%s' % (self.what, self.action)
+ method = "do_%s_%s" % (self.what, self.action)
if hasattr(self, method):
return getattr(self, method)()
# Fallback to standard actions (get, list, create, ...)
- method = 'do_%s' % self.action
+ method = "do_%s" % self.action
if hasattr(self, method):
return getattr(self, method)()
@@ -74,23 +73,22 @@ class GitlabCLI(object):
# Get the object (lazy), then act
if in_obj:
data = {}
- if hasattr(self.mgr, '_from_parent_attrs'):
+ if hasattr(self.mgr, "_from_parent_attrs"):
for k in self.mgr._from_parent_attrs:
data[k] = self.args[k]
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(self.cls):
data[self.cls._id_attr] = self.args.pop(self.cls._id_attr)
o = self.cls(self.mgr, data)
- method_name = self.action.replace('-', '_')
+ method_name = self.action.replace("-", "_")
return getattr(o, method_name)(**self.args)
else:
return getattr(self.mgr, self.action)(**self.args)
def do_project_export_download(self):
try:
- project = self.gl.projects.get(int(self.args['project_id']),
- lazy=True)
+ project = self.gl.projects.get(int(self.args["project_id"]), lazy=True)
data = project.exports.get().download()
- if hasattr(sys.stdout, 'buffer'):
+ if hasattr(sys.stdout, "buffer"):
# python3
sys.stdout.buffer.write(data)
else:
@@ -139,121 +137,163 @@ class GitlabCLI(object):
def _populate_sub_parser_by_class(cls, sub_parser):
- mgr_cls_name = cls.__name__ + 'Manager'
+ mgr_cls_name = cls.__name__ + "Manager"
mgr_cls = getattr(gitlab.v4.objects, mgr_cls_name)
- for action_name in ['list', 'get', 'create', 'update', 'delete']:
+ for action_name in ["list", "get", "create", "update", "delete"]:
if not hasattr(mgr_cls, action_name):
continue
sub_parser_action = sub_parser.add_parser(action_name)
sub_parser_action.add_argument("--sudo", required=False)
- if hasattr(mgr_cls, '_from_parent_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in mgr_cls._from_parent_attrs]
+ if hasattr(mgr_cls, "_from_parent_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._from_parent_attrs
+ ]
if action_name == "list":
- if hasattr(mgr_cls, '_list_filters'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in mgr_cls._list_filters]
+ if hasattr(mgr_cls, "_list_filters"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in mgr_cls._list_filters
+ ]
sub_parser_action.add_argument("--page", required=False)
sub_parser_action.add_argument("--per-page", required=False)
- sub_parser_action.add_argument("--all", required=False,
- action='store_true')
+ sub_parser_action.add_argument("--all", required=False, action="store_true")
- if action_name == 'delete':
+ if action_name == "delete":
if cls._id_attr is not None:
- id_attr = cls._id_attr.replace('_', '-')
+ id_attr = cls._id_attr.replace("_", "-")
sub_parser_action.add_argument("--%s" % id_attr, required=True)
if action_name == "get":
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(cls):
if cls._id_attr is not None:
- id_attr = cls._id_attr.replace('_', '-')
- sub_parser_action.add_argument("--%s" % id_attr,
- required=True)
+ id_attr = cls._id_attr.replace("_", "-")
+ sub_parser_action.add_argument("--%s" % id_attr, required=True)
- if hasattr(mgr_cls, '_optional_get_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in mgr_cls._optional_get_attrs]
+ if hasattr(mgr_cls, "_optional_get_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in mgr_cls._optional_get_attrs
+ ]
if action_name == "create":
- if hasattr(mgr_cls, '_create_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in mgr_cls._create_attrs[0]]
-
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in mgr_cls._create_attrs[1]]
+ if hasattr(mgr_cls, "_create_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._create_attrs[0]
+ ]
+
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in mgr_cls._create_attrs[1]
+ ]
if action_name == "update":
if cls._id_attr is not None:
- id_attr = cls._id_attr.replace('_', '-')
- sub_parser_action.add_argument("--%s" % id_attr,
- required=True)
-
- if hasattr(mgr_cls, '_update_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in mgr_cls._update_attrs[0] if x != cls._id_attr]
+ id_attr = cls._id_attr.replace("_", "-")
+ sub_parser_action.add_argument("--%s" % id_attr, required=True)
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in mgr_cls._update_attrs[1] if x != cls._id_attr]
+ if hasattr(mgr_cls, "_update_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._update_attrs[0]
+ if x != cls._id_attr
+ ]
+
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in mgr_cls._update_attrs[1]
+ if x != cls._id_attr
+ ]
if cls.__name__ in cli.custom_actions:
name = cls.__name__
for action_name in cli.custom_actions[name]:
sub_parser_action = sub_parser.add_parser(action_name)
# Get the attributes for URL/path construction
- if hasattr(mgr_cls, '_from_parent_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in mgr_cls._from_parent_attrs]
+ if hasattr(mgr_cls, "_from_parent_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._from_parent_attrs
+ ]
sub_parser_action.add_argument("--sudo", required=False)
# We need to get the object somehow
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(cls):
if cls._id_attr is not None:
- id_attr = cls._id_attr.replace('_', '-')
- sub_parser_action.add_argument("--%s" % id_attr,
- required=True)
+ id_attr = cls._id_attr.replace("_", "-")
+ sub_parser_action.add_argument("--%s" % id_attr, required=True)
required, optional, dummy = cli.custom_actions[name][action_name]
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in required if x != cls._id_attr]
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in optional if x != cls._id_attr]
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in required
+ if x != cls._id_attr
+ ]
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in optional
+ if x != cls._id_attr
+ ]
if mgr_cls.__name__ in cli.custom_actions:
name = mgr_cls.__name__
for action_name in cli.custom_actions[name]:
sub_parser_action = sub_parser.add_parser(action_name)
- if hasattr(mgr_cls, '_from_parent_attrs'):
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in mgr_cls._from_parent_attrs]
+ if hasattr(mgr_cls, "_from_parent_attrs"):
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._from_parent_attrs
+ ]
sub_parser_action.add_argument("--sudo", required=False)
required, optional, dummy = cli.custom_actions[name][action_name]
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=True)
- for x in required if x != cls._id_attr]
- [sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
- required=False)
- for x in optional if x != cls._id_attr]
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in required
+ if x != cls._id_attr
+ ]
+ [
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
+ for x in optional
+ if x != cls._id_attr
+ ]
def extend_parser(parser):
- subparsers = parser.add_subparsers(title='object', dest='what',
- help="Object to manipulate.")
+ subparsers = parser.add_subparsers(
+ title="object", dest="what", help="Object to manipulate."
+ )
subparsers.required = True
# populate argparse for all Gitlab Object
@@ -272,8 +312,8 @@ def extend_parser(parser):
object_group = subparsers.add_parser(arg_name)
object_subparsers = object_group.add_subparsers(
- title='action',
- dest='action', help="Action to execute.")
+ title="action", dest="action", help="Action to execute."
+ )
_populate_sub_parser_by_class(cls, object_subparsers)
object_subparsers.required = True
@@ -285,18 +325,19 @@ def get_dict(obj, fields):
return obj
if fields:
- return {k: v for k, v in obj.attributes.items()
- if k in fields}
+ return {k: v for k, v in obj.attributes.items() if k in fields}
return obj.attributes
class JSONPrinter(object):
def display(self, d, **kwargs):
import json # noqa
+
print(json.dumps(d))
def display_list(self, data, fields, **kwargs):
import json # noqa
+
print(json.dumps([get_dict(obj, fields) for obj in data]))
@@ -304,39 +345,47 @@ class YAMLPrinter(object):
def display(self, d, **kwargs):
try:
import yaml # noqa
+
print(yaml.safe_dump(d, default_flow_style=False))
except ImportError:
- exit("PyYaml is not installed.\n"
- "Install it with `pip install PyYaml` "
- "to use the yaml output feature")
+ exit(
+ "PyYaml is not installed.\n"
+ "Install it with `pip install PyYaml` "
+ "to use the yaml output feature"
+ )
def display_list(self, data, fields, **kwargs):
try:
import yaml # noqa
- print(yaml.safe_dump(
- [get_dict(obj, fields) for obj in data],
- default_flow_style=False))
+
+ print(
+ yaml.safe_dump(
+ [get_dict(obj, fields) for obj in data], default_flow_style=False
+ )
+ )
except ImportError:
- exit("PyYaml is not installed.\n"
- "Install it with `pip install PyYaml` "
- "to use the yaml output feature")
+ exit(
+ "PyYaml is not installed.\n"
+ "Install it with `pip install PyYaml` "
+ "to use the yaml output feature"
+ )
class LegacyPrinter(object):
def display(self, d, **kwargs):
- verbose = kwargs.get('verbose', False)
- padding = kwargs.get('padding', 0)
- obj = kwargs.get('obj')
+ verbose = kwargs.get("verbose", False)
+ padding = kwargs.get("padding", 0)
+ obj = kwargs.get("obj")
def display_dict(d, padding):
for k in sorted(d.keys()):
v = d[k]
if isinstance(v, dict):
- print('%s%s:' % (' ' * padding, k.replace('_', '-')))
+ print("%s%s:" % (" " * padding, k.replace("_", "-")))
new_padding = padding + 2
self.display(v, verbose=True, padding=new_padding, obj=v)
continue
- print('%s%s: %s' % (' ' * padding, k.replace('_', '-'), v))
+ print("%s%s: %s" % (" " * padding, k.replace("_", "-"), v))
if verbose:
if isinstance(obj, dict):
@@ -346,7 +395,7 @@ class LegacyPrinter(object):
# not a dict, we assume it's a RESTObject
if obj._id_attr:
id = getattr(obj, obj._id_attr, None)
- print('%s: %s' % (obj._id_attr, id))
+ print("%s: %s" % (obj._id_attr, id))
attrs = obj.attributes
if obj._id_attr:
attrs.pop(obj._id_attr)
@@ -355,33 +404,29 @@ class LegacyPrinter(object):
else:
if obj._id_attr:
id = getattr(obj, obj._id_attr)
- print('%s: %s' % (obj._id_attr.replace('_', '-'), id))
- if hasattr(obj, '_short_print_attr'):
+ print("%s: %s" % (obj._id_attr.replace("_", "-"), id))
+ if hasattr(obj, "_short_print_attr"):
value = getattr(obj, obj._short_print_attr)
- value = value.replace('\r', '').replace('\n', ' ')
+ value = value.replace("\r", "").replace("\n", " ")
# If the attribute is a note (ProjectCommitComment) then we do
# some modifications to fit everything on one line
- line = '%s: %s' % (obj._short_print_attr, value)
+ line = "%s: %s" % (obj._short_print_attr, value)
# ellipsize long lines (comments)
if len(line) > 79:
- line = line[:76] + '...'
+ line = line[:76] + "..."
print(line)
def display_list(self, data, fields, **kwargs):
- verbose = kwargs.get('verbose', False)
+ verbose = kwargs.get("verbose", False)
for obj in data:
if isinstance(obj, gitlab.base.RESTObject):
self.display(get_dict(obj, fields), verbose=verbose, obj=obj)
else:
print(obj)
- print('')
+ print("")
-PRINTERS = {
- 'json': JSONPrinter,
- 'legacy': LegacyPrinter,
- 'yaml': YAMLPrinter,
-}
+PRINTERS = {"json": JSONPrinter, "legacy": LegacyPrinter, "yaml": YAMLPrinter}
def run(gl, what, action, args, verbose, output, fields):
@@ -398,5 +443,5 @@ def run(gl, what, action, args, verbose, output, fields):
printer.display(get_dict(data, fields), verbose=verbose, obj=data)
elif isinstance(data, six.string_types):
print(data)
- elif hasattr(data, 'decode'):
+ elif hasattr(data, "decode"):
print(data.decode())