
    Pe7                         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 j                  e      Zd Zd Z G d	 d
      Z G d d      Z G d d      Z G d d      Z G d d      Zy)    N)WaiterDocstring)get_service_module_name   )
xform_name)ClientErrorWaiterConfigErrorWaiterErrorc                    |j                  |       }t        |j                        }t        t	        ||            }d }t        | |j                  j                  |j                  j                  |d      |_	        t        t        |j                  j                        d|       }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                 0    t        j                  | fi | y N)Waiterwait)selfkwargss     1/usr/lib/python3/dist-packages/botocore/waiter.pyr   z'create_waiter_with_client.<locals>.wait6   s    D#F#    F)waiter_nameevent_emitterservice_modelservice_waiter_modelinclude_signaturez.Waiter.r   )
get_waiterr   	operationNormalizedOperationMethodgetattrr   metaeventsr   __doc__strr   typer   )	r   waiter_modelclientsingle_waiter_configoperation_nameoperation_methodr   waiter_class_namedocumented_waiter_clss	            r   create_waiter_with_clientr(      s    $ (22;? 4 > >?N0'$ #kk((kk//)DL "6;;#<#<={	L !!2VI~N !)+; r   c                 P    | j                  d      }t        |t              rd|v ryy)NErrorCodeTF)get
isinstancedict)responseerrors     r   is_valid_waiter_errorr1   P   s&    LL!E%6U?r   c                       e Zd Zd Zd Zy)r   c                     || _         y r   )_client_method)r   client_methods     r   __init__z"NormalizedOperationMethod.__init__X   s
    +r   c                 l    	  | j                   di |S # t        $ r}|j                  cY d }~S d }~ww xY w)N )r4   r   r/   )r   r   es      r   __call__z"NormalizedOperationMethod.__call__[   s7    	&4&&000 	::	s    	3.33N)__name__
__module____qualname__r6   r:   r8   r   r   r   r   W   s    ,r   r   c                   "    e Zd ZdZd Zd Zd Zy)WaiterModel   c                     |d   | _         |j                  dd      }| j                  |       || _        t	        t        |d   j                                     | _        y)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_versionrC   listsortedkeyswaiter_names)r   waiter_configrC   s      r   r6   zWaiterModel.__init__e   sZ     ,I6
  ##Iy9&&w/ i(@(E(E(G!HIr   c                 Z    || j                   k7  rt        d| j                   d|      y )Nz7Unsupported waiter version, supported version must be: z#, but version of waiter config is: 	error_msg)SUPPORTED_VERSIONr   )r   rC   s     r   rF   z%WaiterModel._verify_supported_version}   s5    d,,,# !% 6 6A  -r   c                 n    	 | j                   |   }t        |      S # t        $ r t        d|z        w xY w)NzWaiter does not exist: %s)rE   KeyError
ValueErrorSingleWaiterConfig)r   r   r#   s      r   r   zWaiterModel.get_waiter   sK    	H#'#6#6{#C  ""677  	H8;FGG	Hs    4N)r;   r<   r=   rO   r6   rF   r   r8   r   r   r?   r?   b   s    J08r   r?   c                   &    e Zd ZdZd Zed        Zy)rS   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                 |    || _         |j                  dd      | _        |d   | _        |d   | _        |d   | _        y )Ndescription r   delaymaxAttempts)_configr,   rV   r   rX   max_attempts)r   r#   s     r   r6   zSingleWaiterConfig.__init__   sE    + 033M2F-k:)'2
0?r   c                 j    g }| j                   d   D ]  }t        |      }|j                  |         |S )N	acceptors)rZ   AcceptorConfigappend)r   r]   acceptor_configacceptors       r   r]   zSingleWaiterConfig.acceptors   s?    	#||K8 	'O%o6HX&	' r   N)r;   r<   r=   r   r6   propertyr]   r8   r   r   rS   rS      s!    @  r   rS   c                   F    e Zd Zd Zed        Zd Zd Zd Zd Z	d Z
d Zy	)
r^   c                     |d   | _         |d   | _        |d   | _        |j                  d      | _        | j                         | _        y )Nstatematcherexpectedargument)re   rf   rg   r,   rh   _create_matcher_funcmatcher_func)r   configs     r   r6   zAcceptorConfig.__init__   sF    G_
i(z*

:. 557r   c                    | j                   dk(  r&dj                  | j                  | j                        S | j                   dk(  rd| j                  d| j                  dS | j                   dk(  rd| j                  d| j                  d	S | j                   d
k(  rd| j                  z  S | j                   dk(  rd| j                  z  S d| j                   z  S )Npathz2For expression "{}" we matched expected path: "{}"pathAllzFor expression "z&" all members matched excepted path: ""pathAnyz" we matched expected path: "z" at least oncestatusz%Matched expected HTTP status code: %sr0   z'Matched expected service error code: %sz,No explanation for unknown waiter type: "%s")rf   formatrh   rg   r   s    r   explanationzAcceptorConfig.explanation   s    <<6!GNN  \\Y& ==$--1 \\Y& ==$--1 \\X%:T]]JJ\\W$<t}}LL ?Mr   c                 j   | j                   dk(  r| j                         S | j                   dk(  r| j                         S | j                   dk(  r| j                         S | j                   dk(  r| j	                         S | j                   dk(  r| j                         S t        d| j                   z        )Nrm   rn   rp   rq   r0   zUnknown acceptor: %srM   )rf   _create_path_matcher_create_path_all_matcher_create_path_any_matcher_create_status_matcher_create_error_matcherr   rs   s    r   ri   z#AcceptorConfig._create_matcher_func   s     <<6!,,..\\Y&0022\\Y&0022\\X%..00\\W$--//#04<<? r   c                 l    t        j                  | j                        | j                  fd}|S )Nc                 D    t        |       ry j                  |       k(  S r   )r1   search)r/   rg   
expressions    r   acceptor_matchesz=AcceptorConfig._create_path_matcher.<locals>.acceptor_matches   s$    $X.$$X.(::r   jmespathcompilerh   rg   r   r   rg   r~   s     @@r   rv   z#AcceptorConfig._create_path_matcher   s-    %%dmm4
==	;
  r   c                 l    t        j                  | j                        | j                  fd}|S )Nc                     t        |       ry j                  |       }t        |t              r|sy|D ]	  }|k7  s	 y yNFTr1   r}   r-   rG   r/   resultelementrg   r~   s      r   r   zAAcceptorConfig._create_path_all_matcher.<locals>.acceptor_matches   sN    $X.&&x0Ffd+6
 ! !h& ! r   r   r   s     @@r   rw   z'AcceptorConfig._create_path_all_matcher   s-    %%dmm4
==	  r   c                 l    t        j                  | j                        | j                  fd}|S )Nc                     t        |       ry j                  |       }t        |t              r|sy|D ]	  }|k(  s	 y yr   r   r   s      r   r   zAAcceptorConfig._create_path_any_matcher.<locals>.acceptor_matches  sN    $X.&&x0Ffd+6
 !  h&  r   r   r   s     @@r   rx   z'AcceptorConfig._create_path_any_matcher  s-    %%dmm4
==	  r   c                 *    | j                   fd}|S )Nc                 P    | j                  di       j                  d      }|k(  S )NResponseMetadataHTTPStatusCoder,   )r/   status_coderg   s     r   r   z?AcceptorConfig._create_status_matcher.<locals>.acceptor_matches  s1     #,,'92>BB K (**r   rg   r   r   rg   s     @r   ry   z%AcceptorConfig._create_status_matcher  s    ==	+  r   c                 *    | j                   fd}|S )Nc                 N    | j                  di       j                  dd      k(  S )Nr*   r+   rW   r   )r/   rg   s    r   r   z>AcceptorConfig._create_error_matcher.<locals>.acceptor_matches*  s(     <<,00<HHr   r   r   s     @r   rz   z$AcceptorConfig._create_error_matcher'  s    ==	I  r   N)r;   r<   r=   r6   rb   rt   ri   rv   rw   rx   ry   rz   r8   r   r   r^   r^      s9    8  20	  * *  r   r^   c                       e Zd Zd Zd Zy)r   c                 .    || _         || _        || _        y)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namerk   )r   r   rk   r%   s       r   r6   zWaiter.__init__7  s     "2 	r   c           
      D   t        | j                  j                        }d}|j                  di       }|j	                  d| j                  j
                        }|j	                  d| j                  j                        }d }d}	  | j                  di |}	|dz  }|D ]#  }
|
j                  |	      s|
}|
j                  } nP t        |	      rDt        | j                  d|	d   j	                  d	d
      d|	d   j	                  dd
      |	      |dk(  rt        j                  d       y |dk(  r'd
j                  z  }t        | j                  ||	      ||k\  r,|d}nd
j                  z  }t        | j                  ||	      t!        j"                  |       )NwaitingWaiterConfigDelayMaxAttemptsr   r   zAn error occurred (r*   r+   Unknownz): Message)r   reasonlast_responsesuccessz3Waiting complete, waiter matched the success state.failurez/Waiter encountered a terminal failure state: %szMax attempts exceededz4Max attempts exceeded. Previously accepted state: %sr8   )rG   rk   r]   popr,   rX   r[   r   rj   re   r1   r	   r   loggerdebugrt   timesleep)r   r   r]   current_staterk   sleep_amountr[   last_matched_acceptornum_attemptsr/   ra   r   s               r   r   zWaiter.waitL  s   ../	!NB/zz'4;;+<+<=zz-1I1IJ $-t--77HAL% ((2,4)$,NNM	 )2 &!YY %W-11&)D$W-11)YG 
 '/  	)L 	)J(( "!"* 
 |+(04F O#//1  "!"* 
 JJ|$g r   N)r;   r<   r=   r6   r   r8   r   r   r   r   6  s    *=%r   r   )loggingr   r   botocore.docs.docstringr   botocore.utilsr   rW   r   
exceptionsr   r   r	   	getLoggerr;   r   r(   r1   r   r?   rS   r^   r   r8   r   r   <module>r      st       3 2  C C			8	$2j *8 *8Z 6I  I XS% S%r   