ó
mÜJ]c           @` sã   d  Z  d d l m Z m Z m Z m Z d d l Z e j e ƒ Z	 d d l
 Z
 d d l m Z d d l m Z d d l Z d d l m Z d d l m Z d Z e j d
 „  ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   u$    Internal utils related to Tornado

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   defaultdict(   t   format_exception(   t   geni   (   t   make_idu   yield_for_all_futuresc         c` sb   xL t  rN |  d k r Pn  y t j |  ƒ } Wn t j k
 rC Pq X| V}  q Wt j |  ƒ ‚ d S(   uè    Converts result into a Future by collapsing any futures inside result.

    If result is a Future we yield until it's done, then if the value inside
    the Future is another Future we yield until it's done as well, and so on.
    N(   t   Truet   NoneR   t   convert_yieldedt   BadYieldErrort   Return(   t   resultt   future(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   yield_for_all_futures0   s    	t   _AsyncPeriodicc           B` s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   u  Like ioloop.PeriodicCallback except the 'func' can be async and
        return a Future, and we wait for func to finish each time
        before we call it again.  Plain ioloop.PeriodicCallback
        can "pile up" invocations if they are taking too long.

    c         C` s1   | |  _  | |  _ | |  _ t |  _ t |  _ d  S(   N(   t   _funct   _loopt   _periodt   Falset   _startedt   _stopped(   t   selft   funct   periodt   io_loop(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   __init__X   s
    				c         ` s3   t  j ƒ  ‰  |  j j |  j d ‡  f d †  ƒ ˆ  S(   Ng     @@c           ` s   ˆ  j  d  ƒ S(   N(   t
   set_resultR	   (    (   t   f(    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   <lambda>c   t    (   R   t   FutureR   t
   call_laterR   (   R   (    (   R   s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   sleepa   s    #c         ` sb   ˆ j  r t d ƒ ‚ n  t ˆ _  ‡ f d †  ‰  ‡  ‡ ‡ f d †  ‰ ˆ j j ˆ j ƒ  ˆ ƒ d  S(   Nu&   called start() twice on _AsyncPeriodicc          ` sj   ˆ  j  ƒ  }  ˆ  j ƒ  } | d  k r( |  Sy t j | ƒ } Wn t j k
 rR |  SXt j |  | g ƒ Sd  S(   N(   R"   R   R	   R   R
   R   t   multi(   t   sleep_futureR   t   callback_future(   R   (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   invokek   s    c         ` sš   ˆ j  s" ˆ j j ˆ  ƒ  ˆ ƒ n  |  j ƒ  } | d  k	 r– t j d ƒ t j re t	 |  j
 ƒ  Œ  } n t	 | j | | j ƒ } t j d j | ƒ ƒ n  d  S(   Nu$   Error thrown from periodic callback:u    (   R   R   t
   add_futuret	   exceptionR	   t   logt   errort   sixt   PY2R   t   exc_infot	   __class__t   __traceback__t   join(   R   t   ext   lines(   R&   t   on_doneR   (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR3      s    		(   R   t   RuntimeErrorR   R   R'   R"   (   R   (    (   R&   R3   R   s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   startf   s    		c         C` s   t  |  _ d  S(   N(   R   R   (   R   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   stop   s    (   t   __name__t
   __module__t   __doc__R   R"   R5   R6   (    (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR   P   s
   				't   _CallbackGroupc           B` s}   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 d d „ Z d	 „  Z d d
 „ Z d „  Z RS(   u`    A collection of callbacks added to a Tornado IOLoop that we may
    want to remove as a group. c         C` s   | d  k r t d ƒ ‚ n  | |  _ i  |  _ i  |  _ i  |  _ t j ƒ  |  _ t	 t
 ƒ |  _ t	 t
 ƒ |  _ t	 t
 ƒ |  _ d  S(   Nu   must provide an io loop(   R	   t
   ValueErrorR   t   _next_tick_callback_removerst   _timeout_callback_removerst   _periodic_callback_removerst	   threadingt   Lockt   _removers_lockR   t   sett   _next_tick_removers_by_callablet   _timeout_removers_by_callablet   _periodic_removers_by_callable(   R   R   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR   ”   s    				c         C` s‹   x* t  |  j j ƒ  ƒ D] } |  j | ƒ q Wx* t  |  j j ƒ  ƒ D] } |  j | ƒ qC Wx* t  |  j j ƒ  ƒ D] } |  j | ƒ qp Wd S(   u"    Removes all registered callbacks.N(   t   listR<   t   keyst   remove_next_tick_callbackR=   t   remove_timeout_callbackR>   t   remove_periodic_callback(   R   t   cb_id(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   remove_all_callbacks¤   s    c         C` sU   | |  j  k r |  j S| |  j k r, |  j S| |  j k rB |  j St d | ƒ ‚ d  S(   Nu   Unhandled removers(   R<   RC   R=   RD   R>   RE   R4   (   R   t   removers(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   _get_removers_ids_by_callable­   s    c         C` sU   |  j  F | d  k r" t ƒ  } n | | k r= t d ƒ ‚ n  | | | <| SWd  QXd  S(   Nu?   A callback of the same type has already been added with this ID(   RA   R	   R   R;   (   R   t   callbackt   callback_idRM   t   remover(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   _assign_remover·   s    

c      
   C` s¸   y |  j  ~ | j | ƒ } xg t |  j | ƒ j ƒ  ƒ D]J \ } } y* | j | ƒ | sm |  j | ƒ | =n  Wq8 t k
 r q8 Xq8 WWd  QXWn t k
 r¬ t d ƒ ‚ n X| ƒ  d  S(   Nu:   Removing a callback twice (or after it's already been run)(   RA   t   popRF   RN   t   itemst   removet   KeyErrorR;   (   R   RP   RM   RQ   t   cbt   cb_ids(    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   _execute_removerÀ   s    
(c         ` s_   ‡  ‡ ‡ ‡ f d †  ‰ t  ˆ _ ‡ f d †  } ˆ j ˆ  ˆ ˆ j | ƒ ‰ ˆ j j ˆ ƒ ˆ S(   us    Adds a callback to be run on the next tick.
        Returns an ID that can be used with remove_next_tick_callback.c          ` s+   ˆ j  s# ˆ j ˆ ƒ ˆ  |  | Ž  Sd  Sd  S(   N(   t   removedRH   R	   (   t   argst   kwargs(   RO   RP   R   t   wrapper(    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR]   Ò   s    	c           ` s   t  ˆ  _ d  S(   N(   R   RZ   (    (   R]   (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyRQ   ß   s    (   R   RZ   RR   R<   R   t   add_callback(   R   RO   RP   RQ   (    (   RO   RP   R   R]   s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   add_next_tick_callbackÏ   s    	c         C` s   |  j  | |  j ƒ d S(   u6    Removes a callback added with add_next_tick_callback.N(   RY   R<   (   R   RP   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyRH   æ   s    c         ` se   ‡  ‡ ‡ f d †  } d ‰ ‡ ‡ f d †  } ˆ j ˆ  ˆ ˆ j | ƒ ‰ ˆ j j | d | ƒ ‰ ˆ S(   u€    Adds a callback to be run once after timeout_milliseconds.
        Returns an ID that can be used with remove_timeout_callback.c          ` s   ˆ j  ˆ ƒ ˆ  |  | Ž  S(   N(   RI   (   R[   R\   (   RO   RP   R   (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR]   í   s    c           ` s#   ˆ  d  k	 r ˆ j j ˆ  ƒ n  d  S(   N(   R	   R   t   remove_timeout(    (   t   handleR   (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyRQ   ó   s    g     @@N(   R	   RR   R=   R   R!   (   R   RO   t   timeout_millisecondsRP   R]   RQ   (    (   RO   RP   Ra   R   s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   add_timeout_callbackê   s    c         C` s   |  j  | |  j ƒ d S(   uD    Removes a callback added with add_timeout_callback, before it runs.N(   RY   R=   (   R   RP   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyRI   û   s    c         C` sD   t  | | d |  j ƒ} |  j | | |  j | j ƒ } | j ƒ  | S(   u    Adds a callback to be run every period_milliseconds until it is removed.
        Returns an ID that can be used with remove_periodic_callback.R   (   R   R   RR   R>   R6   R5   (   R   RO   t   period_millisecondsRP   RW   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   add_periodic_callbackÿ   s    
c         C` s   |  j  | |  j ƒ d S(   u5    Removes a callback added with add_periodic_callback.N(   RY   R>   (   R   RP   (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyRJ     s    N(   R7   R8   R9   R	   R   RL   RN   RR   RY   R_   RH   Rc   RI   Re   RJ   (    (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyR:      s   			
						(   u   yield_for_all_futures(   R9   t
   __future__R    R   R   R   t   loggingt	   getLoggerR7   R)   R?   t   collectionsR   t	   tracebackR   R+   t   tornadoR   t   util.serializationR   t   __all__t	   coroutineR   t   objectR   R:   (    (    (    s1   lib/python2.7/site-packages/bokeh/util/tornado.pyt   <module>	   s   " @