a
    !f{                     @   sF   d Z ddlZddlmZ G dd deZdd Zdd	d
Zdd ZdS )zVHelper functions for Cloud Storage utility classes.

These are *not* part of the API.
    N)md5c                   @   s^   e Zd ZdZdddZedd Zedd Zd	d
 ZdddZ	dd Z
dd ZdddZdS )_PropertyMixinzAbstract mixin for cloud storage classes with associated propertties.

    Non-abstract subclasses should implement:
      - client
      - path

    :type name: string
    :param name: The name of the object.
    Nc                 C   s   || _ i | _t | _d S N)name_propertiesset_changes)selfr    r
   X/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/storage/_helpers.py__init__#   s    z_PropertyMixin.__init__c                 C   s   t dS )z$Abstract getter for the object path.NNotImplementedErrorr	   r
   r
   r   path(   s    z_PropertyMixin.pathc                 C   s   t dS )z&Abstract getter for the object client.Nr   r   r
   r
   r   client-   s    z_PropertyMixin.clientc                 C   s   |du r| j }|S )a  Check client or verify over-ride.

        :type client: :class:`gcloud.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:`gcloud.storage.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r   )r	   r   r
   r
   r   _require_client2   s    
z_PropertyMixin._require_clientc                 C   s6   |  |}ddi}|jjd| j|| d}| | dS )a  Reload properties from Cloud Storage.

        :type client: :class:`gcloud.storage.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current object.
        
projectionZnoAclGET)methodr   query_params_target_objectN)r   
connectionapi_requestr   _set_properties)r	   r   r   api_responser
   r
   r   reload@   s    
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: string
        :param name: The field name to update.

        :type value: object
        :param value: The value being updated.
        N)r   addr   )r	   r   valuer
   r
   r   _patch_propertyP   s    z_PropertyMixin._patch_propertyc                 C   s   || _ t | _dS )zSet the properties for the current object.

        :type value: dict or :class:`gcloud.storage.batch._FutureDict`
        :param value: The properties to be set.
        N)r   r   r   r	   r   r
   r
   r   r   b   s    z_PropertyMixin._set_propertiesc                    sL     |}t fdd jD }|jjd j|ddi d} | dS )a[  Sends all changed properties in a PATCH request.

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

        :type client: :class:`gcloud.storage.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current object.
        c                 3   s   | ]}| j | fV  qd S r   )r   ).0keyr   r
   r   	<genexpr>x   s   z'_PropertyMixin.patch.<locals>.<genexpr>PATCHr   full)r   r   datar   r   N)r   dictr   r   r   r   r   )r	   r   Zupdate_propertiesr   r
   r   r   patchl   s    	
z_PropertyMixin.patch)N)N)N)__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r(   r
   r
   r
   r   r      s   





r   c                    s"    fdd} fdd}t ||S )zMCreate a property descriptor around the :class:`_PropertyMixin` helpers.
    c                    s   | j  S )zScalar property getter.)r   getr   	fieldnamer
   r   _getter   s    z!_scalar_property.<locals>._getterc                    s   |   | dS )zScalar property setter.N)r   r    r/   r
   r   _setter   s    z!_scalar_property.<locals>._setter)r-   )r0   r1   r2   r
   r/   r   _scalar_property   s    r3       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: integer
    :param digest_block_size: The block size to write to the hash.
                              Defaults to 8192.
    r   N)readlenupdate)buffer_objecthash_objZdigest_block_sizeblockr
   r
   r   _write_buffer_to_hash   s    

r;   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   r;   digestbase64	b64encode)r8   r9   Zdigest_bytesr
   r
   r   _base64_md5hash   s    

r?   )r4   )	r,   r=   hashlibr   objectr   r3   r;   r?   r
   r
   r
   r   <module>   s   h
