a
    !f*                     @   s|   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 G d	d
 d
eZG dd deZdS )z9Client for interacting with the Google Cloud Storage API.    )_LocalStack)
JSONClient)NotFound)IteratorBatchBucket)
Connectionc                       s   e Zd ZdZeZd fdd	Zedd Zej	dd Zdd	 Z
d
d Zedd Zdd Zdd Zdd Zdd Zdd ZdddZ  ZS )Clienta  Client to bundle configuration needed for API requests.

    :type project: string
    :param project: the project which the client acts on behalf of. Will be
                    passed when creating a topic.  If not passed,
                    falls back to the default inferred from the environment.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for the connection
                        owned by this client. If not passed (and if no ``http``
                        object is passed), falls back to the default inferred
                        from the environment.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests. If not passed, an
                 ``http`` object is created that is bound to the
                 ``credentials`` for the current object.
    Nc                    s(   d | _ tt| j|||d t | _d S )N)projectcredentialshttp)_connectionsuperr   __init__r   _batch_stack)selfr   r   r   	__class__ V/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/storage/client.pyr   2   s
    zClient.__init__c                 C   s   | j dur| j S | jS dS )zGet connection or batch on the client.

        :rtype: :class:`gcloud.storage.connection.Connection`
        :returns: The connection set on the client, or the batch
                  if one is set.
        N)current_batchr   r   r   r   r   
connection8   s    
zClient.connectionc                 C   s   | j durtd|| _ dS )a  Set connection on the client.

        Intended to be used by constructor (since the base class calls)
            self.connection = connection
        Will raise if the connection is set more than once.

        :type value: :class:`gcloud.storage.connection.Connection`
        :param value: The connection set on the client.

        :raises: :class:`ValueError` if connection has already been set.
        Nz Connection already set on client)r   
ValueError)r   valuer   r   r   r   E   s    
c                 C   s   | j | dS )zPush a batch onto our stack.

        "Protected", intended for use by batch context mgrs.

        :type batch: :class:`gcloud.storage.batch.Batch`
        :param batch: newly-active batch
        N)r   push)r   batchr   r   r   _push_batchV   s    zClient._push_batchc                 C   s
   | j  S )a  Pop a batch from our stack.

        "Protected", intended for use by batch context mgrs.

        :raises: IndexError if the stack is empty.
        :rtype: :class:`gcloud.storage.batch.Batch`
        :returns: the top-most batch/transaction, after removing it.
        )r   popr   r   r   r   
_pop_batch`   s    	zClient._pop_batchc                 C   s   | j jS )zCurrently-active batch.

        :rtype: :class:`gcloud.storage.batch.Batch` or ``NoneType`` (if
                no batch is active).
        :returns: The batch at the top of the batch stack.
        )r   topr   r   r   r   r   k   s    zClient.current_batchc                 C   s   t | |dS )a  Factory constructor for bucket object.

        .. note::
          This will not make an HTTP request; it simply instantiates
          a bucket object owned by this client.

        :type bucket_name: string
        :param bucket_name: The name of the bucket to be instantiated.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The bucket object created.
        )clientnamer   r   bucket_namer   r   r   bucketu   s    zClient.bucketc                 C   s
   t | dS )a  Factory constructor for batch object.

        .. note::
          This will not make an HTTP request; it simply instantiates
          a batch object owned by this client.

        :rtype: :class:`gcloud.storage.batch.Batch`
        :returns: The batch object created.
        r#   r   r   r   r   r   r      s    
zClient.batchc                 C   s   t | |d}|j| d |S )a  Get a bucket by name.

        If the bucket isn't found, this will raise a
        :class:`gcloud.storage.exceptions.NotFound`.

        For example::

          >>> try:
          >>>   bucket = client.get_bucket('my-bucket')
          >>> except gcloud.exceptions.NotFound:
          >>>   print 'Sorry, that bucket does not exist!'

        This implements "storage.buckets.get".

        :type bucket_name: string
        :param bucket_name: The name of the bucket to get.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The bucket matching the name provided.
        :raises: :class:`gcloud.exceptions.NotFound`
        r$   r(   )r	   reloadr   r&   r'   r   r   r   
get_bucket   s    zClient.get_bucketc                 C   s&   z|  |W S  ty    Y dS 0 dS )aa  Get a bucket by name, returning None if not found.

        You can use this if you would rather check for a None value
        than catching an exception::

          >>> bucket = client.lookup_bucket('doesnt-exist')
          >>> print bucket
          None
          >>> bucket = client.lookup_bucket('my-bucket')
          >>> print bucket
          <Bucket: my-bucket>

        :type bucket_name: string
        :param bucket_name: The name of the bucket to get.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The bucket matching the name provided or None if not found.
        N)r,   r   r%   r   r   r   lookup_bucket   s    zClient.lookup_bucketc                 C   s   t | |d}|j| d |S )a  Create a new bucket.

        For example::

          >>> bucket = client.create_bucket('my-bucket')
          >>> print bucket
          <Bucket: my-bucket>

        This implements "storage.buckets.insert".

        If the bucket already exists, will raise
        :class:`gcloud.exceptions.Conflict`.

        :type bucket_name: string
        :param bucket_name: The bucket name to create.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The newly created bucket.
        r)   r(   )r	   creater+   r   r   r   create_bucket   s    zClient.create_bucketnoAclc                 C   s`   d| j i}|dur||d< |dur*||d< ||d< |durB||d< t| |d}|dur\||_|S )a  Get all buckets in the project associated to the client.

        This will not populate the list of blobs available in each
        bucket.

          >>> for bucket in client.list_buckets():
          >>>   print bucket

        This implements "storage.buckets.list".

        :type max_results: integer or ``NoneType``
        :param max_results: Optional. Maximum number of buckets to return.

        :type page_token: string or ``NoneType``
        :param page_token: Optional. Opaque marker for the next "page" of
                           buckets. If not passed, will return the first page
                           of buckets.

        :type prefix: string or ``NoneType``
        :param prefix: Optional. Filter results to buckets whose names begin
                       with this prefix.

        :type projection: string or ``NoneType``
        :param projection: If used, must be 'full' or 'noAcl'. Defaults to
                           'noAcl'. Specifies the set of properties to return.

        :type fields: string or ``NoneType``
        :param fields: Selector specifying which fields to include in a
                       partial response. Must be a list of fields. For example
                       to get a partial response with just the next page token
                       and the language of each bucket returned:
                       'items/id,nextPageToken'

        :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
        :returns: All buckets belonging to this project.
        r   NZ
maxResultsprefix
projectionfields)r#   extra_params)r   _BucketIteratorZnext_page_token)r   Zmax_resultsZ
page_tokenr1   r2   r3   r4   resultr   r   r   list_buckets   s    &
zClient.list_buckets)NNN)NNNr0   N)__name__
__module____qualname____doc__r
   Z_connection_classr   propertyr   setterr   r!   r   r'   r   r,   r-   r/   r7   __classcell__r   r   r   r   r      s&   



	  r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )r5   a  An iterator listing all buckets.

    You shouldn't have to use this directly, but instead should use the
    helper methods on :class:`gcloud.storage.connection.Connection`
    objects.

    :type client: :class:`gcloud.storage.client.Client`
    :param client: The client to use for making connections.

    :type extra_params: dict or ``NoneType``
    :param extra_params: Extra query string parameters for the API call.
    Nc                    s   t t| j|d|d d S )Nz/b)r#   pathr4   )r   r5   r   )r   r#   r4   r   r   r   r   $  s    z_BucketIterator.__init__c                 c   s<   | dg D ]*}| d}t| j|}|| |V  qdS )zFactory method which yields :class:`.Bucket` items from a response.

        :type response: dict
        :param response: The JSON API response for a page of buckets.
        itemsr$   N)getr	   r#   Z_set_properties)r   responseitemr$   r'   r   r   r   get_items_from_response(  s
    

z'_BucketIterator.get_items_from_response)N)r8   r9   r:   r;   r   rD   r>   r   r   r   r   r5     s   r5   N)r;   Zgcloud._helpersr   Zgcloud.clientr   Zgcloud.exceptionsr   Zgcloud.iteratorr   Zgcloud.storage.batchr   Zgcloud.storage.bucketr	   Zgcloud.storage.connectionr
   r   r5   r   r   r   r   <module>   s    |