U
    /eD                     @  s   d Z ddlmZ ddlZeeZddlZddlZddl	Z	ddl
mZmZmZ ddlmZ ddlmZmZ erdd	lmZ d
dlmZ dZG dd dZddddddZdS )a   Provide a base class for Bokeh Application handler classes.

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.

Below is an example outline of a custom handler that might modify documents
based off information in some database:

.. code-block:: python

    class DatabaseHandler(Handler):
        """ A Bokeh Application handler to initialize Documents from a database

        """

        def modify_document(self, doc: Document) -> None:
            # do some data base lookup here to generate 'plot'

            # add the plot to the document (i.e modify the document)
            doc.add_root(plot)

    )annotationsN)TYPE_CHECKINGAnyDict   )Document   )ServerContextSessionContext)HTTPServerRequest   )
CodeRunner)Handlerc                   @  s  e Zd ZU dZded< ded< ded< ded< dd	d
dZedd	ddZedd	ddZedd	ddZ	edd	ddZ
dddddZdddddZdddddZdddd d!Zdddd"d#Zd$d%d&d'd(Zdd	d)d*Zdd	d+d,Zd-S ).r   zm Provide a mechanism for Bokeh applications to build up new Bokeh
    Documents.

    .. autoclasstoc::

    bool_failedz
str | None_error_error_detail_staticNone)returnc                 C  s   d| _ d | _d | _d | _d S )NF)r   r   r   r   self r   F/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/application/handlers/handler.py__init__Y   s    zHandler.__init__c                 C  s   | j S )zE If the handler fails, may contain a related error message.

        )r   r   r   r   r   errora   s    zHandler.errorc                 C  s   | j S )zJ If the handler fails, may contain a traceback or other details.

        )r   r   r   r   r   error_detailh   s    zHandler.error_detailc                 C  s   | j S )z; ``True`` if the handler failed to modify the doc

        )r   r   r   r   r   failedo   s    zHandler.failedc                 C  s   dS )NTr   r   r   r   r   safe_to_forkv   s    zHandler.safe_to_forkr   )docr   c                 C  s   t ddS )a   Modify an application document in a specified manner.

        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 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.

        *Subclasses must implement this method*

        Args:
            doc (Document) : A Bokeh Document to update in-place

        Returns:
            Document

        zimplement modify_document()N)NotImplementedError)r   r   r   r   r   modify_document|   s    zHandler.modify_documentr	   )server_contextr   c                 C  s   dS )a.   Execute code when the server is first started.

        Subclasses may implement this method to provide for any one-time
        initialization that is necessary after the server starts, but
        before any sessions are created.

        Args:
            server_context (ServerContext) :

        Nr   r   r"   r   r   r   on_server_loaded   s    zHandler.on_server_loadedc                 C  s   dS )a   Execute code when the server cleanly exits. (Before stopping the
        server's ``IOLoop``.)

        Subclasses may implement this method to provide for any one-time
        tear down that is necessary before the server exits.

        Args:
            server_context (ServerContext) :

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

        Nr   r#   r   r   r   on_server_unloaded   s    zHandler.on_server_unloadedr
   )session_contextr   c                   s   dS )a#   Execute code when a new session is created.

        Subclasses may implement this method to provide for any per-session
        initialization that is necessary before ``modify_doc`` is called for
        the session.

        Args:
            session_context (SessionContext) :

        Nr   r   r&   r   r   r   on_session_created   s    zHandler.on_session_createdc                   s   dS )a    Execute code when a session is destroyed.

        Subclasses may implement this method to provide for any per-session
        tear-down that is necessary when sessions are destroyed.

        Args:
            session_context (SessionContext) :

        Nr   r'   r   r   r   on_session_destroyed   s    
zHandler.on_session_destroyedr   zDict[str, Any])requestr   c                 C  s   i S )a#   Processes incoming HTTP request returning a dictionary of
        additional data to add to the session_context.

        Args:
            request: HTTP request

        Returns:
            A dictionary of JSON serializable data to be included on
            the session context.
        r   )r   r*   r   r   r   process_request   s    zHandler.process_requestc                 C  s   | j r
dS | jS dS )zI Return a path to app-specific static resources, if applicable.

        N)r   r   r   r   r   r   static_path   s    zHandler.static_pathc                 C  s   dS )aR   Returns a default URL path, if applicable.

        Handlers subclasses may optionally implement this method, to inform
        the Bokeh application what URL it should be installed at.

        If multiple handlers specify ``url_path`` the Application will use the
        value from the first handler in its list of handlers.

        Nr   r   r   r   r   url_path   s    
zHandler.url_pathN)__name__
__module____qualname____doc____annotations__r   propertyr   r   r   r   r!   r$   r%   r(   r)   r+   r,   r-   r   r   r   r   r   L   s,   
	r   zHandler | CodeRunner	Exceptionr   )handlerer   c           	   	   C  sf   d| _ t | _t \}}}t|d \}}}}tj	|}| d|d| d| d| 	| _
dS )z4 Record an exception and details on a Handler.

    Tz
File z, line z, in z:
N)r   	traceback
format_excr   sysexc_info
extract_tbospathbasenamer   )	r5   r6   _exc_tracebackfilenameline_numberfunctxtr?   r   r   r   handle_exception   s    
rF   )r1   
__future__r   logging	getLoggerr.   logr=   r:   r8   typingr   r   r   documentr   Zapplicationr	   r
   Ztornado.httputilr   Zcode_runnerr   __all__r   rF   r   r   r   r   <module>   s    
  