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mZ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
ZG dd deZdd Zdd ZdS )z? Generate a gallery of Bokeh plots from a configuration file.

    )absolute_importdivisionprint_functionunicode_literalsN)abspathdirnamejoin)SphinxError)copyfile	ensuredirstatus_iterator   )BokehDirective)GALLERY_PAGE)BokehGalleryDirectiveenv_updated_handlersetupc               @   s   e Zd ZdZdZdd ZdS )r   Fr   c             C   s  | j jjj}|j}|j}t||j}t|| j	d }tt|d}t
| || tt|}|d }t|ddt|dd d}	g |_x|	D ]}
ttd	|
d
 }t||
d d }t|jjj|
d dd}yt|| W n4 tk
r } ztd||f W d d }~X Y nX y(|| |j||d |j| W q tk
rx } ztd||f W d d }~X Y qX qW dd |D }tj|d}||jd< |  |dS )Nr   gallerydetailszcopying gallery files... Zbrownc             S   s   | d d S )Nnamez.py )xr   r   <lib/python3.7/site-packages/bokeh/sphinxext/bokeh_gallery.py<lambda>P   s    z+BokehGalleryDirective.run.<locals>.<lambda>)Zstringify_funcz..pathr   z.py\/z'cannot copy gallery file %r, reason: %s)appz)failed to read gallery doc %r, reason: %sc             S   s   g | ]}|d  qS )r   r   ).0detailr   r   r   
<listcomp>f   s    z-BokehGalleryDirective.run.<locals>.<listcomp>)namesdocnamez<bokeh-gallery>)!stateZdocumentZsettingsenvr   r"   r   Zdoc2pathr   Z	argumentsr   Znote_dependencyjsonloadopenr   lengallery_updatedr   Zconfigbokeh_gallery_dirreplacer
   OSErrorr	   Z	clear_docZread_docappend	Exceptionr   ZrenderZ	temp_data_parse)selfr$   r   Zcurrent_docnameZdocdirZspecpathZdest_dirspecr   Zdetails_iterr   Zsrc_pathZ	dest_pathr"   er!   Zrst_textr   r   r   run:   sD    

"
&
zBokehGalleryDirective.runN)__name__
__module____qualname__Zhas_contentZrequired_argumentsr3   r   r   r   r   r   5   s   r   c             C   s   t |dg S )Nr)   )getattr)r   r$   r   r   r   r   n   s    r   c             C   s0   |  dtddd | dt | dt d S )Nr*   Zdocsr   Zhtmlzenv-updatedzbokeh-gallery)Zadd_config_valuer   Zconnectr   Zadd_directiver   )r   r   r   r   r   s   s    r   )__doc__Z
__future__r   r   r   r   ZloggingZ	getLoggerr4   logr%   os.pathr   r   r   Zsphinx.errorsr	   Zsphinx.utilr
   r   r   Zbokeh_directiver   Z	templatesr   __all__r   r   r   r   r   r   r   <module>	   s   
9