U
    $FZhkZ                     @   s`  d Z ddlZddlZddlmZ ddl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ZdZdZdZdZejedZdZeedZdZdZejjZej j!Z"dd Z#dd Z$dd Z%d:ddZ&dd Z'd d! Z(d"d# Z)G d$d% d%e*Z+d&d' Z,d;d)d*Z-d+d, Z.d-d. Z/d/d0 Z0d1d2 Z1d<d3d4Z2d5d6 Z3d=d8d9Z4dS )>zVHelper functions for Cloud Storage utility classes.

These are *not* part of the API.
    N)md5)urlsplit)
urlunsplit)uuid4)environment_vars)_DEFAULT_TIMEOUT)DEFAULT_RETRY))DEFAULT_RETRY_IF_METAGENERATION_SPECIFIEDZSTORAGE_EMULATOR_HOSTZAPI_ENDPOINT_OVERRIDEZAPI_VERSION_OVERRIDEzgoogleapis.comzstorage.{universe_domain})Zuniverse_domainzhttps://Zv1))if_etag_matchzIf-Match)if_etag_not_matchzIf-None-Match))if_generation_matchZifGenerationMatch)if_generation_not_matchZifGenerationNotMatch)if_metageneration_matchZifMetagenerationMatch)if_metageneration_not_matchZifMetagenerationNotMatch)Zif_source_generation_matchZifSourceGenerationMatch)Zif_source_generation_not_matchZifSourceGenerationNotMatch)Zif_source_metageneration_matchZifSourceMetagenerationMatch)Z"if_source_metageneration_not_matchZifSourceMetagenerationNotMatchc                   C   s   t jtd S N)osenvirongetSTORAGE_EMULATOR_ENV_VAR r   r   d/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/google/cloud/storage/_helpers.py_get_storage_emulator_overrideQ   s    r   c                   C   s   t ttt S r   )r   getenv_API_ENDPOINT_OVERRIDE_ENV_VAR_DEFAULT_SCHEME_TRUE_DEFAULT_STORAGE_HOSTr   r   r   r   _get_default_storage_base_urlU   s     r   c                   C   s   t  tt krt  S dS )zIThis is an experimental configuration variable. Use api_endpoint instead.N)r   r   r   r   r   r   r   _get_api_endpoint_override[   s    r   Fc                 C   s8   t | }| d|j }t|j||r(dndddf}|S )zReturns the scheme and netloc sections of the url, with the bucket
    prepended to the netloc.

    Not intended for use with netlocs which include a username and password.
    ./ )r   netlocr   scheme)urlZbucketZtrailing_slash
parsed_urlZ
new_netlocbase_urlr   r   r   _virtual_hosted_style_base_urlb   s    r&   c                   C   s   t ddkS )NZ!GOOGLE_API_USE_CLIENT_CERTIFICATEtrue)r   r   r   r   r   r   _use_client_certp   s    r(   c                   C   s   t tjt tjS r   )r   r   r   ZPROJECTZLEGACY_PROJECTr   r   r   r   _get_environ_projectt   s    
r)   c                 C   s4   | dkrdS t | d  | d  gs0td| S )zPre-flight ``Bucket`` name validation.

    :type name: str or :data:`NoneType`
    :param name: Proposed bucket name.

    :rtype: str or :data:`NoneType`
    :returns: ``name`` if valid.
    Nr   z8Bucket names must start and end with a number or letter.)allisalnum
ValueError)namer   r   r   _validate_name{   s
    	r/   c                   @   s   e Zd ZdZdddZedd Zedd Zed	d
 Zdd Z	dd Z
edd ZddddddddeedfddZdd Zdd ZdddddeedfddZdddddeedfddZdS )_PropertyMixina1  Abstract mixin for cloud storage classes with associated properties.

    Non-abstract subclasses should implement:
      - path
      - client
      - user_project

    :type name: str
    :param name: The name of the object. Bucket names must start and end with a
                 number or letter.
    Nc                 C   s   || _ i | _t | _d S r   )r.   _propertiesset_changes)selfr.   r   r   r   __init__   s    z_PropertyMixin.__init__c                 C   s   t dS )z$Abstract getter for the object path.NNotImplementedErrorr4   r   r   r   path   s    z_PropertyMixin.pathc                 C   s   t dS )z&Abstract getter for the object client.Nr6   r8   r   r   r   client   s    z_PropertyMixin.clientc                 C   s   t dS )z,Abstract getter for the object user_project.Nr6   r8   r   r   r   user_project   s    z_PropertyMixin.user_projectc                 C   s   |dkr| j }|S )a  Check client or verify over-ride.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current object.

        :rtype: :class:`google.cloud.storage.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r:   )r4   r:   r   r   r   _require_client   s    z_PropertyMixin._require_clientc                 C   s   i S )a@  Return any encryption headers needed to fetch the object.

        .. note::
           Defined here because :meth:`reload` calls it, but this method is
           really only relevant for :class:`~google.cloud.storage.blob.Blob`.

        :rtype: dict
        :returns: a mapping of encryption-related headers.
        r   r8   r   r   r   _encryption_headers   s    
z"_PropertyMixin._encryption_headersc                 C   s   i }| j dk	r| j |d< |S )zDefault query parameters.NZuserProject)r;   )r4   paramsr   r   r   _query_params   s    

z_PropertyMixin._query_paramsZnoAclc                 C   s   |  |}| j}||d< t|||||d |dk	rD||d< | j|d< |  }t|||d |j| j|||	|
| d}| | dS )a  Reload properties from Cloud Storage.

        If :attr:`user_project` is set, bills the API request to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: the client to use. If not passed, falls back to the
                       ``client`` stored on the current object.

        :type projection: str
        :param projection: (Optional) If used, must be 'full' or 'noAcl'.
                           Defaults to ``'noAcl'``. Specifies the set of
                           properties to return.

        :type if_etag_match: Union[str, Set[str]]
        :param if_etag_match: (Optional) See :ref:`using-if-etag-match`

        :type if_etag_not_match: Union[str, Set[str]])
        :param if_etag_not_match: (Optional) See :ref:`using-if-etag-not-match`

        :type if_generation_match: long
        :param if_generation_match:
            (Optional) See :ref:`using-if-generation-match`

        :type if_generation_not_match: long
        :param if_generation_not_match:
            (Optional) See :ref:`using-if-generation-not-match`

        :type if_metageneration_match: long
        :param if_metageneration_match:
            (Optional) See :ref:`using-if-metageneration-match`

        :type if_metageneration_not_match: long
        :param if_metageneration_not_match:
            (Optional) See :ref:`using-if-metageneration-not-match`

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :type soft_deleted: bool
        :param soft_deleted:
            (Optional) If True, looks for a soft-deleted object. Will only return
            the object metadata if the object exists and is in a soft-deleted state.
            :attr:`generation` is required to be set on the blob if ``soft_deleted`` is set to True.
            See: https://cloud.google.com/storage/docs/soft-delete
        
projectionr   r   r   r   NZsoftDeleted
generation)r
   r   )query_paramsheaderstimeoutretry_target_object)	r<   r?    _add_generation_match_parametersrB   r=   _add_etag_match_headersZ_get_resourcer9   _set_properties)r4   r:   r@   r
   r   r   r   r   r   rE   rF   Zsoft_deletedrC   rD   api_responser   r   r   reload   s8    B

  z_PropertyMixin.reloadc                 C   s   | j | || j|< dS )a  Update field of this object's properties.

        This method will only update the field provided and will not
        touch the other fields.

        It **will not** reload the properties from the server. The behavior is
        local only and syncing occurs via :meth:`patch`.

        :type name: str
        :param name: The field name to update.

        :type value: object
        :param value: The value being updated.
        N)r3   addr1   )r4   r.   valuer   r   r   _patch_property2  s    z_PropertyMixin._patch_propertyc                 C   s   || _ t | _dS )zSet the properties for the current object.

        :type value: dict or :class:`google.cloud.storage.batch._FutureDict`
        :param value: The properties to be set.
        N)r1   r2   r3   r4   rN   r   r   r   rJ   D  s    z_PropertyMixin._set_propertiesFc	                    sp     |} j}	d|	d< |r$||	d< t|	||||d  fdd jD }
|j j|
|	 ||d} | dS )	aj  Sends all changed properties in a PATCH request.

        Updates the ``_properties`` with the response from the backend.

        If :attr:`user_project` is set, bills the API request to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: the client to use. If not passed, falls back to the
                       ``client`` stored on the current object.

        :type if_generation_match: long
        :param if_generation_match:
            (Optional) See :ref:`using-if-generation-match`

        :type if_generation_not_match: long
        :param if_generation_not_match:
            (Optional) See :ref:`using-if-generation-not-match`

        :type if_metageneration_match: long
        :param if_metageneration_match:
            (Optional) See :ref:`using-if-metageneration-match`

        :type if_metageneration_not_match: long
        :param if_metageneration_not_match:
            (Optional) See :ref:`using-if-metageneration-not-match`

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :type override_unlocked_retention: bool
        :param override_unlocked_retention:
            (Optional) override_unlocked_retention must be set to True if the operation includes
            a retention property that changes the mode from Unlocked to Locked, reduces the
            retainUntilTime, or removes the retention configuration from the object. See:
            https://cloud.google.com/storage/docs/json_api/v1/objects/patch
        fullr@   overrideUnlockedRetentionrA   c                    s   i | ]}| j | qS r   )r1   ).0keyr8   r   r   
<dictcomp>  s      z(_PropertyMixin.patch.<locals>.<dictcomp>)rC   rG   rE   rF   N)r<   r?   rH   r3   Z_patch_resourcer9   rJ   )r4   r:   r   r   r   r   rE   rF   override_unlocked_retentionrC   Zupdate_propertiesrK   r   r8   r   patchN  s,    6
z_PropertyMixin.patchc	                 C   s^   |  |}| j}	d|	d< |r$||	d< t|	||||d |j| j| j|	||| d}
| |
 dS )a`  Sends all properties in a PUT request.

        Updates the ``_properties`` with the response from the backend.

        If :attr:`user_project` is set, bills the API request to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: the client to use. If not passed, falls back to the
                       ``client`` stored on the current object.

        :type if_generation_match: long
        :param if_generation_match:
            (Optional) See :ref:`using-if-generation-match`

        :type if_generation_not_match: long
        :param if_generation_not_match:
            (Optional) See :ref:`using-if-generation-not-match`

        :type if_metageneration_match: long
        :param if_metageneration_match:
            (Optional) See :ref:`using-if-metageneration-match`

        :type if_metageneration_not_match: long
        :param if_metageneration_not_match:
            (Optional) See :ref:`using-if-metageneration-not-match`

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :type override_unlocked_retention: bool
        :param override_unlocked_retention:
            (Optional) override_unlocked_retention must be set to True if the operation includes
            a retention property that changes the mode from Unlocked to Locked, reduces the
            retainUntilTime, or removes the retention configuration from the object. See:
            https://cloud.google.com/storage/docs/json_api/v1/objects/patch
        rQ   r@   rR   rA   )rC   rE   rF   rG   N)r<   r?   rH   Z_put_resourcer9   r1   rJ   )r4   r:   r   r   r   r   rE   rF   rV   rC   rK   r   r   r   update  s*    6
z_PropertyMixin.update)N)__name__
__module____qualname____doc__r5   propertyr9   r:   r;   r<   r=   r?   r   r   rL   rO   rJ   rW   r	   rX   r   r   r   r   r0      sX   




	
a
Sr0   c                    s"    fdd} fdd}t ||S )zHCreate a property descriptor around the :class:`_PropertyMixin` helpers.c                    s   | j  S )zScalar property getter.)r1   r   r8   	fieldnamer   r   _getter  s    z!_scalar_property.<locals>._getterc                    s   |   | dS )zScalar property setter.N)rO   rP   r^   r   r   _setter  s    z!_scalar_property.<locals>._setter)r]   )r_   r`   ra   r   r^   r   _scalar_property  s    rb       c                 C   s0   |  |}t|dkr,|| |  |}q
dS )a  Read blocks from a buffer and update a hash with them.

    :type buffer_object: bytes buffer
    :param buffer_object: Buffer containing bytes used to update a hash object.

    :type hash_obj: object that implements update
    :param hash_obj: A hash object (MD5 or CRC32-C).

    :type digest_block_size: int
    :param digest_block_size: The block size to write to the hash.
                              Defaults to 8192.
    r   N)readlenrX   )buffer_objecthash_objZdigest_block_sizeblockr   r   r   _write_buffer_to_hash  s    

ri   c                 C   s"   t  }t| | | }t|S )a  Get MD5 hash of bytes (as base64).

    :type buffer_object: bytes buffer
    :param buffer_object: Buffer containing bytes used to compute an MD5
                          hash (as base64).

    :rtype: str
    :returns: A base64 encoded digest of the MD5 hash.
    )r   ri   digestbase64	b64encode)rf   rg   Zdigest_bytesr   r   r   _base64_md5hash  s    

rm   c                 K   sB   t D ]8\}}||}|dk	rt|tr.|g}d|| |< qdS )zAdd generation match parameters into the given parameters list.

    :type headers: dict
    :param headers: Headers dict.

    :type match_parameters: dict
    :param match_parameters: if*etag*match parameters to add.
    N, )_ETAG_MATCH_PARAMETERSr   
isinstancestrjoin)rD   match_parameterssnakecase_nameheader_namerN   r   r   r   rI   !  s    	

rI   c                 K   sZ   t D ]P\}}||}|dk	rt| tr8| ||f qt| trL|| |< qtdqdS )ab  Add generation match parameters into the given parameters list.

    :type parameters: list or dict
    :param parameters: Parameters list or dict.

    :type match_parameters: dict
    :param match_parameters: if*generation*match parameters to add.

    :raises: :exc:`ValueError` if ``parameters`` is not a ``list()``
             or a ``dict()``.
    Nz5`parameters` argument should be a dict() or a list().)_GENERATION_MATCH_PARAMETERSr   rp   listappenddictr-   )
parametersrs   rt   Zcamelcase_namerN   r   r   r   rH   3  s    



rH   c                  K   sb   t dd |  D dkr^dd |  D }d|dd }|d }d	| d
| }t|dS )zRaise ``ValueError`` exception if more than one parameter was set.

    :type error: :exc:`ValueError`
    :param error: Description of which fields were set

    :raises: :class:`~ValueError` containing the fields that were set
    c                 s   s   | ]}|d k	V  qd S r   r   )rS   argr   r   r   	<genexpr>W  s     z._raise_if_more_than_one_set.<locals>.<genexpr>   c                 S   s   g | ]}d | d qS )'r   )rS   r.   r   r   r   
<listcomp>X  s     z/_raise_if_more_than_one_set.<locals>.<listcomp>rn   Nr*   zPass at most one of z and )sumvalueskeysrr   r-   )kwargsZescaped_keysZkeys_but_lastlast_keymsgr   r   r   _raise_if_more_than_one_setO  s    r   c                 C   s&   t | }|jr|jr| S | d|  S )a!  Helper to build bucket bound hostname URL.

    :type host: str
    :param host: Host name.

    :type scheme: str
    :param scheme: (Optional) Web scheme. If passed, use it
                   as a scheme in the result URL.

    :rtype: str
    :returns: A bucket bound hostname URL.
    z://)r   r"   r!   )hostr"   Z	url_partsr   r   r   _bucket_bound_hostname_urlb  s    r   c                   C   s   dt t  S )Nzgccl-invocation-id/)rq   r   r   r   r   r   _get_invocation_idv  s    r   application/json; charset=UTF-8c                 C   s8   |  dt   }|r"|d| 7 }dd| |||p2|dS )a  Get the headers for a request.

    :type user_agent: str
    :param user_agent: The user-agent for requests.

    :type command: str
    :param command:
        (Optional) Information about which interface for the operation was
        used, to be included in the X-Goog-API-Client header. Please leave
        as None unless otherwise directed.

    :rtype: dict
    :returns: The headers to be used for the request.
     z gccl-gcs-cmd/zapplication/jsonzgzip, deflate)AcceptzAccept-Encodingz
User-AgentzX-Goog-API-Clientzcontent-typezx-upload-content-type)r   )
user_agentcontent_typeZx_upload_content_typecommandZx_goog_api_clientr   r   r   _get_default_headersz  s    r   )F)rc   )N)r   NN)5r\   rk   datetimehashlibr   r   urllib.parser   r   uuidr   Zgoogle.authr   Zgoogle.cloud.storage.constantsr   Zgoogle.cloud.storage.retryr   r	   r   r   Z_API_VERSION_OVERRIDE_ENV_VARZ_DEFAULT_UNIVERSE_DOMAINZ_STORAGE_HOST_TEMPLATEformatr   r   r   Z_API_VERSIONro   rv   nowZ_NOWtimezoneutcZ_UTCr   r   r   r&   r(   r)   r/   objectr0   rb   ri   rm   rI   rH   r   r   r   r   r   r   r   r   <module>   s^   
  c

   