o
    !d.                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ eeZdZd	Z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G d!d" d"eZdS )#a(  This module has customizations to unify paging parameters.

For any operation that can be paginated, we will:

    * Hide the service specific pagination params.  This can vary across
    services and we're going to replace them with a consistent set of
    arguments.  The arguments will still work, but they are not
    documented.  This allows us to add a pagination config after
    the fact and still remain backwards compatible with users that
    were manually doing pagination.
    * Add a ``--starting-token`` and a ``--max-items`` argument.

    N)partial)
xform_name)DataNotFoundErrorPaginationError)model)BaseCLIArgumenta/  
<p>A token to specify where to start paginating.  This is the
<code>NextToken</code> from a previously truncated response.</p>
<p>For usage examples, see <a
href="https://docs.aws.amazon.com/cli/latest/userguide/pagination.html"
>Pagination</a> in the <i>AWS Command Line Interface User
Guide</i>.</p>
a`  
<p>The total number of items to return in the command's output.
If the total number of items available is more than the value
specified, a <code>NextToken</code> is provided in the command's
output.  To resume pagination, provide the
<code>NextToken</code> value in the <code>starting-token</code>
argument of a subsequent command.  <b>Do not</b> use the
<code>NextToken</code> response element directly outside of the
AWS CLI.</p>
<p>For usage examples, see <a
href="https://docs.aws.amazon.com/cli/latest/userguide/pagination.html"
>Pagination</a> in the <i>AWS Command Line Interface User
Guide</i>.</p>
a  
<p>The size of each page to get in the AWS service call.  This
does not affect the number of items returned in the command's
output.  Setting a smaller page size results in more calls to
the AWS service, retrieving fewer items in each call.  This can
help prevent the AWS service calls from timing out.</p>
<p>For usage examples, see <a
href="https://docs.aws.amazon.com/cli/latest/userguide/pagination.html"
>Pagination</a> in the <i>AWS Command Line Interface User
Guide</i>.</p>
c                 C   s   |  dt | dt d S )Nzbuilding-argument-tablezdoc-description)registerunify_paging_paramsZregister_lastadd_paging_description)Zevent_handlers r   Busr/lib/python3.10/site-packages/awscli/customizations/paginate.pyregister_paginationL   s   r   c                 C   sJ   z|  |}W n
 ty   Y d S w z||}W |S  ty$   Y d S w N)Zget_paginator_modelr   Zget_paginator
ValueError)sessionservice_nameZoperation_nameZpaginator_modelZoperation_paginator_configr   r   r   get_paginator_configQ   s   r   c                 K   s   t | jtjs	d S | jjj}t| j|| jj}|sd S | j	j
  | j	d| j  |drP|d }t|tur<|g}ddd |D }| j	d|  d S d S )Nz``%s`` is a paginated operation. Multiple API calls may be issued in order to retrieve the entire data set of results. You can disable pagination by providing the ``--no-paginate`` argument.Z
result_key, c                 S   s   g | ]}d | qS )z``%s``r   ).0sr   r   r   
<listcomp>s   s    z*add_paging_description.<locals>.<listcomp>zWhen using ``--output text`` and the ``--query`` argument on a paginated response, the ``--query`` argument must extract data from the results of the following query expressions: %s)
isinstanceobjr   ZOperationModelservice_modelr   r   r   namedocstyleZnew_paragraphwritelngettypelistjoin)Zhelp_commandkwargsr   paginator_configZqueriesr   r   r   r
   ^   s2   

r
   c              	   K   s   t ||jj|j}|d u rd S td|j t| | |dd}i }t| dt	dt
ddd| |jj}d}	d	|v ra||d	  }
|
j}	|	t	jvrTtd
|	|j|d	 t| dt	dt|	dd| t| dt	dt|	dd| ||tttt|||  d S )Nz-Modifying paging parameters for operation: %szbuilding-argument-table.zoperation-args-parsed.zstarting-tokenstringZStartingToken)
parse_typeserialized_nameinteger	limit_keyzCUnsupported pagination type {0} for operation {1} and parameter {2}z	page-sizeZPageSizez	max-itemsZMaxItems)r   r   r   r   loggerdebug!_remove_existing_paging_argumentsreplaceadd_paging_argumentPageArgumentSTARTING_TOKEN_HELPZinput_shapemembers	type_nametype_map	TypeErrorformatPAGE_SIZE_HELPMAX_ITEMS_HELPr   r   check_should_enable_paginationr    _get_all_cli_input_tokens)argument_tableZoperation_modelZ
event_namer   r"   r#   Zparsed_args_eventshadowed_argsZinput_membersr1   Zlimit_key_shaper   r   r   r	   {   sn   


r	   c                 C   s    || v r
| | ||< || |< d S r   r   )r9   Zarg_nameZargumentr:   r   r   r   r-      s   r-   c                 K   sv   ddg}| D ]}| dd}t||d ur!||vr!td d|_q|js7t|| | D ]
\}	}
|
||	< q.d S d S )NZstart_token	max_items-_zPUser has specified a manual pagination arg. Automatically setting --no-paginate.F)r,   getattrr)   r*   Zpaginateensure_paging_params_not_setitems)Zinput_tokensr:   r9   parsed_argsZparsed_globalsr"   Znormalized_paging_argstokenpy_namekeyvaluer   r   r   r7      s   

	

r7   c                    s`   g d}dd |  D  fdd|D }t|dkr.ddd |D }td| d	d S )
N)Zstarting_tokenZ	page_sizer;   c                 S   s   g | ]}| d dqS )r<   r=   r,   r   pr   r   r   r      s    z0ensure_paging_params_not_set.<locals>.<listcomp>c                    s$   g | ]}|vrt  |d r|qS r   )r>   rG   rA   Zshadowed_paramsr   r   r      s    r   r   c                 S   s   g | ]
}d | dd qS )--r=   r<   rF   rG   r   r   r   r      s    z@Cannot specify --no-paginate along with pagination arguments: %s)message)keyslenr!   r   )rA   r:   Zpaging_paramsZparams_usedZconverted_paramsr   rI   r   r?      s   r?   c                 C   s   t |D ]}d| | _qd S )NT)r8   Z_UNDOCUMENTED)r9   pagination_configcli_namer   r   r   r+      s   r+   c                 c   sL    t | }|D ]
}t|d}|V  qd| v r$| d }t|d}|V  d S d S )Nr<   r(   )_get_input_tokensr   )rN   tokens
token_namerO   Zkey_namer   r   r   r8      s   


r8   c                 C   s   | d }t |ts|gS |S )NZinput_token)r   r    )rN   rQ   r   r   r   rP      s   
rP   c                 C   s(   | D ]}|j |kr|jd  S qd S )Nr<   )r   rO   lstrip)Zparam_objectsrR   paramr   r   r   _get_cli_name   s
   
rU   c                   @   sn   e Zd ZeeedZdd Ze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S )r.   )r$   r'   longc                 C   s4   t dddi| _|| _|| _|| _|| _d| _d S )Nr.   r   r$   F)r   ZShapeZargument_model_name_serialized_name_documentation_parse_type	_required)selfr   documentationr%   r&   r   r   r   __init__  s   
zPageArgument.__init__c                 C   s
   d| j  S )NrJ   )rW   r\   r   r   r   rO        
zPageArgument.cli_namec                 C      | j S r   )rZ   r_   r   r   r   cli_type_name     zPageArgument.cli_type_namec                 C   ra   r   r[   r_   r   r   r   required  rc   zPageArgument.requiredc                 C   s
   || _ d S r   rd   )r\   rE   r   r   r   re     r`   c                 C   ra   r   )rY   r_   r   r   r   r]     rc   zPageArgument.documentationc                 C   s    |j | j| j| j| j d d S )N)destr   )add_argumentrO   rC   r2   rZ   )r\   parserr   r   r   add_to_parser!  s   

zPageArgument.add_to_parserc                 C   s.   |d ur| di }||| j< ||d< d S d S )NZPaginationConfig)r   rX   )r\   
parametersrE   rN   r   r   r   add_to_params%  s
   
zPageArgument.add_to_paramsN)__name__
__module____qualname__strintr2   r^   propertyrO   rb   re   setterr]   ri   rk   r   r   r   r   r.      s$    




r.   )__doc__logging	functoolsr   Zbotocorer   Zbotocore.exceptionsr   r   r   Zawscli.argumentsr   	getLoggerrl   r)   r/   r6   r5   r   r   r
   r	   r-   r7   r?   r+   r8   rP   rU   r.   r   r   r   r   <module>   s.   
	0