U
    /e+                     @  s  d Z ddlmZ ddlmZ eeZddlZddl	Z	ddl
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ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dl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- dZ.edZ/e- Z0G dd dej1ej2Z3G dd de)Z4dd Z5dd Z6d d! Z7d"d# Z8d$d$d%d&d'Z9d$d$d(d)d*Z:d+d, Z;dS )-a   Include Bokeh plots in Sphinx HTML documentation.

For other output types, the placeholder text ``[graph]`` will
be generated.

The ``bokeh-plot`` directive can be used by either supplying:

**A path to a source file** as the argument to the directive::

    .. bokeh-plot:: path/to/plot.py

.. note::
    .py scripts are not scanned automatically! In order to include
    certain directories into .py scanning process use following directive
    in sphinx conf.py file: bokeh_plot_pyfile_include_dirs = ["dir1","dir2"]

**Inline code** as the content of the directive::

 .. bokeh-plot::

     from bokeh.plotting import figure, output_file, show

     output_file("example.html")

     x = [1, 2, 3, 4, 5]
     y = [6, 7, 6, 4, 5]

     p = figure(title="example", width=300, height=300)
     p.line(x, y, line_width=2)
     p.circle(x, y, size=10, fill_color="white")

     show(p)

This directive also works in conjunction with Sphinx autodoc, when
used in docstrings.

The ``bokeh-plot`` directive accepts the following options:

process-docstring (bool):
    Whether to display the docstring in a formatted block
    separate from the source.

source-position (enum('above', 'below', 'none')):
    Where to locate the the block of formatted source
    code (if anywhere).

linenos (bool):
    Whether to display line numbers along with the source.

Examples
--------

The inline example code above produces the following output:

.. bokeh-plot::

    from bokeh.plotting import figure, output_file, show

    output_file("example.html")

    x = [1, 2, 3, 4, 5]
    y = [6, 7, 6, 4, 5]

    p = figure(title="example", width=300, height=300)
    p.line(x, y, line_width=2)
    p.circle(x, y, size=10, fill_color="white")

    show(p)

    )annotations)loggingN)getenv)basenamedirnamejoin)uuid4)nodes)choiceflag)SphinxError)copyfile	ensuredirstatus_iterator)set_source_info)Document)autoload_static)Model)BokehDeprecationWarning   )PARALLEL_SAFE)BokehDirective)ExampleHandler)get_sphinx_resources)autoload_scriptBokehPlotDirectivesetupGOOGLE_API_KEYc                   @  s"   e Zd Zedd ZejdfZdS )r   c                 C  sN   |d }|d }|r(| j d| d | j | |rD| j d tjd S )N
script_tagheight_hintz<div style="height:zpx;">z</div>)bodyappendr	   ZSkipNode)Zvisitornoder   r    r#   >/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/sphinxext/bokeh_plot.py
visit_html   s    zautoload_script.visit_htmlN)__name__
__module____qualname__staticmethodr%   __func__htmlr#   r#   r#   r$   r      s   

r   c                   @  sT   e Zd ZdZdZdd dd dd dZdd	 Zd
ddddZdd Zdd Z	dS )r   T   c                 C  s   t | d kS Nr   xr#   r#   r$   <lambda>       zBokehPlotDirective.<lambda>c                 C  s
   t | dS )N)belowabovenone)r
   r/   r#   r#   r$   r1      r2   c                 C  s   t | d kS r-   r.   r/   r#   r#   r$   r1      r2   )process-docstringsource-positionlinenosc              
   C  s  |   \}}| jjdd}dt j d| d}z| |||\}}}}}W n6 tk
r }	 ztd| d|	 W 5 d }	~	X Y nX | jj	
|t| jjf | dt| }
tjdd|
gd	g}| jd
d}|r|r| |dng }| ||\}}t||dg}|| | | | S )N/-zbokeh-content-z.jszError generating z: 

. )idsr6   Fz<bokeh-content>)r   r   )process_args_or_contentenvdocnamereplacer   hexprocess_source	Exceptionr   bokeh_plot_filesaddr   r   r	   targetoptionsgetparseprocess_code_blockr   )selfsourcepathZdashed_docnamejs_filenamer   js_path	docstringr   eZ	target_idrG   Zprocess_docstringZintror4   r3   Zautoloadr#   r#   r$   run   s    &zBokehPlotDirective.runstrz
str | NonerM   rQ   c                 C  s   | j dd}|dkrg g fS t|| }| j dd}tj||d|g d}t| | |dkrj|gg fS |dkr|g |gfS d S )	Nr7   r3   r5   r8   Fpython)languager8   classesr4   )rH   rI   _remove_module_docstringstripr	   Zliteral_blockr   )rL   rM   rQ   Zsource_positionr8   Z
code_blockr#   r#   r$   rK      s    

z%BokehPlotDirective.process_code_blockc              
   C  s   | j r| jrtd| jrFtd| jj | jj}d| j|fS | j d }td| jjd|  |	dst| jj
j|}z.t|}| |fW  5 Q R  W S Q R X W n: tk
r } ztd| jjd|W 5 d }~X Y nX d S )	Nz7bokeh-plot:: directive can't have both args and contentz([bokeh-plot] handling inline content in 
r   z*[bokeh-plot] handling external content in z: r9   z&bokeh-plot:: error reading source for )	argumentscontentr   logdebugr?   r@   bokeh_plot_auxdirr   
startswithappsrcdiropenreadrD   )rL   rN   frR   r#   r#   r$   r>      s    


$z*BokehPlotDirective.process_args_or_contentc              	   C  sn   t   t||| j\}}| }t| jj|}t|t|\}}	t	|d}
|

| W 5 Q R X |	||||fS )Nw)r   Z_clear_extensions_evaluate_sourcer?   Z_sphinx_height_hintr   r`   r   	RESOURCESrd   write)rL   rM   rN   rO   rootrQ   r   rP   Zjsr   rf   r#   r#   r$   rC      s    z!BokehPlotDirective.process_sourceN)
r&   r'   r(   Zhas_contentZoptional_argumentsZoption_specrS   rK   r>   rC   r#   r#   r#   r$   r      s   r   c                 C  s8   t | jjd| j_t| jj t| jds4t | j_d S )NZ
bokeh_plotrE   )r   r?   Z
doctreedirr`   r   hasattrsetrE   rb   r#   r#   r$   builder_inited   s    ro   c                 C  s   t | jj}t|ddt|| jdd d}|D ]n\}}t| jj|t	|}t
t| zt|| W q. tk
r } ztd|d| W 5 d }~X Y q.X q.d S )Nzcopying bokeh-plot files... Zbrownc                 S  s   t | d S )Nr   )r   r/   r#   r#   r$   r1      r2   z build_finished.<locals>.<lambda>)Zstringify_funczcannot copy local file z
, reason: )sortedr?   rE   r   len	verbosityr   ZbuilderZoutdirr   r   r   r   OSErrorr   )rb   	exceptionfilesZ
files_iterfileZdocpathrG   rR   r#   r#   r$   build_finished   s    rw   c                 C  s   | j |j O  _ d S r-   )rE   )rb   r?   Zdocnamesotherr#   r#   r$   env_merge_info
  s    ry   c                 C  sR   |  dt | jttjd | ddd | dt | dt | dt	 t
S )	z+ Required Sphinx extension setup function. z
bokeh-plot)r+   bokeh_missing_google_api_key_okTr+   zbuilder-initedzbuild-finishedzenv-merge-info)Zadd_directiver   add_noder   r+   Zadd_config_valueconnectro   rw   ry   r   rn   r#   r#   r$   r     s    r   rT   )rM   returnc                 C  s<   d| kr| S t d kr0|jjr(| ddS td| dt S )Nr   ZMISSING_API_KEYzThe GOOGLE_API_KEY environment variable is not set. Set GOOGLE_API_KEY to a valid API key, or set bokeh_missing_google_api_key_ok=True in conf.py to build anyway (with broken GMaps))r   configrz   rA   r   )rM   r?   r#   r#   r$   _replace_google_api_key  s    r   rM   filenamec              	   C  s   t | |} t| |d}t }t ( d|jkr>tjdtd || W 5 Q R X |j	rnt
d|j d|  t|jdkrt
dt|j |jd	 |jr|j nd fS )
Nr   	referenceignore)categoryzbokeh-plot:: error:

z

evaluating source:

r   z;bokeh-plot:: directive expects a single Document root, got r   )r   r   r   warningscatch_warningsr@   filterwarningsr   Zmodify_documenterrorRuntimeErrorZerror_detailrq   rootsdocrZ   )rM   r   r?   cdr#   r#   r$   rh   -  s    


rh   c                 C  s(   |d kr| S t dt | dd| S )Nz(\'\'\'|\"\"\")\s*z\s*(\'\'\'|\"\"\")r<   )resubescaperU   r#   r#   r$   rY   C  s    rY   )<__doc__
__future__r   Zsphinx.utilr   	getLoggerr&   r^   r   r   osr   os.pathr   r   r   uuidr   Zdocutilsr	   Zdocutils.parsers.rst.directivesr
   r   Zsphinx.errorsr   r   r   r   Zsphinx.util.nodesr   Zbokeh.documentr   Zbokeh.embedr   Zbokeh.modelr   Zbokeh.util.warningsr   r<   r   Zbokeh_directiver   Zexample_handlerr   utilr   __all__r   ri   ZGeneralElementr   r   ro   rw   ry   r   r   rh   rY   r#   r#   r#   r$   <module>   sB   J
_