U
    /e                     @  s   d Z ddlmZ ddlZeeZddl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	dlmZ dZG dd deZdS )zg Bokeh Application Handler to look for Bokeh server lifecycle callbacks
in a specified Python module.

    )annotationsN)
ModuleType)List   )PathLike)_check_callback   )
CodeRunner)LifecycleHandler)ServerLifecycleHandlerc                      sx   e Zd ZdZg ddddddd f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Z	  Z
S )r   zV Load a script which contains server lifecycle callbacks.

    .. autoclasstoc::

    N)argvpackager   z	List[str]zModuleType | NoneNone)filenamer   r   returnc             	     sx   t    t|ddd}| }W 5 Q R X t||||d _ jjst j  _dd fdd} j	 j| d	S )
a  

        Keyword Args:
            filename (str) : path to a module to load lifecycle callbacks from

            argv (list[str], optional) : a list of string arguments to use as
                ``sys.argv`` when the callback code is executed. (default: [])

        rzutf-8)encoding)r   r   r   c                    s    j j} d| kr| d  _d| kr,| d  _d| kr>| d  _d| krP| d  _t jddd t jddd t jddd t jddd d S )NZon_server_loadedZon_server_unloadedZon_session_createdZon_session_destroyed)Zserver_context)what)Zsession_context)_module__dict__Z_on_server_loadedZ_on_server_unloadedZ_on_session_createdZ_on_session_destroyedr   )contentsself O/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/application/handlers/server_lifecycle.pyextract_callbacksO   s    



z:ServerLifecycleHandler.__init__.<locals>.extract_callbacksN)
super__init__openreadr	   _runnerfailedZ
new_moduler   run)r   r   r   r   fsourcer   	__class__r   r   r   :   s    

zServerLifecycleHandler.__init__z
str | Noner   c                 C  s   | j jS )zE If the handler fails, may contain a related error message.

        )r!   errorr   r   r   r   r(   c   s    zServerLifecycleHandler.errorc                 C  s   | j jS )zJ If the handler fails, may contain a traceback or other details.

        )r!   error_detailr   r   r   r   r)   j   s    z#ServerLifecycleHandler.error_detailboolc                 C  s   | j jS )z@ ``True`` if the lifecycle callbacks failed to execute

        )r!   r"   r   r   r   r   r"   q   s    zServerLifecycleHandler.failedc                 C  s.   | j r
dS dtjtj| jjd  S dS )z_ The last path component for the basename of the path to the
        callback module.

        N/r   )r"   ospathsplitextbasenamer!   r   r   r   r   url_pathz   s    zServerLifecycleHandler.url_path)__name__
__module____qualname____doc__r   propertyr(   r)   r"   r0   __classcell__r   r   r&   r   r   3   s    )r   )r4   
__future__r   logging	getLoggerr1   logr,   typesr   typingr   Z
core.typesr   Zutil.callback_managerr   Zcode_runnerr	   Z	lifecycler
   __all__r   r   r   r   r   <module>   s   
