Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Add progressbar support to most download actions #615
Merged
sergiusens
merged 8 commits into
snapcore:master
from
sergiusens:bugfix/1597387/progressing-everywhere
Jun 29, 2016
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1cf3e6f
Add progressbar support to most download actions
sergiusens f9f0676
fix static
sergiusens 02f01a4
update test
sergiusens 760a4ce
Merge branch 'master' into bugfix/1597387/progressing-everywhere
sergiusens 8a17e1f
Use raise_for_status instead
sergiusens 1d25940
Merge branch 'bugfix/1597387/progressing-everywhere' of github.com:se…
sergiusens 632352e
rename network->indicators
sergiusens cb26909
Merge branch 'master' into bugfix/1597387/progressing-everywhere
sergiusens
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,52 @@ | ||
| +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
| +# | ||
| +# Copyright (C) 2016 Canonical Ltd | ||
| +# | ||
| +# This program is free software: you can redistribute it and/or modify | ||
| +# it under the terms of the GNU General Public License version 3 as | ||
| +# published by the Free Software Foundation. | ||
| +# | ||
| +# This program is distributed in the hope that it will be useful, | ||
| +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| +# GNU General Public License for more details. | ||
| +# | ||
| +# You should have received a copy of the GNU General Public License | ||
| +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| + | ||
| +import os | ||
| + | ||
| +from progressbar import ( | ||
| + AnimatedMarker, | ||
| + Bar, | ||
| + Percentage, | ||
| + ProgressBar, | ||
| + UnknownLength, | ||
| +) | ||
| + | ||
| + | ||
| +def download_requests_stream(request_stream, destination, message=None): | ||
| + """This is a facility to download a request with nice progress bars.""" | ||
| + if not message: | ||
| + message = 'Downloading {!r}'.format(os.path.basename(destination)) | ||
| + | ||
| + total_length = int(request_stream.headers.get('Content-Length', '0')) | ||
| + if total_length: | ||
| + progress_bar = ProgressBar( | ||
| + widgets=[message, | ||
| + Bar(marker='=', left='[', right=']'), | ||
| + ' ', Percentage()], | ||
| + maxval=total_length) | ||
| + else: | ||
| + progress_bar = ProgressBar( | ||
| + widgets=[message, AnimatedMarker()], | ||
| + maxval=UnknownLength) | ||
| + | ||
| + total_read = 0 | ||
| + progress_bar.start() | ||
| + with open(destination, 'wb') as destination_file: | ||
| + for buf in request_stream.iter_content(1024): | ||
| + destination_file.write(buf) | ||
| + total_read += len(buf) | ||
| + progress_bar.update(total_read) | ||
| + progress_bar.finish() |
Yeah, this is really nice.