B
    @\                 @   s~   d Z ddlmZmZmZmZ ddlZeeZ	ddl
Z
ddlZddlmZmZ ddlmZ ddlmZ d	ZG d
d deZdS )a   Provide a Bokeh Application Handler to build up documents by compiling
and executing Python source code.

This Handler is used by the Bokeh server command line tool to build
applications that run off scripts and notebooks.

.. code-block:: python

    def make_doc(doc):

        # do work to modify the document, add plots, widgets, etc.

        return doc

    app = Application(FunctionHandler(make_doc))

    server = Server({'/bkapp': app}, io_loop=IOLoop.current())

    server.start()

    )absolute_importdivisionprint_functionunicode_literalsN   )
set_curdoccurdoc   )
CodeRunner)Handler)CodeHandlerc                   s   e Zd ZdZdddddgZ f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  ZS )r   z0 Run source code which modifies a Document

    Zoutput_notebookZoutput_fileZshowZsaveZreset_outputc                s   t t| j|| d|kr"td|d }d|kr:td|d }|dg }t|||| _i | _xtjD ]}| 	|| j|< qjW dS )a&  

        Args:
            source (str) : python source code

            filename (str) : a filename to use in any debugging or error output

            argv (list[str], optional) : a list of string arguments to make
                available as ``sys.argv`` when the code executes

        sourcezMust pass source to CodeHandlerfilenamez#Must pass a filename to CodeHandlerargvN)
superr   __init__
ValueErrorgetr
   _runner_loggers_io_functions_make_io_logger)selfargskwargsr   r   r   f)	__class__ >lib/python3.7/site-packages/bokeh/application/handlers/code.pyr   M   s    zCodeHandler.__init__c             C   s   | j jS )zE If the handler fails, may contain a related error message.

        )r   error)r   r   r   r   r   m   s    zCodeHandler.errorc             C   s   | j jS )zJ If the handler fails, may contain a traceback or other details.

        )r   error_detail)r   r   r   r   r    t   s    zCodeHandler.error_detailc             C   s   | j jS )z; ``True`` if the handler failed to modify the doc

        )r   failed)r   r   r   r   r!   {   s    zCodeHandler.failedc             C   s
   | j j S )z Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if the code has already been executed.

        )r   Zran)r   r   r   r   safe_to_fork   s    zCodeHandler.safe_to_forkc          	      s~   j  }|dkrdS |tj|j<  j| t }t  	 }z  fdd}j 
|| W d| t| X dS )z


        Nc                 s(   t  } |  k	r$tdjjjf d S )Nz'%s at '%s' replaced the output document)r   RuntimeErrorZ_originr   path)Znewdoc)docr   r   r   
post_check   s    z/CodeHandler.modify_document.<locals>.post_check)r   Z
new_modulesysmodules__name__Z_modulesappendr   r   _monkeypatch_iorun_unmonkeypatch_io)r   r%   moduleZold_docZold_ior&   r   )r%   r   r   modify_document   s    

zCodeHandler.modify_documentc             C   s.   | j r
dS dtjtj| jjd  S dS )zO The last path component for the basename of the configured filename.

        N/r   )r!   osr$   splitextbasenamer   )r   r   r   r   url_path   s    zCodeHandler.url_pathc                s    fdd}|S )Nc                 s   t jjj  d S )N)loginfoZ_logger_textr   r$   )r   r   )namer   r   r   logger   s    z+CodeHandler._make_io_logger.<locals>.loggerr   )r   r7   r8   r   )r7   r   r   r      s    zCodeHandler._make_io_loggerc             C   sD   dd l m} i }x.tjD ]$}t||||< t||| j|  qW |S )Nr   )bokeh.ioior   r   getattrsetattrr   )r   r:   oldr   r   r   r   r+      s    zCodeHandler._monkeypatch_ioc             C   s.   dd l m} x|D ]}t||||  qW d S )Nr   )r9   r:   r<   )r   r=   r:   r   r   r   r   r-      s    
zCodeHandler._unmonkeypatch_io)r)   
__module____qualname____doc__r   r   propertyr   r    r!   r"   r/   r4   r   r+   r-   __classcell__r   r   )r   r   r   D   s    #	r   )r@   Z
__future__r   r   r   r   ZloggingZ	getLoggerr)   r5   r1   r'   Zio.docr   r   Zcode_runnerr
   Zhandlerr   __all__r   r   r   r   r   <module>   s   
