a
    !f                     @   s(   d Z G dd deZG dd deZdS )aU  Iterators for paging through API responses.

These iterators simplify the process of paging through API responses
where the response is a list of results with a ``nextPageToken``.

To make an iterator work, just override the ``get_items_from_response``
method so that given a response (containing a page of results) it parses
those results into an iterable of the actual objects you want::

  class MyIterator(Iterator):
      def get_items_from_response(self, response):
          items = response.get('items', [])
          for item in items:
              my_item = MyItemClass(other_arg=True)
              my_item._set_properties(item)
              yield my_item

You then can use this to get **all** the results from a resource::

    >>> iterator = MyIterator(...)
    >>> list(iterator)  # Convert to a list (consumes all values).

Or you can walk your way through items and call off the search early if
you find what you're looking for (resulting in possibly fewer
requests)::

    >>> for item in MyIterator(...):
    >>>     print item.name
    >>>     if not item.is_valid:
    >>>         break
c                   @   sX   e Zd ZdZdZeegZdddZdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )Iteratora  A generic class for iterating through Cloud JSON APIs list responses.

    :type client: :class:`gcloud.client.Client`
    :param client: The client, which owns a connection to make requests.

    :type path: string
    :param path: The path to query for the list of items.

    :type extra_params: dict or None
    :param extra_params: Extra query string parameters for the API call.
    Z	pageTokenNc                 C   sD   || _ || _d| _d | _|pi | _| j| j}|r@td|fd S )N    zUsing a reserved parameter)clientpathpage_numbernext_page_tokenextra_paramsRESERVED_PARAMSintersection
ValueError)selfr   r   r   Zreserved_in_use r   P/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/iterator.py__init__@   s    
zIterator.__init__c                 c   s,   |   r(|  }| |D ]
}|V  qq dS )z"Iterate through the list of items.N)has_next_pageget_next_page_responseget_items_from_response)r   responseitemr   r   r   __iter__L   s    zIterator.__iter__c                 C   s   | j dkrdS | jduS )zDetermines whether or not this iterator has more pages.

        :rtype: boolean
        :returns: Whether the iterator has more pages or not.
        r   TNr   r   r   r   r   r   r   S   s    
zIterator.has_next_pagec                 C   s&   | j r| j| j ini }|| j |S )zGetter for query parameters for the next request.

        :rtype: dict
        :returns: A dictionary of query parameters.
        )r   
PAGE_TOKENupdater   )r   resultr   r   r   get_query_params^   s    zIterator.get_query_paramsc                 C   sH   |   std| jjjd| j|  d}|  jd7  _|d| _	|S )zRequests the next page from the path provided.

        :rtype: dict
        :returns: The parsed JSON response of the next page's contents.
        z*No more pages. Try resetting the iterator.GET)methodr   Zquery_params   ZnextPageToken)
r   RuntimeErrorr   
connectionZapi_requestr   r   r   getr   r   r   r   r   r   r   i   s    zIterator.get_next_page_responsec                 C   s   d| _ d| _dS )z%Resets the iterator to the beginning.r   Nr   r   r   r   r   resetz   s    zIterator.resetc                 C   s   t dS )a  Factory method called while iterating. This should be overriden.

        This method should be overridden by a subclass.  It should
        accept the API response of a request for the next page of items,
        and return a list (or other iterable) of items.

        Typically this method will construct a Bucket or a Blob from the
        page of results in the response.

        :type response: dict
        :param response: The response of asking for the next page of items.
        N)NotImplementedErrorr!   r   r   r   r      s    z Iterator.get_items_from_response)N)__name__
__module____qualname____doc__r   	frozensetr   r   r   r   r   r   r"   r   r   r   r   r   r   0   s   

r   c                   @   s"   e Zd ZdZdddZdd ZdS )MethodIteratora  Method-based iterator iterating through Cloud JSON APIs list responses.

    :type method: instance method
    :param method: ``list_foo`` method of a domain object, taking as arguments
                   ``page_token``, ``page_size``, and optional additional
                   keyword arguments.

    :type page_token: string or ``NoneType``
    :param page_token: Initial page token to pass.  if ``None``, fetch the
                       first page from the ``method`` API call.

    :type page_size: integer or ``NoneType``
    :param page_size: Maximum number of items to return from the ``method``
                      API call; if ``None``, uses the default for the API.

    :type max_calls: integer or ``NoneType``
    :param max_calls: Maximum number of times to make the ``method``
                      API call; if ``None``, applies no limit.

    :type kw: dict
    :param kw: optional keyword argments to be passed to ``method``.
    Nc                 K   s(   || _ || _|| _|| _|| _d| _d S )Nr   )_method_token
_page_size_kw
_max_calls	_page_num)r   r   
page_token	page_sizeZ	max_callskwr   r   r   r      s    zMethodIterator.__init__c                 c   sl   | j d u s| j| j k rh| jf | j| jd| j\}}|D ]
}|V  q:|d u rRd S |  jd7  _|| _q d S )N)r0   r1   r   )r.   r/   r*   r+   r,   r-   )r   itemsZ	new_tokenr   r   r   r   r      s    
zMethodIterator.__iter__)NNN)r$   r%   r&   r'   r   r   r   r   r   r   r)      s
     
	r)   N)r'   objectr   r)   r   r   r   r   <module>   s   !_