U
    \Sh                     @  s   U d Z ddlmZ ddlZddlZddlmZmZm	Z	 ddl
mZmZ ddddd	Zd
ddddZG dd dZedgZi Zded< ddddddZd
dddddZd"dd
d
ddd d!ZdS )#z.Pattern-matching utility functions for Sphinx.    )annotationsN)CallableIterableIterator)
canon_pathpath_stabilizestr)patreturnc                 C  sh  dt |  }}d}||k r`| | }|d7 }|dkrd||k rZ| | dkrZ|d7 }|d }n|d }q|dkrv|d }q|d	krP|}||k r| | d
kr|d7 }||k r| | dkr|d7 }||k r| | dkr|d7 }q||kr|d }nb| || dd}|d }|d d
kr(d|dd  }n|d dkr>d| }| d	| d}q|t|7 }q|d S )zTranslate a shell-style glob pattern to a regular expression.

    Adapted from the fnmatch module, but enhanced so that single stars don't
    match slashes.
    r       *z.*z[^/]*?z[^/][!]z\[\z\\z^/N^$)lenreplacereescape)r	   inrescjstuff r   G/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/matching.py_translate_pattern   s>    






r!   Iterable[str]z+list[Callable[[str], re.Match[str] | None]])patternsr
   c                 C  s   dd | D S )Nc                 S  s   g | ]}t t|jqS r   )r   compiler!   match.0r	   r   r   r    
<listcomp>=   s     z$compile_matchers.<locals>.<listcomp>r   )r#   r   r   r    compile_matchers:   s    r)   c                   @  s@   e Zd ZdZdddddZddd	d
dZddd	ddZdS )MatcherzA pattern matcher for Multiple shell-style glob patterns.

    Note: this modifies the patterns to work with copy_asset().
          For example, "**/index.rst" matches with "index.rst"
    r"   None)exclude_patternsr
   c                 C  s$   dd |D }t t|| | _d S )Nc                 S  s"   g | ]}| d r|dd qS )z**/   N)
startswithr&   r   r   r    r(   H   s     
 z$Matcher.__init__.<locals>.<listcomp>)r)   listr#   )selfr,   expandedr   r   r    __init__G   s    zMatcher.__init__r   bool)stringr
   c                 C  s
   |  |S N)r%   r0   r4   r   r   r    __call__K   s    zMatcher.__call__c                   s    t   t fdd| jD S )Nc                 3  s   | ]}| V  qd S r5   r   r&   r4   r   r    	<genexpr>P   s     z Matcher.match.<locals>.<genexpr>)r   anyr#   r6   r   r8   r    r%   N   s    zMatcher.matchN)__name__
__module____qualname____doc__r2   r7   r%   r   r   r   r    r*   @   s   r*   z**/.*zdict[str, re.Pattern[str]]
_pat_cachezre.Match[str] | None)namer	   r
   c                 C  s(   |t krtt|t |< t | | S )zbReturn if name matches the regular expression (pattern)
    ``pat```. Adapted from fnmatch module.)r?   r   r$   r!   r%   )r@   r	   r   r   r    patmatchY   s    rA   z	list[str])namesr	   r
   c                 C  s2   |t krtt|t |< t | j}tt|| S )zReturn the subset of the list ``names`` that match
    the regular expression (pattern) ``pat``.

    Adapted from fnmatch module.
    )r?   r   r$   r!   r%   r/   filter)rB   r	   r%   r   r   r    	patfiltera   s    
rD   z**r   zIterator[str])dirnameinclude_patternsr,   r
   c                 c  s   t jt j| } t|}t|}t j| ddD ]\}}}t j|| }|dkrVd}g }	t|D ]\}
tt j	||
}
d}|D ]}||
rd} qq|D ]}||
rd} qq|rb|	
|
 qbg }t|D ]6}tt j	||}|D ]}||r qq|
| q||dd< |	E dH  q2dS )ac  Get all file names in a directory, recursively.

    Filter file names by the glob-style include_patterns and exclude_patterns.
    The default values include all files ("**") and exclude nothing ("").

    Only files matching some pattern in *include_patterns* are included, and
    exclusions from *exclude_patterns* take priority over inclusions.

    T)followlinks.r   FN)ospathnormpathabspathr)   walkrelpathsortedr   joinappend)rF   rG   r,   Zexclude_matchersZinclude_matchersrootdirsfilesZrelative_rootZincluded_filesentryZkeepZmatcherZfiltered_dirsdir_nameZ
normalisedr   r   r    get_matching_filesm   s<    rX   )rE   r   )r>   
__future__r   os.pathrJ   r   typingr   r   r   Zsphinx.util.osutilr   r   r!   r)   r*   ZDOTFILESr?   __annotations__rA   rD   rX   r   r   r   r    <module>   s   .
  