Skip to content

Commit 231d10f

Browse files
committed
Extract pagination parameters to common decorator
This helps us avoid duplicating some stuff nearly identically, at the loss of a tiny bit of detail in the parameter help strings. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 1c47ea1 commit 231d10f

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

git_pw/bundle.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
LOG = logging.getLogger(__name__)
1414

1515
_list_headers = ('ID', 'Name', 'Owner', 'Public')
16+
_sort_fields = ('id', '-id', 'name', '-name')
1617

1718

1819
def _get_bundle(bundle_id):
@@ -117,14 +118,7 @@ def _format_patch(patch):
117118
@click.option('--owner', metavar='OWNER', multiple=True,
118119
help='Show only bundles with these owners. Should be an email, '
119120
'name or ID. Private bundles of other users will not be shown.')
120-
@click.option('--limit', metavar='LIMIT', type=click.INT,
121-
help='Maximum number of bundles to show.')
122-
@click.option('--page', metavar='PAGE', type=click.INT,
123-
help='Page to retrieve bundles from. This is influenced by the '
124-
'size of LIMIT.')
125-
@click.option('--sort', metavar='FIELD', default='name', type=click.Choice(
126-
['id', '-id', 'name', '-name']),
127-
help='Sort output on given field.')
121+
@utils.pagination_options(sort_fields=_sort_fields, default_sort='name')
128122
@utils.format_options(headers=_list_headers)
129123
@click.argument('name', required=False)
130124
@api.validate_multiple_filter_support

git_pw/patch.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
_list_headers = ('ID', 'Date', 'Name', 'Submitter', 'State', 'Archived',
1717
'Delegate')
18+
_sort_fields = ('id', '-id', 'name', '-name', 'date', '-date')
1819

1920

2021
@click.command(name='apply', context_settings=dict(
@@ -194,14 +195,7 @@ def update_cmd(patch_ids, commit_ref, state, delegate, archived, fmt):
194195
'email or username.')
195196
@click.option('--archived', default=False, is_flag=True,
196197
help='Include patches that are archived.')
197-
@click.option('--limit', metavar='LIMIT', type=click.INT,
198-
help='Maximum number of patches to show.')
199-
@click.option('--page', metavar='PAGE', type=click.INT,
200-
help='Page to retrieve patches from. This is influenced by the '
201-
'size of LIMIT.')
202-
@click.option('--sort', metavar='FIELD', default='-date', type=click.Choice(
203-
['id', '-id', 'name', '-name', 'date', '-date']),
204-
help='Sort output on given field.')
198+
@utils.pagination_options(sort_fields=_sort_fields, default_sort='-date')
205199
@utils.format_options(headers=_list_headers)
206200
@click.argument('name', required=False)
207201
@api.validate_multiple_filter_support

git_pw/series.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
LOG = logging.getLogger(__name__)
1515

1616
_list_headers = ('ID', 'Date', 'Name', 'Version', 'Submitter')
17+
_sort_fields = ('id', '-id', 'name', '-name', 'date', '-date')
1718

1819

1920
@click.command(name='apply', context_settings=dict(
@@ -104,14 +105,7 @@ def _format_submission(submission):
104105
@click.option('--submitter', metavar='SUBMITTER', multiple=True,
105106
help='Show only series by these submitters. Should be an '
106107
'email, name or ID.')
107-
@click.option('--limit', metavar='LIMIT', type=click.INT,
108-
help='Maximum number of series to show.')
109-
@click.option('--page', metavar='PAGE', type=click.INT,
110-
help='Page to retrieve series from. This is influenced by the '
111-
'size of LIMIT.')
112-
@click.option('--sort', metavar='FIELD', default='-date', type=click.Choice(
113-
['id', '-id', 'name', '-name', 'date', '-date']),
114-
help='Sort output on given field.')
108+
@utils.pagination_options(sort_fields=_sort_fields, default_sort='-date')
115109
@utils.format_options(headers=_list_headers)
116110
@click.argument('name', required=False)
117111
@api.validate_multiple_filter_support

git_pw/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ def echo(output, headers, fmt):
149149
click.echo(_tabulate(output, headers, fmt))
150150

151151

152+
def pagination_options(sort_fields, default_sort):
153+
"""Shared pagination options."""
154+
155+
def _pagination_options(f):
156+
f = click.option('--limit', metavar='LIMIT', type=click.INT,
157+
help='Maximum number of items to show.')(f)
158+
f = click.option('--page', metavar='PAGE', type=click.INT,
159+
help='Page to retrieve items from. This is '
160+
'influenced by the size of LIMIT.')(f)
161+
f = click.option('--sort', metavar='FIELD', default=default_sort,
162+
type=click.Choice(sort_fields),
163+
help='Sort output on given field.')(f)
164+
165+
return f
166+
167+
return _pagination_options
168+
169+
152170
def format_options(original_function=None, headers=None):
153171
"""Shared output format options."""
154172

0 commit comments

Comments
 (0)