| 31 | |
| 32 | |
| 33 | class AggregatedProgressCallback: |
| 34 | def __init__(self, callbacks, threshold=1024 * 256): |
| 35 | """Aggregates progress updates for every provided progress callback |
| 36 | |
| 37 | :type callbacks: A list of functions that accepts bytes_transferred |
| 38 | as a single argument |
| 39 | :param callbacks: The callbacks to invoke when threshold is reached |
| 40 | |
| 41 | :type threshold: int |
| 42 | :param threshold: The progress threshold in which to take the |
| 43 | aggregated progress and invoke the progress callback with that |
| 44 | aggregated progress total |
| 45 | """ |
| 46 | self._callbacks = callbacks |
| 47 | self._threshold = threshold |
| 48 | self._bytes_seen = 0 |
| 49 | |
| 50 | def __call__(self, bytes_transferred): |
| 51 | self._bytes_seen += bytes_transferred |
| 52 | if self._bytes_seen >= self._threshold: |
| 53 | self._trigger_callbacks() |
| 54 | |
| 55 | def flush(self): |
| 56 | """Flushes out any progress that has not been sent to its callbacks""" |
| 57 | if self._bytes_seen > 0: |
| 58 | self._trigger_callbacks() |
| 59 | |
| 60 | def _trigger_callbacks(self): |
| 61 | for callback in self._callbacks: |
| 62 | callback(bytes_transferred=self._bytes_seen) |
| 63 | self._bytes_seen = 0 |
| 64 | |
| 65 | |
| 66 | class InterruptReader: |
no outgoing calls