ó
šßÈ[c           @   sù  d  d l  Z  d  d l Z d  d l Z d  d l m Z d d l m Z d Z d Z d Z	 d Z
 d	 Z d
 Z d Z d Z d Z d Z e e e e e g Z i d e 6d e 6d e 6d e 6d e 6Z e  j d ƒ Z e  j ƒ  Z e j e ƒ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d  e f d! „  ƒ  YZ  d" e f d# „  ƒ  YZ! d$ „  Z" d d% „ Z$ e d& d' ƒ Z% d e
 d( „ Z& d) e f d* „  ƒ  YZ' d+ e f d, „  ƒ  YZ( d S(-   iÿÿÿÿN(   t
   namedtuplei   (   t   zips"   Brian Quinlan (brian@sweetapp.com)t   FIRST_COMPLETEDt   FIRST_EXCEPTIONt   ALL_COMPLETEDt   _AS_COMPLETEDt   PENDINGt   RUNNINGt	   CANCELLEDt   CANCELLED_AND_NOTIFIEDt   FINISHEDt   pendingt   runningt	   cancelledt   finisheds   concurrent.futurest   Errorc           B   s   e  Z d  Z RS(   s-   Base class for all future-related exceptions.(   t   __name__t
   __module__t   __doc__(    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   3   s   t   CancelledErrorc           B   s   e  Z d  Z RS(   s   The Future was cancelled.(   R   R   R   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   8   s   t   TimeoutErrorc           B   s   e  Z d  Z RS(   s*   The operation exceeded the given deadline.(   R   R   R   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   =   s   t   _Waiterc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s;   Provides the event that `wait` and `as_completed` block on.c         C   s   t  j ƒ  |  _ g  |  _ d  S(   N(   t	   threadingt   Eventt   eventt   finished_futures(   t   self(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   __init__D   s    c         C   s   |  j  j | ƒ d  S(   N(   R   t   append(   R   t   future(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt
   add_resultH   s    c         C   s   |  j  j | ƒ d  S(   N(   R   R   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   add_exceptionK   s    c         C   s   |  j  j | ƒ d  S(   N(   R   R   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   add_cancelledN   s    (   R   R   R   R   R   R   R    (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   B   s
   			t   _AsCompletedWaiterc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Used by `as_completed`.c         C   s&   t  t |  ƒ j ƒ  t j ƒ  |  _ d  S(   N(   t   superR!   R   R   t   Lockt   lock(   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   U   s    c         C   s7   |  j  ( t t |  ƒ j | ƒ |  j j ƒ  Wd  QXd  S(   N(   R$   R"   R!   R   R   t   set(   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   Y   s    
c         C   s7   |  j  ( t t |  ƒ j | ƒ |  j j ƒ  Wd  QXd  S(   N(   R$   R"   R!   R   R   R%   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   ^   s    
c         C   s7   |  j  ( t t |  ƒ j | ƒ |  j j ƒ  Wd  QXd  S(   N(   R$   R"   R!   R    R   R%   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR    c   s    
(   R   R   R   R   R   R   R    (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR!   R   s
   			t   _FirstCompletedWaiterc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s,   Used by `wait(return_when=FIRST_COMPLETED)`.c         C   s'   t  t |  ƒ j | ƒ |  j j ƒ  d  S(   N(   R"   R&   R   R   R%   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   l   s    c         C   s'   t  t |  ƒ j | ƒ |  j j ƒ  d  S(   N(   R"   R&   R   R   R%   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   p   s    c         C   s'   t  t |  ƒ j | ƒ |  j j ƒ  d  S(   N(   R"   R&   R    R   R%   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR    t   s    (   R   R   R   R   R   R    (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR&   i   s   		t   _AllCompletedWaiterc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s>   Used by `wait(return_when=FIRST_EXCEPTION and ALL_COMPLETED)`.c         C   s)   | |  _  | |  _ t t |  ƒ j ƒ  d  S(   N(   t   num_pending_callst   stop_on_exceptionR"   R'   R   (   R   R(   R)   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   |   s    		c         C   s,   |  j  t |  j ƒ k r( |  j j ƒ  n  d  S(   N(   R(   t   lenR   R   R%   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   _decrement_pending_calls   s    c         C   s$   t  t |  ƒ j | ƒ |  j ƒ  d  S(   N(   R"   R'   R   R+   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   …   s    c         C   s=   t  t |  ƒ j | ƒ |  j r/ |  j j ƒ  n
 |  j ƒ  d  S(   N(   R"   R'   R   R)   R   R%   R+   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   ‰   s    	c         C   s$   t  t |  ƒ j | ƒ |  j ƒ  d  S(   N(   R"   R'   R    R+   (   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR       s    (   R   R   R   R   R+   R   R   R    (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR'   y   s   				t   _AcquireFuturesc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sO   A context manager that does an ordered acquire of `Future`
    conditions.
    c         C   s   t  | d t ƒ|  _ d  S(   Nt   key(   t   sortedt   idt   futures(   R   R0   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   š   s    c         C   s%   x |  j  D] } | j j ƒ  q
 Wd  S(   N(   R0   t
   _conditiont   acquire(   R   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt	   __enter__   s    c         G   s%   x |  j  D] } | j j ƒ  q
 Wd  S(   N(   R0   R1   t   release(   R   t   argsR   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   __exit__¡   s    (   R   R   R   R   R3   R6   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR,   •   s   		c         C   sÂ   | t  k r t ƒ  } n… | t k r0 t ƒ  } nm t d „  |  Dƒ ƒ } | t k rg t | d t ƒ} n6 | t k rˆ t | d t	 ƒ} n t
 d j | ƒ ƒ ‚ x |  D] } | j j | ƒ q¤ W| S(   Nc         s   s$   |  ] } | j  t t g k Vq d  S(   N(   t   _stateR	   R
   (   t   .0t   f(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pys	   <genexpr>­   s    R)   s   Invalid return condition: {!r}(   R   R!   R   R&   t   sumR   R'   t   TrueR   t   Falset
   ValueErrort   formatt   _waitersR   (   t   fst   return_whent   waitert   pending_countR9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   _create_and_install_waiters¦   s"    			c   	      c   sq  | d k	 r | t j ƒ  } n  t |  ƒ : t d „  |  Dƒ ƒ } t |  ƒ | } t |  t ƒ } Wd QXzá x | D] } | Vqq WxÄ | rF| d k rž d } nC | t j ƒ  } | d k  rá t d j t | ƒ t |  ƒ ƒ ƒ ‚ n  | j	 j
 | ƒ | j $ | j } g  | _ | j	 j ƒ  Wd QXx  | D] } | V| j | ƒ q'Wqƒ WWd x |  D] } | j j | ƒ qRWXd S(   sD  An iterator over the given futures that yields each as it completes.

    Parameters
    ----------
    fs
        The sequence of Futures (possibly created by different Executors) to
        iterate over.

    timeout
        The maximum number of seconds to wait. If None, then there
        is no limit on the wait time.

    Returns
    -------
    An iterator that yields the given Futures as they complete (finished or
    cancelled).

    Raises
    ------
    TimeoutError
        If the entire result iterator could not be generated
        before the given timeout.
    c         s   s*   |  ]  } | j  t t g k r | Vq d  S(   N(   R7   R	   R
   (   R8   R9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pys	   <genexpr>Ü   s    Ni    s   {} (of {}) futures unfinished(   t   Nonet   timeR,   R%   RD   R   R   R>   R*   R   t   waitR$   R   t   cleart   removeR?   (	   R@   t   timeoutt   end_timeR   R   RB   R   t   wait_timeoutR9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   as_completed¿   s8    			
		t   DoneAndNotDoneFuturess   done not_donec         C   s  t  |  ƒ ¶ t d „  |  Dƒ ƒ } t |  ƒ | } | t k rR | rR t | | ƒ S| t k rŠ | rŠ t d „  | Dƒ ƒ rŠ t | | ƒ Sn  t | ƒ t |  ƒ k r¯ t | | ƒ St |  | ƒ } Wd QX| j j	 | ƒ x |  D] } | j
 j | ƒ qÛ W| j | j ƒ t | t |  ƒ | ƒ S(   s(  Wait for the futures in the given sequence to complete.

    Parameters
    ----------
    fs
        The sequence of Futures (possibly created by different Executors) to
        wait upon.

    timeout
        The maximum number of seconds to wait. If None, then there
        is no limit on the wait time.

    return_when
        Indicates when this function should return. The options
        are:

            FIRST_COMPLETED - Return when any future finishes or is
                              cancelled.
            FIRST_EXCEPTION - Return when any future finishes by raising an
                              exception. If no future raises an exception
                              then it is equivalent to ALL_COMPLETED.
            ALL_COMPLETED -   Return when all futures finish or are cancelled.

    Returns
    -------
    A named 2-tuple of sets. The first set, named 'done', contains the
    futures that completed (is finished or cancelled) before the wait
    completed. The second set, named 'not_done', contains uncompleted
    futures.
    c         s   s*   |  ]  } | j  t t g k r | Vq d  S(   N(   R7   R	   R
   (   R8   R9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pys	   <genexpr>#  s    c         s   s4   |  ]* } | j  ƒ  r | j ƒ  d  k	 r | Vq d  S(   N(   R   t	   exceptionRE   (   R8   R9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pys	   <genexpr>*  s    N(   R,   R%   R   RN   R   t   anyR*   RD   R   RG   R?   RI   t   updateR   (   R@   RJ   RA   t   donet   not_doneRB   R9   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyRG     s     t   Futurec           B   s’   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d d
 „ Z d d „ Z d „  Z d „  Z d „  Z RS(   s5   Represents the result of an asynchronous computation.c         C   s@   t  j ƒ  |  _ t |  _ d |  _ d |  _ g  |  _ g  |  _	 d S(   s8   Initializes the future. Should not be called by clients.N(
   R   t	   ConditionR1   R   R7   RE   t   _resultt
   _exceptionR?   t   _done_callbacks(   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   >  s    				c         C   sJ   xC |  j  D]8 } y | |  ƒ Wq
 t k
 rA t j d |  ƒ q
 Xq
 Wd  S(   Ns!   exception calling callback for %r(   RX   t	   Exceptiont   LOGGERRO   (   R   t   callback(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   _invoke_callbacksG  s
    c         C   s°   |  j  ¡ |  j t k rƒ |  j rQ d j t t |  ƒ ƒ t |  j |  j j j	 ƒ Sd j t t |  ƒ ƒ t |  j |  j
 j j	 ƒ Sn  d j t t |  ƒ ƒ t |  j ƒ SWd  QXd  S(   Ns!   <Future at {} state={} raised {}>s#   <Future at {} state={} returned {}>s   <Future at {} state={}>(   R1   R7   R
   RW   R>   t   hexR/   t   _STATE_TO_DESCRIPTION_MAPt	   __class__R   RV   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   __repr__N  s    
	

c         C   sf   |  j  M |  j t t g k r# t S|  j t t g k r< t St |  _ |  j  j ƒ  Wd QX|  j	 ƒ  t S(   s»   Cancel the future if possible.

        Returns True if the future was cancelled, False otherwise. A future
        cannot be cancelled if it is running or has already completed.
        N(
   R1   R7   R   R
   R<   R   R	   R;   t
   notify_allR\   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   cancel^  s    
	
c         C   s'   |  j   |  j t t g k SWd QXd S(   s(   Return True if the future has cancelled.N(   R1   R7   R   R	   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   q  s    
c         C   s!   |  j   |  j t k SWd QXd S(   s1   Return True if the future is currently executing.N(   R1   R7   R   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR   v  s    
c         C   s*   |  j   |  j t t t g k SWd QXd S(   s>   Return True of the future was cancelled or finished executing.N(   R1   R7   R   R	   R
   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyRR   {  s    
c         C   s    |  j  r |  j  ‚ n |  j Sd  S(   N(   RW   RV   (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   __get_result€  s    	c         C   sJ   |  j  1 |  j t t t g k r6 |  j j | ƒ d SWd QX| |  ƒ d S(   s0  Attaches a callable that will be called when the future finishes.

        Parameters
        ----------
        fn
            A callable that will be called with this future as its only
            argument when the future completes or is cancelled. The callable
            will always be called by a thread in the same process in which
            it was added. If the future has already completed or been
            cancelled then the callable will be called immediately. These
            callables are called in the order that they were added.
        N(   R1   R7   R   R	   R
   RX   R   (   R   t   fn(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   add_done_callback†  s    

c         C   s¡   |  j  ’ |  j t t g k r+ t ƒ  ‚ n |  j t k rD |  j ƒ  S|  j  j | ƒ |  j t t g k ru t ƒ  ‚ n" |  j t k rŽ |  j ƒ  St ƒ  ‚ Wd QXd S(   s‰  Return the result of the call that the future represents.

        Parameters
        ----------
        timeout
            The number of seconds to wait for the result if the future
            isn't done. If None, then there is no limit on the wait time.

        Returns
        -------
        The result of the call that the future represents.

        Raises
        ------
        CancelledError
            If the future was cancelled.

        TimeoutError
            If the future didn't finish executing before the given
            timeout.

        Exception
            If the call raised then that exception will be raised.
        N(	   R1   R7   R   R	   R   R
   t   _Future__get_resultRG   R   (   R   RJ   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   resultš  s    


c         C   s›   |  j  Œ |  j t t g k r+ t ƒ  ‚ n |  j t k rA |  j S|  j  j | ƒ |  j t t g k rr t ƒ  ‚ n |  j t k rˆ |  j St ƒ  ‚ Wd QXd S(   sŒ  Return the exception raised by the call that the future represents.

        Parameters
        ----------
        timeout
            The number of seconds to wait for the exception if the
            future isn't done. If None, then there is no limit on the wait
            time.

        Returns
        -------
        The exception raised by the call that the future represents or None
        if the call completed without raising.

        Raises
        ------
        CancelledError
            If the future was cancelled.

        TimeoutError
            If the future didn't finish executing before the given
            timeout.
        N(	   R1   R7   R   R	   R   R
   RW   RG   R   (   R   RJ   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyRO   Â  s    
c         C   s›   |  j  Œ |  j t k rG t |  _ x |  j D] } | j |  ƒ q, Wt S|  j t k rc t |  _ t	 St
 j d t |  j ƒ |  j j ƒ t d ƒ ‚ Wd QXd S(   sØ  Mark the future as running or process any cancel notifications.

        Should only be used by `Executor` implementations and unit tests.

        If the future has been cancelled (`cancel` was called and returned
        True) then any threads waiting on the future completing (though calls
        to `as_completed` or `wait`) are notified and False is returned.

        If the future was not cancelled then it is put in the running state
        (future calls to `running` will return True) and True is returned.

        This method should be called by `Executor` implementations before
        executing the work associated with this future. If this method returns
        False then the work should not be executed.

        Returns
        -------
        False if the Future was cancelled, True otherwise.

        Raises
        ------
        RuntimeError
            if this method was already called or if `set_result`
            or `set_exception` was called.
        s!   Future %s in unexpected state: %ss   Future in unexpected stateN(   R1   R7   R   R	   R?   R    R<   R   R   R;   RZ   t   criticalR/   R   t   RuntimeError(   R   RB   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   set_running_or_notify_cancelë  s    
			c         C   s^   |  j  E | |  _ t |  _ x |  j D] } | j |  ƒ q& W|  j  j ƒ  Wd QX|  j ƒ  d S(   s   Sets the return value of work associated with the future.

        Should only be used by `Executor` implementations and unit tests.
        N(   R1   RV   R
   R7   R?   R   Ra   R\   (   R   Rg   RB   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt
   set_result  s    
		c         C   s^   |  j  E | |  _ t |  _ x |  j D] } | j |  ƒ q& W|  j  j ƒ  Wd QX|  j ƒ  d S(   s   Sets the result of the future as being the given exception.

        Should only be used by `Executor` implementations and unit tests.
        N(   R1   RW   R
   R7   R?   R   Ra   R\   (   R   RO   RB   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   set_exception#  s    
		N(   R   R   R   R   R\   R`   Rb   R   R   RR   Rf   Re   RE   Rg   RO   Rj   Rk   Rl   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyRT   ;  s   										()	+	t   Executorc           B   s>   e  Z d  Z d „  Z d „  Z e d „ Z d „  Z d „  Z RS(   sC   This is an abstract base class for concrete asynchronous executors.c         O   s   t  ƒ  ‚ d S(   s/  Submits a callable to be executed with the given arguments.

        Schedules the callable to be executed as `fn(*args, **kwargs)` and
        returns a `Future` instance representing the execution of the callable.

        Returns
        -------
        A Future representing the given call.
        N(   t   NotImplementedError(   R   Rd   R5   t   kwargs(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   submit4  s    
c   	      o   sÃ   | j  d ƒ } | d k	 r. | t j ƒ  } n  g  t | Œ  D] } |  j | | Œ ^ q; } zG x@ | D]8 } | d k rƒ | j ƒ  Vqc | j | t j ƒ  ƒ Vqc WWd x | D] } | j ƒ  qª WXd S(   sÇ  Returns a iterator equivalent to `map(fn, iter)`.

        Parameters
        ----------
        fn
            A callable that will take take as many arguments as there are
            passed iterables.

        timeout
            The maximum number of seconds to wait. If None, then there
            is no limit on the wait time.

        Returns
        -------
        An iterator equivalent to: `map(func, *iterables)` but the calls may
        be evaluated out-of-order.

        Raises
        ------
        TimeoutError
            If the entire result iterator could not be generated
            before the given timeout.

        Exception
            If `fn(*args)` raises for any values.
        RJ   N(   t   getRE   RF   R   Rp   Rg   Rb   (	   R   Rd   t	   iterablesRo   RJ   RK   R5   R@   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   map@  s    + c         C   s   d S(   s¤  Clean-up the resources associated with the `Executor`.

        It is safe to call this method several times. Otherwise, no other
        methods can be called after this one.

        Parameters
        ----------
        wait
            If True then shutdown will not return until all running
            futures have finished executing and the resources used by the
            executor have been reclaimed.
        N(    (   R   RG   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   shutdownk  s    c         C   s   |  S(   N(    (   R   (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR3   z  s    c         C   s   |  j  d t ƒ t S(   NRG   (   Rt   R;   R<   (   R   t   exc_typet   exc_valt   exc_tb(    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyR6   }  s    (	   R   R   R   Rp   Rs   R;   Rt   R3   R6   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyRm   1  s   		+	()   t   loggingR   RF   t   collectionsR    t   extern.six.movesR   t
   __author__R   R   R   R   R   R   R   R	   R
   t   _FUTURE_STATESR^   t	   getLoggerRZ   t   StreamHandlert   STDERR_HANDLERt
   addHandlerRY   R   R   R   t   objectR   R!   R&   R'   R,   RD   RE   RM   RN   RG   RT   Rm   (    (    (    sA   lib/python2.7/site-packages/astropy/utils/compat/futures/_base.pyt   <module>   sV   	
	@8ö