U
    /e                     @  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 ddlmZmZ ddlmZmZ ddlmZ dd	lmZ d
dlmZ dZG dd dZdS )z^ Provide a utility class ``CodeRunner`` for use by handlers that execute
Python source code.

    )annotationsN)basename)CodeType
ModuleType)CallableList   )PathLike)make_globally_unique_id   )handle_exception)
CodeRunnerc                   @  s&  e Zd ZU dZded< ded< ded< ded< ded	< d
ed< ded< ded< ded< ded< ded< ded< d0d
dd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
eddd#d$Zed
dd%d&Zddd'd(Zddd)d*Zd1d+d,dd-d.d/ZdS )2r   zA Compile and run Python source code.

    .. autoclasstoc::

    zCodeType | None_codez
str | None_doc_permanent_error_permanent_error_detailr	   _pathstr_sourcez	List[str]_argvzModuleType | None_packageboolran_failed_error_error_detailNNone)sourcepathargvpackagereturnc           
   
   C  s  |rt |dkrtdd| _d| _|   ddl}d| _zL||t	|}t
||ddd| _tt| jj| jj}|dd| _W nr tk
r } zTd| _|jdk	rtj |jnd	}	d
|	d|jpd	 d|jpd	 | _t | _W 5 d}~X Y nX || _|| _|| _|| _d| _dS )a  

        Args:
            source (str) :
                A string containing Python source code to execute

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

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

            package (bool) :
                An optional package module to configure

        Raises:
            ValueError, if package is specified for an __init__.py

        __init__.pyz)__init__.py cannot have package specifiedNr   execT)filenamemodedont_inherit__doc__z???zInvalid syntax in z	 on line z:
F)r   
ValueErrorr   r   reset_run_errorsastr   parseosfspathcompiledictzipco_names	co_constsgetr   SyntaxErrorr$   r   linenotext	traceback
format_excr   r   r   r   r   )
selfr   r   r   r    r*   Znodesder$    r<   J/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/application/handlers/code_runner.py__init__J   s,    $zCodeRunner.__init__)r!   c                 C  s   | j S )z7 Contents of docstring, if code contains one.

        )r   r9   r<   r<   r=   doc}   s    zCodeRunner.docc                 C  s   | j dkr| jS | j S )zH If code execution fails, may contain a related error message.

        N)r   r   r?   r<   r<   r=   error   s    zCodeRunner.errorc                 C  s   | j dkr| jS | j S )zM If code execution fails, may contain a traceback or other details.

        N)r   r   r?   r<   r<   r=   error_detail   s    zCodeRunner.error_detailc                 C  s   | j p| jdkS )z, ``True`` if code execution failed

        N)r   r   r?   r<   r<   r=   failed   s    zCodeRunner.failedc                 C  s   | j S )z= The path that new modules will be configured with.

        )r   r?   r<   r<   r=   r      s    zCodeRunner.pathc                 C  s   | j S )z[ The configured source code that will be executed when ``run`` is
        called.

        )r   r?   r<   r<   r=   r      s    zCodeRunner.sourcec                 C  s   |    | jdkrdS dt dd }t|}tj| j|j	d< | j
rf| j
j|_tj| jg|_t| jdkr||_tj| jg|_|S )zN Make a fresh module to run in.

        Returns:
            Module

        NZ
bokeh_app_- __file__r"   )r)   r   r
   replacer   r,   r   abspathr   __dict__r   __name____package__dirname__path__r   )r9   module_namemoduler<   r<   r=   
new_module   s    

zCodeRunner.new_modulec                 C  s   d| _ d| _d| _dS )zg Clears any transient error conditions from a previous run.

        Returns
            None

        FN)r   r   r   r?   r<   r<   r=   r)      s    zCodeRunner.reset_run_errorsr   zCallable[[], None] | None)rO   
post_checkr!   c              
   C  s   t  }ttj}ttj}tjdt j| j t j	| jg| j
 t_| jdk	sZtzNzt| j|j |rv|  W n, tk
r } zt| | W 5 d}~X Y nX W 5 t | |t_|t_d| _X dS )at   Execute the configured source code in a module and run any post
        checks.

        Args:
            module (Module) :
                A module to execute the configured code in.

            post_check (callable, optional) :
                A function that raises an exception if expected post-conditions
                are not met after code execution.

        r   NT)r,   getcwdlistsysr   r   insertrL   r   r   r   r   AssertionErrorchdirr   r#   rI   	Exceptionr   )r9   rO   rQ   Z_cwdZ	_sys_pathZ	_sys_argvr;   r<   r<   r=   run   s     


 
zCodeRunner.run)N)N)rJ   
__module____qualname__r'   __annotations__r>   propertyr@   rA   rB   rC   r   r   rP   r)   rY   r<   r<   r<   r=   r   5   s:   
3	r   )r'   
__future__r   logging	getLoggerrJ   logr,   rT   r7   os.pathr   typesr   r   typingr   r   Z
core.typesr	   Zutil.serializationr
   handlerr   __all__r   r<   r<   r<   r=   <module>   s   
