
    (ph9                     B   S r SSKJr  SSKrSSKrSSKrSSKrSSK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	rS
rSrSrSrSrSrSr\R.                  " \5      rS r\4S jr\4S jrS#S jrS rS rS r S r!S r"S r#S r$S r%S r&S r'S r(S  r) " S! S"\*5      r+g)$z4Shared utilities used by both downloads and uploads.    )absolute_importN)parse_qs)	urlencode)urlsplit)
urlunsplit)commonrangezcontent-rangezcontent-encodingzCurrently using crcmod in pure python form. This is a slow implementation. Python 3 has a faster implementation, `google-crc32c`, which will be used if it is installed.zx-goog-generationzx-goog-hashzx-goog-stored-content-encodingzNo {checksum_type} checksum was returned from the service while downloading {}
(which happens for composite objects), so client-side content integrity
checking is not being performed.c                      g)zSimple default callback.N r       R/var/www/html/venv/lib/python3.13/site-packages/google/resumable_media/_helpers.py
do_nothingr   5       r   c                 b    U" U 5      nX;  a  U" 5         [         R                  " U SU5      eXA   $ )ab  Checks that a specific header is in a headers dictionary.

Args:
    response (object): An HTTP response object, expected to have a
        ``headers`` attribute that is a ``Mapping[str, str]``.
    name (str): The name of a required header.
    get_headers (Callable[Any, Mapping[str, str]]): Helper to get headers
        from an HTTP response.
    callback (Optional[Callable]): A callback that takes no arguments,
        to be executed when an exception is being raised.

Returns:
    str: The desired header.

Raises:
    ~google.resumable_media.common.InvalidResponse: If the header
        is missing.
z$Response headers must contain header)r   InvalidResponse)responsenameget_headerscallbackheaderss        r   header_requiredr   9   s>    & (#G
$$<d
 	
 =r   c                     U" U 5      nXA;  a5  U[         R                  ;  a  U" 5         [         R                  " U SUS/UQ76 eU$ )a,  Require a response has a status code among a list.

Args:
    response (object): The HTTP response object.
    status_codes (tuple): The acceptable status codes.
    get_status_code (Callable[Any, int]): Helper to get a status code
        from a response.
    callback (Optional[Callable]): A callback that takes no arguments,
        to be executed when an exception is being raised.

Returns:
    int: The status code.

Raises:
    ~google.resumable_media.common.InvalidResponse: If the status code
        is not one of the values in ``status_codes``.
zRequest failed with status codezExpected one of)r   	RETRYABLEr   )r   status_codesget_status_coder   status_codes        r   require_status_coder   V   sX    $ "(+K&f...J$$-	

 
 	
 r   c                 X    X -  nX1:  a  Un[         R                  " SS5      nX3SU-  -   4$ )a  Calculate the amount of time to wait before a retry attempt.

Wait time grows exponentially with the number of attempts, until
``max_sleep``.

A random amount of jitter (between 0 and 1 seconds) is added to spread out
retry attempts from different clients.

Args:
    base_wait (float): The "base" wait time (i.e. without any jitter)
        that will be multiplied until it reaches the maximum sleep.
    max_sleep (float): Maximum value that a sleep time is allowed to be.
    multiplier (float): Multiplier to apply to the base wait.

Returns:
    Tuple[float, float]: The new base wait time as well as the wait time
    to be applied (with a random amount of jitter between 0 and 1 seconds
    added).
r   i  gMbP?)randomrandint)	base_wait	max_sleep
multipliernew_base_wait	jitter_mss        r   calculate_retry_waitr&   v   s:    ( *M !q$'I%)*;;;;r   c                       SSK n U R                  5       nU$ ! [         aG     SSKnUR                  R                  S5      n[        5          U$ ! [         a    [        S5      ef = ff = f)zGet crc32c object
Attempt to use the Google-CRC32c package. If it isn't available, try
to use CRCMod. CRCMod might be using a 'slow' varietal. If so, warn...
r   Nzcrc-32cz3Failed to import either `google-crc32c` or `crcmod`)google_crc32cChecksumImportErrorcrcmod
predefinedCrc_is_fast_crcmod)r(   crc_objr+   s      r   _get_crc32c_objectr0      s~    
U((* N  U	U''++I6G
 N  	USTT	UUs    
A))AA%%A)c                      [        S[        5       [        5       S/S5      n [        U SS5      nU(       d  [        R
                  " [        [        SS9  U$ )Nzcrcmod.crcmod_usingExtensionr   F   )
stacklevel)
__import__globalslocalsgetattrwarningswarn_SLOW_CRC32C_WARNINGRuntimeWarning)nested_crcmodfast_crcs     r   r.   r.      sL    			M }&7?H*NqIOr   c                     U S:X  a  gU $ )Nmd5md5Hashr   checksum_types    r   _get_metadata_keyrD      s    r   c                 P    [         R                  " U 5      nUR                  S5      $ )zConvert a checksum object into a digest encoded for an HTTP header.

Args:
    bytes: A checksum digest bytestring.

Returns:
    str: A base64 string representation of the input.
zutf-8)base64	b64encodedecode)digest_bytestringencoded_digests     r   prepare_checksum_digestrK      s&     %%&78N  ))r   c                 |   US;  a  [        S5      eUS;   a  U" U 5      n[        UR                  [        5      XS9nUcC  [        R                  X#R                  5       S9n[        R                  U5        [        5       nXW4$ US:X  a  [        R                  " 5       nXW4$ [        5       n XW4$ Sn[        5       nXW4$ )a  Get the expected checksum and checksum object for the download response.

Args:
    response (~requests.Response): The HTTP response object.
    get_headers (callable: response->dict): returns response headers.
    media_url (str): The URL containing the media to be downloaded.
    checksum_type Optional(str): The checksum type to read from the headers,
        exactly as it will appear in the headers (case-sensitive). Must be
        "md5", "crc32c" or None.

Returns:
    Tuple (Optional[str], object): The expected checksum of the response,
    if it can be detected from the ``X-Goog-Hash`` header, and the
    appropriate checksum object for the expected checksum.
r@   crc32cN4checksum must be ``'md5'``, ``'crc32c'`` or ``None``r@   rN   checksum_labelNrB   r@   )
ValueError_parse_checksum_headerget_HASH_HEADER_MISSING_CHECKSUMformatupper_LOGGERinfo_DoNothingHashhashlibr@   r0   )r   r   	media_urlrC   r   expected_checksummsgchecksum_objects           r   _get_expected_checksumrb      s      33OPP	+	+h'2KK%x
 $#**)<)<)> + C LL,.O // %")++- // #5"6
 // !(*//r   c                     US;  a  [        S5      eUS;   a'  U" U 5      n[        UR                  [        5      XS9nU$ SnU$ )aU  Get the computed checksum and checksum object from the response headers.

Args:
    response (~requests.Response): The HTTP response object.
    get_headers (callable: response->dict): returns response headers.
    checksum_type Optional(str): The checksum type to read from the headers,
        exactly as it will appear in the headers (case-sensitive). Must be
        "md5", "crc32c" or None.

Returns:
    Tuple (Optional[str], object): The checksum of the response,
    if it can be detected from the ``X-Goog-Hash`` header, and the
    appropriate checksum object for the expected checksum.
rM   rO   rP   rQ   N)rS   rT   rU   rV   )r   r   rC   r   remote_checksums        r   #_get_uploaded_checksum_from_headersre      sY     33OPP	+	+h'0KK%x
  r   c                 L   U c  g/ nU R                  S5       H>  nUR                  SS5      u  pVUR                  5       U:X  d  M-  UR                  U5        M@     [        U5      S:X  a  g[        U5      S:X  a  US   $ [        R
                  " USR                  U5      U U5      e)a  Parses the checksum header from an ``X-Goog-Hash`` value.

.. _header reference: https://cloud.google.com/storage/docs/                          xml-api/reference-headers#xgooghash

Expects ``header_value`` (if not :data:`None`) to be in one of the three
following formats:

* ``crc32c=n03x6A==``
* ``md5=Ojk9c3dhfxgoKVVHYwFbHQ==``
* ``crc32c=n03x6A==,md5=Ojk9c3dhfxgoKVVHYwFbHQ==``

See the `header reference`_ for more information.

Args:
    header_value (Optional[str]): The ``X-Goog-Hash`` header from
        a download response.
    response (~requests.Response): The HTTP response object.
    checksum_label (str): The label of the header value to read, as in the
        examples above. Typically "md5" or "crc32c"

Returns:
    Optional[str]: The expected checksum of the response, if it
    can be detected from the ``X-Goog-Hash`` header; otherwise, None.

Raises:
    ~google.resumable_media.common.InvalidResponse: If there are
        multiple checksums of the requested type in ``header_value``.
N,=   r   z.X-Goog-Hash header had multiple ``{}`` values.)splitlstripappendlenr   r   rX   )header_valuer   rR   matcheschecksumr   values          r   rT   rT     s    < G &&s+nnS!,;;=N*NN5!	 , 7|q	W	qz$$<CCNS	
 	
r   c                 v    U S:X  a  [         R                  " 5       $ U S:X  a
  [        5       $ U c  g[        S5      e)zvRespond with a checksum object for a supported type, if not None.

Raises ValueError if checksum_type is unsupported.
r@   rN   NrO   )r]   r@   r0   rS   rB   s    r   _get_checksum_objectrs   G  s>    
 {{}	(	"!##		OPPr   c                 \    U" U 5      nUR                  [        S5      nUc  g[        U5      $ )ae  Parses the generation header from an ``X-Goog-Generation`` value.

Args:
    response (~requests.Response): The HTTP response object.
    get_headers (callable: response->dict): returns response headers.

Returns:
    Optional[long]: The object generation from the response, if it
    can be detected from the ``X-Goog-Generation`` header; otherwise, None.
N)rU   _GENERATION_HEADERint)r   r   r   object_generations       r   _parse_generation_headerrx   V  s5     (#G$6= $%%r   c                     [        U 5      u      pn[        U5      nUR                  SS5      nUc  g[        US   5      $ )zRetrieve the object generation query param specified in the media url.

Args:
    media_url (str): The URL containing the media to be downloaded.

Returns:
    long: The object generation from the media url if exists; otherwise, None.

generationNr   )r   r   rU   rv   )r^   _queryquery_paramsrw   s        r   _get_generation_from_urlr~   j  sM     !+Aq!AE?L$((t< $Q'((r   c                     [        U5      S:X  a  U $ [        U 5      u  p#pEn[        U5      n0 UEUEn[        USS9n[	        X#XEU45      $ )zAdd query parameters to a base url.

Args:
    media_url (str): The URL containing the media to be downloaded.
    query_params (dict): Names and values of the query parameters to add.

Returns:
    str: URL with additional query strings appended.
r   T)doseq)rm   r   r   r   r   )	r^   r}   schemenetlocpathr|   fragparams
new_paramss	            r   add_query_parametersr   ~  s^     <A(0(;%FDe_F+F+l+Jj-EvtD9::r   c                     U" U 5      nUR                  [        5      S:H  =(       a    UR                  [        5      S:g  $ )a  Returns True if the object was served decompressed. This happens when the
"x-goog-stored-content-encoding" header is "gzip" and "content-encoding" header
is not "gzip". See more at: https://cloud.google.com/storage/docs/transcoding#transcoding_and_gzip
Args:
    response (~requests.Response): The HTTP response object.
    get_headers (callable: response->dict): returns response headers.
Returns:
    bool: Returns True if decompressive transcoding has occurred; otherwise, False.
gzip)rU   _STORED_CONTENT_ENCODING_HEADERCONTENT_ENCODING_HEADER)r   r   r   s      r   _is_decompressive_transcodingr     s<     (#G34> 	;KK/0F:r   c                       \ rS rSrSrS rSrg)r\   i  zDo-nothing hash object.

Intended as a stand-in for ``hashlib.md5`` or a crc32c checksum
implementation in cases where it isn't necessary to compute the hash.
c                     g)zDo-nothing ``update`` method.

Intended to match the interface of ``hashlib.md5`` and other checksums.

Args:
    unused_chunk (bytes): A chunk of data.
Nr   )selfunused_chunks     r   update_DoNothingHash.update  r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r\   r\     s    r   r\   )g       @),r   
__future__r   rF   r]   loggingr   r9   urllib.parser   r   r   r   google.resumable_mediar   RANGE_HEADERCONTENT_RANGE_HEADERr   r;   ru   rV   r   rW   	getLoggerr   rZ   r   r   r   r&   r0   r.   rD   rK   rb   re   rT   rs   rx   r~   r   r   objectr\   r   r   r   <module>r      s    ; &      ! " ! # ) & , - 
 ) "B $  

H
%# ;E : KU @<8,*'0T82
jQ&()(;*"V r   