Skip to content

Commit fd45397

Browse files
author
Gauvain Pocentek
committed
Manage optional parameters for list() and get()
* List these elements in the API doc * Implement for License objects
1 parent 417d27c commit fd45397

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

docs/ext/docstrings.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,37 @@ def _process_docstring(app, what, name, obj, options, lines):
3030
class GitlabDocstring(GoogleDocstring):
3131
def _build_doc(self):
3232
cls = self._obj.obj_cls
33+
opt_get_list = cls.optionalGetAttrs
34+
opt_list_list = cls.optionalListAttrs
3335
md_create_list = list(itertools.chain(cls.requiredUrlAttrs,
3436
cls.requiredCreateAttrs))
3537
opt_create_list = cls.optionalCreateAttrs
3638

39+
opt_get_keys = "None"
40+
if opt_get_list:
41+
opt_get_keys = ", ".join(['``%s``' % i for i in opt_get_list])
42+
43+
opt_list_keys = "None"
44+
if opt_list_list:
45+
opt_list_keys = ", ".join(['``%s``' % i for i in opt_list_list])
46+
3747
md_create_keys = opt_create_keys = "None"
3848
if md_create_list:
39-
md_create_keys = "%s" % ", ".join(['``%s``' % i for i in
40-
md_create_list])
49+
md_create_keys = ", ".join(['``%s``' % i for i in md_create_list])
4150
if opt_create_list:
42-
opt_create_keys = "%s" % ", ".join(['``%s``' % i for i in
43-
opt_create_list])
51+
opt_create_keys = ", ".join(['``%s``' % i for i in
52+
opt_create_list])
4453

4554
md_update_list = list(itertools.chain(cls.requiredUrlAttrs,
4655
cls.requiredUpdateAttrs))
4756
opt_update_list = cls.optionalUpdateAttrs
4857

4958
md_update_keys = opt_update_keys = "None"
5059
if md_update_list:
51-
md_update_keys = "%s" % ", ".join(['``%s``' % i for i in
52-
md_update_list])
60+
md_update_keys = ", ".join(['``%s``' % i for i in md_update_list])
5361
if opt_update_list:
54-
opt_update_keys = "%s" % ", ".join(['``%s``' % i for i in
55-
opt_update_list])
62+
opt_update_keys = ", ".join(['``%s``' % i for i in
63+
opt_update_list])
5664

5765
tmpl_file = os.path.join(os.path.dirname(__file__), 'template.j2')
5866
with open(tmpl_file) as fd:
@@ -62,7 +70,9 @@ def _build_doc(self):
6270
md_create_keys=md_create_keys,
6371
opt_create_keys=opt_create_keys,
6472
md_update_keys=md_update_keys,
65-
opt_update_keys=opt_update_keys)
73+
opt_update_keys=opt_update_keys,
74+
opt_get_keys=opt_get_keys,
75+
opt_list_keys=opt_list_keys)
6676

6777
return output.split('\n')
6878

docs/ext/template.j2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ Mandatory arguments for object update: {{ md_create_keys }}
1919

2020
Optional arguments for object update: {{ opt_create_keys }}
2121
{% endif %}
22+
23+
{% if cls.canList %}
24+
Optional arguments for object listing: {{ opt_list_keys }}
25+
{% endif %}
26+
27+
{% if cls.canGet %}
28+
Optional arguments for object listing: {{ opt_get_keys }}
29+
{% endif %}

gitlab/cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def _populate_sub_parser_by_class(cls, sub_parser):
313313
sub_parser_action.add_argument("--page", required=False)
314314
sub_parser_action.add_argument("--per-page", required=False)
315315

316-
elif action_name in ["get", "delete"]:
316+
if action_name in ["get", "delete"]:
317317
if cls not in [gitlab.CurrentUser]:
318318
if cls.getRequiresId:
319319
id_attr = cls.idAttr.replace('_', '-')
@@ -323,15 +323,25 @@ def _populate_sub_parser_by_class(cls, sub_parser):
323323
required=True)
324324
for x in cls.requiredGetAttrs if x != cls.idAttr]
325325

326-
elif action_name == "create":
326+
if action_name == "get":
327+
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
328+
required=False)
329+
for x in cls.optionalGetAttrs]
330+
331+
if action_name == "list":
332+
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
333+
required=False)
334+
for x in cls.optionalListAttrs]
335+
336+
if action_name == "create":
327337
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
328338
required=True)
329339
for x in cls.requiredCreateAttrs]
330340
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
331341
required=False)
332342
for x in cls.optionalCreateAttrs]
333343

334-
elif action_name == "update":
344+
if action_name == "update":
335345
id_attr = cls.idAttr.replace('_', '-')
336346
sub_parser_action.add_argument("--%s" % id_attr,
337347
required=True)

gitlab/objects.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ class GitlabObject(object):
183183
requiredUrlAttrs = []
184184
#: Attributes that are required when retrieving list of objects.
185185
requiredListAttrs = []
186+
#: Attributes that are optional when retrieving list of objects.
187+
optionalListAttrs = []
188+
#: Attributes that are optional when retrieving single object.
189+
optionalGetAttrs = []
186190
#: Attributes that are required when retrieving single object.
187191
requiredGetAttrs = []
188192
#: Attributes that are required when deleting object.
@@ -754,6 +758,9 @@ class License(GitlabObject):
754758
canCreate = False
755759
idAttr = 'key'
756760

761+
optionalListAttrs = ['popular']
762+
optionalGetAttrs = ['project', 'fullname']
763+
757764

758765
class LicenseManager(BaseManager):
759766
obj_cls = License

0 commit comments

Comments
 (0)