U
    öÕ/e  ã                   @  sŠ   d Z ddlmZ ddlZe e¡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mZ errdd	lmZ d
ZG dd„ deƒZdS )z

é    )ÚannotationsN)ÚTYPE_CHECKING)Ú
CodeRunner)ÚHandler)ÚPathLike)ÚcurdocÚ
set_curdoc)ÚDocument)ÚExampleHandlerc                      s¤   e Zd ZdZdddgZddgZddd	d
œ‡ fdd„Zdd	dœdd„Zdd„ Z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‡  ZS ) r
   zgA stripped-down handler similar to CodeHandler but that does
    some appropriate monkeypatching.

    Zoutput_notebookZoutput_fileZreset_outputÚshowÚsaveÚstrr   ÚNone)ÚsourceÚfilenameÚreturnc                   s   t ƒ  ¡  t||g ƒ| _d S ©N)ÚsuperÚ__init__r   Ú_runner)Úselfr   r   ©Ú	__class__© úC/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/sphinxext/example_handler.pyr   =   s    
zExampleHandler.__init__r	   )Údocr   c              
   C  sl   | j r
d S | j ¡ }|j |¡ tƒ }t|ƒ |  ¡ \}}z| j 	|dd„ ¡ W 5 |  ||¡ t|ƒ X d S )Nc                   S  s   d S r   r   r   r   r   r   Ú<lambda>O   ó    z0ExampleHandler.modify_document.<locals>.<lambda>)
Úfailedr   Z
new_moduleÚmodulesÚaddr   r   Ú_monkeypatchÚ_unmonkeypatchÚrun)r   r   ÚmoduleZorig_curdocÚold_ioÚold_docr   r   r   Úmodify_documentA   s    
zExampleHandler.modify_documentc                 C  s¶   dd„ }dd„ }dd„ }dd l m} dd lm} ||g}i }| j| j D ]}t||ƒ||< qH|D ]4}	| jD ]}t|	||ƒ qj| jD ]}t|	||ƒ q‚q`dd lm	}
 |
j
}||
_
||fS )Nc                  _  s   d S r   r   ©ÚargsÚkwr   r   r   Ú_passU   s    z*ExampleHandler._monkeypatch.<locals>._passc                 _  s   t ƒ  | ¡ d S r   )r   Zadd_root)Úobjr)   r*   r   r   r   Ú	_add_rootX   s    z.ExampleHandler._monkeypatch.<locals>._add_rootc                  _  s   t ƒ S r   )r   r(   r   r   r   Ú_curdoc[   s    z,ExampleHandler._monkeypatch.<locals>._curdocr   )Úbokeh.ioÚioÚbokeh.plottingÚplottingÚ_output_funcsÚ	_io_funcsÚgetattrÚsetattrÚbokeh.documentÚdocumentr	   )r   r+   r-   r.   r0   ÚpÚmodsr%   ÚfÚmodÚdr&   r   r   r   r!   T   s$    

zExampleHandler._monkeypatchc           	      C  sZ   dd l m} dd lm} ||g}|D ]}|D ]}t|||| ƒ q,q$dd lm} ||_d S )Nr   )r/   r0   r1   r2   r6   r7   r8   r	   )	r   r%   r&   r0   r9   r:   r<   r;   r=   r   r   r   r"   x   s    zExampleHandler._unmonkeypatchÚbool)r   c                 C  s   | j jS r   )r   r   ©r   r   r   r   r   †   s    zExampleHandler.failedz
str | Nonec                 C  s   | j jS r   )r   Úerrorr?   r   r   r   r@   Š   s    zExampleHandler.errorc                 C  s   | j jS r   )r   Úerror_detailr?   r   r   r   rA   Ž   s    zExampleHandler.error_detailc                 C  s   | j jS r   )r   r   r?   r   r   r   r   ’   s    zExampleHandler.doc)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r4   r   r'   r!   r"   Úpropertyr   r@   rA   r   Ú__classcell__r   r   r   r   r
   4   s   
$r
   )rE   Ú
__future__r   ÚloggingÚ	getLoggerrB   ÚlogÚtypingr   Z&bokeh.application.handlers.code_runnerr   Z"bokeh.application.handlers.handlerr   Zbokeh.core.typesr   Zbokeh.io.docr   r   r7   r	   Ú__all__r
   r   r   r   r   Ú<module>   s   
