Skip to content

Commit a86fcca

Browse files
Copilotalexarje
andauthored
Add show_progress() to globally suppress MGT progress bars
Agent-Logs-Url: https://github.com/fourMs/MGT-python/sessions/a3802311-4b27-4543-accc-47d81cf01b43 Co-authored-by: alexarje <114316+alexarje@users.noreply.github.com>
1 parent ff27542 commit a86fcca

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

musicalgestures/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ffmpeg_cmd,
1515
get_length,
1616
generate_outfilename,
17+
show_progress,
1718
)
1819
from musicalgestures._mglist import MgList
1920

musicalgestures/_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
from typing import Union, Tuple
22

3+
#: Module-level flag controlling whether progress bars are shown.
4+
#: Use :func:`show_progress` to change this setting.
5+
_SHOW_PROGRESS: bool = True
6+
7+
8+
def show_progress(enabled: bool) -> None:
9+
"""Enable or disable the MGT progress bars globally.
10+
11+
Disabling the progress bars is useful when running batch processing jobs or
12+
when the output is captured by a logging framework where the repeated
13+
``\\r`` updates would clutter the log.
14+
15+
Args:
16+
enabled (bool): Pass ``True`` to show progress bars (default behaviour)
17+
or ``False`` to suppress them.
18+
19+
Examples:
20+
>>> import musicalgestures as mg
21+
>>> mg.show_progress(False) # suppress all progress bars
22+
>>> # … batch processing …
23+
>>> mg.show_progress(True) # re-enable for interactive use
24+
"""
25+
global _SHOW_PROGRESS
26+
_SHOW_PROGRESS = bool(enabled)
27+
28+
329
class MgProgressbar():
430
"""
531
Calls in a loop to create terminal progress bar.
@@ -132,6 +158,10 @@ def progress(self, iteration):
132158
"""
133159
if self.finished:
134160
return
161+
if not _SHOW_PROGRESS:
162+
if iteration >= self.total:
163+
self.finished = True
164+
return
135165
import sys
136166
import shutil
137167

tests/test_utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import musicalgestures
22
from musicalgestures._utils import *
3+
import musicalgestures._utils as _utils_module
34
import numpy as np
45
import os
56
import itertools
@@ -169,6 +170,71 @@ def test_repr(self):
169170
assert print(pb) == None
170171

171172

173+
class Test_show_progress:
174+
def setup_method(self):
175+
# Always restore the default state after each test
176+
_utils_module._SHOW_PROGRESS = True
177+
178+
def teardown_method(self):
179+
_utils_module._SHOW_PROGRESS = True
180+
181+
def test_show_progress_default_is_true(self):
182+
assert _utils_module._SHOW_PROGRESS is True
183+
184+
def test_show_progress_disable(self):
185+
show_progress(False)
186+
assert _utils_module._SHOW_PROGRESS is False
187+
188+
def test_show_progress_enable(self):
189+
_utils_module._SHOW_PROGRESS = False
190+
show_progress(True)
191+
assert _utils_module._SHOW_PROGRESS is True
192+
193+
def test_show_progress_exposed_on_module(self):
194+
# Ensure show_progress is accessible via the top-level package
195+
assert hasattr(musicalgestures, 'show_progress')
196+
assert callable(musicalgestures.show_progress)
197+
198+
def test_progress_bar_suppressed_does_not_print(self, capsys):
199+
show_progress(False)
200+
pb = MgProgressbar(total=100)
201+
for step in range(103):
202+
pb.progress(step)
203+
captured = capsys.readouterr()
204+
assert captured.out == ""
205+
206+
def test_progress_bar_suppressed_marks_finished(self):
207+
show_progress(False)
208+
pb = MgProgressbar(total=100)
209+
assert not pb.finished
210+
pb.progress(200)
211+
assert pb.finished
212+
213+
def test_progress_bar_suppressed_finished_returns_early(self):
214+
show_progress(False)
215+
pb = MgProgressbar(total=100)
216+
pb.finished = True
217+
# Should return immediately without touching _SHOW_PROGRESS check
218+
assert pb.progress(50) == None
219+
220+
def test_progress_bar_enabled_after_disable(self, capsys):
221+
show_progress(False)
222+
pb = MgProgressbar(total=100)
223+
pb.could_not_get_terminal_window = True
224+
for step in range(50):
225+
pb.progress(step)
226+
show_progress(True)
227+
pb2 = MgProgressbar(total=100)
228+
pb2.could_not_get_terminal_window = True
229+
import time
230+
for step in range(103):
231+
time.sleep(0.01)
232+
pb2.progress(step)
233+
captured = capsys.readouterr()
234+
# pb2 ran with progress bars enabled so output should be non-empty
235+
assert len(captured.out) > 0
236+
237+
172238
class Test_roundup:
173239
def test_positive(self):
174240
assert roundup(10, 4) == 12

0 commit comments

Comments
 (0)