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/utils.py ADDED
@@ -0,0 +1,445 @@
1
+ from __future__ import annotations
2
+
3
+ import atexit
4
+ import contextlib
5
+ import datetime
6
+ import io
7
+ import logging
8
+ import os
9
+ import re
10
+ import sys
11
+ from types import TracebackType
12
+ from typing import Iterable, Iterator
13
+
14
+ from python_utils import types
15
+ from python_utils.converters import scale_1024
16
+ from python_utils.terminal import get_terminal_size
17
+ from python_utils.time import epoch, format_time, timedelta_to_seconds
18
+
19
+ from progressbar import base, env, terminal
20
+
21
+ if types.TYPE_CHECKING:
22
+ from .bar import ProgressBar, ProgressBarMixinBase
23
+
24
+ # Make sure these are available for import
25
+ assert timedelta_to_seconds is not None
26
+ assert get_terminal_size is not None
27
+ assert format_time is not None
28
+ assert scale_1024 is not None
29
+ assert epoch is not None
30
+
31
+ StringT = types.TypeVar('StringT', bound=types.StringTypes)
32
+
33
+
34
+ def deltas_to_seconds(
35
+ *deltas,
36
+ default: types.Optional[types.Type[ValueError]] = ValueError,
37
+ ) -> int | float | None:
38
+ '''
39
+ Convert timedeltas and seconds as int to seconds as float while coalescing.
40
+
41
+ >>> deltas_to_seconds(datetime.timedelta(seconds=1, milliseconds=234))
42
+ 1.234
43
+ >>> deltas_to_seconds(123)
44
+ 123.0
45
+ >>> deltas_to_seconds(1.234)
46
+ 1.234
47
+ >>> deltas_to_seconds(None, 1.234)
48
+ 1.234
49
+ >>> deltas_to_seconds(0, 1.234)
50
+ 0.0
51
+ >>> deltas_to_seconds()
52
+ Traceback (most recent call last):
53
+ ...
54
+ ValueError: No valid deltas passed to `deltas_to_seconds`
55
+ >>> deltas_to_seconds(None)
56
+ Traceback (most recent call last):
57
+ ...
58
+ ValueError: No valid deltas passed to `deltas_to_seconds`
59
+ >>> deltas_to_seconds(default=0.0)
60
+ 0.0
61
+ '''
62
+ for delta in deltas:
63
+ if delta is None:
64
+ continue
65
+ if isinstance(delta, datetime.timedelta):
66
+ return timedelta_to_seconds(delta)
67
+ elif not isinstance(delta, float):
68
+ return float(delta)
69
+ else:
70
+ return delta
71
+
72
+ if default is ValueError:
73
+ raise ValueError('No valid deltas passed to `deltas_to_seconds`')
74
+ else:
75
+ # mypy doesn't understand the `default is ValueError` check
76
+ return default # type: ignore
77
+
78
+
79
+ def no_color(value: StringT) -> StringT:
80
+ '''
81
+ Return the `value` without ANSI escape codes.
82
+
83
+ >>> no_color(b'\u001b[1234]abc')
84
+ b'abc'
85
+ >>> str(no_color(u'\u001b[1234]abc'))
86
+ 'abc'
87
+ >>> str(no_color('\u001b[1234]abc'))
88
+ 'abc'
89
+ >>> no_color(123)
90
+ Traceback (most recent call last):
91
+ ...
92
+ TypeError: `value` must be a string or bytes, got 123
93
+ '''
94
+ if isinstance(value, bytes):
95
+ pattern: bytes = bytes(terminal.ESC, 'ascii') + b'\\[.*?[@-~]'
96
+ return re.sub(pattern, b'', value) # type: ignore
97
+ elif isinstance(value, str):
98
+ return re.sub('\x1b\\[.*?[@-~]', '', value) # type: ignore
99
+ else:
100
+ raise TypeError('`value` must be a string or bytes, got %r' % value)
101
+
102
+
103
+ def len_color(value: types.StringTypes) -> int:
104
+ '''
105
+ Return the length of `value` without ANSI escape codes.
106
+
107
+ >>> len_color(b'\u001b[1234]abc')
108
+ 3
109
+ >>> len_color(u'\u001b[1234]abc')
110
+ 3
111
+ >>> len_color('\u001b[1234]abc')
112
+ 3
113
+ '''
114
+ return len(no_color(value))
115
+
116
+
117
+ class WrappingIO:
118
+ buffer: io.StringIO
119
+ target: base.IO
120
+ capturing: bool
121
+ listeners: set
122
+ needs_clear: bool = False
123
+
124
+ def __init__(
125
+ self,
126
+ target: base.IO,
127
+ capturing: bool = False,
128
+ listeners: types.Optional[types.Set[ProgressBar]] = None,
129
+ ) -> None:
130
+ self.buffer = io.StringIO()
131
+ self.target = target
132
+ self.capturing = capturing
133
+ self.listeners = listeners or set()
134
+ self.needs_clear = False
135
+
136
+ def write(self, value: str) -> int:
137
+ ret = 0
138
+ if self.capturing:
139
+ ret += self.buffer.write(value)
140
+ if '\n' in value: # pragma: no branch
141
+ self.needs_clear = True
142
+ for listener in self.listeners: # pragma: no branch
143
+ listener.update()
144
+ else:
145
+ ret += self.target.write(value)
146
+ if '\n' in value: # pragma: no branch
147
+ self.flush_target()
148
+
149
+ return ret
150
+
151
+ def flush(self) -> None:
152
+ self.buffer.flush()
153
+
154
+ def _flush(self) -> None:
155
+ if value := self.buffer.getvalue():
156
+ self.flush()
157
+ self.target.write(value)
158
+ self.buffer.seek(0)
159
+ self.buffer.truncate(0)
160
+ self.needs_clear = False
161
+
162
+ # when explicitly flushing, always flush the target as well
163
+ self.flush_target()
164
+
165
+ def flush_target(self) -> None: # pragma: no cover
166
+ if not self.target.closed and getattr(self.target, 'flush', None):
167
+ self.target.flush()
168
+
169
+ def __enter__(self) -> WrappingIO:
170
+ return self
171
+
172
+ def fileno(self) -> int:
173
+ return self.target.fileno()
174
+
175
+ def isatty(self) -> bool:
176
+ return self.target.isatty()
177
+
178
+ def read(self, n: int = -1) -> str:
179
+ return self.target.read(n)
180
+
181
+ def readable(self) -> bool:
182
+ return self.target.readable()
183
+
184
+ def readline(self, limit: int = -1) -> str:
185
+ return self.target.readline(limit)
186
+
187
+ def readlines(self, hint: int = -1) -> list[str]:
188
+ return self.target.readlines(hint)
189
+
190
+ def seek(self, offset: int, whence: int = os.SEEK_SET) -> int:
191
+ return self.target.seek(offset, whence)
192
+
193
+ def seekable(self) -> bool:
194
+ return self.target.seekable()
195
+
196
+ def tell(self) -> int:
197
+ return self.target.tell()
198
+
199
+ def truncate(self, size: types.Optional[int] = None) -> int:
200
+ return self.target.truncate(size)
201
+
202
+ def writable(self) -> bool:
203
+ return self.target.writable()
204
+
205
+ def writelines(self, lines: Iterable[str]) -> None:
206
+ return self.target.writelines(lines)
207
+
208
+ def close(self) -> None:
209
+ self.flush()
210
+ self.target.close()
211
+
212
+ def __next__(self) -> str:
213
+ return self.target.__next__()
214
+
215
+ def __iter__(self) -> Iterator[str]:
216
+ return self.target.__iter__()
217
+
218
+ def __exit__(
219
+ self,
220
+ __t: type[BaseException] | None,
221
+ __value: BaseException | None,
222
+ __traceback: TracebackType | None,
223
+ ) -> None:
224
+ self.close()
225
+
226
+
227
+ class StreamWrapper:
228
+ '''Wrap stdout and stderr globally.'''
229
+
230
+ stdout: base.TextIO | WrappingIO
231
+ stderr: base.TextIO | WrappingIO
232
+ original_excepthook: types.Callable[
233
+ [
234
+ types.Type[BaseException],
235
+ BaseException,
236
+ TracebackType | None,
237
+ ],
238
+ None,
239
+ ]
240
+ wrapped_stdout: int = 0
241
+ wrapped_stderr: int = 0
242
+ wrapped_excepthook: int = 0
243
+ capturing: int = 0
244
+ listeners: set
245
+
246
+ def __init__(self):
247
+ self.stdout = self.original_stdout = sys.stdout
248
+ self.stderr = self.original_stderr = sys.stderr
249
+ self.original_excepthook = sys.excepthook
250
+ self.wrapped_stdout = 0
251
+ self.wrapped_stderr = 0
252
+ self.wrapped_excepthook = 0
253
+ self.capturing = 0
254
+ self.listeners = set()
255
+
256
+ if env.env_flag('WRAP_STDOUT', default=False): # pragma: no cover
257
+ self.wrap_stdout()
258
+
259
+ if env.env_flag('WRAP_STDERR', default=False): # pragma: no cover
260
+ self.wrap_stderr()
261
+
262
+ def start_capturing(self, bar: ProgressBarMixinBase | None = None) -> None:
263
+ if bar: # pragma: no branch
264
+ self.listeners.add(bar)
265
+
266
+ self.capturing += 1
267
+ self.update_capturing()
268
+
269
+ def stop_capturing(self, bar: ProgressBarMixinBase | None = None) -> None:
270
+ if bar: # pragma: no branch
271
+ with contextlib.suppress(KeyError):
272
+ self.listeners.remove(bar)
273
+
274
+ self.capturing -= 1
275
+ self.update_capturing()
276
+
277
+ def update_capturing(self) -> None: # pragma: no cover
278
+ if isinstance(self.stdout, WrappingIO):
279
+ self.stdout.capturing = self.capturing > 0
280
+
281
+ if isinstance(self.stderr, WrappingIO):
282
+ self.stderr.capturing = self.capturing > 0
283
+
284
+ if self.capturing <= 0:
285
+ self.flush()
286
+
287
+ def wrap(self, stdout: bool = False, stderr: bool = False) -> None:
288
+ if stdout:
289
+ self.wrap_stdout()
290
+
291
+ if stderr:
292
+ self.wrap_stderr()
293
+
294
+ def wrap_stdout(self) -> WrappingIO:
295
+ self.wrap_excepthook()
296
+
297
+ if not self.wrapped_stdout:
298
+ self.stdout = sys.stdout = WrappingIO( # type: ignore
299
+ self.original_stdout,
300
+ listeners=self.listeners,
301
+ )
302
+ self.wrapped_stdout += 1
303
+
304
+ return sys.stdout # type: ignore
305
+
306
+ def wrap_stderr(self) -> WrappingIO:
307
+ self.wrap_excepthook()
308
+
309
+ if not self.wrapped_stderr:
310
+ self.stderr = sys.stderr = WrappingIO( # type: ignore
311
+ self.original_stderr,
312
+ listeners=self.listeners,
313
+ )
314
+ self.wrapped_stderr += 1
315
+
316
+ return sys.stderr # type: ignore
317
+
318
+ def unwrap_excepthook(self) -> None:
319
+ if self.wrapped_excepthook:
320
+ self.wrapped_excepthook -= 1
321
+ sys.excepthook = self.original_excepthook
322
+
323
+ def wrap_excepthook(self) -> None:
324
+ if not self.wrapped_excepthook:
325
+ logger.debug('wrapping excepthook')
326
+ self.wrapped_excepthook += 1
327
+ sys.excepthook = self.excepthook
328
+
329
+ def unwrap(self, stdout: bool = False, stderr: bool = False) -> None:
330
+ if stdout:
331
+ self.unwrap_stdout()
332
+
333
+ if stderr:
334
+ self.unwrap_stderr()
335
+
336
+ def unwrap_stdout(self) -> None:
337
+ if self.wrapped_stdout > 1:
338
+ self.wrapped_stdout -= 1
339
+ else:
340
+ sys.stdout = self.original_stdout
341
+ self.wrapped_stdout = 0
342
+
343
+ def unwrap_stderr(self) -> None:
344
+ if self.wrapped_stderr > 1:
345
+ self.wrapped_stderr -= 1
346
+ else:
347
+ sys.stderr = self.original_stderr
348
+ self.wrapped_stderr = 0
349
+
350
+ def needs_clear(self) -> bool: # pragma: no cover
351
+ stdout_needs_clear = getattr(self.stdout, 'needs_clear', False)
352
+ stderr_needs_clear = getattr(self.stderr, 'needs_clear', False)
353
+ return stderr_needs_clear or stdout_needs_clear
354
+
355
+ def flush(self) -> None:
356
+ if self.wrapped_stdout and isinstance(self.stdout, WrappingIO):
357
+ try:
358
+ self.stdout._flush()
359
+ except io.UnsupportedOperation: # pragma: no cover
360
+ self.wrapped_stdout = False
361
+ logger.warning(
362
+ 'Disabling stdout redirection, %r is not seekable',
363
+ sys.stdout,
364
+ )
365
+
366
+ if self.wrapped_stderr and isinstance(self.stderr, WrappingIO):
367
+ try:
368
+ self.stderr._flush()
369
+ except io.UnsupportedOperation: # pragma: no cover
370
+ self.wrapped_stderr = False
371
+ logger.warning(
372
+ 'Disabling stderr redirection, %r is not seekable',
373
+ sys.stderr,
374
+ )
375
+
376
+ def excepthook(self, exc_type, exc_value, exc_traceback):
377
+ self.original_excepthook(exc_type, exc_value, exc_traceback)
378
+ self.flush()
379
+
380
+
381
+ class AttributeDict(dict):
382
+ '''
383
+ A dict that can be accessed with .attribute.
384
+
385
+ >>> attrs = AttributeDict(spam=123)
386
+
387
+ # Reading
388
+
389
+ >>> attrs['spam']
390
+ 123
391
+ >>> attrs.spam
392
+ 123
393
+
394
+ # Read after update using attribute
395
+
396
+ >>> attrs.spam = 456
397
+ >>> attrs['spam']
398
+ 456
399
+ >>> attrs.spam
400
+ 456
401
+
402
+ # Read after update using dict access
403
+
404
+ >>> attrs['spam'] = 123
405
+ >>> attrs['spam']
406
+ 123
407
+ >>> attrs.spam
408
+ 123
409
+
410
+ # Read after update using dict access
411
+
412
+ >>> del attrs.spam
413
+ >>> attrs['spam']
414
+ Traceback (most recent call last):
415
+ ...
416
+ KeyError: 'spam'
417
+ >>> attrs.spam
418
+ Traceback (most recent call last):
419
+ ...
420
+ AttributeError: No such attribute: spam
421
+ >>> del attrs.spam
422
+ Traceback (most recent call last):
423
+ ...
424
+ AttributeError: No such attribute: spam
425
+ '''
426
+
427
+ def __getattr__(self, name: str) -> int:
428
+ if name in self:
429
+ return self[name]
430
+ else:
431
+ raise AttributeError(f'No such attribute: {name}')
432
+
433
+ def __setattr__(self, name: str, value: int) -> None:
434
+ self[name] = value
435
+
436
+ def __delattr__(self, name: str) -> None:
437
+ if name in self:
438
+ del self[name]
439
+ else:
440
+ raise AttributeError(f'No such attribute: {name}')
441
+
442
+
443
+ logger = logging.getLogger(__name__)
444
+ streams = StreamWrapper()
445
+ atexit.register(streams.flush)