o
    Q `o                     @   s   d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ej
r/ddlmZmZmZmZmZ e	dZd	d
 Zdd Zdd Zdd Zdd ZdddZdS )zMatch wildcard filenames.
    )unicode_literalsprint_functionN)partial   )LRUCache)CallableIterableTextTuplePatterni  c                 C   sX   zt | df }W n ty$   dt|  d }t| t | df< }Y nw ||duS )zTest whether a name matches a wildcard pattern.

    Arguments:
        pattern (str): A wildcard pattern, e.g. ``"*.py"``.
        name (str): A filename.

    Returns:
        bool: `True` if the filename matches the pattern.

    T(?ms)\ZN)_PATTERN_CACHEKeyError
_translaterecompilematchpatternnameZre_patres r   -/usr/lib/python3/dist-packages/fs/wildcard.pyr      s   r   c                 C   s`   zt | df }W n  ty(   dt| dd d }t|tj t | df< }Y nw ||duS )zTest whether a name matches a wildcard pattern (case insensitive).

    Arguments:
        pattern (str): A wildcard pattern, e.g. ``"*.py"``.
        name (bool): A filename.

    Returns:
        bool: `True` if the filename matches the pattern.

    Fr   )case_sensitiver   N)r   r   r   r   r   
IGNORECASEr   r   r   r   r   imatch(   s   r   c                       | sdS t  fdd| D S )aQ  Test if a name matches any of a list of patterns.

    Will return `True` if ``patterns`` is an empty list.

    Arguments:
        patterns (list): A list of wildcard pattern, e.g ``["*.py",
            "*.pyc"]``
        name (str): A filename.

    Returns:
        bool: `True` if the name matches at least one of the patterns.

    Tc                 3       | ]}t | V  qd S N)r   .0r   r   r   r   	<genexpr>M       zmatch_any.<locals>.<genexpr>anypatternsr   r   r"   r   	match_any<      r)   c                    r   )ad  Test if a name matches any of a list of patterns (case insensitive).

    Will return `True` if ``patterns`` is an empty list.

    Arguments:
        patterns (list): A list of wildcard pattern, e.g ``["*.py",
            "*.pyc"]``
        name (str): A filename.

    Returns:
        bool: `True` if the name matches at least one of the patterns.

    Tc                 3   r   r   )r   r    r"   r   r   r#   a   r$   zimatch_any.<locals>.<genexpr>r%   r'   r   r"   r   
imatch_anyP   r*   r+   c                 C   s$   | sdd S |rt t| S t t| S )a  Get a callable that matches names against the given patterns.

    Arguments:
        patterns (list): A list of wildcard pattern. e.g. ``["*.py",
            "*.pyc"]``
        case_sensitive (bool): If ``True``, then the callable will be case
            sensitive, otherwise it will be case insensitive.

    Returns:
        callable: a matcher that will return `True` if the name given as
        an argument matches any of the given patterns.

    Example:
        >>> from fs import wildcard
        >>> is_python = wildcard.get_matcher(['*.py'], True)
        >>> is_python('__init__.py')
        True
        >>> is_python('foo.txt')
        False

    c                 S   s   dS )NTr   r"   r   r   r   <lambda>|   s    zget_matcher.<locals>.<lambda>)r   r)   r+   )r(   r   r   r   r   get_matcherd   s
   

r-   Tc                 C   sV  |s|   } dt| }}d}||k r| | }|d }|dkr$|d }n|dkr-|d }nx|dkr|}||k rA| | d	krA|d }||k rO| | d
krO|d }||k rg| | d
krg|d }||k rg| | d
ksY||krp|d }n5| || dd}|d }|d d	krd|dd  }n
|d dkrd| }d||f }n|t| }||k s|S )aU  Translate a wildcard pattern to a regular expression.

    There is no way to quote meta-characters.

    Arguments:
        pattern (str): A wildcard pattern.
        case_sensitive (bool): Set to `False` to use a case
            insensitive regex (default `True`).

    Returns:
        str: A regex equivalent to the given pattern.

    r    r   *z[^/]*?.[!]z\[\z\\^Nz%s[%s])lowerlenreplacer   escape)r   r   inr   cjstuffr   r   r   r      s@   


r   )T)__doc__Z
__future__r   r   r   typing	functoolsr   Zlrucacher   ZTYPE_CHECKINGr   r   r	   r
   r   r   r   r   r)   r+   r-   r   r   r   r   r   <module>   s    