Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from ansiwrap import strip_color
class AwsError(Exception):
"""Raised when an AWS Exception is encountered."""
class FileExistsError(AwsError):
"""Raised when a File already exists on S3."""
class PapermillException(Exception):
"""Raised when an exception is encountered when operating on a notebook."""
class PapermillMissingParameterException(PapermillException):
"""Raised when a parameter without a value is required to operate on a notebook."""
class PapermillExecutionError(PapermillException):
"""Raised when an exception is encountered in a notebook."""
def __init__(self, exec_count, source, ename, evalue, traceback):
self.exec_count = exec_count
decompress = zlib.decompressobj(16 + zlib.MAX_WBITS)
size = 0
bytes_read = 0
err = None
undecoded = ''
if key:
# try to read the file multiple times
for i in range(100):
obj = self.s3.Object(key.bucket.name, key.name)
buffersize = buffersize if buffersize is not None else 2 ** 20
if not size:
size = obj.content_length
elif size != obj.content_length:
raise AwsError('key size unexpectedly changed while reading')
r = obj.get(Range="bytes={}-".format(bytes_read))
try:
while bytes_read < size:
# this making this weird check because this call is
# about 100 times slower if the amt is too high
if size - bytes_read > buffersize:
bytes = r['Body'].read(amt=buffersize)
else:
bytes = r['Body'].read()
if compressed:
s = decompress.decompress(bytes)
else:
s = bytes
logger.error("Error while decompressing [%s]", key.name)
raise
except UnicodeDecodeError:
raise
except Exception:
err = True
pass
if size <= bytes_read:
break
if size != bytes_read:
if err:
raise Exception
else:
raise AwsError('Failed to fully read [%s]' % source.name)
if undecoded:
assert encoding is not None # only time undecoded is set
# allow exception to be raised if one is thrown
decoded = undecoded.decode(encoding)
yield decoded