@@ -79,6 +79,7 @@ def _rich_progress_bar(
79
79
* ,
80
80
bar_type : str ,
81
81
size : int ,
82
+ quiet : bool ,
82
83
) -> Generator [bytes , None , None ]:
83
84
assert bar_type == "on" , "This should only be used in the default mode."
84
85
@@ -89,7 +90,9 @@ def _rich_progress_bar(
89
90
total = size
90
91
columns = _known_size_columns ()
91
92
92
- progress = Progress (* columns , console = Console (stderr = True ), refresh_per_second = 5 )
93
+ progress = Progress (
94
+ * columns , console = Console (stderr = True , quiet = quiet ), refresh_per_second = 5
95
+ )
93
96
task_id = progress .add_task (_progress_task_prefix (), total = total )
94
97
with progress :
95
98
for chunk in iterable :
@@ -101,12 +104,15 @@ def _raw_progress_bar(
101
104
iterable : Iterable [bytes ],
102
105
* ,
103
106
size : Optional [int ],
107
+ quiet : bool ,
104
108
) -> Generator [bytes , None , None ]:
105
109
prefix = _progress_task_prefix ()
106
110
107
111
def write_progress (current : int , total : int ) -> None :
108
- sys .stdout .write (f"{ prefix } Progress { current } of { total } \n " )
109
- sys .stdout .flush ()
112
+ if quiet :
113
+ return
114
+ sys .stderr .write (f"{ prefix } Progress { current } of { total } \n " )
115
+ sys .stderr .flush ()
110
116
111
117
current = 0
112
118
total = size or 0
@@ -122,16 +128,21 @@ def write_progress(current: int, total: int) -> None:
122
128
123
129
124
130
def get_download_progress_renderer (
125
- * , bar_type : str , size : Optional [int ] = None
131
+ * ,
132
+ bar_type : str ,
133
+ size : Optional [int ] = None ,
134
+ quiet : bool = False ,
126
135
) -> DownloadProgressRenderer :
127
136
"""Get an object that can be used to render the download progress.
128
137
129
138
Returns a callable, that takes an iterable to "wrap".
130
139
"""
131
140
if bar_type == "on" :
132
- return functools .partial (_rich_progress_bar , bar_type = bar_type , size = size )
141
+ return functools .partial (
142
+ _rich_progress_bar , bar_type = bar_type , size = size , quiet = quiet
143
+ )
133
144
elif bar_type == "raw" :
134
- return functools .partial (_raw_progress_bar , size = size )
145
+ return functools .partial (_raw_progress_bar , size = size , quiet = quiet )
135
146
else :
136
147
return iter # no-op, when passed an iterator
137
148
@@ -158,7 +169,10 @@ def __exit__(self, ty: Any, val: Any, tb: Any) -> None: ...
158
169
@classmethod
159
170
@abc .abstractmethod
160
171
def create (
161
- cls , num_tasks : int , known_total_length : Optional [int ]
172
+ cls ,
173
+ num_tasks : int ,
174
+ known_total_length : Optional [int ],
175
+ quiet : bool ,
162
176
) -> "BatchedProgress" : ...
163
177
164
178
@classmethod
@@ -191,7 +205,10 @@ def __exit__(self, ty: Any, val: Any, tb: Any) -> None:
191
205
192
206
@classmethod
193
207
def create (
194
- cls , num_tasks : int , known_total_length : Optional [int ]
208
+ cls ,
209
+ num_tasks : int ,
210
+ known_total_length : Optional [int ],
211
+ quiet : bool ,
195
212
) -> "BatchedNoOpProgressBar" :
196
213
return cls ()
197
214
@@ -201,20 +218,24 @@ def __init__(
201
218
self ,
202
219
total_bytes : Optional [int ],
203
220
prefix : str ,
221
+ quiet : bool ,
204
222
) -> None :
205
223
self ._total_bytes = total_bytes
206
224
self ._prefix = prefix
207
225
self ._total_progress = 0
208
226
self ._subtasks : List [Tuple [str , Optional [int ]]] = []
209
227
self ._rate_limiter = RateLimiter (0.25 )
210
- self ._stream = sys .stdout
228
+ self ._stream = sys .stderr
229
+ self ._quiet = quiet
211
230
212
231
def add_subtask (self , description : str , total : Optional [int ]) -> TaskID :
213
232
task_id = len (self ._subtasks )
214
233
self ._subtasks .append ((description , total ))
215
234
return TaskID (task_id )
216
235
217
236
def _write_immediate (self , line : str ) -> None :
237
+ if self ._quiet :
238
+ return
218
239
self ._stream .write (f"{ self ._prefix } { line } \n " )
219
240
self ._stream .flush ()
220
241
@@ -266,10 +287,10 @@ def __exit__(self, ty: Any, val: Any, tb: Any) -> None:
266
287
267
288
@classmethod
268
289
def create (
269
- cls , num_tasks : int , known_total_length : Optional [int ]
290
+ cls , num_tasks : int , known_total_length : Optional [int ], quiet : bool
270
291
) -> "BatchedRawProgressBar" :
271
292
prefix = _progress_task_prefix ()
272
- return cls (known_total_length , prefix )
293
+ return cls (known_total_length , prefix , quiet = quiet )
273
294
274
295
275
296
class BatchedRichProgressBar (BatchedProgress ):
@@ -279,11 +300,13 @@ def __init__(
279
300
total_task_id : TaskID ,
280
301
progress : Progress ,
281
302
total_bytes_task_id : TaskID ,
303
+ quiet : bool ,
282
304
) -> None :
283
305
self ._task_progress = task_progress
284
306
self ._total_task_id = total_task_id
285
307
self ._progress = progress
286
308
self ._total_bytes_task_id = total_bytes_task_id
309
+ self ._quiet = quiet
287
310
self ._live : Optional [Live ] = None
288
311
289
312
_TRIM_LEN = 20
@@ -328,7 +351,9 @@ def __enter__(self) -> "BatchedRichProgressBar":
328
351
padding = (0 , 0 ),
329
352
)
330
353
)
331
- self ._live = Live (table , console = Console (stderr = True ), refresh_per_second = 5 )
354
+ self ._live = Live (
355
+ table , console = Console (stderr = True , quiet = self ._quiet ), refresh_per_second = 5
356
+ )
332
357
self ._task_progress .start_task (self ._total_task_id )
333
358
self ._progress .start_task (self ._total_bytes_task_id )
334
359
self ._live .__enter__ ()
@@ -340,7 +365,7 @@ def __exit__(self, ty: Any, val: Any, tb: Any) -> None:
340
365
341
366
@classmethod
342
367
def create (
343
- cls , num_tasks : int , known_total_length : Optional [int ]
368
+ cls , num_tasks : int , known_total_length : Optional [int ], quiet : bool
344
369
) -> "BatchedRichProgressBar" :
345
370
task_columns = _task_columns ()
346
371
task_progress = Progress (* task_columns )
@@ -365,4 +390,6 @@ def create(
365
390
total = total ,
366
391
)
367
392
368
- return cls (task_progress , total_task_id , progress , total_bytes_task_id )
393
+ return cls (
394
+ task_progress , total_task_id , progress , total_bytes_task_id , quiet = quiet
395
+ )
0 commit comments