o
    rj@c7                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ ddlmZ ddl	m
Z
mZmZ e eZdd Z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)WaiterDocstring)get_service_module_name   )
xform_name)ClientErrorWaiterConfigErrorWaiterErrorc           	      C   s|   | | }t|j}tt||}dd }t| |jj|jj|dd|_	t
dt|jj| f }t|tfd|i}|| ||S )a  

    :type waiter_name: str
    :param waiter_name: The name of the waiter.  The name should match
        the name (including the casing) of the key name in the waiter
        model file (typically this is CamelCasing).

    :type waiter_model: botocore.waiter.WaiterModel
    :param waiter_model: The model for the waiter configuration.

    :type client: botocore.client.BaseClient
    :param client: The botocore client associated with the service.

    :rtype: botocore.waiter.Waiter
    :return: The waiter object.

    c                 [   s   t j| fi | d S N)Waiterwait)selfkwargs r   3usr/lib/python3.10/site-packages/botocore/waiter.pyr   6   s   z'create_waiter_with_client.<locals>.waitF)waiter_nameZevent_emitterservice_modelZservice_waiter_modelZinclude_signaturez%s.Waiter.%sr   )
get_waiterr   	operationNormalizedOperationMethodgetattrr   metaeventsr   __doc__strr   typer
   )	r   Zwaiter_modelZclientsingle_waiter_configZoperation_nameoperation_methodr   Zwaiter_class_nameZdocumented_waiter_clsr   r   r   create_waiter_with_client   s,   

	r   c                 C   s$   |  d}t|trd|v rdS dS )NErrorCodeTF)get
isinstancedict)responseerrorr   r   r   is_valid_waiter_errorP   s   
r%   c                   @      e Zd Zdd Zdd ZdS )r   c                 C   s
   || _ d S r	   )_client_method)r   Zclient_methodr   r   r   __init__X   s   
z"NormalizedOperationMethod.__init__c              
   K   s>   z	| j di |W S  ty } z	|jW  Y d }~S d }~ww )Nr   )r'   r   r#   )r   r   er   r   r   __call__[   s   z"NormalizedOperationMethod.__call__N)__name__
__module____qualname__r(   r*   r   r   r   r   r   W   s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	WaiterModel   c                 C   s@   |d | _ |dd}| | || _tt|d  | _dS )a  

        Note that the WaiterModel takes ownership of the waiter_config.
        It may or may not mutate the waiter_config.  If this is a concern,
        it is best to make a copy of the waiter config before passing it to
        the WaiterModel.

        :type waiter_config: dict
        :param waiter_config: The loaded waiter config
            from the <service>*.waiters.json file.  This can be
            obtained from a botocore Loader object as well.

        waitersversionunknownN)_waiter_configr    _verify_supported_versionr1   listsortedkeysZwaiter_names)r   Zwaiter_configr1   r   r   r   r(   e   s
   

zWaiterModel.__init__c                 C   s"   || j krtd| j |f dd S )Nz^Unsupported waiter version, supported version must be: %s, but version of waiter config is: %sZ	error_msg)SUPPORTED_VERSIONr   )r   r1   r   r   r   r4   }   s   
z%WaiterModel._verify_supported_versionc                 C   s0   z
| j | }W t|S  ty   td| w )NzWaiter does not exist: %s)r3   KeyError
ValueErrorSingleWaiterConfig)r   r   r   r   r   r   r      s   zWaiterModel.get_waiterN)r+   r,   r-   r9   r(   r4   r   r   r   r   r   r.   b   s
    
r.   c                   @   s$   e Zd ZdZdd Zedd ZdS )r<   zRepresents the waiter configuration for a single waiter.

    A single waiter is considered the configuration for a single
    value associated with a named waiter (i.e TableExists).

    c                 C   s6   || _ |dd| _|d | _|d | _|d | _d S )Ndescription r   delayZmaxAttempts)_configr    r=   r   r?   max_attempts)r   r   r   r   r   r(      s
   

zSingleWaiterConfig.__init__c                 C   s*   g }| j d D ]}t|}|| q|S )N	acceptors)r@   AcceptorConfigappend)r   rB   Zacceptor_configacceptorr   r   r   rB      s
   zSingleWaiterConfig.acceptorsN)r+   r,   r-   r   r(   propertyrB   r   r   r   r   r<      s
    
r<   c                   @   sP   e Zd 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S )rC   c                 C   s8   |d | _ |d | _|d | _|d| _|  | _d S )Nstatematcherexpectedargument)rG   rH   rI   r    rJ   _create_matcher_funcmatcher_func)r   configr   r   r   r(      s
   


zAcceptorConfig.__init__c                 C   s   | j dkrd| j| jS | j dkrd| j| jf S | j dkr'd| j| jf S | j dkr1d| j S | j d	kr;d
| j S d| j  S )Npathz2For expression "{}" we matched expected path: "{}"pathAllz;For expression "%s" all members matched excepted path: "%s"pathAnyz@For expression "%s" we matched expected path: "%s" at least oncestatusz%Matched expected HTTP status code: %sr$   z'Matched expected service error code: %sz,No explanation for unknown waiter type: "%s")rH   formatrJ   rI   r   r   r   r   explanation   s*   








zAcceptorConfig.explanationc                 C   sj   | j dkr	|  S | j dkr|  S | j dkr|  S | j dkr$|  S | j dkr-|  S td| j  d)NrN   rO   rP   rQ   r$   zUnknown acceptor: %sr8   )rH   _create_path_matcher_create_path_all_matcher_create_path_any_matcher_create_status_matcher_create_error_matcherr   rS   r   r   r   rK      s   
	



z#AcceptorConfig._create_matcher_funcc                    $   t | j| j  fdd}|S )Nc                    s   t | rd S |  kS r	   )r%   searchr#   rI   Z
expressionr   r   acceptor_matches   s   z=AcceptorConfig._create_path_matcher.<locals>.acceptor_matchesjmespathcompilerJ   rI   r   r^   r   r]   r   rU      s   z#AcceptorConfig._create_path_matcherc                    rZ   )Nc                    sD   t | rd S | }t|tr|sdS |D ]	}| kr dS qdS NFTr%   r[   r!   r5   r#   resultelementr]   r   r   r^         
zAAcceptorConfig._create_path_all_matcher.<locals>.acceptor_matchesr_   rb   r   r]   r   rV         z'AcceptorConfig._create_path_all_matcherc                    rZ   )Nc                    sD   t | rd S | }t|tr|sdS |D ]	}| kr dS qdS rc   rd   re   r]   r   r   r^     rh   zAAcceptorConfig._create_path_any_matcher.<locals>.acceptor_matchesr_   rb   r   r]   r   rW     ri   z'AcceptorConfig._create_path_any_matcherc                       | j   fdd}|S )Nc                    s   |  di  d}| kS )NZResponseMetadataZHTTPStatusCoder    )r#   Zstatus_coderI   r   r   r^     s   z?AcceptorConfig._create_status_matcher.<locals>.acceptor_matchesrl   rb   r   rl   r   rX        	z%AcceptorConfig._create_status_matcherc                    rj   )Nc                    s   |  di  dd kS )Nr   r   r>   rk   r\   rl   r   r   r^   *  s   z>AcceptorConfig._create_error_matcher.<locals>.acceptor_matchesrl   rb   r   rl   r   rY   '  rm   z$AcceptorConfig._create_error_matcherN)r+   r,   r-   r(   rF   rT   rK   rU   rV   rW   rX   rY   r   r   r   r   rC      s    
rC   c                   @   r&   )r
   c                 C   s   || _ || _|| _dS )a  

        :type name: string
        :param name: The name of the waiter

        :type config: botocore.waiter.SingleWaiterConfig
        :param config: The configuration for the waiter.

        :type operation_method: callable
        :param operation_method: A callable that accepts **kwargs
            and returns a response.  For example, this can be
            a method from a botocore client.

        N)_operation_methodnamerM   )r   ro   rM   r   r   r   r   r(   7  s   
zWaiter.__init__c                 K   s,  t | jj}d}|di }|d| jj}|d| jj}d }d}	 | jdi |}	|d7 }|D ]}
|
|	r?|
}|
j	} nq1t
|	r\t| jd|	d	 d
d|	d	 ddf |	d|dkrgtd d S |dkrxd|
j }t| j||	d||kr|d u rd}nd|
j }t| j||	dt| q#)NZwaitingZWaiterConfigZDelayZMaxAttemptsr   Tr   zAn error occurred (%s): %sr   r   UnknownMessage)ro   reasonZlast_responsesuccessz3Waiting complete, waiter matched the success state.Zfailurez/Waiter encountered a terminal failure state: %szMax attempts exceededz4Max attempts exceeded. Previously accepted state: %sr   )r5   rM   rB   popr    r?   rA   rn   rL   rG   r%   r   ro   loggerdebugrT   timesleep)r   r   rB   Zcurrent_staterM   Zsleep_amountrA   Zlast_matched_acceptorZnum_attemptsr#   rE   rr   r   r   r   r   L  sl   
	
zWaiter.waitN)r+   r,   r-   r(   r   r   r   r   r   r
   6  s    r
   )loggingrw   r`   Zbotocore.docs.docstringr   Zbotocore.utilsr   r>   r   
exceptionsr   r   r   	getLoggerr+   ru   r   r%   r   r.   r<   rC   r
   r   r   r   r   <module>   s    
5- 