From 46266e86ea7b736d49fbd73d255f5e8220d99abc Mon Sep 17 00:00:00 2001 From: FrantiĊĦek Zatloukal Date: Jan 03 2019 12:33:22 +0000 Subject: Support both progressbar and progressbar2 These libraries are not 100% compatible, so this adds runtime detection and adapts to support both of them. --- diff --git a/libtaskotron/file_utils.py b/libtaskotron/file_utils.py index 7f9ea00..d7dd07d 100644 --- a/libtaskotron/file_utils.py +++ b/libtaskotron/file_utils.py @@ -185,7 +185,12 @@ def _download(url, dest, timeout=REQUESTS_TIMEOUT, chunk_size=REQUESTS_CHUNK_SIZ else: widgets = [progressbar.Percentage(), progressbar.Bar(), progressbar.ETA(), progressbar.FileTransferSpeed()] - pbar = progressbar.ProgressBar(widgets=widgets, maxval=int(total_len)).start() + + # Determine if we are using progressbar or progressbar2 + if hasattr(progressbar.ProgressBar(), 'max_value'): + pbar = progressbar.ProgressBar(widgets=widgets, max_value=int(total_len)).start() + else: + pbar = progressbar.ProgressBar(widgets=widgets, maxval=int(total_len)).start() read = 0 for chunk in r.iter_content(chunk_size): @@ -195,9 +200,12 @@ def _download(url, dest, timeout=REQUESTS_TIMEOUT, chunk_size=REQUESTS_CHUNK_SIZ # chunk can be smaller than chunk_size (end of file) or larger than it (content # decompression) read += min(len(chunk), chunk_size) - # don't exceed pbar.maxval, which can happen due to content decompression (see - # T755). This should not occur except for the last chunk. - pbar.update(min(read, pbar.maxval)) + # don't exceed pbar.maxval/pbar.max_value, which can happen due to content + # decompression (see T755). This should not occur except for the last chunk. + if hasattr(progressbar.ProgressBar(), 'max_value'): + pbar.update(min(read, pbar.max_value)) + else: + pbar.update(min(read, pbar.maxval)) # http://stackoverflow.com/questions/11325019