ó
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
 m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l m Z m Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   uA    Provides the Application, Server, and Session context classes.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   geni   (   t   ServerSessioni   (   t   ServerContextt   SessionContext(   t   Document(   t   ProtocolError(   t   _CallbackGroupt   yield_for_all_futuresu   ApplicationContextu   BokehServerContextu   BokehSessionContextt   BokehServerContextc           B` s_   e  Z d  „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   c         C` s"   | |  _  t |  j  j ƒ |  _ d  S(   N(   t   application_contextR
   t   io_loopt
   _callbacks(   t   selfR   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   __init__;   s    	c         C` s   |  j  j ƒ  d  S(   N(   R   t   remove_all_callbacks(   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   _remove_all_callbacks?   s    c         C` s.   g  } x! |  j  j D] } | j | ƒ q W| S(   N(   R   t   sessionst   append(   R   t   resultt   session(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   B   s    c         C` s   |  j  j | ƒ S(   N(   R   t   add_next_tick_callback(   R   t   callback(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   I   s    c         C` s   |  j  j | ƒ d  S(   N(   R   t   remove_next_tick_callback(   R   t   callback_id(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   L   s    c         C` s   |  j  j | | ƒ S(   N(   R   t   add_timeout_callback(   R   R   t   timeout_milliseconds(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   O   s    c         C` s   |  j  j | ƒ d  S(   N(   R   t   remove_timeout_callback(   R   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   R   s    c         C` s   |  j  j | | ƒ S(   N(   R   t   add_periodic_callback(   R   R   t   period_milliseconds(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   U   s    c         C` s   |  j  j | ƒ d  S(   N(   R   t   remove_periodic_callback(   R   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR!   X   s    (   t   __name__t
   __module__R   R   t   propertyR   R   R   R   R   R   R!   (    (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   :   s   							t   BokehSessionContextc           B` sY   e  Z d  „  Z d „  Z e j d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 RS(   c         C` s8   | |  _  d  |  _ t t |  ƒ j | | ƒ d  |  _ d  S(   N(   t	   _documentt   Nonet   _sessiont   superR%   R   t   _request(   R   t
   session_idt   server_contextt   document(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   \   s
    		c         C` s   | |  _  d  S(   N(   R(   (   R   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   _set_sessiond   s    c         c` s@   |  j  d  k r& t | |  j ƒ ƒ Vn |  j  j | |  j ƒ d  S(   N(   R(   R'   R   R&   t   with_document_locked(   R   t   func(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   with_locked_documentg   s    c         C` s!   |  j  d  k r t S|  j  j Sd  S(   N(   R(   R'   t   Falset	   destroyed(   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR3   p   s    c         C` s   |  j  S(   N(   R*   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   requestx   s    c         C` s   |  j  S(   N(   R(   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   |   s    (   R"   R#   R   R.   R   t	   coroutineR1   R$   R3   R4   R   (    (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR%   [   s   			t   ApplicationContextc           B` s¼   e  Z d  Z d d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 d „  Z d „  Z e j d d	 „ ƒ Z d
 „  Z e j d „  ƒ Z e j d „  ƒ Z RS(   uè    Server-side holder for ``bokeh.application.Application`` plus any associated data.
        This holds data that's global to all sessions, while ``ServerSession`` holds
        data specific to an "instance" of the application.
    c         C` sL   | |  _  | |  _ t ƒ  |  _ t ƒ  |  _ t ƒ  |  _ d  |  _ | |  _ d  S(   N(	   t   _applicationt   _loopt   dictt	   _sessionst   _pending_sessionst   _session_contextsR'   t   _server_contextt   _url(   R   t   applicationR   t   url(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   ‡   s    			c         C` s   |  j  S(   N(   R8   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR      s    c         C` s   |  j  S(   N(   R7   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR?   ”   s    c         C` s   |  j  S(   N(   R>   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR@   ˜   s    c         C` s(   |  j  d  k r! t |  ƒ |  _  n  |  j  S(   N(   R=   R'   R   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR,   œ   s    c         C` s   |  j  j ƒ  S(   N(   R:   t   values(   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   ¢   s    c         C` sk   y; |  j  j |  j ƒ } t | t j ƒ r: t j d ƒ n  Wn) t k
 rf } t j d | d t	 ƒn Xd  S(   Nuq   on_server_loaded returned a Future; this doesn't make sense because we run this hook before starting the IO loop.u   Error in server loaded hook %rt   exc_info(
   R7   t   on_server_loadedR,   t
   isinstanceR   t   Futuret   logt   errort	   Exceptiont   True(   R   R   t   e(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   run_load_hook¦   s    c         C` sx   y; |  j  j |  j ƒ } t | t j ƒ r: t j d ƒ n  Wn) t k
 rf } t j d | d t	 ƒn X|  j j
 ƒ  d  S(   Nu†   on_server_unloaded returned a Future; this doesn't make sense because we stop the IO loop right away after calling on_server_unloaded.u    Error in server unloaded hook %rRB   (   R7   t   on_server_unloadedR,   RD   R   RE   RF   RG   RH   RI   R   (   R   R   RJ   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   run_unload_hook¯   s    c         c` s|  t  | ƒ d k r! t d ƒ ‚ n  | |  j k r<| |  j k r<t j ƒ  } |  j | <t ƒ  } t | |  j | ƒ } t	 | ƒ | _
 | | _ y t |  j j | ƒ ƒ VWn) t k
 rÒ } t j d | d t ƒn X|  j j | ƒ t | | d |  j ƒ} |  j | =| |  j | <| j | ƒ | |  j | <| j | ƒ n  | |  j k r\|  j | V} n |  j | } t j | ƒ ‚ d  S(   Ni    u   Session ID must not be emptyu'   Failed to run session creation hooks %rRB   R   (   t   lenR	   R:   R;   R   RE   R   R%   R,   t   _RequestProxyR*   t   _session_contextR   R7   t   on_session_createdRH   RF   RG   RI   t   initialize_documentR   R8   R.   R<   t
   set_resultt   Return(   R   R+   R4   t   futuret   doct   session_contextRJ   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   create_session_if_neededº   s4    			
c         C` s4   | |  j  k r  |  j  | } | St d | ƒ ‚ d  S(   Nu   No such session (   R:   R	   (   R   R+   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   get_sessionë   s    c         #` sÖ   ˆ j  d k r t d ƒ ‚ n  t j d ˆ j ˆ j ƒ ˆ  j ˆ j } ‡  ‡ ‡ f d †  } ˆ j | ƒ V| j rÃ y! ˆ  j	 j
 | ƒ } t | ƒ VWqÃ t k
 r¿ } t j d | d t ƒqÃ Xn  t j d  ƒ ‚ d  S(   Ni    u8   Should not be discarding a session with open connectionsu5   Discarding session %r last in use %r milliseconds agoc           ` sl   ˆ ˆ ƒ rU ˆ j  d k rU ˆ j ƒ  ˆ  j ˆ j =ˆ  j ˆ j =t j d ˆ j ƒ n t j d ˆ j ƒ d  S(   Ni   u%   Session %r was successfully discardedu9   Session %r was scheduled to discard but came back to life(   t   expiration_blocked_countt   destroyR:   t   idR<   RF   t   tracet   warning(    (   R   R   t   should_discard(    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt
   do_discardü   s    
u&   Failed to run session destroy hooks %rRB   (   t   connection_countt   RuntimeErrorRF   t   debugR\   t#   milliseconds_since_last_unsubscribeR<   R/   R3   R7   t   on_session_destroyedR   RH   RG   RI   R   RT   R'   (   R   R   R_   RW   R`   R   RJ   (    (   R   R   R_   s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   _discard_sessionò   s    	c         #` sÏ   ‡  f d †  } g  } x= |  j  j ƒ  D], } | | ƒ r% | j r% | j | ƒ q% q% Wt | ƒ d k r t j d t | ƒ ƒ n  x8 | D]0 } | | ƒ rˆ | j rˆ |  j | | ƒ Vqˆ qˆ Wt j	 d  ƒ ‚ d  S(   Nc         ` s%   |  j  d k o$ |  j ˆ  k p$ |  j S(   Ni    (   Ra   Rd   t   expiration_requested(   R   (   t"   unused_session_linger_milliseconds(    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   should_discard_ignoring_block  s    i    u!   Scheduling %s sessions to discard(   R:   RA   t   expiration_blockedR   RN   RF   Rc   Rf   R   RT   R'   (   R   Rh   Ri   t
   to_discardR   (    (   Rh   s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   _cleanup_sessions  s    N(   R"   R#   t   __doc__R'   R   R$   R   R?   R@   R,   R   RK   RM   R   R5   RX   RY   Rf   Rl   (    (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR6      s   				0	$RO   c           B` s    e  Z d  „  Z e d „  ƒ Z RS(   c         C` sH   t  | j ƒ } d | k r% | d =n  d | k r; | d =n  | |  _ d  S(   Nu   bokeh-protocol-versionu   bokeh-session-id(   R9   t	   argumentst   _args(   R   R4   t	   args_copy(    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyR   0  s     
 
c         C` s   |  j  S(   N(   Ro   (   R   (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyRn   5  s    (   R"   R#   R   R$   Rn   (    (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyRO   /  s   	(   u   ApplicationContextu   BokehServerContextu   BokehSessionContext(   Rm   t
   __future__R    R   R   R   t   loggingt	   getLoggerR"   RF   t   tornadoR   R   R   t   application.applicationR   R   R-   R   t   protocol.exceptionsR	   t   util.tornadoR
   R   t   __all__R   R%   t   objectR6   RO   (    (    (    s4   lib/python2.7/site-packages/bokeh/server/contexts.pyt   <module>	   s    "	  !&®