Skip to content

Commit

Permalink
Reblacken.
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Aug 29, 2019
1 parent 4d76d9e commit 227dc28
Show file tree
Hide file tree
Showing 29 changed files with 1,343 additions and 1,182 deletions.
10 changes: 10 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by synthtool. DO NOT EDIT!
[run]
branch = True

Expand All @@ -7,3 +8,12 @@ show_missing = True
exclude_lines =
# Re-enable the standard pragma
pragma: NO COVER
# Ignore debug-only repr
def __repr__
# Ignore abstract methods
raise NotImplementedError
omit =
*/gapic/*.py
*/proto/*.py
*/core/*.py
*/site-packages/*.py
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Generated by synthtool. DO NOT EDIT!
[flake8]
ignore = E203, E266, E501, W503
exclude =
# Exclude generated code.
**/proto/**
**/gapic/**
*_pb2.py

# Standard linting exemptions.
__pycache__,
.git,
*.pyc,
Expand Down
5 changes: 3 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include README.rst LICENSE
recursive-include google
recursive-include google *.json *.proto
recursive-include tests *
global-exclude *.pyc __pycache__
global-exclude *.py[co]
global-exclude __pycache__
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",
]
110 changes: 63 additions & 47 deletions google/resumable_media/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@


_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'
_ZERO_CONTENT_RANGE_HEADER = u'bytes */0'
_GET = u"GET"
_ZERO_CONTENT_RANGE_HEADER = u"bytes */0"


class DownloadBase(object):
Expand All @@ -51,8 +52,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 @@ -78,7 +78,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 @@ -90,7 +90,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 @@ -102,7 +102,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 @@ -148,7 +148,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 @@ -168,7 +168,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 @@ -183,7 +184,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 @@ -214,14 +215,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 @@ -290,9 +291,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,34 +342,44 @@ def _process_response(self, response):
.. _sans-I/O: https://sans-io.readthedocs.io/
"""
# Verify the response before updating the current instance.
if _check_for_zero_content_range(response, self._get_status_code,
self._get_headers):
if _check_for_zero_content_range(
response, self._get_status_code, self._get_headers
):
self._finished = True
return

_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 @@ -397,7 +408,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 @@ -437,18 +448,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 @@ -470,19 +481,22 @@ 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")),
)


Expand All @@ -501,11 +515,13 @@ def _check_for_zero_content_range(response, get_status_code, get_headers):
Returns:
bool: True if content range total bytes is zero, false otherwise.
"""
if get_status_code(response) == http_client. \
REQUESTED_RANGE_NOT_SATISFIABLE:
if get_status_code(response) == http_client.REQUESTED_RANGE_NOT_SATISFIABLE:
content_range = _helpers.header_required(
response, _helpers.CONTENT_RANGE_HEADER,
get_headers, callback=_helpers.do_nothing)
response,
_helpers.CONTENT_RANGE_HEADER,
get_headers,
callback=_helpers.do_nothing,
)
if content_range == _ZERO_CONTENT_RANGE_HEADER:
return True
return False
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 227dc28

Please sign in to comment.