Skip to content

Commit

Permalink
Rebase and reblacken.
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Aug 27, 2019
1 parent 0a132ca commit f30f3f1
Show file tree
Hide file tree
Showing 23 changed files with 1,160 additions and 1,020 deletions.
2 changes: 2 additions & 0 deletions google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

try:
import pkg_resources

pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
12 changes: 6 additions & 6 deletions google/resumable_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@


__all__ = [
u'DataCorruption',
u'InvalidResponse',
u'PERMANENT_REDIRECT',
u'RetryStrategy',
u'TOO_MANY_REQUESTS',
u'UPLOAD_CHUNK_SIZE',
u"DataCorruption",
u"InvalidResponse",
u"PERMANENT_REDIRECT",
u"RetryStrategy",
u"TOO_MANY_REQUESTS",
u"UPLOAD_CHUNK_SIZE",
]
93 changes: 53 additions & 40 deletions google/resumable_media/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@


_CONTENT_RANGE_RE = re.compile(
r'bytes (?P<start_byte>\d+)-(?P<end_byte>\d+)/(?P<total_bytes>\d+)',
flags=re.IGNORECASE)
r"bytes (?P<start_byte>\d+)-(?P<end_byte>\d+)/(?P<total_bytes>\d+)",
flags=re.IGNORECASE,
)
_ACCEPTABLE_STATUS_CODES = (http_client.OK, http_client.PARTIAL_CONTENT)
_GET = u'GET'
_GET = u"GET"


class DownloadBase(object):
Expand All @@ -50,8 +51,7 @@ class DownloadBase(object):
end (Optional[int]): The last byte in a range to be downloaded.
"""

def __init__(self, media_url, stream=None,
start=None, end=None, headers=None):
def __init__(self, media_url, stream=None, start=None, end=None, headers=None):
self.media_url = media_url
self._stream = stream
self.start = start
Expand All @@ -77,7 +77,7 @@ def _get_status_code(response):
Raises:
NotImplementedError: Always, since virtual.
"""
raise NotImplementedError(u'This implementation is virtual.')
raise NotImplementedError(u"This implementation is virtual.")

@staticmethod
def _get_headers(response):
Expand All @@ -89,7 +89,7 @@ def _get_headers(response):
Raises:
NotImplementedError: Always, since virtual.
"""
raise NotImplementedError(u'This implementation is virtual.')
raise NotImplementedError(u"This implementation is virtual.")

@staticmethod
def _get_body(response):
Expand All @@ -101,7 +101,7 @@ def _get_body(response):
Raises:
NotImplementedError: Always, since virtual.
"""
raise NotImplementedError(u'This implementation is virtual.')
raise NotImplementedError(u"This implementation is virtual.")


class Download(DownloadBase):
Expand Down Expand Up @@ -147,7 +147,7 @@ def _prepare_request(self):
.. _sans-I/O: https://sans-io.readthedocs.io/
"""
if self.finished:
raise ValueError(u'A download can only be used once.')
raise ValueError(u"A download can only be used once.")

add_bytes_range(self.start, self.end, self._headers)
return _GET, self.media_url, None, self._headers
Expand All @@ -167,7 +167,8 @@ def _process_response(self, response):
# Tombstone the current Download so it cannot be used again.
self._finished = True
_helpers.require_status_code(
response, _ACCEPTABLE_STATUS_CODES, self._get_status_code)
response, _ACCEPTABLE_STATUS_CODES, self._get_status_code
)

def consume(self, transport):
"""Consume the resource to be downloaded.
Expand All @@ -182,7 +183,7 @@ def consume(self, transport):
Raises:
NotImplementedError: Always, since virtual.
"""
raise NotImplementedError(u'This implementation is virtual.')
raise NotImplementedError(u"This implementation is virtual.")


class ChunkedDownload(DownloadBase):
Expand Down Expand Up @@ -213,14 +214,14 @@ class ChunkedDownload(DownloadBase):
ValueError: If ``start`` is negative.
"""

def __init__(self, media_url, chunk_size, stream,
start=0, end=None, headers=None):
def __init__(self, media_url, chunk_size, stream, start=0, end=None, headers=None):
if start < 0:
raise ValueError(
u'On a chunked download the starting '
u'value cannot be negative.')
u"On a chunked download the starting " u"value cannot be negative."
)
super(ChunkedDownload, self).__init__(
media_url, stream=stream, start=start, end=end, headers=headers)
media_url, stream=stream, start=start, end=end, headers=headers
)
self.chunk_size = chunk_size
self._bytes_downloaded = 0
self._total_bytes = None
Expand Down Expand Up @@ -289,9 +290,9 @@ def _prepare_request(self):
.. _sans-I/O: https://sans-io.readthedocs.io/
"""
if self.finished:
raise ValueError(u'Download has finished.')
raise ValueError(u"Download has finished.")
if self.invalid:
raise ValueError(u'Download is invalid and cannot be re-used.')
raise ValueError(u"Download is invalid and cannot be re-used.")

curr_start, curr_end = self._get_byte_range()
add_bytes_range(curr_start, curr_end, self._headers)
Expand Down Expand Up @@ -341,28 +342,37 @@ def _process_response(self, response):
"""
# Verify the response before updating the current instance.
_helpers.require_status_code(
response, _ACCEPTABLE_STATUS_CODES,
self._get_status_code, callback=self._make_invalid)
response,
_ACCEPTABLE_STATUS_CODES,
self._get_status_code,
callback=self._make_invalid,
)
headers = self._get_headers(response)
response_body = self._get_body(response)

start_byte, end_byte, total_bytes = get_range_info(
response, self._get_headers, callback=self._make_invalid)
response, self._get_headers, callback=self._make_invalid
)

transfer_encoding = headers.get(u'transfer-encoding')
transfer_encoding = headers.get(u"transfer-encoding")

if transfer_encoding is None:
content_length = _helpers.header_required(
response, u'content-length', self._get_headers,
callback=self._make_invalid)
response,
u"content-length",
self._get_headers,
callback=self._make_invalid,
)
num_bytes = int(content_length)
if len(response_body) != num_bytes:
self._make_invalid()
raise common.InvalidResponse(
response,
u'Response is different size than content-length',
u'Expected', num_bytes,
u'Received', len(response_body),
u"Response is different size than content-length",
u"Expected",
num_bytes,
u"Received",
len(response_body),
)
else:
# 'content-length' header not allowed with chunked encoding.
Expand Down Expand Up @@ -391,7 +401,7 @@ def consume_next_chunk(self, transport):
Raises:
NotImplementedError: Always, since virtual.
"""
raise NotImplementedError(u'This implementation is virtual.')
raise NotImplementedError(u"This implementation is virtual.")


def add_bytes_range(start, end, headers):
Expand Down Expand Up @@ -431,18 +441,18 @@ def add_bytes_range(start, end, headers):
return
else:
# NOTE: This assumes ``end`` is non-negative.
bytes_range = u'0-{:d}'.format(end)
bytes_range = u"0-{:d}".format(end)
else:
if end is None:
if start < 0:
bytes_range = u'{:d}'.format(start)
bytes_range = u"{:d}".format(start)
else:
bytes_range = u'{:d}-'.format(start)
bytes_range = u"{:d}-".format(start)
else:
# NOTE: This is invalid if ``start < 0``.
bytes_range = u'{:d}-{:d}'.format(start, end)
bytes_range = u"{:d}-{:d}".format(start, end)

headers[_helpers.RANGE_HEADER] = u'bytes=' + bytes_range
headers[_helpers.RANGE_HEADER] = u"bytes=" + bytes_range


def get_range_info(response, get_headers, callback=_helpers.do_nothing):
Expand All @@ -464,17 +474,20 @@ def get_range_info(response, get_headers, callback=_helpers.do_nothing):
``bytes {start}-{end}/{total}``.
"""
content_range = _helpers.header_required(
response, _helpers.CONTENT_RANGE_HEADER,
get_headers, callback=callback)
response, _helpers.CONTENT_RANGE_HEADER, get_headers, callback=callback
)
match = _CONTENT_RANGE_RE.match(content_range)
if match is None:
callback()
raise common.InvalidResponse(
response, u'Unexpected content-range header', content_range,
u'Expected to be of the form "bytes {start}-{end}/{total}"')
response,
u"Unexpected content-range header",
content_range,
u'Expected to be of the form "bytes {start}-{end}/{total}"',
)

return (
int(match.group(u'start_byte')),
int(match.group(u'end_byte')),
int(match.group(u'total_bytes'))
int(match.group(u"start_byte")),
int(match.group(u"end_byte")),
int(match.group(u"total_bytes")),
)
21 changes: 12 additions & 9 deletions google/resumable_media/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from google.resumable_media import common


RANGE_HEADER = u'range'
CONTENT_RANGE_HEADER = u'content-range'
RANGE_HEADER = u"range"
CONTENT_RANGE_HEADER = u"content-range"
RETRYABLE = (
common.TOO_MANY_REQUESTS,
http_client.INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -61,13 +61,13 @@ def header_required(response, name, get_headers, callback=do_nothing):
if name not in headers:
callback()
raise common.InvalidResponse(
response, u'Response headers must contain header', name)
response, u"Response headers must contain header", name
)

return headers[name]


def require_status_code(response, status_codes, get_status_code,
callback=do_nothing):
def require_status_code(response, status_codes, get_status_code, callback=do_nothing):
"""Require a response has a status code among a list.
Args:
Expand All @@ -89,8 +89,12 @@ def require_status_code(response, status_codes, get_status_code,
if status_code not in status_codes:
callback()
raise common.InvalidResponse(
response, u'Request failed with status code',
status_code, u'Expected one of', *status_codes)
response,
u"Request failed with status code",
status_code,
u"Expected one of",
*status_codes
)
return status_code


Expand Down Expand Up @@ -151,8 +155,7 @@ def wait_and_retry(func, get_status_code, retry_strategy):
num_retries = 0
base_wait = 0.5 # When doubled will give 1.0
while retry_strategy.retry_allowed(total_sleep, num_retries):
base_wait, wait_time = calculate_retry_wait(
base_wait, retry_strategy.max_sleep)
base_wait, wait_time = calculate_retry_wait(base_wait, retry_strategy.max_sleep)
num_retries += 1
total_sleep += wait_time
time.sleep(wait_time)
Expand Down
Loading

0 comments on commit f30f3f1

Please sign in to comment.