This commit is contained in:
Jakub Roztocil 2013-04-11 16:23:15 -03:00
parent 7ccdece39f
commit 674acfe2c2
4 changed files with 14 additions and 13 deletions

View File

@ -64,7 +64,7 @@ def main(args=sys.argv[1:], env=Environment()):
def error(msg, *args, **kwargs): def error(msg, *args, **kwargs):
msg = msg % args msg = msg % args
level = kwargs.get('level', 'error') level = kwargs.get('level', 'error')
env.stderr.write('http: %s: %s\n' % (level, msg)) env.stderr.write('\nhttp: %s: %s\n' % (level, msg))
debug = '--debug' in args debug = '--debug' in args
traceback = debug or '--traceback' in args traceback = debug or '--traceback' in args

View File

@ -110,7 +110,7 @@ class Download(object):
""" """
# Disable content encoding so that we can resume, etc. # Disable content encoding so that we can resume, etc.
request_headers['Accept-Encoding'] = '' request_headers['Accept-Encoding'] = None
if self._resume: if self._resume:
try: try:
bytes_have = os.path.getsize(self._output_file.name) bytes_have = os.path.getsize(self._output_file.name)
@ -118,16 +118,17 @@ class Download(object):
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
else: else:
self._resumed_from = bytes_have
# Set ``Range`` header to resume the download # Set ``Range`` header to resume the download
# TODO: Use "If-Range: mtime" to make sure it's fresh?
request_headers['Range'] = 'bytes=%d-' % bytes_have request_headers['Range'] = 'bytes=%d-' % bytes_have
self._resumed_from = bytes_have
def start(self, response): def start(self, response):
""" """
Initiate and return a stream for `response` body with progress Initiate and return a stream for `response` body with progress
callback attached. Can be called only once. callback attached. Can be called only once.
:param response: Initiated response object. :param response: Initiated response object with headers already fetched
:type response: requests.models.Response :type response: requests.models.Response
:return: RawStream, output_file :return: RawStream, output_file
@ -135,9 +136,10 @@ class Download(object):
""" """
assert not self._progress._time_started assert not self._progress._time_started
total_size = response.headers.get('Content-Length') try:
if total_size: total_size = int(response.headers['Content-Length'])
total_size = int(total_size) except (KeyError, ValueError):
total_size = None
if self._output_file: if self._output_file:
if self._resume and response.status_code == PARTIAL_CONTENT: if self._resume and response.status_code == PARTIAL_CONTENT:
@ -236,7 +238,7 @@ class Progress(object):
CLEAR_LINE = '\r\033[K' CLEAR_LINE = '\r\033[K'
PROGRESS = '{percentage:0.2f}% ({downloaded}) of {total} ({speed}/s)' PROGRESS = '{percentage:0.2f}% ({downloaded}) of {total} ({speed}/s)'
PROGRESS_NO_CONTENT_LENGTH = '{downloaded} ({speed}/s)' PROGRESS_NO_CONTENT_LENGTH = '{downloaded} ({speed}/s)'
SUMMARY = '{downloaded} of {total} in {time:0.5f}s ({speed}/s)\n' SUMMARY = 'Done. {downloaded} of {total} in {time:0.5f}s ({speed}/s)\n'
def __init__(self, output): def __init__(self, output):
""" """
@ -248,7 +250,7 @@ class Progress(object):
self.total_size = None self.total_size = None
self._resumed_from = 0 self._resumed_from = 0
self._downloaded_prev = 0 self._downloaded_prev = 0
self._content_length_humanized = '?' self._total_size_humanized = '?'
self._time_started = None self._time_started = None
self._time_finished = None self._time_finished = None
self._time_prev = None self._time_prev = None
@ -257,7 +259,7 @@ class Progress(object):
def started(self, resumed_from=0, total_size=None): def started(self, resumed_from=0, total_size=None):
assert self._time_started is None assert self._time_started is None
if total_size is not None: if total_size is not None:
self._content_length_humanized = humanize_bytes(total_size) self._total_size_humanized = humanize_bytes(total_size)
self.total_size = total_size self.total_size = total_size
self.downloaded = self._resumed_from = resumed_from self.downloaded = self._resumed_from = resumed_from
self._time_started = time() self._time_started = time()
@ -290,7 +292,7 @@ class Progress(object):
self.output.write(self.CLEAR_LINE + template.format( self.output.write(self.CLEAR_LINE + template.format(
percentage=percentage, percentage=percentage,
downloaded=humanize_bytes(self.downloaded), downloaded=humanize_bytes(self.downloaded),
total=self._content_length_humanized, total=self._total_size_humanized,
speed=humanize_bytes(self._speed) speed=humanize_bytes(self._speed)
)) ))

View File

@ -1,2 +0,0 @@
#
git://github.com/kennethreitz/httpbin.git

View File

@ -1449,5 +1449,6 @@ class DownloadsTest(BaseTestCase):
# invalid byte-range-resp-spec # invalid byte-range-resp-spec
_parse_content_range('bytes 100-100/*', 100) _parse_content_range('bytes 100-100/*', 100)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()