
                             d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lZd Z	 	 	 ddZ	 	 	 ddZy
)zIImplementation functions for downloads from the Google Cloud Storage API.    )absolute_import)division)unicode_literals)http_wrapper)errors)
retry_util)
exceptions)logNc                      y)z+Disables Apitools' default print callbacks.N )unused_responseunused_objects     7lib/googlecloudsdk/api_lib/storage/gcs_json/download.py_no_op_callbackr       s        c                     |s|!| j                  |||d       | j                  S | j                  |t        t        d       | j                  S )ad  GCS-specific download implementation.

  Args:
    apitools_download (apitools.transfer.Download): Apitools object for managing
      downloads.
    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.
    additional_headers (dict|None): Headers to add to HTTP request.

  Returns:
    Encoding string for object if requested. Otherwise, None.
  F)additional_headersstartend
use_chunks)r   callbackfinish_callbackr   )GetRangeStreamMediar   encoding)apitools_download
start_byteend_byter   s       r   launchr   %   sh    & 8'-	   
	#	## !!- '	 " 
 
	#	##r   c                      d|it        j                          fd}fd}t        j                  ||      S )z$Wraps download to make it retriable.r   c                    t        j                  |      \  }}t        |t        j                  j
                        r|j                  dk  rE|j                  dk7  r6yt        |t        j                        st        |t        j                        syj                         }|	d   kD  r|	d<   d|_        t        j                  dj                  || |             t!        j"                  j$                         y)Ni  i  Fr   r   z=Retrying download from byte {} after exception: {}. Trace: {}T)calliope_errorsConvertKnownError
isinstanceoauth2clientclientHttpAccessTokenRefreshErrorstatuscore_exceptionsNetworkIssueErrorcloud_errorsRetryableApiErrortellretrialr
   debugformatapitools_http_wrapperRebuildHttpConnections
bytes_http)
exc_type	exc_valueexc_tracebackstateconverted_error_r   r   download_streamprogress_states
          r    _should_retry_resumable_downloadz:launch_retriable.<locals>._should_retry_resumable_downloadQ   s    (::9EOQ)\00LLM			C	I$4$4$;/*K*KL,*H*HI %%'JN<00%/n\"emII !6*hFH 001B1M1MNr   c                  (    t        d          S )Nr   )r   r   r   )r   )r   r   r   r;   s   r   _call_launchz&launch_retriable.<locals>._call_launchh   s!    !,/-	/ /r   )targetshould_retry_if)r   set_retry_funcretryer)r:   r   r   r   r   r<   r>   r;   s   `` ``  @r   launch_retriablerC   G   sE     !*-.-../ 
		+K
M Mr   )r   NN)__doc__
__future__r   r   r   apitools.base.pyr   r1   googlecloudsdk.api_lib.storager   r+   r   googlecloudsdk.callioper	   r"   googlecloudsdk.corer)   r
   r%   r   r   rC   r   r   r   <module>rJ      sP    P &  ' B A 5 A = #  "$H !""(,	)Mr   