U
    ꥡc                     @   sf   d dl Z d dlZd dlZd dlZd dlZdddZdddZd	Zd
d Zdd Z	dddZ
dd ZdS )    Ni     c                 C   s   ddl m} || }|j\}}|\}}|| ||  }	}
|	|
krT|}t||	 }n|}t||
 }|||f|j}|| dS )z>Create a thumbnail whose shortest dimension matches the windowr   ImageN)PILr   opensizeintresizeZ	ANTIALIASsave)Zimage_filenameZthumb_filenameZwindow_sizer   imZim_widthZ	im_heightwidthheightZwidth_factorZheight_factorZfinal_widthZfinal_heightZthumb r   :/tmp/pip-unpacked-wheel-y9_o96ar/altair/sphinxext/utils.pycreate_thumbnail   s    

r      i,  Tc                 C   s   ddl m} ddl}t|dks$t||d |d df}|rb||dd|d dddf 7 }||d	}|	|  dS )
zCreate a generic imager   r   N               Zuint8)
r   r   ZnumpylenAssertionErrorzerosZlinspaceZ	fromarrayZastyper   )filenameshapeZgradientr   npZarrr   r   r   r   create_generic_image   s    "r   zC
SyntaxError
===========
Example script with invalid Python syntax
c              	   C   s^   t | ddd}| }W 5 Q R X |dd}zt|}W n tk
rT   d}Y nX ||fS )aM  Parse source file into AST node

    Parameters
    ----------
    filename : str
        File path

    Returns
    -------
    node : AST node
    content : utf-8 encoded string

    Notes
    -----
    This function adapted from the sphinx-gallery project; license: BSD-3
    https://github.com/sphinx-gallery/sphinx-gallery/
    rutf-8)encodingz

N)r   readreplaceastparseSyntaxError)r   Zfidcontentnoder   r   r   _parse_source_file3   s    
r+   c                 C   s  t | \}}tdtj}||}|dk	rF| d }|d|}nd}|dkr^t||dfS t|t	j
s|td|jjzt|j}ddl}ddlm} |||j}	d}
|	D ]}|jdkr|j\}
} qqd	|d	|
d }|
d }W n tk
r   |jrt|jd t	jrt|jd jt	jt	jfr|jd }|jj}t|d
rnt|t sn|!d}t"|ddp|j#}|d	|d }|d7 }nd\}}Y nX |st$d| ||||fS )a  Separate ``filename`` content between docstring and the rest

    Strongly inspired from ast.get_docstring.

    Parameters
    ----------
    filename: str
        The path to the file containing the code to be read

    Returns
    -------
    docstring: str
        docstring of ``filename``
    category: list
        list of categories specified by the "# category:" comment
    rest: str
        ``filename`` content without the docstring
    lineno: int
         the line number on which the code starts

    Notes
    -----
    This function adapted from the sphinx-gallery project; license: BSD-3
    https://github.com/sphinx-gallery/sphinx-gallery/
    z^#\s*category:\s*(.*)$Nr    r   z4This function only supports modules. You provided {})BytesIOr   r#   decoder!   Z
end_lineno)r,   r,   zXCould not find docstring in file "{0}". A docstring is required for the example gallery.)%r+   recompile	MULTILINEsearchgroupssubSYNTAX_ERROR_DOCSTRING
isinstancer&   Module	TypeErrorformat	__class____name__	docstringtokenizeior-   readline
exact_typeendjoinsplitAttributeErrorbodyZExprvalueZStrConstantshasattrstrr.   getattrlineno
ValueError)r   r*   r)   Zfind_categorymatchcategoryr=   r>   r-   tsZds_linesZtk_restrM   Zdocstring_noder   r   r   get_docstring_and_restR   sf    





rT   c                 C   s<   t | d\}}}t|d tt |g||t ||gS )z<Utility to return (prev, this, next) tuples from an iteratorr   N)	itertoolsteenextzipchain)itsentineli1i2Zi3r   r   r   prev_this_next   s    
r^   c                 C   sH   t j| dd}zt|}W n" tk
r>   t| }Y nX | S )z-Return a hash of the contents of a dictionaryT)	sort_keys)jsondumpshashlibmd5r9   encode	hexdigest)dctZ
serializedmr   r   r   	dict_hash   s    rh   )r   )r   T)N)r&   rb   rU   r`   r0   r   r   r6   r+   rT   r^   rh   r   r   r   r   <module>   s   

e
