B
    @\5                 @   s   d Z ddlmZmZmZmZ ddlZe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G dd deZG dd deeZG dd deeZdS )a   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.

    )absolute_importdivisionprint_functionunicode_literalsN)ABCMetaabstractmethodabstractproperty)gen   )Document)settings)with_metaclass)yield_for_all_futures)ApplicationServerContextSessionContextc               @   s   e Zd ZdZdZdd Zedd Zedd Zed	d
 Z	edd Z
dd Zdd Zdd Zdd Zdd Zejdd Zejdd ZdS )r   z: An Application is a factory for Document instances.

    Tc             O   sR   | dd}|r$td| d  d| _g | _|| _x|D ]}| | q<W dS )a?   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.

        metadataNzInvalid keyword argument: %sr   )pop	TypeErrorkeys_static_path	_handlers	_metadataadd)selfhandlerskwargsr   h r   <lib/python3.7/site-packages/bokeh/application/application.py__init__H   s    
zApplication.__init__c             C   s
   t | jS )zL The ordered list of handlers this Application is configured with.

        )tupler   )r   r   r   r   r   p   s    zApplication.handlersc             C   s   | j S )zO Arbitrary user-supplied metadata to associate with this application.

        )r   )r   r   r   r   r   w   s    zApplication.metadatac             C   s   t dd | jD S )z


        c             s   s   | ]}|j V  qd S )N)safe_to_fork).0handlerr   r   r   	<genexpr>   s    z+Application.safe_to_fork.<locals>.<genexpr>)allr   )r   r   r   r   r"   ~   s    zApplication.safe_to_forkc             C   s   | j S )zI Path to any (optional) static resources specified by handlers.

        )r   )r   r   r   r   static_path   s    zApplication.static_pathc             C   sj   | j | tdd | jD }|d t|dkrHtdt| nt|dkr`| | _	nd| _	dS )z 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   | ]}|  V  qd S )N)r'   )r#   r   r   r   r   r%      s    z"Application.add.<locals>.<genexpr>N   z/More than one static path requested for app: %r)
r   appendsetr   discardlenRuntimeErrorlistr   r   )r   r$   Zstatic_pathsr   r   r   r      s    
zApplication.addc             C   s   t  }| | |S )zO Creates and initializes a document using the Application's handlers.

        )r   initialize_document)r   docr   r   r   create_document   s    
zApplication.create_documentc             C   sH   x2| j D ](}|| |jrtd||j|j qW t rD|  dS )zD Fills in a new document using the Application's handlers.

        z,Error running application handler %r: %s %s N)	r   Zmodify_documentZfailedlogerrorZerror_detailr   Zperform_document_validationZvalidate)r   r0   r   r   r   r   r/      s    
zApplication.initialize_documentc             C   s   x| j D ]}|| qW dS )z 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   on_server_loaded)r   server_contextr   r   r   r   r4      s    zApplication.on_server_loadedc             C   s   x| j D ]}|| qW dS )a{   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   on_server_unloaded)r   r5   r   r   r   r   r6      s    zApplication.on_server_unloadedc             c   s2   x"| j D ]}||}t|V  qW tddS )a7   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   on_session_createdr   r	   Return)r   session_contextr   resultr   r   r   r7      s    
zApplication.on_session_createdc             c   s2   x"| j D ]}||}t|V  qW tddS )a   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   on_session_destroyedr   r	   r8   )r   r9   r   r:   r   r   r   r;      s    

z Application.on_session_destroyedN)__name__
__module____qualname____doc__Z_is_a_bokeh_application_classr    propertyr   r   r"   r'   r   r1   r/   r4   r6   r	   	coroutiner7   r;   r   r   r   r   r   =   s   (	
r   c               @   sd   e Zd ZdZedd Zedd Zedd Zedd	 Z	ed
d Z
edd Zedd ZdS )r   z 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 )zx ``SessionContext`` instances belonging to this application.

        *Subclasses must implement this method.*

        Nr   )r   r   r   r   sessions   s    zServerContext.sessionsc             C   s   dS )a   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``.

        Nr   )r   callbackr   r   r   add_next_tick_callback  s    z$ServerContext.add_next_tick_callbackc             C   s   dS )aX   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``.

        Nr   )r   rC   Zperiod_millisecondsr   r   r   add_periodic_callback  s    z#ServerContext.add_periodic_callbackc             C   s   dS )a^   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``.

        Nr   )r   rC   Ztimeout_millisecondsr   r   r   add_timeout_callback/  s    z"ServerContext.add_timeout_callbackc             C   s   dS )z 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``

        Nr   )r   callback_idr   r   r   remove_next_tick_callbackE  s    z'ServerContext.remove_next_tick_callbackc             C   s   dS )z Removes a callback added with ``add_periodic_callback``.

        *Subclasses must implement this method.*

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

        Nr   )r   rG   r   r   r   remove_periodic_callbackR  s    
z&ServerContext.remove_periodic_callbackc             C   s   dS )z 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``

        Nr   )r   rG   r   r   r   remove_timeout_callback^  s    z%ServerContext.remove_timeout_callbackN)r<   r=   r>   r?   r   rB   r   rD   rE   rF   rH   rI   rJ   r   r   r   r   r      s   r   c               @   sH   e Zd ZdZdd Zedd Zedd Zedd	 Z	e
d
d ZdS )r   z 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 )z


        N)_server_context_id)r   r5   Z
session_idr   r   r   r    s  s    zSessionContext.__init__c             C   s   dS )z 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.

        Nr   )r   r   r   r   	destroyed|  s    zSessionContext.destroyedc             C   s   | j S )zF The unique ID for the session associated with this context.

        )rL   )r   r   r   r   id  s    zSessionContext.idc             C   s   | j S )z6 The server context for this session context

        )rK   )r   r   r   r   r5     s    zSessionContext.server_contextc             C   s   dS )a   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

        Nr   )r   funcr   r   r   with_locked_document  s    z#SessionContext.with_locked_documentN)r<   r=   r>   r?   r    r   rM   r@   rN   r5   r   rP   r   r   r   r   r   k  s   	
	r   )r?   Z
__future__r   r   r   r   ZloggingZ	getLoggerr<   r2   abcr   r   r   Ztornador	   Zdocumentr   r   Zutil.futurer   Zutil.tornador   __all__objectr   r   r   r   r   r   r   <module>   s   
 5z