
    Fe                        U 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
mZ erddlmZmZ ddZ	 	 	 	 ddZ G d	 d
      Z edg      Zi Zded<   ddZddZ	 	 d	 	 	 	 	 	 	 ddZy)z.Pattern-matching utility functions for Sphinx.    )annotationsN)TYPE_CHECKINGCallable)
canon_pathpath_stabilize)IterableIteratorc                   dt        |       }}d}||k  r| |   }|dz  }|dk(  r||k  r| |   dk(  r|dz  }|dz   }n|dz   }n|dk(  r|dz   }n|d	k(  r|}||k  r| |   d
k(  r|dz  }||k  r| |   dk(  r|dz  }||k  r| |   dk7  r|dz  }||k  r	| |   dk7  r||k\  r|dz   }nY| || j                  dd      }|dz   }|d   d
k(  r	d|dd z   }n|d   dk(  rd|z   }| d	| d}n|t        j                  |      z  }||k  r|dz   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)patinrescjstuffs          6/usr/lib/python3/dist-packages/sphinx/util/matching.py_translate_patternr!      s|    c#hqA
C
a%F	Q81uQ3QDj Gm#X,C#XA1uQ3Q1uQ3Qa%CFcMQ a%CFcMAvEkAa((v6E8s? 59,E1X_ 5LEQugQ'299Q<CE a%F 9    c                x    | D cg c]*  }t        j                  t        |            j                  , c}S c c}w N)r   compiler!   match)patternsr   s     r    compile_matchersr(   =   s/     BJJ#BJJ)#./55JJJs   /7c                  (    e Zd ZdZddZddZddZy)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"
    c                    |D cg c]  }|j                  d      s|dd   }}t        t        |      |z         | _        y c c}w )Nz**/   )
startswithr(   listr'   )selfexclude_patternsr   expandeds       r    __init__zMatcher.__init__J   sC    '7Q3>>%;PCGQQ(.>)?()JK Rs
   AAc                $    | j                  |      S r$   )r&   r/   strings     r    __call__zMatcher.__call__N   s    zz&!!r"   c                V    t              t        fd| j                  D              S )Nc              3  .   K   | ]  } |        y wr$    ).0r   r5   s     r    	<genexpr>z Matcher.match.<locals>.<genexpr>S   s     833v;8s   )r   anyr'   r4   s    `r    r&   zMatcher.matchQ   s"    F#8$--888r"   N)r0   Iterable[str]returnNone)r5   strr>   bool)__name__
__module____qualname____doc__r2   r6   r&   r9   r"   r    r*   r*   C   s    L"9r"   r*   z**/.*zdict[str, re.Pattern[str]]
_pat_cachec                    |t         vr%t        j                  t        |            t         |<   t         |   j	                  |       S )zbReturn if name matches the regular expression (pattern)
    ``pat```. Adapted from fnmatch module.)rF   r   r%   r!   r&   )namer   s     r    patmatchrI   \   s9     ***%7%<=
3c?  &&r"   c                    |t         vr%t        j                  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.
    )rF   r   r%   r!   r&   r.   filter)namesr   r&   s      r    	patfilterrM   d   sE     ***%7%<=
3sO!!Eue$%%r"   c              #    K   t         j                  j                  t         j                  j                  |             } t	        |      }t	        |      }t        j
                  | d      D ]  \  }}}t         j                  j                  ||       }|dk(  rd}g }	t        |      D ]g  }
t        t         j                  j                  ||
            }
d}|D ]  } ||
      sd} n |D ]  } ||
      sd} n |sW|	j                  |
       i g }t        |      D ]M  }t        t         j                  j                  ||            }|D ]  } ||      s < |j                  |       O ||dd |	E d{     y7 w)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)dirnameinclude_patternsr0   exclude_matchersinclude_matchersrootdirsfilesrelative_rootincluded_filesentrykeepmatcherfiltered_dirsdir_name
normaliseds                   r    get_matching_filesri   p   s     ggrwww78G'(89'(89WWW$? %"dEg6CM E] 	-E"277<<u#EFED+ 5>D
 , 5> D
 %%e,	-" t 	/H']H(MNJ+ /:&/
 $$X.	/  Q "!!K%"J 	"s+   C!F$F7F?AF F9F:	F)r   r@   r>   r@   )r'   r=   r>   z+list[Callable[[str], re.Match[str] | None]])rH   r@   r   r@   r>   zre.Match[str] | None)rL   r=   r   r@   r>   z	list[str]))z**r9   )rZ   zstr | os.PathLike[str]r[   r=   r0   r=   r>   zIterator[str])rE   
__future__r   os.pathrQ   r   typingr   r   sphinx.util.osutilr   r   collections.abcr   r	   r!   r(   r*   DOTFILESrF   __annotations__rI   rM   ri   r9   r"   r    <module>rq      s    4 "  	 * 92+\KK0K9 9& G9 *,
& +'	& '.&(9"#9"#9" $9" 	9"r"   