
    J                     Z    S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\	5      r
g	)
z1Common download workflow used by GCS API clients.    )absolute_import)division)unicode_literals)
retry_util)logc                   :    \ rS rSrSrS rS rS rS rS
S jr	Sr
g	)GcsDownload   z(Base class for performing GCS downloads.c                 4    Xl         X l        X0l        X l        g)a  Initializes a GcsDownload instance.

Args:
  download_stream (stream): Stream to send the object data to.
  start_byte (int): Starting point for download (for resumable downloads and
    range requests). Can be set to negative to request a range of bytes
    (python equivalent of [:-3]).
  end_byte (int): Ending byte number, inclusive, for download (for range
    requests). If None, download the rest of the object.

N)_download_stream_initial_start_byte	_end_byte_start_byte)selfdownload_stream
start_byteend_bytes       2lib/googlecloudsdk/api_lib/storage/gcs_download.py__init__GcsDownload.__init__   s     ,)N!    c                     [        5       e)z,Returns True if the error should be retried.NotImplementedError)r   exc_type	exc_valueexc_tracebacks       r   should_retryGcsDownload.should_retry-   s    

r   c                     [        5       e)a  Performs the download.

The child classes should implement this function.

Returns:
  Encoding string for object if requested. Otherwise, None.

Raises:
  CloudApiError: API returned an error.
  NotImplementedError: This function was not implemented by a class using
    this interface.
r   )r   s    r   launchGcsDownload.launch1   s     
r   c                     U R                  XU5      (       d  gU R                  R                  5       nXPR                  :  a  XPl        SUl        [
        R                  " SR                  XQU5      5        g)a  Returns True if the error should be retried.

This method also updates the start_byte to be used for request
to be retried.

Args:
  exc_type (type): The type of Exception.
  exc_value (Exception): The error instance.
  exc_traceback (traceback): The traceback for the exception.
  state (core.util.retry.RetryState): The state object
    maintained by the retryer.

Returns:
  True if the error should be retried.
Fr   z=Retrying download from byte {} after exception: {}. Trace: {}T)r   r   tellr   retrialr   debugformat)r   r   r   r   stater   s         r   _should_retry_wrapper!GcsDownload._should_retry_wrapper@   sj      X-@@ &&++-J$$$#emII !6*FHr   c                     U(       d  U R                  5       $ [        R                  " U R                   U R                  S9$ )zPerforms downlaod.

Args:
  retriable_in_flight (bool): Indicates if a download can be retried
    on network error, resuming the download from the last downloaded byte.

Returns:
  The result returned by launch method.
)targetshould_retry_if)r!   r   retryerr)   )r   retriable_in_flights     r   runGcsDownload.run]   s7     [[]{{D,F,FH Hr   )r   r   r   r   N)T)__name__
__module____qualname____firstlineno____doc__r   r   r!   r)   r0   __static_attributes__ r   r   r	   r	      s    0""  :Hr   r	   N)r6   
__future__r   r   r   googlecloudsdk.api_lib.storager   googlecloudsdk.corer   objectr	   r8   r   r   <module>r=      s)    8 &  ' 5 #RH& RHr   