o
    ?Hhj                     @   s   d dl 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	m
Z
 eeZG dd dZG dd dZG d	d
 d
ZG dd dZG dd dZG dd dZdS )    N)tee)PaginationError)merge_dictsset_value_from_jmespathc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TokenEncoderab  Encodes dictionaries into opaque strings.

    This for the most part json dumps + base64 encoding, but also supports
    having bytes in the dictionary in addition to the types that json can
    handle by default.

    This is intended for use in encoding pagination tokens, which in some
    cases can be complex structures and / or contain bytes.
    c              	   C   s^   zt |}W n ttfy#   | |g \}}||d< t |}Y nw t|ddS )a.  Encodes a dictionary to an opaque string.

        :type token: dict
        :param token: A dictionary containing pagination information,
            particularly the service pagination token(s) but also other boto
            metadata.

        :rtype: str
        :returns: An opaque string
        boto_encoded_keysutf-8)	jsondumps	TypeErrorUnicodeDecodeError_encodebase64	b64encodeencodedecode)selftokenjson_stringencoded_tokenencoded_keys r   Q/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/botocore/paginate.pyr   &   s   zTokenEncoder.encodec                 C   sJ   t |tr| ||S t |tr| ||S t |tr!| ||S |g fS )z@Encode bytes in given data, keeping track of the path traversed.)
isinstancedict_encode_dictlist_encode_listbytes_encode_bytesr   datapathr   r   r   r   C   s   


zTokenEncoder._encodec           
      C   sP   g }g }t |D ]\}}||g }| ||\}}	|| ||	 q||fS )z@Encode any bytes in a list, noting the index of what is encoded.)	enumerater   appendextend)
r   r!   r"   new_dataencodedivaluenew_path	new_valuenew_encodedr   r   r   r   N   s   

zTokenEncoder._encode_listc           
      C   sN   i }g }|  D ]\}}||g }| ||\}}	|||< ||	 q||fS )z@Encode any bytes in a dict, noting the index of what is encoded.)itemsr   r%   )
r   r!   r"   r&   r'   keyr)   r*   r+   r,   r   r   r   r   Y   s   
zTokenEncoder._encode_dictc                 C   s   t |d|gfS )zBase64 encode a byte string.r   )r   r   r   r    r   r   r   r   d   s   zTokenEncoder._encode_bytesN)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r      s    
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TokenDecoderzDecodes token strings back into dictionaries.

    This performs the inverse operation to the TokenEncoder, accepting
    opaque strings and decoding them into a useable form.
    c                 C   sD   t |dd}t|}|dd}|du r|S | ||S )ad  Decodes an opaque string to a dictionary.

        :type token: str
        :param token: A token string given by the botocore pagination
            interface.

        :rtype: dict
        :returns: A dictionary containing pagination information,
            particularly the service pagination token(s) but also other boto
            metadata.
        r   r   N)r   	b64decoder   r   r	   loadspop_decode)r   r   r   decoded_tokenr   r   r   r   r   p   s   
zTokenDecoder.decodec                 C   s8   |D ]}|  ||}t|d}| ||| q|S )z&Find each encoded value and decode it.r   )	_path_getr   r4   r   	_path_set)r   r   r   r.   r'   decodedr   r   r   r7      s
   zTokenDecoder._decodec                 C   s   |}|D ]}|| }q|S )zReturn the nested data at the given path.

        For instance:
            data = {'foo': ['bar', 'baz']}
            path = ['foo', 0]
            ==> 'bar'
        r   )r   r!   r"   dstepr   r   r   r9      s   
zTokenDecoder._path_getc                 C   s$   |  ||dd }|||d < dS )zSet the value of a key in the given data.

        Example:
            data = {'foo': ['bar', 'baz']}
            path = ['foo', 1]
            value = 'bin'
            ==> data = {'foo': ['bar', 'bin']}
        N)r9   )r   r!   r"   r)   	containerr   r   r   r:      s   	zTokenDecoder._path_setN)r/   r0   r1   r2   r   r7   r9   r:   r   r   r   r   r3   i   s    r3   c                   @   s   e Zd Zdd Zdd ZdS )PaginatorModelc                 C   s   |d | _ d S )N
pagination)_paginator_config)r   paginator_configr   r   r   __init__   s   zPaginatorModel.__init__c                 C   s.   z| j | }W |S  ty   td| w )Nz(Paginator for operation does not exist: )rB   KeyError
ValueError)r   operation_namesingle_paginator_configr   r   r   get_paginator   s   zPaginatorModel.get_paginatorN)r/   r0   r1   rD   rI   r   r   r   r   r@      s    r@   c                   @   s   e Zd ZdZdd Zedd Zedd Zejdd Zed	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*PageIteratorzAn iterable object to paginate API results.
    Please note it is NOT a python iterator.
    Use ``iter`` to wrap this as a generator.
    c                 C   sb   || _ || _|| _|| _|| _|| _|| _|	| _|
| _|| _	d | _
|| _i | _t | _t | _d S N)_method_input_token_output_token_more_results_result_keys
_max_items
_limit_key_starting_token
_page_size
_op_kwargs_resume_token_non_aggregate_key_exprs_non_aggregate_partr   _token_encoderr3   _token_decoder)r   methodinput_tokenoutput_tokenmore_resultsresult_keysnon_aggregate_keys	limit_key	max_itemsstarting_token	page_size	op_kwargsr   r   r   rD      s   zPageIterator.__init__c                 C      | j S rK   rP   r   r   r   r   r_         zPageIterator.result_keysc                 C   rf   )z&Token to specify to resume pagination.)rV   rh   r   r   r   resume_token   s   zPageIterator.resume_tokenc                 C   sp   t |tstd| d|v rt| jdg }nt| j}t| }||kr1| j|| _d S td| )NBad starting token: boto_truncate_amount)	r   r   rF   sortedrM   keysrY   r   rV   )r   r)   
token_keys	dict_keysr   r   r   rj      s   

c                 C   rf   rK   )rX   rh   r   r   r   non_aggregate_part   ri   zPageIterator.non_aggregate_partc                 c   s|   | j }d }dd | jD }| jd ur|  d }d}d}| jd }d}| | 	 | |}| |}	|rJ| jd urB| |	||}d}| 	|	 nd}|
|	}
|
d u rWg }
t|
}d}| jd uri|| | j }|dkr{| |	|||| |V  d S |V  ||7 }| |	}tdd | D rd S | jd ur|| jkr|| _d S |d ur||krd| }t|d	| || |}q*)
Nc                 S   s   i | ]}|d qS rK   r   ).0r.   r   r   r   
<dictcomp>   s    z)PageIterator.__iter__.<locals>.<dictcomp>r   TFc                 s   s    | ]}|d u V  qd S rK   r   )rr   tr   r   r   	<genexpr>5  s    z(PageIterator.__iter__.<locals>.<genexpr>z(The same next token was received twice: message)rU   rM   rS   _parse_starting_tokenr_   _inject_starting_params_make_request_extract_parsed_response_handle_first_request _record_non_aggregate_key_valuessearchlenrQ   _truncate_response_get_next_tokenallvaluesrj   r   _inject_token_into_kwargs)r   current_kwargsprevious_next_token
next_tokentotal_itemsfirst_requestprimary_result_keystarting_truncationresponseparsedcurrent_responsenum_current_responsetruncate_amountrw   r   r   r   __iter__   sv   











zPageIterator.__iter__c                 c   s@    t |}| D ]}||}t|tr|E dH  q|V  qdS )a  Applies a JMESPath expression to a paginator

        Each page of results is searched using the provided JMESPath
        expression. If the result is not a list, it is yielded
        directly. If the result is a list, each element in the result
        is yielded individually (essentially implementing a flatmap in
        which the JMESPath search is the mapping function).

        :type expression: str
        :param expression: JMESPath expression to apply to each page.

        :return: Returns an iterator that yields the individual
            elements of applying a JMESPath expression to each page of
            results.
        N)jmespathcompiler~   r   r   )r   
expressioncompiledpageresultsr   r   r   r~   K  s   


zPageIterator.searchc                 C   s   | j di |S )Nr   )rL   )r   r   r   r   r   rz   d  s   zPageIterator._make_requestc                 C   s   |S rK   r   )r   r   r   r   r   r{   g  s   z%PageIterator._extract_parsed_responsec                 C   s2   i }| j D ]}||}t||j| q|| _d S rK   )rW   r~   r   r   rX   )r   r   r`   r   resultr   r   r   r}   j  s   


z-PageIterator._record_non_aggregate_key_valuesc                 C   s@   | j d ur|  d }| || | jd ur| j|| j< d S d S )Nr   )rS   rx   r   rT   rR   )r   re   r   r   r   r   ry   s  s   

z$PageIterator._inject_starting_paramsc                 C   s>   |  D ]\}}|d ur|dkr|||< q||v r||= qd S )NNone)r-   )r   re   r   namer   r   r   r   r     s   
z&PageIterator._inject_token_into_kwargsc           	      C   s   |   d }||}t|ttfr||d  }nd }t||j| | jD ]/}||kr,q%||}t|tr9g }nt|trAd}nt|tt	frKd}nd }t||j| q%|S )N    r   )
rx   r~   r   r   strr   r   r_   intfloat)	r   r   r   r   all_datar!   r   sampleempty_valuer   r   r   r|     s&   




z"PageIterator._handle_first_requestc           	      C   sR   | |}|d u rg }t|| }|d | }t||j| || |d< || _d S )Nrl   )r~   r   r   r   rj   )	r   r   r   r   r   r   originalamount_to_keep	truncatedr   r   r   r     s   

zPageIterator._truncate_responsec                 C   sZ   | j d ur| j |si S i }t| j| jD ]\}}||}|r&|||< qd ||< q|S rK   )rO   r~   ziprN   rM   )r   r   next_tokensr]   	input_keyr   r   r   r   r     s   



zPageIterator._get_next_tokenc                 C   s&   t | t| j}dd t|| jD S )Nc                 S   s   g | ]	\}}t ||qS r   )ResultKeyIterator)rr   r(   
result_keyr   r   r   
<listcomp>  s    z1PageIterator.result_key_iters.<locals>.<listcomp>)r   r   r_   r   )r   teed_resultsr   r   r   result_key_iters  s   
zPageIterator.result_key_itersc                 C   s   i }| D ]P}|}t |trt|dkr|d }| jD ]9}||}|d u r&q||}|d u r7t||j| qt |trB|| qt |t	t
tfrSt||j||  qqt|| j | jd ure| j|d< |S )N   r   	NextToken)r   tupler   r_   r~   r   r   r   r%   r   r   r   r   rq   rj   )r   complete_resultr   r   result_expressionresult_valueexisting_valuer   r   r   build_full_result  s>   





zPageIterator.build_full_resultc              	   C   sz   | j d u rd S | j }z| j|}d}d|v r$|d}|d= W ||fS W ||fS  ttfy<   |  \}}Y ||fS w )Nr   rl   )rS   rZ   r   getrF   r    _parse_starting_token_deprecated)r   r   indexr   r   r   rx     s    

z"PageIterator._parse_starting_tokenc                 C   s   t d| j  | jdu rdS | jd}g }d}t|t| jd kr;zt| }W n ty:   | jg}Y nw |D ]}|dkrI|	d q=|	| q=| 
||fS )z|
        This handles parsing of old style starting tokens, and attempts to
        coerce them into the new style.
        zAAttempting to fall back to old starting token parser. For token: N___r   r   r   )logdebugrS   splitr   rM   r   r6   rF   r$   "_convert_deprecated_starting_token)r   partsr   r   partr   r   r   r     s*   
z-PageIterator._parse_starting_token_deprecatedc                 C   sh   t |}t | j}||krtd| j ||k r,td t|| D ]}|d q$tt	| j|S )zb
        This attempts to convert a deprecated starting token into the new
        style.
        rk   zaOld format starting token does not contain all input tokens. Setting the rest, in order, as None.N)
r   rM   rF   rS   r   r   ranger$   r   r   )r   deprecated_tokenlen_deprecated_tokenlen_input_tokenr(   r   r   r   r   <  s   
z/PageIterator._convert_deprecated_starting_tokenN)r/   r0   r1   r2   rD   propertyr_   rj   setterrq   r   r~   rz   r{   r}   ry   r   r|   r   r   r   r   rx   r   r   r   r   r   r   rJ      s4    



O	 1rJ   c                   @   sd   e Zd Ze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 Zdd Zdd ZdS )	Paginatorc                 C   sj   || _ || _|| _| | j| _| | j| _| | j| _| 	| j| _
| | j| _| | j| _d S rK   )_modelrL   _pagination_cfg_get_output_tokensrN   _get_input_tokensrM   _get_more_results_tokenrO   _get_non_aggregate_keys_non_aggregate_keys_get_result_keysrP   _get_limit_keyrR   )r   r[   pagination_configmodelr   r   r   rD   R  s   zPaginator.__init__c                 C   rf   rK   rg   rh   r   r   r   r_   _  ri   zPaginator.result_keysc                 C   s*   g }| dg D ]
}|t| q|S )Nr`   )r   r$   r   r   )r   configrn   r.   r   r   r   r   c  s   z!Paginator._get_non_aggregate_keysc                 C   s:   g }|d }t |ts|g}|D ]
}|t| q|S )Nr]   )r   r   r$   r   r   )r   r   outputr]   r   r   r   r   i  s   
zPaginator._get_output_tokensc                 C   s   | j d }t|ts|g}|S )Nr\   )r   r   r   )r   r   r\   r   r   r   r   r  s   

zPaginator._get_input_tokensc                 C   s    | d}|d urt|S d S )Nr^   )r   r   r   )r   r   r^   r   r   r   r   x  s   

z!Paginator._get_more_results_tokenc                 C   s8   | d}|d urt|ts|g}dd |D }|S d S )Nr   c                 S   s   g | ]}t |qS r   )r   r   )rr   rkr   r   r   r     s    z.Paginator._get_result_keys.<locals>.<listcomp>)r   r   r   )r   r   r   r   r   r   r   }  s   

zPaginator._get_result_keysc                 C   s
   | dS )Nra   )r   )r   r   r   r   r   r     s   
zPaginator._get_limit_keyc                 K   sB   |  |}| | j| j| j| j| j| j| j|d |d |d |S )zCreate paginator object for an operation.

        This returns an iterable object.  Iterating over
        this object will yield a single page of a response
        at a time.

        MaxItemsStartingTokenPageSize)	_extract_paging_paramsPAGE_ITERATOR_CLSrL   rM   rN   rO   rP   r   rR   )r   kwargspage_paramsr   r   r   paginate  s   
zPaginator.paginatec                 C   s   | di }|dd }|d urt|}|dd }|d urF| jd u r(tdd| jjj}|| j}|jdkrBt	|t
sAt
|}nt|}||dd |dS )	NPaginationConfigr   r   zTPageSize parameter is not supported for the pagination interface for this operation.rv   stringr   )r   r   r   )r6   r   r   rR   r   r   input_shapemembers	type_namer   r   )r   r   r   rb   rd   input_memberslimit_key_shaper   r   r   r     s*   




z Paginator._extract_paging_paramsN)r/   r0   r1   rJ   r   rD   r   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   O  s    
	r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   a  Iterates over the results of paginated responses.

    Each iterator is associated with a single result key.
    Iterating over this object will give you each element in
    the result key list.

    :param pages_iterator: An iterator that will give you
        pages of results (a ``PageIterator`` class).
    :param result_key: The JMESPath expression representing
        the result key.

    c                 C   s   || _ || _d S rK   )_pages_iteratorr   )r   pages_iteratorr   r   r   r   rD     s   
zResultKeyIterator.__init__c                 c   s4    | j D ]}| j|}|d u rg }|E d H  qd S rK   )r   r   r~   )r   r   r   r   r   r   r     s   
zResultKeyIterator.__iter__N)r/   r0   r1   r2   rD   r   r   r   r   r   r     s    r   )r   r	   logging	itertoolsr   r   botocore.exceptionsr   botocore.utilsr   r   	getLoggerr/   r   r   r3   r@   rJ   r   r   r   r   r   r   <module>   s"   
ND   j