U
    \Sh                  	   @  s   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZ erhddlmZ dd	d	d
dddddZdd dddfd	d	dd
ddddddZdS )z"File utility functions for Sphinx.    )annotationsN)TYPE_CHECKINGCallable)relative_path)copyfile	ensuredir)PathMatcher)BaseRendererstrzdict | NonezBaseRenderer | NoneNone)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 5 Q R X W 5 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   fsrcfdst r$   G/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/fileutil.pycopy_asset_file   s    ,r&   c                 C  s   dS )NFr$   )r   r$   r$   r%   <lambda>4       r'   r   z'Callable[[str, Exception], None] | None)r   r   excludedr   r   onerrorr   c                 C  s6  t j| sdS |dkr*ddlm} | }t| t j| rPt| ||| dS t j| ddD ]\}}}	t	| |}
|dd D ]2}|t
|
|r|| q~tt
||
| q~|	D ]x}|t
|
|sz"tt
||t
||
|| W q tk
r, } z |r|t
||| n W 5 d}~X Y qX qq^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_asset4   s6    

 r7   )NN)__doc__
__future__r   r   r.   typingr   r   Zdocutils.utilsr   Zsphinx.util.osutilr   r   Zsphinx.util.typingr   r   r	   r&   r7   r$   r$   r$   r%   <module>   s"     " 