B
    .Kc              
   @   s   d Z ddlZddlZddl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 erdddlmZ deeee ed dd	d
dZdd dddfeeeee ed eeeegdf  ddddZdS )z"File utility functions for Sphinx.    N)TYPE_CHECKINGCallableDictOptional)relative_path)copyfile	ensuredir)PathMatcher)BaseRendererr
   )sourcedestinationcontextrendererreturnc          
   C   s   t j| sdS t j|r2t j|t j| }|  dr|dk	r|dkrbddlm	} | }t
| ddP}| dr|dd }t
|ddd}||| | W dQ R X W dQ R X n
t| | dS )	a  Copy an asset file to destination.

    On copying, it expands the template variables if context argument is given and
    the asset is a template file.

    :param source: The path to source file
    :param destination: The path to destination file or directory
    :param context: The template variables.  If not given, template files are simply copied
    :param renderer: The template engine.  If not given, SphinxRenderer is used by default
    NZ_tr   )SphinxRendererzutf-8)encodingw)ospathexistsisdirjoinbasenamelowerendswithsphinx.util.templater   openwriteZrender_stringreadr   )r   r   r   r   r   ZfsrcZfdst r    3lib/python3.7/site-packages/sphinx/util/fileutil.pycopy_asset_file   s    ,r"   c             C   s   dS )NFr    )r   r    r    r!   <lambda>2       r#   )r   r   excludedr   r   onerrorr   c             C   sB  t j| sdS |dkr*ddlm} | }t| t j| rPt| ||| dS xt j| ddD ]\}}}	t	| |}
xB|dd D ]2}|t
|
|r|| qtt
||
| qW x|	D ]x}|t
|
|sy"tt
||t
||
|| W q tk
r4 } z |r"|t
||| n W dd}~X Y qX qW q`W dS )aM  Copy asset files to destination recursively.

    On copying, it expands the template variables if context argument is given and
    the asset is a template file.

    :param source: The path to source file or directory
    :param destination: The path to destination directory
    :param excluded: The matcher to determine the given path should be copied or not
    :param context: The template variables.  If not given, template files are simply copied
    :param renderer: The template engine.  If not given, SphinxRenderer is used by default
    :param onerror: The error handler.
    Nr   )r   T)followlinks)r   r   r   r   r   r   isfiler"   walkr   	posixpathr   remove	Exception)r   r   r%   r   r   r&   r   rootdirsfilesZreldirdirfilenameexcr    r    r!   
copy_asset2   s2    


r3   )NN)__doc__r   r*   typingr   r   r   r   Zdocutils.utilsr   Zsphinx.util.osutilr   r   Zsphinx.util.typingr	   r   r
   strr"   r,   r3   r    r    r    r!   <module>   s      