o
    Ni                     @  s   d Z ddlmZ ddlZddl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 dd	lmZmZ erLdd
lmZ ddlmZ ddlmZ eeZd(ddZ		d)ddd*ddZd d! dddfddd+d&d'ZdS ),z"File utility functions for Sphinx.    )annotationsN)Path)TYPE_CHECKINGAny)relative_path)__)logging)copyfile	ensuredir)Callable)BaseRenderer)PathMatcherfilenamestr | os.PathLike[str]return
str | Nonec                 C  sL   t j| }| drt| dd S | dr$t| dd S dS )zGiven an input filename:
    If the input looks like a template, then return the filename output should
    be written to.  Otherwise, return no result (None).
    _tNz.jinjai)ospathbasenamelowerendswithstr)r   r    r   H/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/util/fileutil.py_template_basename   s   r   Fforcesourcedestinationcontextdict[str, Any] | NonerendererBaseRenderer | Noner   boolNonec                C  sV  t j| sdS t|}| r|t j|  }t| r|dur|du r-ddlm} | }t	| dd}|
 }W d   n1 sBw   Y  |||}|sm| rm||krmtd}	tj|	t | t |ddd	 dS t|pr|}t	|d
dd}
td}	tj|	t |ddd	 |
| W d   dS 1 sw   Y  dS t| ||d 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
    :param bool force: Overwrite the destination file even if it exists.
    Nr   SphinxRendererzutf-8)encodingz`Aborted attempted copy from rendered template %s to %s (the destination path has existing data).misccopy_overwrite)typesubtypewz'Writing evaluated template result to %stemplate_evaluationr   )r   r   existsr   is_dirr   r   sphinx.util.templater(   openreadrender_stringr   loggerwarningfsdecodeinfowriter	   )r   r    r!   r#   r   r(   fsrctemplate_contentrendered_templatemsgfdstr   r   r   copy_asset_file&   sJ   
"
r@   c                 C  s   dS )NFr   )r   r   r   r   <lambda>j   s    rA   excludedr   onerror'Callable[[str, Exception], None] | Nonec                C  s8  t j| sdS |du rddlm} | }t| t j| r*t| ||||d dS t j| ddD ]h\}}	}
t	| |}|	
 D ]}|t||rO|	| q?tt||| q?|
D ]=}|t||sztt||t|||||d W q[ ty } z|r|t||| n W Y d}~q[d}~ww q[q1d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.

    Use ``copy_asset_file`` instead to copy a single 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.
    :param bool force: Overwrite the destination file even if it exists.
    Nr   r'   )r!   r#   r   T)followlinks)r   r   r0   r2   r(   r
   isfiler@   walkr   copy	posixpathjoinremove	Exception)r   r    rB   r!   r#   rC   r   r(   rootdirsfilesreldirdirr   excr   r   r   
copy_assetg   sJ   




rS   )r   r   r   r   )NN)r   r   r    r   r!   r"   r#   r$   r   r%   r   r&   )r   r   r    r   rB   r   r!   r"   r#   r$   rC   rD   r   r%   r   r&   )__doc__
__future__r   r   rI   pathlibr   typingr   r   docutils.utilsr   sphinx.localer   sphinx.utilr   sphinx.util.osutilr	   r
   collections.abcr   r2   r   sphinx.util.typingr   	getLogger__name__r6   r   r@   rS   r   r   r   r   <module>   s8    

D