U
    /e9                     @  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	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ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.

    )annotationsN)abspathdirnameexistsgetmtimeisdirisfilejoin)SphinxError)	ensuredirstatus_iterator   )PARALLEL_SAFE)BokehDirective)GALLERY_DETAILGALLERY_PAGE)BokehGalleryDirectivesetupc                   @  s   e Zd ZdZdZdd ZdS )r   Fr   c                 C  s   t | j| jj}t|| jd }tt t |d}t|sZt|rZtd|d|t	
t|}dd |d D }tj|d}| |d	S )
Nr   galleryzgallery dir z missing for gallery file c                 S  s   g | ]}|d  qS name ).0detailr   r   A/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/sphinxext/bokeh_gallery.py
<listcomp>O   s     z-BokehGalleryDirective.run.<locals>.<listcomp>details)namesz<bokeh-gallery>)r   envZdoc2pathZdocnamer	   	argumentsr   r   r
   jsonloadopenr   renderparse)selfZdocdirgallery_filegallery_dirspecr   Zrst_textr   r   r   runE   s    zBokehGalleryDirective.runN)__name__
__module____qualname__Zhas_contentZrequired_argumentsr)   r   r   r   r   r   @   s   r   c              
   C  sr  t | j|j}| d}t|s<t|r<td|d|t|}t| tt	
|}tt|}|d }dd |D }t|t|k rtd|dt|d	d
t|| jdd d}	|	D ]}
|
d d }t ||}||kr|| t|rt||krqt|d:}tt | jdd|
d }|tj|
d d |d W 5 Q R X q|D ]}t	t || qVd S )Nz.jsonzcould not find gallery file z for configured gallery dir r   c                 S  s   h | ]}|d  qS r   r   )r   xr   r   r   	<setcomp>i   s     z(config_inited_handler.<locals>.<setcomp>zgallery file z has duplicate namesz!creating gallery file entries... Zbrownc                 S  s   | d d S )Nr   .rstr   )r-   r   r   r   <lambda>m       z'config_inited_handler.<locals>.<lambda>)Zstringify_funcr   r/   wz..pathz.py)filenamesource_path)r	   srcdirbokeh_gallery_dirr   r   r
   r   r   setoslistdirr    r!   r"   lenr   	verbosityremover   writer   r#   )appconfigr'   r&   Zgallery_file_mtimeextrasr(   r   r   Zdetails_iterr   Zdetail_file_nameZdetail_file_pathfr5   Z
extra_filer   r   r   config_inited_handlerV   s2    


(rC   c                 C  s0   |  dtddd | dt | dt tS )z+ Required Sphinx extension setup function. r7   Zdocsr   htmlzconfig-initedzbokeh-gallery)Zadd_config_valuer	   connectrC   Zadd_directiver   r   )r?   r   r   r   r      s    r   )!__doc__
__future__r   logging	getLoggerr*   logr    r9   os.pathr   r   r   r   r   r   r	   Zsphinx.errorsr
   Zsphinx.utilr   r    r   Zbokeh_directiver   Z	templatesr   r   __all__r   rC   r   r   r   r   r   <module>   s   
$,