forked from wolph/python-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar.py
More file actions
461 lines (405 loc) · 17.6 KB
/
bar.py
File metadata and controls
461 lines (405 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
from __future__ import annotations
import abc
import contextlib
import itertools
import logging
import math
import os
import sys
import time
import timeit
import warnings
from copy import deepcopy
from datetime import datetime
from python_utils import converters, types
import progressbar.env
import progressbar.terminal
import progressbar.terminal.stream
from . import base, utils, widgets, widgets as widgets_module
from .terminal import os_specific
logger = logging.getLogger(__name__)
NumberT = float
T = types.TypeVar('T')
class ProgressBarMixinBase(abc.ABC):
_started = False
_finished = False
_last_update_time: types.Optional[float] = None
term_width: int = 80
widgets: types.MutableSequence[widgets_module.WidgetBase | str]
max_error: bool
prefix: types.Optional[str]
suffix: types.Optional[str]
left_justify: bool
widget_kwargs: types.Dict[str, types.Any]
custom_len: types.Callable[[str], int]
initial_start_time: types.Optional[datetime]
poll_interval: types.Optional[float]
min_poll_interval: float
num_intervals: int = 0
next_update: int = 0
value: NumberT
previous_value: types.Optional[NumberT]
min_value: NumberT
max_value: NumberT | types.Type[base.UnknownLength]
end_time: types.Optional[datetime]
start_time: types.Optional[datetime]
seconds_elapsed: float
extra: types.Dict[str, types.Any]
last_update_time = property(get_last_update_time, set_last_update_time)
def __init__(self, **kwargs):
pass
def __del__(self):
if not self._finished and self._started:
try:
self.finish()
except AttributeError:
pass
def __getstate__(self):
return self.__dict__
class ProgressBarBase(types.Iterable, ProgressBarMixinBase):
_index_counter = itertools.count()
index: int = -1
label: str = ''
def __init__(self, **kwargs):
self.index = next(self._index_counter)
super().__init__(**kwargs)
def __repr__(self):
label = f': {self.label}' if self.label else ''
return f'<{self.__class__.__name__}#{self.index}{label}>'
class DefaultFdMixin(ProgressBarMixinBase):
fd: base.TextIO = sys.stderr
is_ansi_terminal: bool | None = False
is_terminal: bool | None
line_breaks: bool | None = True
enable_colors: progressbar.env.ColorSupport = progressbar.env.COLOR_SUPPORT
def __init__(self, fd: base.TextIO=sys.stderr, is_terminal: bool | None=None, line_breaks: bool | None=None, enable_colors: progressbar.env.ColorSupport | None=None, line_offset: int=0, **kwargs):
if fd is sys.stdout:
fd = utils.streams.original_stdout
elif fd is sys.stderr:
fd = utils.streams.original_stderr
fd = self._apply_line_offset(fd, line_offset)
self.fd = fd
self.is_ansi_terminal = progressbar.env.is_ansi_terminal(fd)
self.is_terminal = progressbar.env.is_terminal(fd, is_terminal)
self.line_breaks = self._determine_line_breaks(line_breaks)
self.enable_colors = self._determine_enable_colors(enable_colors)
super().__init__(**kwargs)
def _determine_enable_colors(self, enable_colors: progressbar.env.ColorSupport | None) -> progressbar.env.ColorSupport:
"""
Determines the color support for the progress bar.
This method checks the `enable_colors` parameter and the environment
variables `PROGRESSBAR_ENABLE_COLORS` and `FORCE_COLOR` to determine
the color support.
If `enable_colors` is:
- `None`, it checks the environment variables and the terminal
compatibility to ANSI.
- `True`, it sets the color support to XTERM_256.
- `False`, it sets the color support to NONE.
- For different values that are not instances of
`progressbar.env.ColorSupport`, it raises a ValueError.
Args:
enable_colors (progressbar.env.ColorSupport | None): The color
support setting from the user. It can be None, True, False,
or an instance of `progressbar.env.ColorSupport`.
Returns:
progressbar.env.ColorSupport: The determined color support.
Raises:
ValueError: If `enable_colors` is not None, True, False, or an
instance of `progressbar.env.ColorSupport`.
"""
pass
def _format_line(self):
"""Joins the widgets and justifies the line."""
pass
class ResizableMixin(ProgressBarMixinBase):
def __init__(self, term_width: int | None=None, **kwargs):
ProgressBarMixinBase.__init__(self, **kwargs)
self.signal_set = False
if term_width:
self.term_width = term_width
else:
with contextlib.suppress(Exception):
self._handle_resize()
import signal
self._prev_handle = signal.getsignal(signal.SIGWINCH)
signal.signal(signal.SIGWINCH, self._handle_resize)
self.signal_set = True
def _handle_resize(self, signum=None, frame=None):
"""Tries to catch resize signals sent from the terminal."""
pass
class StdRedirectMixin(DefaultFdMixin):
redirect_stderr: bool = False
redirect_stdout: bool = False
stdout: utils.WrappingIO | base.IO
stderr: utils.WrappingIO | base.IO
_stdout: base.IO
_stderr: base.IO
def __init__(self, redirect_stderr: bool=False, redirect_stdout: bool=False, **kwargs):
DefaultFdMixin.__init__(self, **kwargs)
self.redirect_stderr = redirect_stderr
self.redirect_stdout = redirect_stdout
self._stdout = self.stdout = sys.stdout
self._stderr = self.stderr = sys.stderr
class ProgressBar(StdRedirectMixin, ResizableMixin, ProgressBarBase):
"""The ProgressBar class which updates and prints the bar.
Args:
min_value (int): The minimum/start value for the progress bar
max_value (int): The maximum/end value for the progress bar.
Defaults to `_DEFAULT_MAXVAL`
widgets (list): The widgets to render, defaults to the result of
`default_widget()`
left_justify (bool): Justify to the left if `True` or the right if
`False`
initial_value (int): The value to start with
poll_interval (float): The update interval in seconds.
Note that if your widgets include timers or animations, the actual
interval may be smaller (faster updates). Also note that updates
never happens faster than `min_poll_interval` which can be used for
reduced output in logs
min_poll_interval (float): The minimum update interval in seconds.
The bar will _not_ be updated faster than this, despite changes in
the progress, unless `force=True`. This is limited to be at least
`_MINIMUM_UPDATE_INTERVAL`. If available, it is also bound by the
environment variable PROGRESSBAR_MINIMUM_UPDATE_INTERVAL
widget_kwargs (dict): The default keyword arguments for widgets
custom_len (function): Method to override how the line width is
calculated. When using non-latin characters the width
calculation might be off by default
max_error (bool): When True the progressbar will raise an error if it
goes beyond it's set max_value. Otherwise the max_value is simply
raised when needed
prefix (str): Prefix the progressbar with the given string
suffix (str): Prefix the progressbar with the given string
variables (dict): User-defined variables variables that can be used
from a label using `format='{variables.my_var}'`. These values can
be updated using `bar.update(my_var='newValue')` This can also be
used to set initial values for variables' widgets
line_offset (int): The number of lines to offset the progressbar from
your current line. This is useful if you have other output or
multiple progressbars
A common way of using it is like:
>>> progress = ProgressBar().start()
>>> for i in range(100):
... progress.update(i + 1)
... # do something
...
>>> progress.finish()
You can also use a ProgressBar as an iterator:
>>> progress = ProgressBar()
>>> some_iterable = range(100)
>>> for i in progress(some_iterable):
... # do something
... pass
...
Since the progress bar is incredibly customizable you can specify
different widgets of any type in any order. You can even write your own
widgets! However, since there are already a good number of widgets you
should probably play around with them before moving on to create your own
widgets.
The term_width parameter represents the current terminal width. If the
parameter is set to an integer then the progress bar will use that,
otherwise it will attempt to determine the terminal width falling back to
80 columns if the width cannot be determined.
When implementing a widget's update method you are passed a reference to
the current progress bar. As a result, you have access to the
ProgressBar's methods and attributes. Although there is nothing preventing
you from changing the ProgressBar you should treat it as read only.
"""
_iterable: types.Optional[types.Iterator]
_DEFAULT_MAXVAL: type[base.UnknownLength] = base.UnknownLength
_MINIMUM_UPDATE_INTERVAL: float = 0.05
_last_update_time: types.Optional[float] = None
paused: bool = False
def __init__(self, min_value: NumberT=0, max_value: NumberT | types.Type[base.UnknownLength] | None=None, widgets: types.Optional[types.Sequence[widgets_module.WidgetBase | str]]=None, left_justify: bool=True, initial_value: NumberT=0, poll_interval: types.Optional[float]=None, widget_kwargs: types.Optional[types.Dict[str, types.Any]]=None, custom_len: types.Callable[[str], int]=utils.len_color, max_error=True, prefix=None, suffix=None, variables=None, min_poll_interval=None, **kwargs):
"""Initializes a progress bar with sane defaults."""
StdRedirectMixin.__init__(self, **kwargs)
ResizableMixin.__init__(self, **kwargs)
ProgressBarBase.__init__(self, **kwargs)
if not max_value and kwargs.get('maxval') is not None:
warnings.warn('The usage of `maxval` is deprecated, please use `max_value` instead', DeprecationWarning, stacklevel=1)
max_value = kwargs.get('maxval')
if not poll_interval and kwargs.get('poll'):
warnings.warn('The usage of `poll` is deprecated, please use `poll_interval` instead', DeprecationWarning, stacklevel=1)
poll_interval = kwargs.get('poll')
if max_value and min_value > types.cast(NumberT, max_value):
raise ValueError('Max value needs to be bigger than the min value')
self.min_value = min_value
self.max_value = max_value
self.max_error = max_error
self.widgets = []
for widget in widgets or []:
if getattr(widget, 'copy', True):
widget = deepcopy(widget)
self.widgets.append(widget)
self.prefix = prefix
self.suffix = suffix
self.widget_kwargs = widget_kwargs or {}
self.left_justify = left_justify
self.value = initial_value
self._iterable = None
self.custom_len = custom_len
self.initial_start_time = kwargs.get('start_time')
self.init()
poll_interval = utils.deltas_to_seconds(poll_interval, default=None)
min_poll_interval = utils.deltas_to_seconds(min_poll_interval, default=None)
self._MINIMUM_UPDATE_INTERVAL = utils.deltas_to_seconds(self._MINIMUM_UPDATE_INTERVAL) or self._MINIMUM_UPDATE_INTERVAL
self.poll_interval = poll_interval
self.min_poll_interval = max(min_poll_interval or self._MINIMUM_UPDATE_INTERVAL, self._MINIMUM_UPDATE_INTERVAL, float(os.environ.get('PROGRESSBAR_MINIMUM_UPDATE_INTERVAL', 0)))
self.variables = utils.AttributeDict(variables or {})
for widget in self.widgets:
if isinstance(widget, widgets_module.VariableMixin) and widget.name not in self.variables:
self.variables[widget.name] = None
def init(self):
"""
(re)initialize values to original state so the progressbar can be
used (again).
"""
pass
@property
def percentage(self) -> float | None:
"""Return current percentage, returns None if no max_value is given.
>>> progress = ProgressBar()
>>> progress.max_value = 10
>>> progress.min_value = 0
>>> progress.value = 0
>>> progress.percentage
0.0
>>>
>>> progress.value = 1
>>> progress.percentage
10.0
>>> progress.value = 10
>>> progress.percentage
100.0
>>> progress.min_value = -10
>>> progress.percentage
100.0
>>> progress.value = 0
>>> progress.percentage
50.0
>>> progress.value = 5
>>> progress.percentage
75.0
>>> progress.value = -5
>>> progress.percentage
25.0
>>> progress.max_value = None
>>> progress.percentage
"""
pass
def data(self) -> types.Dict[str, types.Any]:
"""
Returns:
dict:
- `max_value`: The maximum value (can be None with
iterators)
- `start_time`: Start time of the widget
- `last_update_time`: Last update time of the widget
- `end_time`: End time of the widget
- `value`: The current value
- `previous_value`: The previous value
- `updates`: The total update count
- `total_seconds_elapsed`: The seconds since the bar started
- `seconds_elapsed`: The seconds since the bar started modulo
60
- `minutes_elapsed`: The minutes since the bar started modulo
60
- `hours_elapsed`: The hours since the bar started modulo 24
- `days_elapsed`: The hours since the bar started
- `time_elapsed`: The raw elapsed `datetime.timedelta` object
- `percentage`: Percentage as a float or `None` if no max_value
is available
- `dynamic_messages`: Deprecated, use `variables` instead.
- `variables`: Dictionary of user-defined variables for the
:py:class:`~progressbar.widgets.Variable`'s.
"""
pass
def __call__(self, iterable, max_value=None):
"""Use a ProgressBar to iterate through an iterable."""
if max_value is not None:
self.max_value = max_value
elif self.max_value is None:
try:
self.max_value = len(iterable)
except TypeError:
self.max_value = base.UnknownLength
self._iterable = iter(iterable)
return self
def __iter__(self):
return self
def __next__(self):
try:
if self._iterable is None:
value = self.value
else:
value = next(self._iterable)
if self.start_time is None:
self.start()
else:
self.update(self.value + 1)
except StopIteration:
self.finish()
raise
except GeneratorExit:
self.finish(dirty=True)
raise
else:
return value
def __exit__(self, exc_type, exc_value, traceback):
self.finish(dirty=bool(exc_type))
def __enter__(self):
return self
next = __next__
def __iadd__(self, value):
"""Updates the ProgressBar by adding a new value."""
return self.increment(value)
def _needs_update(self):
"""Returns whether the ProgressBar should redraw the line."""
pass
def update(self, value=None, force=False, **kwargs):
"""Updates the ProgressBar to a new value."""
pass
def start(self, max_value=None, init=True, *args, **kwargs):
"""Starts measuring time, and prints the bar at 0%.
It returns self so you can use it like this:
Args:
max_value (int): The maximum value of the progressbar
init (bool): (Re)Initialize the progressbar, this is useful if you
wish to reuse the same progressbar but can be disabled if
data needs to be persisted between runs
>>> pbar = ProgressBar().start()
>>> for i in range(100):
... # do something
... pbar.update(i+1)
...
>>> pbar.finish()
"""
pass
def finish(self, end='\n', dirty=False):
"""
Puts the ProgressBar bar in the finished state.
Also flushes and disables output buffering if this was the last
progressbar running.
Args:
end (str): The string to end the progressbar with, defaults to a
newline
dirty (bool): When True the progressbar kept the current state and
won't be set to 100 percent
"""
pass
@property
def currval(self):
"""
Legacy method to make progressbar-2 compatible with the original
progressbar package.
"""
pass
class DataTransferBar(ProgressBar):
"""A progress bar with sensible defaults for downloads etc.
This assumes that the values its given are numbers of bytes.
"""
class NullBar(ProgressBar):
"""
Progress bar that does absolutely nothing. Useful for single verbosity
flags.
"""