U
    SZf+                     @   sp   d dl Z d dlZd dlZd dlZd dlZdZdd Zdd ZG dd deZ	G d	d
 d
eZ
G dd deZdS )    Ni?c                    s    fdd}|S )Nc                    s
   t |  S N)
isinstance)	exceptionretryable_types ,/tmp/pip-unpacked-wheel-_utclykc/retrying.py_retry_if_exception_these_types   s    zD_retry_if_exception_of_type.<locals>._retry_if_exception_these_typesr   )r   r	   r   r   r   _retry_if_exception_of_type   s    r
   c                     sB   t  dkr,t d r,dd }| d S  fdd}|S dS )z
    Decorator function that instantiates the Retrying object
    @param *dargs: positional arguments passed to Retrying object
    @param **dkw: keyword arguments passed to the Retrying object
       r   c                    s   t   fdd}|S )Nc                     s   t  j f| |S r   Retryingcallargskwfr   r   	wrapped_f+   s    z-retry.<locals>.wrap_simple.<locals>.wrapped_fsixwrapsr   r   r   r   r   wrap_simple*   s    zretry.<locals>.wrap_simplec                    s   t   fdd}|S )Nc                     s   t  jf| |S r   r   r   )dargsdkwr   r   r   r   6   s    z&retry.<locals>.wrap.<locals>.wrapped_fr   r   r   r   r   r   wrap5   s    zretry.<locals>.wrapN)lencallable)r   r   r   r   r   r   r   retry!   s
    r    c                   @   sz   e Zd ZdddZdd Zdd Zed	d
 Zdd Zdd Z	dd Z
dd Zedd Zedd Zdd Zdd ZdS )r   NFc                    s@  |d krdn|| _ |d krdn|| _|d kr0dn|| _|d krBdn|| _|d krTdn|| _|d krfdn|| _|	d krxdn|	| _|d krdn|| _|d krtn|| _	|
d krtn|
| _
|d krdn|| _|| _|| _g  |d k	r | j |d k	r | j |d k	r|| _n&|d kr* fdd| _nt| || _dd g|d k	rV| j |d k	sj|d k	rv| j |d k	s|	d k	r| j |d k	s|d k	r| j |d k	r|| _n&|d krfd	d| _nt| || _|d kr| j| _nt|trt|}|| _|d kr0| j| _n|| _|| _d S )
N   d     r   r   c                    s   t  fddD S )Nc                 3   s   | ]}| V  qd S r   r   .0r   attemptsdelayr   r   	<genexpr>   s    6Retrying.__init__.<locals>.<lambda>.<locals>.<genexpr>)anyr&   )
stop_funcsr&   r   <lambda>   s   z#Retrying.__init__.<locals>.<lambda>c                  _   s   dS )Nr   r   )r   kwargsr   r   r   r-          c                    s   t  fddD S )Nc                 3   s   | ]}| V  qd S r   r   r$   r&   r   r   r)      s    r*   )maxr&   )
wait_funcsr&   r   r-      s   ) _stop_max_attempt_number_stop_max_delay_wait_fixed_wait_random_min_wait_random_max_wait_incrementing_start_wait_incrementing_increment_wait_exponential_multiplierMAX_WAIT_wait_exponential_max_wait_incrementing_max_wait_jitter_max_before_attempts_after_attemptsappendstop_after_attemptstop_after_delaystopgetattrfixed_sleeprandom_sleepincrementing_sleepexponential_sleepwaitalways_reject_retry_on_exceptionr   tupler
   never_reject_retry_on_result_wrap_exception)selfrC   rI   stop_max_attempt_numberstop_max_delay
wait_fixedwait_random_minwait_random_maxwait_incrementing_startwait_incrementing_incrementZwait_incrementing_maxwait_exponential_multiplierwait_exponential_maxretry_on_exceptionretry_on_resultwrap_exception	stop_func	wait_funcwait_jitter_maxZbefore_attemptsZafter_attemptsr   )r,   r1   r   __init__@   sn    









zRetrying.__init__c                 C   s
   || j kS )z;Stop after the previous attempt >= stop_max_attempt_number.)r2   rP   previous_attempt_numberdelay_since_first_attempt_msr   r   r   rA      s    zRetrying.stop_after_attemptc                 C   s
   || j kS )z=Stop after the time from the first attempt >= stop_max_delay.)r3   ra   r   r   r   rB      s    zRetrying.stop_after_delayc                 C   s   dS )z#Don't sleep at all before retrying.r   r   )rb   rc   r   r   r   no_sleep   s    zRetrying.no_sleepc                 C   s   | j S )z0Sleep a fixed amount of time between each retry.)r4   ra   r   r   r   rE      s    zRetrying.fixed_sleepc                 C   s   t | j| jS )zISleep a random amount of time between wait_random_min and wait_random_max)randomrandintr5   r6   ra   r   r   r   rF      s    zRetrying.random_sleepc                 C   s4   | j | j|d   }|| jkr$| j}|dk r0d}|S )z
        Sleep an incremental amount of time after each attempt, starting at
        wait_incrementing_start and incrementing by wait_incrementing_increment
        r   r   )r7   r8   r<   )rP   rb   rc   resultr   r   r   rG      s    
zRetrying.incrementing_sleepc                 C   s2   d| }| j | }|| jkr"| j}|dk r.d}|S )N   r   )r9   r;   )rP   rb   rc   exprg   r   r   r   rH      s    

zRetrying.exponential_sleepc                 C   s   dS )NFr   rg   r   r   r   rM      s    zRetrying.never_rejectc                 C   s   dS )NTr   rj   r   r   r   rJ      s    zRetrying.always_rejectc                 C   s4   d}|j r || |jd O }n|| |jO }|S )NFr   )has_exceptionrK   valuerN   )rP   attemptrejectr   r   r   should_reject   s
    zRetrying.should_rejectc                 O   s  t tt d }d}| jr(| | zt||||d}W n    t }t||d}Y nX | |sv|| j	S | j
r| 
| t tt d | }| ||r| j	s|jr| nt|n<| ||}	| jrt | j }
|	td|
 }	t|	d  |d7 }qd S )Nr#   r   FTr   g     @@)introundtimer>   Attemptsysexc_inforo   getrO   r?   rC   rk   
RetryErrorrI   r=   re   r0   sleep)rP   fnr   r.   
start_timeattempt_numberrm   tbrc   rx   jitterr   r   r   r      s0    




zRetrying.call)NNNNNNNNNNNNNNFNNNNN)__name__
__module____qualname__r`   rA   rB   staticmethodrd   rE   rF   rG   rH   rM   rJ   ro   r   r   r   r   r   r   ?   sF                       
v
	

	r   c                   @   s*   e Zd ZdZdd Zd
ddZdd Zd	S )rs   z
    An Attempt encapsulates a call to a target function that may end as a
    normal return value from the function or an Exception depending on what
    occurred during the execution.
    c                 C   s   || _ || _|| _d S r   )rl   r{   rk   )rP   rl   r{   rk   r   r   r   r`     s    zAttempt.__init__Fc                 C   s@   | j r6|rt| q<t| jd | jd | jd  n| jS dS )z
        Return the return value of this Attempt instance or raise an Exception.
        If wrap_exception is true, this Attempt is wrapped inside of a
        RetryError before being raised.
        r   r   rh   N)rk   rw   r   reraiserl   )rP   r\   r   r   r   rv   #  s
    
"zAttempt.getc              	   C   s:   | j r&d| jdt| jd S d| j| jS d S )NzAttempts: {0}, Error:
{1} rh   zAttempts: {0}, Value: {1})rk   formatr{   join	traceback	format_tbrl   rP   r   r   r   __repr__1  s     zAttempt.__repr__N)F)r~   r   r   __doc__r`   rv   r   r   r   r   r   rs     s   
rs   c                   @   s    e Zd ZdZdd Zdd ZdS )rw   zU
    A RetryError encapsulates the last Attempt instance right before giving up.
    c                 C   s
   || _ d S r   )last_attempt)rP   r   r   r   r   r`   ?  s    zRetryError.__init__c                 C   s   d | jS )NzRetryError[{0}])r   r   r   r   r   r   __str__B  s    zRetryError.__str__N)r~   r   r   r   r`   r   r   r   r   r   rw   :  s   rw   )re   r   rt   rr   r   r:   r
   r    objectr   rs   	Exceptionrw   r   r   r   r   <module>   s    Y#