progressbar2 4.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
progressbar/base.py ADDED
@@ -0,0 +1,31 @@
1
+ from python_utils import types
2
+
3
+
4
+ class FalseMeta(type):
5
+ @classmethod
6
+ def __bool__(cls): # pragma: no cover
7
+ return False
8
+
9
+ @classmethod
10
+ def __cmp__(cls, other): # pragma: no cover
11
+ return -1
12
+
13
+ __nonzero__ = __bool__
14
+
15
+
16
+ class UnknownLength(metaclass=FalseMeta):
17
+ pass
18
+
19
+
20
+ class Undefined(metaclass=FalseMeta):
21
+ pass
22
+
23
+
24
+ try: # pragma: no cover
25
+ IO = types.IO # type: ignore
26
+ TextIO = types.TextIO # type: ignore
27
+ except AttributeError: # pragma: no cover
28
+ from typing.io import IO, TextIO # type: ignore
29
+
30
+ assert IO is not None
31
+ assert TextIO is not None
progressbar/env.py ADDED
@@ -0,0 +1,159 @@
1
+ from __future__ import annotations
2
+
3
+ import enum
4
+ import os
5
+ import re
6
+ import typing
7
+
8
+ from . import base
9
+
10
+
11
+ @typing.overload
12
+ def env_flag(name: str, default: bool) -> bool:
13
+ ...
14
+
15
+
16
+ @typing.overload
17
+ def env_flag(name: str, default: bool | None = None) -> bool | None:
18
+ ...
19
+
20
+
21
+ def env_flag(name, default=None):
22
+ '''
23
+ Accepts environt variables formatted as y/n, yes/no, 1/0, true/false,
24
+ on/off, and returns it as a boolean.
25
+
26
+ If the environment variable is not defined, or has an unknown value,
27
+ returns `default`
28
+ '''
29
+ v = os.getenv(name)
30
+ if v and v.lower() in ('y', 'yes', 't', 'true', 'on', '1'):
31
+ return True
32
+ if v and v.lower() in ('n', 'no', 'f', 'false', 'off', '0'):
33
+ return False
34
+ return default
35
+
36
+
37
+ class ColorSupport(enum.IntEnum):
38
+ '''Color support for the terminal.'''
39
+
40
+ NONE = 0
41
+ XTERM = 16
42
+ XTERM_256 = 256
43
+ XTERM_TRUECOLOR = 16777216
44
+
45
+ @classmethod
46
+ def from_env(cls):
47
+ '''Get the color support from the environment.
48
+
49
+ If any of the environment variables contain `24bit` or `truecolor`,
50
+ we will enable true color/24 bit support. If they contain `256`, we
51
+ will enable 256 color/8 bit support. If they contain `xterm`, we will
52
+ enable 16 color support. Otherwise, we will assume no color support.
53
+
54
+ If `JUPYTER_COLUMNS` or `JUPYTER_LINES` is set, we will assume true
55
+ color support.
56
+
57
+ Note that the highest available value will be used! Having
58
+ `COLORTERM=truecolor` will override `TERM=xterm-256color`.
59
+ '''
60
+ variables = (
61
+ 'FORCE_COLOR',
62
+ 'PROGRESSBAR_ENABLE_COLORS',
63
+ 'COLORTERM',
64
+ 'TERM',
65
+ )
66
+
67
+ if os.environ.get('JUPYTER_COLUMNS') or os.environ.get(
68
+ 'JUPYTER_LINES',
69
+ ):
70
+ # Jupyter notebook always supports true color.
71
+ return cls.XTERM_TRUECOLOR
72
+
73
+ support = cls.NONE
74
+ for variable in variables:
75
+ value = os.environ.get(variable)
76
+ if value is None:
77
+ continue
78
+ elif value in {'truecolor', '24bit'}:
79
+ # Truecolor support, we don't need to check anything else.
80
+ support = cls.XTERM_TRUECOLOR
81
+ break
82
+ elif '256' in value:
83
+ support = max(cls.XTERM_256, support)
84
+ elif value == 'xterm':
85
+ support = max(cls.XTERM, support)
86
+
87
+ return support
88
+
89
+
90
+ def is_ansi_terminal(
91
+ fd: base.IO,
92
+ is_terminal: bool | None = None,
93
+ ) -> bool: # pragma: no cover
94
+ if is_terminal is None:
95
+ # Jupyter Notebooks define this variable and support progress bars
96
+ if 'JPY_PARENT_PID' in os.environ:
97
+ is_terminal = True
98
+ # This works for newer versions of pycharm only. With older versions
99
+ # there is no way to check.
100
+ elif os.environ.get('PYCHARM_HOSTED') == '1' and not os.environ.get(
101
+ 'PYTEST_CURRENT_TEST',
102
+ ):
103
+ is_terminal = True
104
+
105
+ if is_terminal is None:
106
+ # check if we are writing to a terminal or not. typically a file object
107
+ # is going to return False if the instance has been overridden and
108
+ # isatty has not been defined we have no way of knowing so we will not
109
+ # use ansi. ansi terminals will typically define one of the 2
110
+ # environment variables.
111
+ try:
112
+ is_tty = fd.isatty()
113
+ # Try and match any of the huge amount of Linux/Unix ANSI consoles
114
+ if is_tty and ANSI_TERM_RE.match(os.environ.get('TERM', '')):
115
+ is_terminal = True
116
+ # ANSICON is a Windows ANSI compatible console
117
+ elif 'ANSICON' in os.environ:
118
+ is_terminal = True
119
+ else:
120
+ is_terminal = None
121
+ except Exception:
122
+ is_terminal = False
123
+
124
+ return bool(is_terminal)
125
+
126
+
127
+ def is_terminal(fd: base.IO, is_terminal: bool | None = None) -> bool:
128
+ if is_terminal is None:
129
+ # Full ansi support encompasses what we expect from a terminal
130
+ is_terminal = is_ansi_terminal(fd) or None
131
+
132
+ if is_terminal is None:
133
+ # Allow a environment variable override
134
+ is_terminal = env_flag('PROGRESSBAR_IS_TERMINAL', None)
135
+
136
+ if is_terminal is None: # pragma: no cover
137
+ # Bare except because a lot can go wrong on different systems. If we do
138
+ # get a TTY we know this is a valid terminal
139
+ try:
140
+ is_terminal = fd.isatty()
141
+ except Exception:
142
+ is_terminal = False
143
+
144
+ return bool(is_terminal)
145
+
146
+
147
+ COLOR_SUPPORT = ColorSupport.from_env()
148
+ ANSI_TERMS = (
149
+ '([xe]|bv)term',
150
+ '(sco)?ansi',
151
+ 'cygwin',
152
+ 'konsole',
153
+ 'linux',
154
+ 'rxvt',
155
+ 'screen',
156
+ 'tmux',
157
+ 'vt(10[02]|220|320)',
158
+ )
159
+ ANSI_TERM_RE = re.compile(f"^({'|'.join(ANSI_TERMS)})", re.IGNORECASE)
progressbar/multi.py ADDED
@@ -0,0 +1,372 @@
1
+ from __future__ import annotations
2
+
3
+ import enum
4
+ import io
5
+ import itertools
6
+ import operator
7
+ import sys
8
+ import threading
9
+ import time
10
+ import timeit
11
+ import typing
12
+ from datetime import timedelta
13
+
14
+ import python_utils
15
+
16
+ from . import bar, terminal
17
+ from .terminal import stream
18
+
19
+ SortKeyFunc = typing.Callable[[bar.ProgressBar], typing.Any]
20
+
21
+
22
+ class SortKey(str, enum.Enum):
23
+ '''
24
+ Sort keys for the MultiBar.
25
+
26
+ This is a string enum, so you can use any
27
+ progressbar attribute or property as a sort key.
28
+
29
+ Note that the multibar defaults to lazily rendering only the changed
30
+ progressbars. This means that sorting by dynamic attributes such as
31
+ `value` might result in more rendering which can have a small performance
32
+ impact.
33
+ '''
34
+
35
+ CREATED = 'index'
36
+ LABEL = 'label'
37
+ VALUE = 'value'
38
+ PERCENTAGE = 'percentage'
39
+
40
+
41
+ class MultiBar(typing.Dict[str, bar.ProgressBar]):
42
+ fd: typing.TextIO
43
+ _buffer: io.StringIO
44
+
45
+ #: The format for the label to append/prepend to the progressbar
46
+ label_format: str
47
+ #: Automatically prepend the label to the progressbars
48
+ prepend_label: bool
49
+ #: Automatically append the label to the progressbars
50
+ append_label: bool
51
+ #: If `initial_format` is `None`, the progressbar rendering is used
52
+ # which will *start* the progressbar. That means the progressbar will
53
+ # have no knowledge of your data and will run as an infinite progressbar.
54
+ initial_format: str | None
55
+ #: If `finished_format` is `None`, the progressbar rendering is used.
56
+ finished_format: str | None
57
+
58
+ #: The multibar updates at a fixed interval regardless of the progressbar
59
+ # updates
60
+ update_interval: float
61
+ remove_finished: float | None
62
+
63
+ #: The kwargs passed to the progressbar constructor
64
+ progressbar_kwargs: dict[str, typing.Any]
65
+
66
+ #: The progressbar sorting key function
67
+ sort_keyfunc: SortKeyFunc
68
+
69
+ _previous_output: list[str]
70
+ _finished_at: dict[bar.ProgressBar, float]
71
+ _labeled: set[bar.ProgressBar]
72
+ _print_lock: threading.RLock = threading.RLock()
73
+ _thread: threading.Thread | None = None
74
+ _thread_finished: threading.Event = threading.Event()
75
+ _thread_closed: threading.Event = threading.Event()
76
+
77
+ def __init__(
78
+ self,
79
+ bars: typing.Iterable[tuple[str, bar.ProgressBar]] | None = None,
80
+ fd: typing.TextIO = sys.stderr,
81
+ prepend_label: bool = True,
82
+ append_label: bool = False,
83
+ label_format='{label:20.20} ',
84
+ initial_format: str | None = '{label:20.20} Not yet started',
85
+ finished_format: str | None = None,
86
+ update_interval: float = 1 / 60.0, # 60fps
87
+ show_initial: bool = True,
88
+ show_finished: bool = True,
89
+ remove_finished: timedelta | float = timedelta(seconds=3600),
90
+ sort_key: str | SortKey = SortKey.CREATED,
91
+ sort_reverse: bool = True,
92
+ sort_keyfunc: SortKeyFunc | None = None,
93
+ **progressbar_kwargs,
94
+ ):
95
+ self.fd = fd
96
+
97
+ self.prepend_label = prepend_label
98
+ self.append_label = append_label
99
+ self.label_format = label_format
100
+ self.initial_format = initial_format
101
+ self.finished_format = finished_format
102
+
103
+ self.update_interval = update_interval
104
+
105
+ self.show_initial = show_initial
106
+ self.show_finished = show_finished
107
+ self.remove_finished = python_utils.delta_to_seconds_or_none(
108
+ remove_finished,
109
+ )
110
+
111
+ self.progressbar_kwargs = progressbar_kwargs
112
+
113
+ if sort_keyfunc is None:
114
+ sort_keyfunc = operator.attrgetter(sort_key)
115
+
116
+ self.sort_keyfunc = sort_keyfunc
117
+ self.sort_reverse = sort_reverse
118
+
119
+ self._labeled = set()
120
+ self._finished_at = {}
121
+ self._previous_output = []
122
+ self._buffer = io.StringIO()
123
+
124
+ super().__init__(bars or {})
125
+
126
+ def __setitem__(self, key: str, bar: bar.ProgressBar):
127
+ '''Add a progressbar to the multibar.'''
128
+ if bar.label != key or not key: # pragma: no branch
129
+ bar.label = key
130
+ bar.fd = stream.LastLineStream(self.fd)
131
+ bar.paused = True
132
+ # Essentially `bar.print = self.print`, but `mypy` doesn't like that
133
+ bar.print = self.print # type: ignore
134
+
135
+ # Just in case someone is using a progressbar with a custom
136
+ # constructor and forgot to call the super constructor
137
+ if bar.index == -1:
138
+ bar.index = next(bar._index_counter)
139
+
140
+ super().__setitem__(key, bar)
141
+
142
+ def __delitem__(self, key):
143
+ '''Remove a progressbar from the multibar.'''
144
+ super().__delitem__(key)
145
+ self._finished_at.pop(key, None)
146
+ self._labeled.discard(key)
147
+
148
+ def __getitem__(self, key):
149
+ '''Get (and create if needed) a progressbar from the multibar.'''
150
+ try:
151
+ return super().__getitem__(key)
152
+ except KeyError:
153
+ progress = bar.ProgressBar(**self.progressbar_kwargs)
154
+ self[key] = progress
155
+ return progress
156
+
157
+ def _label_bar(self, bar: bar.ProgressBar):
158
+ if bar in self._labeled: # pragma: no branch
159
+ return
160
+
161
+ assert bar.widgets, 'Cannot prepend label to empty progressbar'
162
+
163
+ if self.prepend_label: # pragma: no branch
164
+ self._labeled.add(bar)
165
+ bar.widgets.insert(0, self.label_format.format(label=bar.label))
166
+
167
+ if self.append_label and bar not in self._labeled: # pragma: no branch
168
+ self._labeled.add(bar)
169
+ bar.widgets.append(self.label_format.format(label=bar.label))
170
+
171
+ def render(self, flush: bool = True, force: bool = False):
172
+ '''Render the multibar to the given stream.'''
173
+ now = timeit.default_timer()
174
+ expired = now - self.remove_finished if self.remove_finished else None
175
+
176
+ # sourcery skip: list-comprehension
177
+ output: list[str] = []
178
+ for bar_ in self.get_sorted_bars():
179
+ if not bar_.started() and not self.show_initial:
180
+ continue
181
+
182
+ output.extend(
183
+ iter(self._render_bar(bar_, expired=expired, now=now)),
184
+ )
185
+
186
+ with self._print_lock:
187
+ # Clear the previous output if progressbars have been removed
188
+ for i in range(len(output), len(self._previous_output)):
189
+ self._buffer.write(
190
+ terminal.clear_line(i + 1),
191
+ ) # pragma: no cover
192
+
193
+ # Add empty lines to the end of the output if progressbars have
194
+ # been added
195
+ for _ in range(len(self._previous_output), len(output)):
196
+ # Adding a new line so we don't overwrite previous output
197
+ self._buffer.write('\n')
198
+
199
+ for i, (previous, current) in enumerate(
200
+ itertools.zip_longest(
201
+ self._previous_output,
202
+ output,
203
+ fillvalue='',
204
+ ),
205
+ ):
206
+ if previous != current or force: # pragma: no branch
207
+ self.print(
208
+ '\r' + current.strip(),
209
+ offset=i + 1,
210
+ end='',
211
+ clear=False,
212
+ flush=False,
213
+ )
214
+
215
+ self._previous_output = output
216
+
217
+ if flush: # pragma: no branch
218
+ self.flush()
219
+
220
+ def _render_bar(
221
+ self,
222
+ bar_: bar.ProgressBar,
223
+ now,
224
+ expired,
225
+ ) -> typing.Iterable[str]:
226
+ def update(force=True, write=True): # pragma: no cover
227
+ self._label_bar(bar_)
228
+ bar_.update(force=force)
229
+ if write:
230
+ yield typing.cast(stream.LastLineStream, bar_.fd).line
231
+
232
+ if bar_.finished():
233
+ yield from self._render_finished_bar(bar_, now, expired, update)
234
+
235
+ elif bar_.started():
236
+ update()
237
+ else:
238
+ if self.initial_format is None:
239
+ bar_.start()
240
+ update()
241
+ else:
242
+ yield self.initial_format.format(label=bar_.label)
243
+
244
+ def _render_finished_bar(
245
+ self,
246
+ bar_: bar.ProgressBar,
247
+ now,
248
+ expired,
249
+ update,
250
+ ) -> typing.Iterable[str]:
251
+ if bar_ not in self._finished_at:
252
+ self._finished_at[bar_] = now
253
+ # Force update to get the finished format
254
+ update(write=False)
255
+
256
+ if (
257
+ self.remove_finished
258
+ and expired is not None
259
+ and expired >= self._finished_at[bar_]
260
+ ):
261
+ del self[bar_.label]
262
+ return
263
+
264
+ if not self.show_finished:
265
+ return
266
+
267
+ if bar_.finished(): # pragma: no branch
268
+ if self.finished_format is None:
269
+ update(force=False)
270
+ else: # pragma: no cover
271
+ yield self.finished_format.format(label=bar_.label)
272
+
273
+ def print(
274
+ self,
275
+ *args,
276
+ end='\n',
277
+ offset=None,
278
+ flush=True,
279
+ clear=True,
280
+ **kwargs,
281
+ ):
282
+ '''
283
+ Print to the progressbar stream without overwriting the progressbars.
284
+
285
+ Args:
286
+ end: The string to append to the end of the output
287
+ offset: The number of lines to offset the output by. If None, the
288
+ output will be printed above the progressbars
289
+ flush: Whether to flush the output to the stream
290
+ clear: If True, the line will be cleared before printing.
291
+ **kwargs: Additional keyword arguments to pass to print
292
+ '''
293
+ with self._print_lock:
294
+ if offset is None:
295
+ offset = len(self._previous_output)
296
+
297
+ if not clear:
298
+ self._buffer.write(terminal.PREVIOUS_LINE(offset))
299
+
300
+ if clear:
301
+ self._buffer.write(terminal.PREVIOUS_LINE(offset))
302
+ self._buffer.write(terminal.CLEAR_LINE_ALL())
303
+
304
+ print(*args, **kwargs, file=self._buffer, end=end)
305
+
306
+ if clear:
307
+ self._buffer.write(terminal.CLEAR_SCREEN_TILL_END())
308
+ for line in self._previous_output:
309
+ self._buffer.write(line.strip())
310
+ self._buffer.write('\n')
311
+
312
+ else:
313
+ self._buffer.write(terminal.NEXT_LINE(offset))
314
+
315
+ if flush:
316
+ self.flush()
317
+
318
+ def flush(self):
319
+ self.fd.write(self._buffer.getvalue())
320
+ self._buffer.truncate(0)
321
+ self.fd.flush()
322
+
323
+ def run(self, join=True):
324
+ '''
325
+ Start the multibar render loop and run the progressbars until they
326
+ have force _thread_finished.
327
+ '''
328
+ while not self._thread_finished.is_set(): # pragma: no branch
329
+ self.render()
330
+ time.sleep(self.update_interval)
331
+
332
+ if join or self._thread_closed.is_set():
333
+ # If the thread is closed, we need to check if the progressbars
334
+ # have finished. If they have, we can exit the loop
335
+ for bar_ in self.values(): # pragma: no cover
336
+ if not bar_.finished():
337
+ break
338
+ else:
339
+ # Render one last time to make sure the progressbars are
340
+ # correctly finished
341
+ self.render(force=True)
342
+ return
343
+
344
+ def start(self):
345
+ assert not self._thread, 'Multibar already started'
346
+ self._thread_closed.set()
347
+ self._thread = threading.Thread(target=self.run, args=(False,))
348
+ self._thread.start()
349
+
350
+ def join(self, timeout=None):
351
+ if self._thread is not None:
352
+ self._thread_closed.set()
353
+ self._thread.join(timeout=timeout)
354
+ self._thread = None
355
+
356
+ def stop(self, timeout: float | None = None):
357
+ self._thread_finished.set()
358
+ self.join(timeout=timeout)
359
+
360
+ def get_sorted_bars(self):
361
+ return sorted(
362
+ self.values(),
363
+ key=self.sort_keyfunc,
364
+ reverse=self.sort_reverse,
365
+ )
366
+
367
+ def __enter__(self):
368
+ self.start()
369
+ return self
370
+
371
+ def __exit__(self, exc_type, exc_val, exc_tb):
372
+ self.join()
progressbar/py.typed ADDED
File without changes
@@ -0,0 +1,22 @@
1
+ from . import bar
2
+
3
+
4
+ def progressbar(
5
+ iterator,
6
+ min_value=0,
7
+ max_value=None,
8
+ widgets=None,
9
+ prefix=None,
10
+ suffix=None,
11
+ **kwargs,
12
+ ):
13
+ progressbar = bar.ProgressBar(
14
+ min_value=min_value,
15
+ max_value=max_value,
16
+ widgets=widgets,
17
+ prefix=prefix,
18
+ suffix=suffix,
19
+ **kwargs,
20
+ )
21
+
22
+ yield from progressbar(iterator)
@@ -0,0 +1,4 @@
1
+ from __future__ import annotations
2
+
3
+ from .base import * # noqa F403
4
+ from .stream import * # noqa F403