HEX
Server: LiteSpeed
System: Linux us-phx-web629.main-hosting.eu 5.14.0-503.23.2.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Feb 12 05:52:18 EST 2025 x86_64
User: u756937133 (756937133)
PHP: 8.2.27
Disabled: passthru,chgrp
Upload Files
File: //opt/gsutil/third_party/apitools/apitools/base/py/__pycache__/transfer.cpython-39.pyc
a

�z�g���@sFdZddlmZddlmZddlmmZ	ddl
mmZddl
Z
ddlZddlZddlZddlZddlZddlmZddlmZddlmZddlmZddlmZdd	lmZdd
lmZgd�ZdZd
ZdZdd�Z dd�Z!dd�Z"dd�Z#Gdd�de$�Z%Gdd�de%�Z&ej'�r2Gdd�dej(�Z)Gdd�de%�Z*dS)z)Upload and download support for apitools.�)�print_functionN)�http_client)�buffered_stream)�compression)�
exceptions)�http_wrapper)�stream_slice)�util)�Download�Upload�RESUMABLE_UPLOAD�
SIMPLE_UPLOAD�DownloadProgressPrinter�DownloadCompletePrinter�UploadProgressPrinter�UploadCompletePrinteriP�simple�	resumablecCs0d|jvrtd|jd�ntd|j�dS)z*Print download progress based on response.�
content-rangezReceived %szReceived %d bytesN)�info�print�length)�response�unused_download�r�=/opt/gsutil/third_party/apitools/apitools/base/py/transfer.pyr7s
rcCstd�dS)z-Print information about a completed download.zDownload completeN�r)�unused_responserrrrr?srcCstd|jd�dS)z(Print upload progress based on response.zSent %s�rangeN)rr)r�
unused_uploadrrrrDsrcCstd�dS)z+Print information about a completed upload.zUpload completeNr)rrrrrrIsrc@s�eZdZdZd&dd�Zdd	�Zed
d��Zedd
��Zedd��Z	e	j
dd��Z	edd��Zej
dd��Zedd��Zedd��Z
dd�Zedd��Zedd��Zdd�Zd d!�Zd"d#�Zd$d%�ZdS)'�	_Transferz-Generic bits common to Uploads and Downloads.FNT�cCsFd|_||_||_||_d|_d|_||_tj|_	||_
|p>d|_dS)Nr!i)�_Transfer__bytes_http�_Transfer__close_stream�_Transfer__http�_Transfer__stream�_Transfer__url�_Transfer__num_retries�num_retriesr�)HandleExceptionsAndRebuildHttpConnections�
retry_func�
auto_transfer�	chunksize)�self�stream�close_streamr,r+�httpr(rrr�__init__Rs�z_Transfer.__init__cCst|�S�N)�str�r-rrr�__repr__csz_Transfer.__repr__cCs|jSr2)r#r4rrrr/fsz_Transfer.close_streamcCs|jSr2)r$r4rrrr0jsz_Transfer.httpcCs|jp
|jSr2)r"r0r4rrr�
bytes_httpnsz_Transfer.bytes_httpcCs
||_dSr2)r"�r-�valuerrrr6rscCs|jSr2)r'r4rrrr(vsz_Transfer.num_retriescCs*t�|tj�|dkr t�d��||_dS)Nrz*Cannot have negative value for num_retries)r	�	Typecheck�six�
integer_typesr�InvalidDataErrorr'r7rrrr(zs�cCs|jSr2)r%r4rrrr.�sz_Transfer.streamcCs|jSr2)r&r4rrr�url�sz
_Transfer.urlcCs*|��|jdur |pt��|_||_dS)a�Initialize this download by setting self.http and self.url.

        We want the user to be able to override self.http by having set
        the value in the constructor; in that case, we ignore the provided
        http.

        Args:
          http: An httplib2.Http instance or None.
          url: The url for this transfer.

        Returns:
          None. Initializes self.
        N)�EnsureUninitializedr0r�GetHttpr$r&)r-r0r=rrr�_Initialize�s
z_Transfer._InitializecCs|jduo|jduSr2)r=r0r4rrr�initialized�sz_Transfer.initializedcCs
t|�jSr2)�type�__name__r4rrr�
_type_name�sz_Transfer._type_namecCs|jst�d|j��dS)NzCannot use uninitialized %s�rAr�TransferInvalidErrorrDr4rrr�EnsureInitialized�s�z_Transfer.EnsureInitializedcCs|jrt�d|j��dS)NzCannot re-initialize %srEr4rrrr>�s�z_Transfer.EnsureUninitializedcCs|jr|j��dSr2)r#r%�closer4rrr�__del__�sz_Transfer.__del__cCs"|durtj|||fd���dS)N)�target�args)�	threading�Thread�start)r-�callbackrrrr�_ExecuteCallback�sz_Transfer._ExecuteCallback)FNTNr!)rC�
__module__�__qualname__�__doc__r1r5�propertyr/r0r6�setterr(r.r=r@rArDrGr>rIrPrrrrr Ns<�










r cseZdZdZeejejejej	f�Z
ed�Zd.�fdd�	Ze
dd��Ze
dd	��Zed/dd
��Zed0dd��Zed1dd��Ze
dd��Ze
dd��Zdd�Zdd�Zdd�Zd2dd�Zd3dd�Zd4d d!�Zd5d"d#�Zd6d$d%�Zd&d'�Zd7d(d)�Zd8d*d+�Z d9d,d-�Z!�Z"S):r
zqData for a single download.

    Public attributes:
      chunksize: default chunksize to use for transfers.
    �r+�progress�
total_sizer=NcsL|�dd�}tt|�j|fi|��d|_d|_||_d|_||_||_	dS)NrXr)
�pop�superr
r1�_Download__initial_response�_Download__progress�_Download__total_size�_Download__encoding�progress_callback�finish_callback)r-r.r_r`�kwdsrX��	__class__rrr1�szDownload.__init__cCs|jSr2)r\r4rrrrW�szDownload.progresscCs|jSr2)r^r4rrr�encoding�szDownload.encodingFTcKsFtj�|�}tj�|�r*|s*t�d|��|t|d�fd|d�|��S)z-Create a new download object from a filename.z*File %s exists and overwrite not specified�wbT)r/r+)�os�path�
expanduser�existsr�InvalidUserInputError�open)�cls�filenameZ	overwriter+rargrrr�FromFile�s���zDownload.FromFilecKs||f||d�|��S)z+Create a new Download object from a stream.)r+rXr)rlr.r+rXrarrr�
FromStream�s�zDownload.FromStreamcKs�t�|�}|jt|���}|r4t�dd�|���|j|fi|��}	|durV||	_	n
|d|	_	|durx|�
|d�}
n|d}
t|	d|d�t|	d|d	�|	�||
�|	S)
z?Create a new Download object from a stream and serialized data.�,Invalid serialization data, missing keys: %s�, Nr+r=r\rWr]rX)
�json�loads�_REQUIRED_SERIALIZATION_KEYS�set�keysrr<�joinror+�FinalizeTransferUrl�setattrr@)rlr.�	json_datar0r+�clientrar�missing_keys�downloadr=rrr�FromData�s*
��
�zDownload.FromDatacCs|��|j|j|j|jd�S)NrV)rGr+rWrXr=r4rrr�serialization_datas�zDownload.serialization_datacCs|jSr2)r]r4rrrrXszDownload.total_sizecCs|js
dSd|j|j|jfS)NzDownload (uninitialized)z1Download with %d/%s bytes transferred from url %s�rArWrXr=r4rrr�__str__s
�zDownload.__str__cCs$d|jd<d|jdf|jd<dS)N�media�altz
bytes=0-%d��Range)�query_paramsr,�headers)r-�http_request�url_builderrrr�ConfigureRequests
zDownload.ConfigureRequestcCsBd|vr.|d�d�\}}}|dkr.t|�|_|jdur>d|_dS)z;Sets the total size based off info if possible otherwise 0.r�/�*Nr)�
rpartition�intr]rX)r-r�_�totalrrrZ
__SetTotal#s

zDownload.__SetTotalcCs�|��|dur"|dur"t�d��|p*|j}|durB|�|j�|_|j}|jr�|�d�}|�|d|�t	�
|jpr||�}|j|j
vr�tj�|��||_|�|j�|j�d|j�}|dur�|�|�}|�||�|jr�|��dS)a�Initialize this download by making a request.

        Args:
          http_request: The HttpRequest to use to initialize this download.
          http: The httplib2.Http instance for this request.
          client: If provided, let this client process the final URL before
              sending any additional requests. If client is provided and
              http is not, client.http will be used instead.
        N�Must provide client or http.rzcontent-location)r>r�	UserErrorr0rxr=r+�_Download__ComputeEndByte�_Download__SetRangeHeaderr�MakeRequestr6�status_code�_ACCEPTABLE_STATUSES�	HttpError�FromResponser[�_Download__SetTotalr�get�request_urlr@�StreamInChunks)r-r�r0r{r=�end_byterrrr�InitializeDownload0s.




�
zDownload.InitializeDownloadcCs�|dur||dkr(t�dd||f��n"||jkrJt�dd||jf��t||jd�}||krtt�d||f��||fS|dkr�td||j�}||jdfSdS)	z4Normalizes start and end values based on total size.Nrz0Cannot have end index with negative start index z[start=%d, end=%d]z0Cannot have start index greater than total size z[start=%d, total_size=%d]r�z(Range requested with end[%s] < start[%s])rrFrX�min�max)r-rN�endrrrZ__NormalizeStartEndSs,
��
��
�zDownload.__NormalizeStartEndcCsN|dkrd||jd<n2|dus(||kr8d||jd<nd||f|jd<dS)Nrzbytes=%drz	bytes=%d-zbytes=%d-%d)r�)r-�requestrNr�rrrZ__SetRangeHeaderhs
zDownload.__SetRangeHeadercCsl|}|dkr|js|S|r@||jd}|dur<t||�}n|}|jrh|jd}|durdt||�}n|}|S)a�Compute the last byte to fetch for this request.

        This is all based on the HTTP spec for Range and
        Content-Range.

        Note that this is potentially confusing in several ways:
          * the value for the last byte is 0-based, eg "fetch 10 bytes
            from the beginning" would return 9 here.
          * if we have no information about size, and don't want to
            use the chunksize, we'll return None.
        See the tests for more examples.

        Args:
          start: byte to start at.
          end: (int or None, default: None) Suggested last byte.
          use_chunks: (bool, default: True) If False, ignore self.chunksize.

        Returns:
          Last byte to use in a Range header, or None.

        rr�N)rXr,r�)r-rNr��
use_chunksr��	alternaterrrZ__ComputeEndByteps
zDownload.__ComputeEndBytecCsR|��tj|jd�}|j|||d�|dur:|j�|�tj|j||j	|j
d�S)z/Retrieve a chunk, and return the full response.)r=)r�N)r*�retries)rGr�Requestr=r�r��updater�r6r*r()r-rNr��additional_headersr�rrrZ
__GetChunk�s
�zDownload.__GetChunkcCs�|j|jvr8|jtjtjfvr,tj�|��nt�|j	��|jtj
tjfvr�z|j�
t�|j	��Wn&ty�|j�
t�|j	��Yn0|j|j7_|jr�d|jvr�|jd|_n|jtjkr�|j�
d�|S)z?Process response (by updating self and writing to self.stream).zcontent-encoding�)r�r�r�	FORBIDDEN�	NOT_FOUNDrr�r��TransferRetryError�content�OK�PARTIAL_CONTENTr.�writer:�
ensure_binary�	TypeError�ensure_textr\rrr^�
NO_CONTENT�r-rrrrZ__ProcessResponse�s&��zDownload.__ProcessResponsec	Cs�|��d}|jdur,|�||�\}}d}n|}|}|rH|dusH||kr�|j|||d�}|j|||d�}|s�|�|j�|�||�\}}d}|�|�}||j7}|jdkr4|j	t
jkr�dSt�
d��q4dS)a�Retrieve a given byte range from this download, inclusive.

        Range must be of one of these three forms:
        * 0 <= start, end = None: Fetch from start to the end of the file.
        * 0 <= start <= end: Fetch the bytes from start to end.
        * start < 0, end = None: Fetch the last -start bytes of the file.

        (These variations correspond to those described in the HTTP 1.1
        protocol for range headers in RFC 2616, sec. 14.35.1.)

        Args:
          start: (int) Where to start fetching bytes. (See above.)
          end: (int, optional) Where to stop fetching bytes. (See above.)
          additional_headers: (bool, optional) Any additional headers to
              pass with the request.
          use_chunks: (bool, default: True) If False, ignore self.chunksize
              and fetch this range in a single request.

        Returns:
          None. Streams bytes into self.stream.
        FNT)r�r��r�rz5Zero bytes unexpectedly returned in download response)rGrX�_Download__NormalizeStartEndr��_Download__GetChunkr�r�_Download__ProcessResponserr�rr�rr�)	r-rNr�r�r�Zprogress_end_normalizedrWr�rrrr�GetRange�s8
���


�zDownload.GetRangecCs|j|||dd�dS)z%Stream the entire download in chunks.T�rOr`r�r�N)�StreamMedia�r-rOr`r�rrrr��s�zDownload.StreamInChunkscCs�|p|j}|p|j}|��|jdur4|j}d|_n"|j|j|d�}|j|j||d�}|jdurl|�|j	�|�
|�}|�||�|jt
jks�|j|jkrq�q|�||�dS)a4Stream the entire download.

        Args:
          callback: (default: None) Callback to call as each chunk is
              completed.
          finish_callback: (default: None) Callback to call when the
              download is complete.
          additional_headers: (default: None) Additional headers to
              include in fetching bytes.
          use_chunks: (bool, default: True) If False, ignore self.chunksize
              and stream this download in a single request.

        Returns:
            None. Streams bytes into self.stream.
        N)r�r�)r_r`rGr[r�rWr�rXr�rr�rPr�rr�)r-rOr`r�r�rr�rrrr��s,


��


�zDownload.StreamMedia)NN)FT)TN)NNN)NN)N)N)NT)N)NNT)NNN)NNNT)#rCrQrRrSrurr�r�r��REQUESTED_RANGE_NOT_SATISFIABLEr�rtr1rTrWrd�classmethodrnror~rrXr�r�r�r�r�r�r�r�r�r�r�r��
__classcell__rrrbrr
�sV�

	�
	

#


+
�
6�
�r
c@s$eZdZdZdd�Zdd�ZeZdS)�MultipartBytesGeneratoraoGenerates a bytes Message object tree for multipart messages

        This is a BytesGenerator that has been modified to not attempt line
        termination character modification in the bytes payload. Known to
        work with the compat32 policy only. It may work on others, but not
        tested. The outfp object must accept bytes in its write method.
        cCs|jdurdS|�|j�dSr2)�_payloadr�)r-�msgrrr�_handle_text1s
z$MultipartBytesGenerator._handle_textcCs|�dd�S)N�ascii�surrogateescape)�encode)r-�srrr�_encode8szMultipartBytesGenerator._encodeN)rCrQrRrSr�r��
_writeBodyrrrrr�)sr�csVeZdZdZed�Zd@�fdd�	Zedd	��Ze	dAd
d��Z
e	dBdd
��Ze	dCdd��Zedd��Z
edd��Zedd��Zdd�Zedd��Zejdd��Zedd��Zejdd��Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�ZdDd,d-�Zd.d/�ZdEd0d1�Zd2d3�ZdFd4d5�ZdGd6d7�Z dHd8d9�Z!d:d;�Z"dId<d=�Z#dJd>d?�Z$�Z%S)Kra�Data for a single Upload.

    Fields:
      stream: The stream to upload.
      mime_type: MIME type of the upload.
      total_size: (optional) Total upload size for the stream.
      close_stream: (default: False) Whether or not we should close the
          stream when finished with the upload.
      auto_transfer: (default: True) If True, stream all bytes as soon as
          the upload is created.
    �r+�	mime_typerXr=NFTcshtt|�j|f||||d�|��d|_d|_||_d|_d|_d|_d|_	|
|_
||_|	|_||_
dS)N)r/r,r+r0Fr)rZrr1�_Upload__complete�_Upload__final_response�_Upload__mime_type�_Upload__progress�!_Upload__server_chunk_granularity�_Upload__strategy�_Upload__total_size�_Upload__gzip_encodedr_r`rX)r-r.r�rXr0r/r,r+r_r`�gzip_encodedrarbrrr1Os&
���zUpload.__init__cCs|jSr2)r�r4rrrrWcszUpload.progressc	Ks|tj�|�}tj�|�s&t�d|��|sNt�|�\}}|durNt�d|��t�	|�j
}|t|d�|f|d||d�|��S)z+Create a new Upload object from a filename.zCould not find file %sNz Could not guess mime type for %s�rbT�rXr/r+r�)rfrgrhrir�
NotFoundError�	mimetypes�
guess_typerj�stat�st_sizerk)	rlrmr�r+r�rargr��sizerrrrngs ���zUpload.FromFilecKs.|durt�d��|||f|d||d�|��S)z)Create a new Upload object from a stream.Nz!No mime_type specified for streamFr�)rrj)rlr.r�rXr+r�rarrrroxs�
��zUpload.FromStreamcKs�t�|�}|jt|���}	|	r4t�dd�|	���d|vrFt�d��|j	||df|�
d�|d�|��}
t|tj
�r�|��s�t�d��|dur�||
_n
|d	|
_|dur�|�|d
�}n|d
}t|
_|
�||�|
��|
��|
jr�|
��|
S)zACreate a new Upload of stream from serialized json_data and http.rprqrXz/Cannot override total_size on serialized Uploadr�)rXr�z6Cannot restart resumable upload on non-seekable streamNr+r=)rrrsrtrurvrr<rwrjror��
isinstance�io�IOBase�seekabler+rxr�strategyr@�RefreshResumableUploadStaterGr�)rlr.rzr0r+r�r{rarr|�uploadr=rrrr~�sJ
������
�zUpload.FromDatacCs2|��|jtkrt�d��|j|j|j|jd�S)Nz2Serialization only supported for resumable uploadsr�)	rGr�rrr<r+r�rXr=r4rrrr�s
��zUpload.serialization_datacCs|jSr2)r�r4rrr�complete�szUpload.completecCs|jSr2)r�r4rrrr��szUpload.mime_typecCs"|js
dSd|j|jpd|jfS)NzUpload (uninitialized)z.Upload with %d/%s bytes transferred for url %sz???r�r4rrrr��s
�zUpload.__str__cCs|jSr2)r�r4rrrr��szUpload.strategycCs$|ttfvrt�d|��||_dS)NzOInvalid value "%s" for upload strategy, must be one of "simple" or "resumable".)r
rrr�r�r7rrrr��s�cCs|jSr2)r�r4rrrrX�szUpload.total_sizecCs|��||_dSr2)r>r�r7rrrrX�scCs^|jdurt|_|jdurdSt}|jdur:|jtkr:t}|jrJ|jsJt}|jsTt}||_dS)aDetermine and set the default upload strategy for this upload.

        We generally prefer simple or multipart, unless we're forced to
        use resumable. This happens when any of (1) the upload is too
        large, (2) the simple endpoint doesn't support multipart requests
        and we have metadata, or (3) there is no simple upload endpoint.

        Args:
          upload_config: Configuration for the upload endpoint.
          http_request: The associated http request.

        Returns:
          None.
        N)	�resumable_pathr
r�rX�_RESUMABLE_UPLOAD_THRESHOLDr�body�simple_multipart�simple_path)r-�
upload_configr�r�rrrZ__SetDefaultUploadStrategy�s


�z!Upload.__SetDefaultUploadStrategycCs�|jr.|jr.|j|jkr.t�d|j|jf��t�|j|j�sTt�d|j|jf��|�||�|j	t
kr�|j|_|j
r�d|jd<|�|�nd|jd<|�|�|jr�d|jd<t�t�|j
��d��|_
n|j|_d	|jd<|�|�d
S)z.Configure the request and url for this upload.z*Upload too big: %s larger than max size %sz7MIME type %s does not match any accepted MIME ranges %s�	multipartZ
uploadTyper��gzip�Content-EncodingrrN)rX�max_sizerrjr	�AcceptableMimeType�acceptr��!_Upload__SetDefaultUploadStrategyr�r
r��
relative_pathr�r��"_Upload__ConfigureMultipartRequest�_Upload__ConfigureMediaRequestr�r�r�CompressStreamr:�BytesIO�readr��"_Upload__ConfigureResumableRequest)r-r�r�r�rrrr��sB
�����





���
zUpload.ConfigureRequestcCs"|j|jd<|j��|_d|_dS)z;Configure http_request as a simple request for this upload.�content-type�<media body>N)r�r�r.r�r��
loggable_body�r-r�rrrZ__ConfigureMediaRequest"szUpload.__ConfigureMediaRequestcCs&t�d�}t|ddd��tj|jd�d��}|�|j�|�	|�tj|j
�d��}d|d<|�|j���|�	|�t
��}t
jr�t}ntj}||d	d
�}|j|d	d�|��|_|��}d||jd<t|t
j�r�|�d
�}|j�|�}|d�d�\}	}
}
d�|	dg�|d<|�|�|_dS)z>Configure http_request as a multipart request for this upload.�related�_write_headerscSsdSr2rr4rrr�<lambda>-�z4Upload.__ConfigureMultipartRequest.<locals>.<lambda>r�r��binaryzContent-Transfer-EncodingF)�mangle_from_)�unixfromzmultipart/related; boundary=%rr����s

s<media body>

--N)�mime_multipart�
MIMEMultipartry�mime_nonmultipart�MIMENonMultipartr��split�set_payloadr��attachr�r.r�r:r��PY3r��email_generator�	Generator�flatten�getvalue�get_boundaryr��	text_typer��	partitionrwr�)r-r�Zmsg_rootr��fpZgenerator_class�gZmultipart_boundaryZbody_componentsr�r�rrrZ__ConfigureMultipartRequest(s6
�


�
z"Upload.__ConfigureMultipartRequestcCs*|j|jd<|jdur&t|j�|jd<dS)NzX-Upload-Content-TypezX-Upload-Content-Length)r�r�rXr3r�rrrZ__ConfigureResumableRequestRs
��z"Upload.__ConfigureResumableRequestcCs�|jtkrdS|��tj|jdddid�}tj|j|d|jd�}|�	|�}|j
tjtj
fvr�d|_|j|_|j�|j�||_nH|j
tjkr�|dur�d|_n|�|�d	|_|j�|j�ntj�|��dS)
z�Talk to the server and refresh the state of this resumable upload.

        Returns:
          Response if the upload is complete.
        N�PUT�
Content-Rangez	bytes */*)r=�http_methodr�r)�redirectionsr�Tr�)r�rrGrr�r=r�r0r(�_GetRangeHeaderFromResponser�rr��CREATEDr�rXr�r.�seekrWr��RESUME_INCOMPLETE�_Upload__GetLastByterr�r�)r-Zrefresh_requestZrefresh_response�range_headerrrrr�Xs2
��
�z"Upload.RefreshResumableUploadStatecCs|j�d|j�d��S)Nr�r)rr�r�rrrrzsz"Upload._GetRangeHeaderFromResponsecCs�|jdurt�d��|dur.|dur.t�d��|jtkr<dS|pD|j}|dur\|�|j�|_|��tj	|||j
d�}|jtj
kr�tj�|��|j�d�|_|jd}|dur�|�|�}|�||�|jr�|��S|S)z3Initialize this upload from the given http_request.Nz6No upload strategy set; did you call ConfigureRequest?r�)r�zX-Goog-Upload-Chunk-Granularity�location)r�rr�rr0rxr=r>rr�r(r�rr�r�r�rr�r�r@r+r�)r-r�r0r{�
http_responser=rrr�InitializeUpload}s6
�


��

zUpload.InitializeUploadcCs|�d�\}}}t|�S)N�-)rr�)r-r!r�r�rrrZ
__GetLastByte�szUpload.__GetLastBytecCs6|jdurdS|p|j}||jr2t�d|j��dS)Nz0Server requires chunksize to be a multiple of %d)r�r,r�ConfigurationValueError)r-r,rrrZ__ValidateChunksize�s


��zUpload.__ValidateChunksizecCs|jdkp|jtjkp|jS)Ni�)r�r�TOO_MANY_REQUESTS�retry_afterr�rrrZ
__IsRetryable�s


��zUpload.__IsRetryablecs��jtkrt�d��|p�j}|p&�j}�j}��fdd�}��fdd�}|rR|n|}|sj�jrjt�d��|rz���j	���
��j�sH|�j�
��}|jtjtjfvr�d�_�qH|jtjtjtjfv�r�jtks��|�s�tj�|�������||�q�����|���_�jd�j�
�k�r:t�d	�j����||�q��j�r�t�jd
��r��j�
�}	�j� dt!j"��j�
�}
�j� |	�|	|
k�r�t�#dt$|
�t$|	�����||�|S)
z1Helper function for StreamMedia / StreamInChunks.z"Cannot stream non-resumable uploadcs�j|�d�S�Nr�)�_Upload__SendChunk�rN�r�r-rr�
CallSendChunk�s�z+Upload.__StreamMedia.<locals>.CallSendChunkcs�j|�d�Sr))�_Upload__SendMediaBodyr+r,rr�CallSendMediaBody�s�z/Upload.__StreamMedia.<locals>.CallSendMediaBodyz%Cannot gzip encode non-chunked uploadTr�z?Failed to transfer all bytes in chunk, upload paused at byte %drrz7Upload complete with %s additional bytes left in stream)%r�rrrjr_r`r�r��_Upload__ValidateChunksizer,rGr�r.�tellr�rr�rr�rr�_Upload__IsRetryabler�r�r�rPr rr�rW�CommunicationError�hasattrrrf�SEEK_ENDrFr�)r-rOr`r�r�rr-r/Z	send_funcZcurrent_posZend_posrr,rZ
__StreamMedia�sp
�


��
����


��zUpload.__StreamMediacCs|j|||dd�S)a�Send this resumable upload in a single request.

        Args:
          callback: Progress callback function with inputs
              (http_wrapper.Response, transfer.Upload)
          finish_callback: Final callback function with inputs
              (http_wrapper.Response, transfer.Upload)
          additional_headers: Dict of headers to include with the upload
              http_wrapper.Request.

        Returns:
          http_wrapper.Response of final response.
        Fr���_Upload__StreamMediar�rrrr��s�zUpload.StreamMediacCs|j|||d�S)z'Send this (resumable) upload in chunks.)rOr`r�r6r�rrrr�s�zUpload.StreamInChunkscCs^dd�}tj|j||j|j|d�}|jtjkrZ|�|�|��}|d|krZ|j	�
|d�|S)z6Request helper function for SendMediaBody & SendChunk.cSs|durt�d|j��dS)Nz,Request to url %s did not return a response.)r�RequestErrorr�)rrrr�
CheckResponse
s��z0Upload.__SendMediaRequest.<locals>.CheckResponse)r*r��check_response_funcr�)rr�r6r*r(r�rr rr.r)r-r�r�r9rZ	last_byterrrZ__SendMediaRequests
��zUpload.__SendMediaRequestcCs�|��|jdurt�d��t�|j|j|�}tj|j	d|d�}|j
|jd<||jkrdd|j}nd||jd|jf}||jd	<|r�|j�|�|�
||j�S)
z1Send the entire media stream in a single request.Nz*Total size must be known for SendMediaBodyr)r=rr��Content-Type�
bytes */%s�bytes %s-%s/%sr�r)rGrXrrFr�StreamSlicer.rr�r=r�r�r��_Upload__SendMediaRequest)r-rNr��body_streamr��range_stringrrrZ__SendMediaBodys*
��
�
�
zUpload.__SendMediaBodyc
CsP|��|jdu}tj|jdd�}|jrfd|jd<t�|j	|j
�\}}}||}|jdur�|r�||_n`|jdur�t�
|j	||j
�}|j}|jr�||_|�|j
�}n$t||j
|j�}t�|j	||�}||_|j|jd<|r�d|_|jdur�d||d	f}	n*||k�rd
|j}	nd||d	|jf}	|	|jd<|�rD|j�|�|�||�S)
zSend the specified chunk.Nr)r=rr�r�r;r�z
bytes %s-%s/*r�r<r=r)rGrXrr�r=r�r�rr�r.r,r�rZBufferedStreamZstream_end_positionZstream_exhaustedr�r�rr>r�r�r�r�r?)
r-rNr�Zno_log_bodyr�r@Zread_lengthZ	exhaustedr�rArrrZ__SendChunk7sD

�


�


zUpload.__SendChunk)NNFNTNNF)NTF)NTF)NFN)NN)N)NNNT)NNN)NNN)N)N)&rCrQrRrSrurtr1rTrWr�rnror~rr�r�r�r�rUrXr�r�r�r�r�r�rr$r r0r2r7r�r�r?r.r*r�rrrbrr?sv�
��
�$






-*"

	�
B�
�

r)+rS�
__future__r�email.generator�	generatorr�email.mime.multipart�mimer�r�email.mime.nonmultipart�nonmultipartr	r�rrr�rfrLr:�	six.movesr�apitools.base.pyrrrrrr	�__all__r�r
rrrrr�objectr r
r�BytesGeneratorr�rrrrr�<module>s@kq