B
    .ÛKc˜  ã               @   s  U d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZ ddlmZmZ eedœdd„Zee e	eegee
e  f  dœd	d
„ZG dd„ dƒZedgƒZi Zeeef ed< eeee
e  dœdd„Zee ee	e dœdd„Zdeee ee ee dœdd„ZdS )z.Pattern-matching utility functions for Sphinx.é    N)ÚCallableÚDictÚIterableÚIteratorÚListÚMatchÚOptionalÚPattern)Ú
canon_pathÚpath_stabilize)ÚpatÚreturnc             C   sn  dt | ƒ }}d}xP||k rd| | }|d7 }|dkrh||k r^| | dkr^|d7 }|d }n|d }q|dkrz|d }q|d	krT|}||k r¤| | d
kr¤|d7 }||k rÀ| | dkrÀ|d7 }x ||k rà| | dkrà|d7 }qÂW ||krô|d }n^| ||…  dd¡}|d }|d d
kr0d|dd…  }n|d dkrFd| }d||f }q|t |¡7 }qW |d S )z£Translate 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ú^z%s[%s]ú$)ÚlenÚreplaceÚreÚescape)r   ÚiÚnZresÚcÚjZstuff© r    ú3lib/python3.7/site-packages/sphinx/util/matching.pyÚ_translate_pattern
   s>    




r"   )Ú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>9   s    z$compile_matchers.<locals>.<listcomp>r    )r#   r    r    r!   Úcompile_matchers8   s    r(   c               @   sD   e Zd ZdZee ddœdd„Zeedœdd„Zeedœd	d
„Z	dS )ÚMatcherz¾A 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"
    N)Úexclude_patternsr   c             C   s$   dd„ |D ƒ}t t|ƒ| ƒ| _d S )Nc             S   s"   g | ]}|  d ¡r|dd… ‘qS )z**/é   N)Ú
startswith)r&   r   r    r    r!   r'   D   s    z$Matcher.__init__.<locals>.<listcomp>)r(   Úlistr#   )Úselfr*   Zexpandedr    r    r!   Ú__init__C   s    zMatcher.__init__)Ústringr   c             C   s
   |   |¡S )N)r%   )r.   r0   r    r    r!   Ú__call__G   s    zMatcher.__call__c                s    t ˆ ƒ‰ t‡ fdd„| jD ƒƒS )Nc             3   s   | ]}|ˆ ƒV  qd S )Nr    )r&   r   )r0   r    r!   ú	<genexpr>L   s    z Matcher.match.<locals>.<genexpr>)r
   Úanyr#   )r.   r0   r    )r0   r!   r%   J   s    zMatcher.match)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr/   Úboolr1   r%   r    r    r    r!   r)   <   s   r)   z**/.*Ú
_pat_cache)Ú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!   ÚpatmatchU   s    r<   )Únamesr   r   c             C   s2   |t krt t|ƒ¡t |< t | j}tt|| ƒƒS )z‡Return the subset of the list ``names`` that match
    the regular expression (pattern) ``pat``.

    Adapted from fnmatch module.
    )r:   r   r$   r"   r%   r-   Úfilter)r=   r   r%   r    r    r!   Ú	patfilter]   s    
r?   ©z**r    )ÚdirnameÚinclude_patternsr*   r   c             c   s4  t j t j | ¡¡} t|ƒ}t|ƒ}xt j| ddD ]ö\}}}t j || ¡}|dkrZd}g }	xlt|ƒD ]`}
tt j 	||
¡ƒ}
d}x|D ]}||
ƒrˆd}P qˆW x|D ]}||
ƒr¤d}P q¤W |rh|	 
|
¡ qhW g }xDt|ƒD ]8}tt j 	||¡ƒ}x |D ]}||ƒröP qöW | 
|¡ qÚW ||dd…< |	E dH  q6W d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)rA   rB   r*   Zexclude_matchersZinclude_matchersÚrootÚdirsÚfilesZrelative_rootZincluded_filesÚentryZkeepZmatcherZfiltered_dirsZdir_nameZ
normalisedr    r    r!   Úget_matching_filesi   s<    


rR   )r@   r    )r7   Zos.pathrE   r   Útypingr   r   r   r   r   r   r   r	   Zsphinx.util.osutilr
   r   r8   r"   r(   r)   ZDOTFILESr:   Ú__annotations__r<   r?   rR   r    r    r    r!   Ú<module>   s   (.*
 