o
    .&ßaÊ.  ã                   @   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 paramters.

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_paramsÚregister_lastÚadd_paging_description)Úevent_handlers© r   ú@/usr/lib/python3/dist-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)Úget_paginator_modelr   Úget_paginatorÚ
ValueError)ÚsessionÚservice_nameÚoperation_nameÚpaginator_modelÚ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.Ú
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>zµWhen 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   ÚOperationModelÚservice_modelr   r   r   ÚnameÚdocÚstyleÚnew_paragraphÚwritelnÚgetÚtypeÚlistÚjoin)Úhelp_commandÚkwargsr   Úpaginator_configÚ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ÚstringÚStartingToken)Ú
parse_typeÚserialized_nameÚintegerÚ	limit_keyzCUnsupported pagination type {0} for operation {1} and parameter {2}z	page-sizeÚPageSizez	max-itemsÚMaxItems)r   r"   r   r#   ÚloggerÚdebugÚ!_remove_existing_paging_argumentsÚreplaceÚadd_paging_argumentÚPageArgumentÚSTARTING_TOKEN_HELPÚ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_tableÚoperation_modelÚ
event_namer   r-   r.   Úparsed_args_eventÚshadowed_argsÚinput_membersrA   Úlimit_key_shaper   r   r   r	   {   sn   þÿ
ÿþü
þþþüþü
þþr	   c                 C   s    || v r
| | ||< || |< d S r   r   )rI   Úarg_nameÚargumentrM   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 )NÚstart_tokenÚ	max_itemsú-Ú_zPUser has specified a manual pagination arg. Automatically setting --no-paginate.F)r;   Úgetattrr8   r9   ÚpaginateÚensure_paging_params_not_setÚitems)Úinput_tokensrM   rI   Úparsed_argsÚparsed_globalsr-   Únormalized_paging_argsÚtokenÚpy_nameÚkeyÚvaluer   r   r   rG   ¶   s   
€
	
õ
rG   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)Ústarting_tokenÚ	page_sizerS   c                 S   s   g | ]}|  d d¡‘qS )rT   rU   ©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   )rV   re   ©r[   Úshadowed_paramsr   r   r   Ô   s    ÿr   r   c                 S   s   g | ]
}d |  dd¡ ‘qS )ú--rU   rT   rd   re   r   r   r   r   Ù   s    z@Cannot specify --no-paginate along with pagination arguments: %s)Úmessage)ÚkeysÚlenr+   r   )r[   rM   Úpaging_paramsÚparams_usedÚconverted_paramsr   rg   r   rX   Ñ   s   ÿÿÿýrX   c                 C   s   t |ƒD ]}d| | _qd S )NT)rH   Ú_UNDOCUMENTED)rI   Ú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 )NrT   r5   )Ú_get_input_tokensr   )rq   ÚtokensÚ
token_namerr   Úkey_namer   r   r   rH   ä   s   €


ýrH   c                 C   s   | d }t |tƒs|gS |S )NÚinput_token)r   r*   )rq   rt   r   r   r   rs   ñ   s   
rs   c                 C   s(   | D ]}|j |kr|j d¡  S qd S )NrT   )r#   rr   Úlstrip)Úparam_objectsru   Úparamr   r   r   Ú_get_cli_nameø   s
   
ÿÿr{   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=   )r0   r4   Úlongc                 C   s4   t  dddi¡| _|| _|| _|| _|| _d| _d S )Nr=   r)   r0   F)r   ÚShapeÚargument_modelÚ_nameÚ_serialized_nameÚ_documentationÚ_parse_typeÚ	_required)Úselfr#   Údocumentationr2   r3   r   r   r   Ú__init__  s   
zPageArgument.__init__c                 C   s
   d| j  S )Nri   )r   ©r„   r   r   r   rr     ó   
zPageArgument.cli_namec                 C   ó   | j S r   )r‚   r‡   r   r   r   Úcli_type_name  ó   zPageArgument.cli_type_namec                 C   r‰   r   ©rƒ   r‡   r   r   r   Úrequired  r‹   zPageArgument.requiredc                 C   s
   || _ d S r   rŒ   )r„   ra   r   r   r   r�     rˆ   c                 C   r‰   r   )r�   r‡   r   r   r   r…     r‹   zPageArgument.documentationc                 C   s    |j | j| j| j| j d� d S )N)Údestr)   )Úadd_argumentrr   r_   rB   r‚   )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 )NÚPaginationConfig)r(   r€   )r„   Ú
parametersra   rq   r   r   r   Úadd_to_params%  s
   
ýzPageArgument.add_to_paramsN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚintrB   r†   Úpropertyrr   rŠ   r�   Úsetterr…   r‘   r”   r   r   r   r   r=   þ   s$    ý




r=   )Ú__doc__ÚloggingÚ	functoolsr   Úbotocorer   Úbotocore.exceptionsr   r   r   Úawscli.argumentsr   Ú	getLoggerr•   r8   r>   rF   rE   r   r   r   r	   r<   rG   rX   r:   rH   rs   r{   r=   r   r   r   r   Ú<module>   s.   
	0