Skip to content

Commit 2e98f92

Browse files
authored
Merge pull request #27 from nexB/click-progress
Display Click progress correctly
2 parents b21f9f1 + 65ea3e2 commit 2e98f92

File tree

3 files changed

+78
-47
lines changed

3 files changed

+78
-47
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Release notes
22
=============
33

4+
Version 21.8.27
5+
---------------
6+
7+
- Ensure that the progressbar displays a counter correctly.
8+
This is a fix for https://github.com/nexB/scancode-toolkit/issues/2583
9+
10+
411
Version 21.7.23
512
---------------
613

src/commoncode/cliutils.py

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,34 @@ def format_options(self, ctx, formatter):
152152
formatter.write_dl(sorted_records)
153153

154154

155-
class EnhancedProgressBar(ProgressBar):
155+
# overriden and copied from Click to work around Click woes for
156+
# https://github.com/nexB/scancode-toolkit/issues/2583
157+
class DebuggedProgressBar(ProgressBar):
158+
159+
# overriden and copied from Click to work around Click woes for
160+
# https://github.com/nexB/scancode-toolkit/issues/2583
161+
def make_step(self, n_steps):
162+
# always increment
163+
self.pos += n_steps or 1
164+
super(DebuggedProgressBar, self).make_step(n_steps)
165+
166+
# overriden and copied from Click to work around Click woes for
167+
# https://github.com/nexB/scancode-toolkit/issues/2583
168+
def generator(self):
169+
if self.is_hidden:
170+
yield from self.iter
171+
else:
172+
for rv in self.iter:
173+
self.current_item = rv
174+
self.update(1)
175+
self.render_progress()
176+
yield rv
177+
178+
self.finish()
179+
self.render_progress()
180+
181+
182+
class EnhancedProgressBar(DebuggedProgressBar):
156183
"""
157184
Enhanced progressbar ensuring that nothing is displayed when the bar is hidden.
158185
"""
@@ -205,17 +232,29 @@ def render_finish(self):
205232
BAR_SEP_LEN = len(' ')
206233

207234

208-
def progressmanager(iterable=None, length=None, label=None, show_eta=True,
209-
show_percent=None, show_pos=True, item_show_func=None,
210-
fill_char='#', empty_char='-', bar_template=None,
211-
info_sep=' ', width=BAR_WIDTH, file=None, color=None, # NOQA
212-
verbose=False):
213-
235+
def progressmanager(
236+
iterable=None,
237+
length=None,
238+
fill_char='#',
239+
empty_char='-',
240+
bar_template=None,
241+
info_sep=' ',
242+
show_eta=False,
243+
show_percent=False,
244+
show_pos=True,
245+
item_show_func=None,
246+
label=None,
247+
file=None,
248+
color=None, # NOQA
249+
update_min_steps=0,
250+
width=BAR_WIDTH,
251+
verbose=False,
252+
):
214253
"""
215254
Return an iterable context manager showing progress as a progress bar
216255
(default) or item-by-item log (if verbose is True) while iterating.
217256
218-
Its arguments are similar to Click.termui.progressbar with these new
257+
It's arguments are similar to Click.termui.progressbar with these new
219258
arguments added at the end of the signature:
220259
221260
:param verbose: if True, display a progress log. Otherwise, a progress bar.
@@ -224,14 +263,27 @@ def progressmanager(iterable=None, length=None, label=None, show_eta=True,
224263
progress_class = ProgressLogger
225264
else:
226265
progress_class = EnhancedProgressBar
227-
bar_template = ('[%(bar)s]' + ' ' + '%(info)s'
228-
if bar_template is None else bar_template)
229-
230-
return progress_class(iterable=iterable, length=length,
231-
show_eta=show_eta, show_percent=show_percent, show_pos=show_pos,
232-
item_show_func=item_show_func, fill_char=fill_char,
233-
empty_char=empty_char, bar_template=bar_template, info_sep=info_sep,
234-
file=file, label=label, width=width, color=color)
266+
bar_template = (
267+
'[%(bar)s]' + ' ' + '%(info)s'
268+
if bar_template is None else bar_template
269+
)
270+
return progress_class(
271+
iterable=iterable,
272+
length=length,
273+
fill_char=fill_char,
274+
empty_char=empty_char,
275+
bar_template=bar_template,
276+
info_sep=info_sep,
277+
show_eta=show_eta,
278+
show_percent=show_percent,
279+
show_pos=show_pos,
280+
item_show_func=item_show_func,
281+
label=label,
282+
file=file,
283+
color=color,
284+
update_min_steps=update_min_steps,
285+
width=width,
286+
)
235287

236288

237289
def fixed_width_file_name(path, max_length=25):
@@ -416,7 +468,6 @@ def get_help_record(self, ctx):
416468
return click.Option.get_help_record(self, ctx)
417469

418470

419-
420471
def validate_option_dependencies(ctx):
421472
"""
422473
Validate all PluggableCommandLineOption dependencies in the `ctx` Click

tests/test_cliutils.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
import click
1212
click.disable_unicode_literals_warning = True
13-
14-
from click.termui import progressbar
1513
from click.testing import CliRunner
1614

1715
from commoncode.testcase import FileDrivenTesting
@@ -20,31 +18,6 @@
2018
from commoncode.cliutils import PluggableCommandLineOption
2119

2220

23-
class TestUtils(FileDrivenTesting):
24-
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
25-
26-
def test_click_progressbar_with_labels(self):
27-
28-
# test related to https://github.com/mitsuhiko/click/issues/406
29-
@click.command()
30-
def mycli():
31-
"""Sample cmd with progress bar"""
32-
click.echo('Start')
33-
with progressbar(range(10), label='xyz') as it:
34-
for _ in it:
35-
pass
36-
click.echo('End')
37-
38-
runner = CliRunner()
39-
result = runner.invoke(mycli)
40-
assert result.exit_code == 0
41-
expected = '''Start
42-
xyz
43-
End
44-
'''
45-
assert result.output == expected
46-
47-
4821
class TestFixedWidthFilename(FileDrivenTesting):
4922

5023
def test_fixed_width_file_name_with_file_name_larger_than_max_length_is_shortened(self):
@@ -139,10 +112,10 @@ def test_GroupedHelpCommand_help_with_group(self):
139112

140113
@click.command(name='scan', cls=GroupedHelpCommand)
141114
@click.option(
142-
'--opt',
143-
is_flag=True,
115+
'--opt',
116+
is_flag=True,
144117
help='Help text for option',
145-
help_group=CORE_GROUP,
118+
help_group=CORE_GROUP,
146119
cls=PluggableCommandLineOption,
147120
)
148121
def scan(opt):

0 commit comments

Comments
 (0)