o
    Zh                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 e
dZe	d ZG dd dee ZG dd	 d	ee ZG d
d dee ZdS )zPagers for the GenAI List APIs.    N)AnyAsyncIterator	AwaitableCallableGenericIteratorLiteralTypeVarT)Z
batch_jobsmodelsZtuning_jobsfilesZcached_contentsc                   @   s   e Zd ZdZdededef dedefddZdededef dedefd	d
Ze	de
e fddZe	defddZe	defddZe	deeef fddZdefddZdedefddZdeddfddZdS )
_BasePagerz9Base pager class for iterating through paginated results.namerequest.responseconfigc                 C   sx   || _ || _t|| j pg | _d| _|si }nt|tr"t|}nt|}t|d|d< || _	|
dt| j| _d S )Nr   Znext_page_token
page_token	page_size)_name_requestgetattr_page_idx
isinstancedictcopydeepcopy_configgetlen
_page_size)selfr   r   r   r   Zrequest_config r"   J/var/www/html/lang_env/lib/python3.10/site-packages/google/genai/pagers.py
_init_page!   s   
z_BasePager._init_pagec                 C   s   |  |||| d S N)r$   r!   r   r   r   r   r"   r"   r#   __init__9   s   z_BasePager.__init__returnc                 C      | j S )a`  Returns a subset of the entire list of items. 

    For the number of items returned, see `pageSize()`.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"first page: {batch_jobs_pager.page}")
      # first page: [BatchJob(name='projects/./locations/./batchPredictionJobs/1
    )r   r!   r"   r"   r#   pageB   s   z_BasePager.pagec                 C   r)   )zReturns the type of paged item (for example, ``batch_jobs``).

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"name: {batch_jobs_pager.name}")
      # name: batch_jobs
    )r   r*   r"   r"   r#   r   S      z_BasePager.namec                 C   r)   )a  Returns the maximum number of items fetched by the pager at one time.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"page_size: {batch_jobs_pager.page_size}")
      # page_size: 5
    )r    r*   r"   r"   r#   r   b   r,   z_BasePager.page_sizec                 C   r)   )a  Returns the configuration when making the API request for the next page.

    A configuration is a set of optional parameters and arguments that can be
    used to customize the API request. For example, the ``page_token`` parameter
    contains the token to request the next page.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"config: {batch_jobs_pager.config}")
      # config: {'page_size': 5, 'page_token': 'AMEw9yO5jnsGnZJLHSKDFHJJu'}
    )r   r*   r"   r"   r#   r   q   s   z_BasePager.configc                 C   s
   t | jS )z6Returns the total number of items in the current page.)r   r+   r*   r"   r"   r#   __len__      
z_BasePager.__len__indexc                 C   s
   | j | S )z$Returns the item at the given index.)r+   )r!   r/   r"   r"   r#   __getitem__   r.   z_BasePager.__getitem__Nc                 C   s   |  | j| j|| j dS )zInitializes the next page from the response.

    This is an internal method that should be called by subclasses after
    fetching the next page.

    Args:
      response: The response object from the API request.
    N)r$   r   r   r   r!   r   r"   r"   r#   _init_next_page   s   	z_BasePager._init_next_page)__name__
__module____qualname____doc__	PagedItemr   r   r$   r'   propertylistr
   r+   r   intr   r   strr   r-   r0   r2   r"   r"   r"   r#   r      s>    



	r   c                   @   sB   e Zd ZdZdefddZdee fddZdee fddZ	d	S )
Pagerz4Pager class for iterating through paginated results.r(   c                 C   sL   | j t| krz|   W n	 ty   tw | j| j  }|  j d7  _ |S )zReturns the next item.   )r   r   	next_page
IndexErrorStopIterationr+   r!   itemr"   r"   r#   __next__   s   zPager.__next__c                 C   
   d| _ | S )z#Returns an iterator over the items.r   r   r*   r"   r"   r#   __iter__      zPager.__iter__c                 C   s2   | j ds
td| j| j d}| | | jS )a  Fetches the next page of items. This makes a new API request.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"current page: {batch_jobs_pager.page}")
      batch_jobs_pager.next_page()
      print(f"next page: {batch_jobs_pager.page}")
      # current page: [BatchJob(name='projects/.../batchPredictionJobs/1
      # next page: [BatchJob(name='projects/.../batchPredictionJobs/6
    r   No more pages to fetch.r   r   r   r?   r   r2   r+   r1   r"   r"   r#   r>      s
   
zPager.next_pageN)
r3   r4   r5   r6   r
   rC   r   rF   r9   r>   r"   r"   r"   r#   r<      s
    r<   c                       sp   e Zd ZdZdededee f dedef fddZd	e	e
 fd
dZd	e
fddZd	ee
 fddZ  ZS )
AsyncPagerz9AsyncPager class for iterating through paginated results.r   r   .r   r   c                    s   t  |||| d S r%   )superr'   r&   	__class__r"   r#   r'      s   zAsyncPager.__init__r(   c                 C   rD   )z)Returns an async iterator over the items.r   rE   r*   r"   r"   r#   	__aiter__   rG   zAsyncPager.__aiter__c                    sT   | j t| krz	|  I dH  W n	 ty   tw | j| j  }|  j d7  _ |S )z%Returns the next item asynchronously.Nr=   )r   r   r>   r?   StopAsyncIterationr+   rA   r"   r"   r#   	__anext__   s   zAsyncPager.__anext__c                    s:   | j dstd| j| j dI dH }| | | jS )aS  Fetches the next page of items asynchronously.

    This makes a new API request.

    Returns:
      The next page of items.

    Raises:
      IndexError: No more pages to fetch.

    Usage:

    .. code-block:: python

      batch_jobs_pager = await client.aio.batches.list(config={'page_size': 5})
      print(f"current page: {batch_jobs_pager.page}")
      await batch_jobs_pager.next_page()
      print(f"next page: {batch_jobs_pager.page}")
      # current page: [BatchJob(name='projects/.../batchPredictionJobs/1
      # next page: [BatchJob(name='projects/.../batchPredictionJobs/6
    r   rH   rI   NrJ   r1   r"   r"   r#   r>      s   
zAsyncPager.next_page)r3   r4   r5   r6   r7   r   r   r   r'   r   r
   rO   rQ   r9   r>   __classcell__r"   r"   rM   r#   rK      s    	rK   )r6   r   typingr   r   r   r   r   r   r   r	   r
   r7   r   r<   rK   r"   r"   r"   r#   <module>   s   (z+