B
    öÀ„\s  ã               @   sj   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddd„Zdd	„ ddfd
d„ZdS )zÍ
    sphinx.util.fileutil
    ~~~~~~~~~~~~~~~~~~~~

    File utility functions for Sphinx.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    )Úabsolute_importN)Úrelative_path)ÚcopyfileÚ	ensuredirÚwalkc          
   C   sâ   t j | ¡sdS t j |¡r>t j |¡r>t j |t j | ¡¡}|  ¡  d¡rÔ|rÔ|dkrjddlm	} |ƒ }t
j| dddR}| ¡  d¡r–|dd… }t
j|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   )ÚSphinxRendererÚrzutf-8)ÚencodingéþÿÿÿÚw)ÚosÚpathÚexistsÚisdirÚjoinÚbasenameÚlowerÚendswithÚsphinx.util.templater   ÚcodecsÚopenÚwriteZrender_stringÚreadr   )ÚsourceÚdestinationÚcontextÚrendererr   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>=   s    r    c             C   sú   t j | ¡sdS |dkr*ddlm} |ƒ }t|ƒ t j | ¡rPt| |||ƒ dS x¤t| ddD ]”\}}}t	| |ƒ}	xB|dd… D ]2}
|t
 |	|
¡ƒr | |
¡ q€tt
 ||	|
¡ƒ q€W x:|D ]2}|t
 |	|¡ƒs¼tt
 ||¡t
 ||	¡||ƒ q¼W q^W dS )a&  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
    Nr   )r   T)Úfollowlinks)r   r   r   r   r   r   Úisfiler   r   r   Ú	posixpathr   Úremove)r   r   Zexcludedr   r   r   ÚrootÚdirsÚfilesZreldirÚdirÚfilenamer   r   r   Ú
copy_asset=   s(    


r*   )NN)Ú__doc__Z
__future__r   r   r   r#   Zdocutils.utilsr   Zsphinx.util.osutilr   r   r   r   r*   r   r   r   r   Ú<module>
   s   	
!