ó
P'^c           @   s  d  d l  m  Z  m Z d  d l Z d  d l Z d d l m Z m Z m Z m Z d d l	 m
 Z
 m Z m Z m Z m Z d d d d	 d
 d d d d d d d d d d d d g Z e d  Z d d  Z d   Z y d  d l m Z Wn' e k
 rd	 e f d     YZ n Xy d  d l m Z Wn' e k
 rMd
 e f d     YZ n Xe d    Z e
 e d d d    Z d!   Z d"   Z d e f d#     YZ e d$  Z e
 d%    Z  e  e!  Z" d e" _# d& e" _$ e
 d'    Z% d(   Z& e&   Z' d) e' _$ d*   Z( e
 d+    Z) d S(,   iĸĸĸĸ(   t   datetimet	   timedeltaNi   (   t   mapt   ranget
   raise_fromt   Hashable(   t	   decoratort   wrapst   get_argnamest	   arggettert   contextmanagert   raisert   ignoret   silentt   suppresst   nullcontextt   reraiset   retryt   fallbackt   limit_error_ratet   ErrorRateExceededt   post_processingt
   collectingt   joiningt   oncet   once_pert   once_per_argst	   wrap_withc            s       f d   } | S(   sb   Constructs function that raises the given exception
       with given arguments on any invocation.c             s(     s  r        n   d  S(   N(    (   t   at   kw(   t   argst   exception_or_classt   kwargs(    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt   _raiser   s    (    (   R   R   R    R!   (    (   R   R   R    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR      s    c            s"   t        f d   } | S(   sB   Alters function to ignore given errors, returning default instead.c            s%   t         f d    } | S(   Nc             s*   y  |  |   SWn  k
 r%   SXd  S(   N(    (   R   R    (   t   defaultt   errorst   func(    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt   wrapper$   s    (   R   (   R$   R%   (   R"   R#   (   R$   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   #   s    !(   t   _ensure_exceptable(   R#   R"   R   (    (   R"   R#   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR      s    c         C   s   t  t  |   S(   s)   Alters function to ignore all exceptions.(   R   t	   Exception(   R$   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   -   s    (   R   c           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sķ   Context manager to suppress specified exceptions

        After the exception is suppressed, execution proceeds with the next
        statement following the with statement.
        c         G   s   | |  _  d  S(   N(   t   _exceptions(   t   selft
   exceptions(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt   __init__<   s    c         C   s   d  S(   N(    (   R)   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt	   __enter__?   s    c         C   s   | d  k	 o t | |  j  S(   N(   t   Nonet
   issubclassR(   (   R)   t   exctypet   excinstt   exctb(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt   __exit__B   s    
(   t   __name__t
   __module__t   __doc__R+   R,   R2   (    (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   5   s   		(   R   c           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   sg  Context manager that does no additional processing.

        Used as a stand-in for a normal context manager, when a particular
        block of code is only sometimes used with a normal context manager:

        cm = optional_cm if condition else nullcontext()
        with cm:
            # Perform operation, using optional_cm if condition is True
        c         C   s   | |  _  d  S(   N(   t   enter_result(   R)   R6   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR+   ^   s    c         C   s   |  j  S(   N(   R6   (   R)   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR,   a   s    c         G   s   d  S(   N(    (   R)   t   excinfo(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR2   d   s    N(   R3   R4   R5   R-   R+   R,   R2   (    (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   S   s   		c         c   s<   t  |   }  y	 d VWn  |  k
 r7 } t | |  n Xd S(   s#   Reraises errors as other exception.N(   R&   R   (   R#   t   intot   e(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   h   s
    	i    c         C   sķ   t  |  } xĢ t |  D] } y |    SWq | k
 r­ } | d k pQ | |  sZ   n  | d | k rp   qŪ t |  r | |  n | } | d k rŪ t j |  qŪ q Xq Wd S(   s   Makes decorated function retry up to tries times.
       Retries only on specified errors.
       Sleeps timeout or timeout(attempt) seconds between tries.i   i    N(   R&   R   R-   t   callablet   timet   sleep(   t   callt   triesR#   t   timeoutt   filter_errorst   attemptR9   t   timeout_value(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   r   s    c          G   sd   x] |  D]U } t  |  r% | t f n | \ } } t |  } y |   SWq | k
 r[ q Xq Wd S(   si   Tries several approaches until one works.
       Each approach has a form of (callable, expected_errors).N(   R:   R'   R&   (   t
   approachest   approachR$   t   catch(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR      s    $c         C   s2   t  |  t  o t |  t  } | r( |  St |   S(   sk   Ensures that errors are passable to except clause.
       I.e. should be BaseException subclass or a tuple.(   t
   isinstancet   typeR.   t   BaseExceptiont   tuple(   R#   t   is_exception(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR&      s    c           B   s   e  Z RS(    (   R3   R4   (    (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR      s   c            s:   t   t  r! t d    n      f d   } | S(   s   If function fails to complete fails times in a row,
       calls to it will be intercepted for timeout with exception raised instead.t   secondsc            s:   d   _  d    _ t         f d    } | S(   Ni    c             s    j  r7 t j    j   k  r+    q7 d   _  n  y  |  |   } Wn:  j d 7_  j  k r t j    _  n    n Xd  _ | Sd  S(   Ni   i    (   t   blockedR    t   nowR-   t   fails(   R   R    t   result(   t	   exceptionRN   R$   R?   (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR%   §   s    			(   RN   R-   RL   R   (   R$   R%   (   RP   RN   R?   (   R$   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   Ģ   s    		$(   RF   t   intR   (   RN   R?   RP   R   (    (   RP   RN   R?   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR      s    c         C   s   | |     S(   s3   Post processes decorated function result with func.(    (   R=   R$   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   ŋ   s    s4   Transforms a generator into list returning function.c         C   s   | j  t | j |      S(   s*   Joins decorated function results with sep.(   t   joinR   t	   __class__(   R=   t   sep(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   Č   s    c             s     f d   } | S(   sE   Call function only once for every combination of the given arguments.c            sX   t  j    t    t     t    t          f d    } | S(   Nc             s    | t      f d    D  } t | t  rK   j } } n   j } } | | k r~ | |        SWd  QXd  S(   Nc         3   s!   |  ] }  |     Vq d  S(   N(    (   t   .0t   name(   R   t   get_argR    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pys	   <genexpr>Ü   s    (   RI   RF   R   t   addt   append(   R   R    t   valuest   doneRX   (   t   argnamest	   done_listt   done_setR$   RW   t   lock(   R   R    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR%   Ų   s    "
(   t	   threadingt   Lockt   sett   listR	   R   (   R$   R%   (   R\   (   R]   R^   R$   RW   R_   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   Ō   s    		*(    (   R\   R   (    (   R\   s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   Ð   s    s5   Let function execute once, noop all subsequent calls.c         C   s   t  t |     |   S(   sD   Call function once for every combination of values of its arguments.(   R   R   (   R$   (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   ë   s    c         C   s   |  |    SWd QXd S(   s%   Turn context manager into a decoratorN(    (   R=   t   ctx(    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyR   ð   s    (*   R    R   R;   R`   t   compatR   R   R   R   t
   decoratorsR   R   R   R	   R
   t   __all__R'   R   R-   R   R   t
   contextlibR   t   ImportErrort   objectR   R   R   R   R&   R   R   R   Rc   R   R3   R5   R   R   R   R   R   (    (    (    s,   /tmp/pip-unpacked-wheel-WgnHs6/funcy/flow.pyt   <module>   sJ   "(				
		"						