ó
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 m Z m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d Z d e f d „  ƒ  YZ d e e ƒ f d „  ƒ  YZ d e e ƒ f d „  ƒ  YZ d S(   uÙ   Provide the ``Application`` class.

Application instances are factories for creating new Bokeh Documents.

When a Bokeh server session is initiated, the Bokeh server asks the Application
for a new Document to service the session. To do this, the Application first
creates a new empty Document, then it passes this new Document to the
``modify_document`` method of each of its handlers. When all handlers have
updated the Document, it is used to service the user session.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   ABCMetat   abstractmethodt   abstractproperty(   t   geni   (   t   Document(   t   settings(   t   with_metaclass(   t   yield_for_all_futuresu   Applicationu   ServerContextu   SessionContextt   Applicationc           B` sª   e  Z d  Z e Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z e j d „  ƒ Z e j d „  ƒ Z RS(   u:    An Application is a factory for Document instances.

    c         O` sx   | j  d d ƒ } | r; t d t | j ƒ  ƒ d ƒ ‚ n  d |  _ g  |  _ | |  _ x | D] } |  j | ƒ q] Wd S(   u?   Application factory.

        Args:
            handlers (seq[Handler]): List of handlers to call.
                The URL is taken from the first one only.

        Keyword Args:
            metadata (dict): arbitrary user-supplied JSON data to make available
                with the application.

                The server will provide a URL ``http://applicationurl/metadata``
                which returns a JSON blob of the form:

                .. code-block:: json

                    {
                        "data": {
                            "hi": "hi",
                            "there": "there"
                        },
                        "url": "/myapp"
                    }

                The user-supplied metadata is returned as-is under the
                ``"data"`` key in the blob.

        u   metadatau   Invalid keyword argument: %si    N(	   t   popt   Nonet	   TypeErrort   listt   keyst   _static_patht	   _handlerst	   _metadatat   add(   t   selft   handlerst   kwargst   metadatat   h(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   __init__H   s    			c         C` s   t  |  j ƒ S(   uL    The ordered list of handlers this Application is configured with.

        (   t   tupleR   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   p   s    c         C` s   |  j  S(   uO    Arbitrary user-supplied metadata to associate with this application.

        (   R   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   w   s    c         C` s   t  d „  |  j Dƒ ƒ S(   u
   

        c         s` s   |  ] } | j  Vq d  S(   N(   t   safe_to_fork(   t   .0t   handler(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pys	   <genexpr>ƒ   s    (   t   allR   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   ~   s    c         C` s   |  j  S(   uI    Path to any (optional) static resources specified by handlers.

        (   R   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   static_path…   s    c         C` s’   |  j  j | ƒ t d „  |  j Dƒ ƒ } | j d ƒ t | ƒ d k ra t d t | ƒ ƒ ‚ n- t | ƒ d k r… | j	 ƒ  |  _
 n	 d |  _
 d S(   uÄ    Add a handler to the pipeline used to initialize new documents.

        Args:
            handler (Handler) : a handler for this Application to use to
                process Documents

        c         s` s   |  ] } | j  ƒ  Vq d  S(   N(   R!   (   R   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pys	   <genexpr>™   s    i   u/   More than one static path requested for app: %rN(   R   t   appendt   setR   t   discardR   t   lent   RuntimeErrorR   R   R   (   R   R   t   static_paths(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   Ž   s    c         C` s   t  ƒ  } |  j | ƒ | S(   uO    Creates and initializes a document using the Application's handlers.

        (   R   t   initialize_document(   R   t   doc(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   create_document¢   s    	c         C` sf   xF |  j  D]; } | j | ƒ | j r
 t j d | | j | j ƒ q
 q
 Wt j ƒ  rb | j ƒ  n  d S(   uD    Fills in a new document using the Application's handlers.

        u,   Error running application handler %r: %s %s N(	   R   t   modify_documentt   failedt   logt   errort   error_detailR	   t   perform_document_validationt   validate(   R   R)   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR(   ª   s    	#c         C` s%   x |  j  D] } | j | ƒ q
 Wd S(   uÉ    Invoked to execute code when a new session is created.

        This method calls ``on_server_loaded`` on each handler, in order,
        with the server context passed as the only argument.

        N(   R   t   on_server_loaded(   R   t   server_contextR   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR2   ¹   s    c         C` s%   x |  j  D] } | j | ƒ q
 Wd S(   u{   Invoked to execute code when the server cleanly exits. (Before
        stopping the server's ``IOLoop``.)

        This method calls ``on_server_unloaded`` on each handler, in order,
        with the server context passed as the only argument.

        .. warning::
            In practice this code may not run, since servers are often killed
            by a signal.

        N(   R   t   on_server_unloaded(   R   R3   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR4   Ã   s    c         c` sA   x+ |  j  D]  } | j | ƒ } t | ƒ Vq
 Wt j d ƒ ‚ d S(   u7   Invoked to execute code when a new session is created.

        This method calls ``on_session_created`` on each handler, in order,
        with the session context passed as the only argument.

        May return a ``Future`` which will delay session creation until the
        ``Future`` completes.

        N(   R   t   on_session_createdR   R   t   ReturnR   (   R   t   session_contextR   t   result(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR5   Ò   s    c         c` sA   x+ |  j  D]  } | j | ƒ } t | ƒ Vq
 Wt j d ƒ ‚ d S(   u   Invoked to execute code when a session is destroyed.

        This method calls ``on_session_destroyed`` on each handler, in order,
        with the session context passed as the only argument.

        Afterwards, ``session_context.destroyed`` will be ``True``.

        N(   R   t   on_session_destroyedR   R   R6   R   (   R   R7   R   R8   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR9   â   s    
(   t   __name__t
   __module__t   __doc__t   Truet   _is_a_bokeh_application_classR   t   propertyR   R   R   R!   R   R*   R(   R2   R4   R   t	   coroutineR5   R9   (    (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   =   s   	(					
	t   ServerContextc           B` sw   e  Z d  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z RS(   u­    A harness for server-specific information and tasks related to
    collections of Bokeh sessions.

    *This base class is probably not of interest to general users.*

    c         C` s   d S(   ux    ``SessionContext`` instances belonging to this application.

        *Subclasses must implement this method.*

        N(    (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   sessionsû   s    c         C` s   d S(   uÚ   Add a callback to be run on the next tick of the event loop.

        *Subclasses must implement this method.*

        Args:
            callback (callable) : a callback to add

                The callback will execute on the next tick of the event loop,
                and should have the form ``def callback()`` (i.e. it should
                not accept any arguments)

        Returns:
            an ID that can be used with ``remove_next_tick_callback``.

        N(    (   R   t   callback(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   add_next_tick_callback  s    c         C` s   d S(   uX   Add a callback to be run periodically until it is removed.

        *Subclasses must implement this method.*

        Args:
            callback (callable) : a callback to add

                The callback will execute periodically on the event loop
                as specified, and should have the form ``def callback()``
                (i.e. it should not accept any arguments)

            period_milliseconds (int) : number of milliseconds to wait
                between executing the callback.

        Returns:
            an ID that can be used with ``remove_periodic_callback``.

        N(    (   R   RC   t   period_milliseconds(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   add_periodic_callback  s    c         C` s   d S(   u^   Add a callback to be run once after timeout_milliseconds.

        *Subclasses must implement this method.*

        Args:
            callback (callable) : a callback to add

                The callback will execute once on the event loop after the
                timeout has passed, and should have the form ``def callback()``
                (i.e. it should not accept any arguments)

            timeout_milliseconds (int) : number of milliseconds to wait before
                executing the callback.

        Returns:
            an ID that can be used with ``remove_timeout_callback``.

        N(    (   R   RC   t   timeout_milliseconds(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   add_timeout_callback/  s    c         C` s   d S(   uç    Remove a callback added with ``add_next_tick_callback``, before
        it runs.

         *Subclasses must implement this method.*

        Args:
            callback_id : the ID returned from ``add_next_tick_callback``

        N(    (   R   t   callback_id(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   remove_next_tick_callbackE  s    c         C` s   d S(   uÍ    Removes a callback added with ``add_periodic_callback``.

        *Subclasses must implement this method.*

        Args:
            callback_id : the ID returned from ``add_periodic_callback``

        N(    (   R   RI   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   remove_periodic_callbackR  s    
c         C` s   d S(   uâ    Remove a callback added with ``add_timeout_callback``, before it
        runs.

        *Subclasses must implement this method.*

        Args:
            callback_id : the ID returned from ``add_timeout_callback``

        N(    (   R   RI   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   remove_timeout_callback^  s    (   R:   R;   R<   R   RB   R   RD   RF   RH   RJ   RK   RL   (    (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyRA   ñ   s   t   SessionContextc           B` sS   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e	 d „  ƒ Z
 RS(   už    A harness for server-specific information and tasks related to
    Bokeh sessions.

    *This base class is probably not of interest to general users.*

    c         C` s   | |  _  | |  _ d S(   u
   

        N(   t   _server_contextt   _id(   R   R3   t
   session_id(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR   s  s    	c         C` s   d S(   u¿    If ``True``, the session has been discarded and cannot be used.

        A new session with the same ID could be created later but this instance
        will not come back to life.

        N(    (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt	   destroyed|  s    c         C` s   |  j  S(   uF    The unique ID for the session associated with this context.

        (   RO   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   id†  s    c         C` s   |  j  S(   u6    The server context for this session context

        (   RN   (   R   (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyR3     s    c         C` s   d S(   u   Runs a function with the document lock held, passing the
        document to the function.

        *Subclasses must implement this method.*

        Args:
            func (callable): function that takes a single parameter (the Document)
                and returns ``None`` or a ``Future``

        Returns:
            a ``Future`` containing the result of the function

        N(    (   R   t   func(    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   with_locked_document–  s    (   R:   R;   R<   R   R   RQ   R?   RR   R3   R   RT   (    (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyRM   k  s   		
	(   u   Applicationu   ServerContextu   SessionContext(   R<   t
   __future__R    R   R   R   t   loggingt	   getLoggerR:   R-   t   abcR   R   R   t   tornadoR   t   documentR   R	   t   util.futureR
   t   util.tornadoR   t   __all__t   objectR   RA   RM   (    (    (    s<   lib/python2.7/site-packages/bokeh/application/application.pyt   <module>   s   "  ´z