ó
ĤĠ\c           @` s   d  d l  m Z m Z m Z d  d l m Z d d g Z d e f d     YZ d   Z	 e d
 d   Z d   Z d e f d	     YZ d
 S(   i    (   t   absolute_importt   divisiont   print_function(   t   contextmanagert   Callbackt   add_callbacksc           B` sb   e  Z d  Z e   Z d d d d d d  Z e d    Z d   Z	 d   Z
 d   Z d   Z RS(   sc   Base class for using the callback mechanism

    Create a callback with functions of the following signatures:

    >>> def start(dsk):
    ...     pass
    >>> def start_state(dsk, state):
    ...     pass
    >>> def pretask(key, dsk, state):
    ...     pass
    >>> def posttask(key, result, dsk, state, worker_id):
    ...     pass
    >>> def finish(dsk, state, failed):
    ...     pass

    You may then construct a callback object with any number of them

    >>> cb = Callback(pretask=pretask, finish=finish)  # doctest: +SKIP

    And use it either as a context manager over a compute/get call

    >>> with cb:  # doctest: +SKIP
    ...     x.compute()  # doctest: +SKIP

    Or globally with the ``register`` method

    >>> cb.register()  # doctest: +SKIP
    >>> cb.unregister()  # doctest: +SKIP

    Alternatively subclass the ``Callback`` class with your own methods.

    >>> class PrintKeys(Callback):
    ...     def _pretask(self, key, dask, state):
    ...         print("Computing: {0}!".format(repr(key)))

    >>> with PrintKeys():  # doctest: +SKIP
    ...     x.compute()  # doctest: +SKIP
    c         C` s^   | r | |  _  n  | r$ | |  _ n  | r6 | |  _ n  | rH | |  _ n  | rZ | |  _ n  d  S(   N(   t   _startt   _start_statet   _pretaskt	   _posttaskt   _finish(   t   selft   startt   start_statet   pretaskt   posttaskt   finish(    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   __init__1   s    c         ` s/   d d d d d g } t    f d   | D  S(   NR   R   R   R	   R
   c         3` s!   |  ] } t    | d   Vq d  S(   N(   t   getattrt   None(   t   .0t   i(   R   (    s-   lib/python2.7/site-packages/dask/callbacks.pys	   <genexpr>@   s    (   t   tuple(   R   t   fields(    (   R   s-   lib/python2.7/site-packages/dask/callbacks.pyt	   _callback=   s    c         C` s    t  |   |  _ |  j j   |  S(   N(   R   t   _cmt	   __enter__(   R   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR   B   s    c         G` s   |  j  j |   d  S(   N(   R   t   __exit__(   R   t   args(    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR   G   s    c         C` s   t  j j |  j  d  S(   N(   R   t   activet   addR   (   R   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   registerJ   s    c         C` s   t  j j |  j  d  S(   N(   R   R   t   removeR   (   R   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt
   unregisterM   s    N(   t   __name__t
   __module__t   __doc__t   setR   R   R   t   propertyR   R   R   R   R!   (    (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR      s   &				c         C` sS   |  r< g  t  |    D]% } g  | D] } | r  | ^ q  ^ q Sd d d d d g Sd S(   s>   Take an iterable of callbacks, return a list of each callback.N(    (    (    (    (    (   t   zip(   t   cbst   fR   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   unpack_callbacksQ   s    6c         c` sT   |  d k } | r+ t j t   }  t _ n  z |  p7 d VWd | rO |  t _ n  Xd S(   sÌ   Allows callbacks to work with nested schedulers.

    Callbacks will only be used by the first started scheduler they encounter.
    This means that only the outermost scheduler will use global callbacks.N(    (   R   R   R   R%   (   t	   callbackst   global_callbacks(    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   local_callbacksY   s    c         C` s9   t  |  t  r |  j St  |  t  r) |  St d   d S(   s    Normalizes a callback to a tuples.   Callbacks must be either `Callback` or `tuple`N(   t
   isinstanceR   R   R   t	   TypeError(   t   cb(    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   normalize_callbacki   s
    c           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   sÇ  Context manager for callbacks.

    Takes several callbacks and applies them only in the enclosed context.
    Callbacks can either be represented as a ``Callback`` object, or as a tuple
    of length 4.

    Examples
    --------
    >>> def pretask(key, dsk, state):
    ...     print("Now running {0}").format(key)
    >>> callbacks = (None, pretask, None, None)
    >>> with add_callbacks(callbacks):    # doctest: +SKIP
    ...     res.compute()
    c         G` s9   g  | D] } t  |  ^ q |  _ t j j |  j  d  S(   N(   R1   R+   R   R   t   update(   R   R+   t   c(    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR      s    "c         C` s   d  S(   N(    (   R   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR      s    c         C` s(   x! |  j  D] } t j j |  q
 Wd  S(   N(   R+   R   R   t   discard(   R   t   typet   valuet	   tracebackR3   (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR      s    (   R"   R#   R$   R   R   R   (    (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyR   s   s   		N(   t
   __future__R    R   R   t
   contextlibR   t   __all__t   objectR   R*   R   R-   R1   R   (    (    (    s-   lib/python2.7/site-packages/dask/callbacks.pyt   <module>   s   I		
