U
    ”Ê¹j²  ã                	   @   sÔ   d Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	m
Z
mZ e
dƒZdddd	d
dddgZddgZddddœee e	eee ef  e	eee ef  e	eegef  eeddf dœdd„Zeedœdd„ZdS )z6Contains utilities to handle paths in Huggingface Hub.é    ©Úfnmatch)ÚPath)ÚCallableÚ	GeneratorÚIterableÚListÚOptionalÚTypeVarÚUnionÚTz.gitz.git/*z*/.gitz
**/.git/**z.cache/huggingfacez.cache/huggingface/*z*/.cache/huggingfacez**/.cache/huggingface/**z.cacheN)Úallow_patternsÚignore_patternsÚkey)Úitemsr   r   r   Úreturnc                #   sÄ   t |tƒr|g}t |tƒr |g}|dk	r6dd„ |D ƒ}|dk	rLdd„ |D ƒ}|dkrhttdœdd„}|}| D ]R}||ƒ‰ |dk	r˜t‡ fdd	„|D ƒƒs˜ql|dk	r¸t‡ fd
d	„|D ƒƒr¸ql|V  qldS )aÀ  Filter repo objects based on an allowlist and a denylist.

    Input must be a list of paths (`str` or `Path`) or a list of arbitrary objects.
    In the later case, `key` must be provided and specifies a function of one argument
    that is used to extract a path from each element in iterable.

    Patterns are Unix shell-style wildcards which are NOT regular expressions. See
    https://docs.python.org/3/library/fnmatch.html for more details.

    Args:
        items (`Iterable`):
            List of items to filter.
        allow_patterns (`str` or `List[str]`, *optional*):
            Patterns constituting the allowlist. If provided, item paths must match at
            least one pattern from the allowlist.
        ignore_patterns (`str` or `List[str]`, *optional*):
            Patterns constituting the denylist. If provided, item paths must not match
            any patterns from the denylist.
        key (`Callable[[T], str]`, *optional*):
            Single-argument function to extract a path from each item. If not provided,
            the `items` must already be `str` or `Path`.

    Returns:
        Filtered list of objects, as a generator.

    Raises:
        :class:`ValueError`:
            If `key` is not provided and items are not `str` or `Path`.

    Example usage with paths:
    ```python
    >>> # Filter only PDFs that are not hidden.
    >>> list(filter_repo_objects(
    ...     ["aaa.PDF", "bbb.jpg", ".ccc.pdf", ".ddd.png"],
    ...     allow_patterns=["*.pdf"],
    ...     ignore_patterns=[".*"],
    ... ))
    ["aaa.pdf"]
    ```

    Example usage with objects:
    ```python
    >>> list(filter_repo_objects(
    ... [
    ...     CommitOperationAdd(path_or_fileobj="/tmp/aaa.pdf", path_in_repo="aaa.pdf")
    ...     CommitOperationAdd(path_or_fileobj="/tmp/bbb.jpg", path_in_repo="bbb.jpg")
    ...     CommitOperationAdd(path_or_fileobj="/tmp/.ccc.pdf", path_in_repo=".ccc.pdf")
    ...     CommitOperationAdd(path_or_fileobj="/tmp/.ddd.png", path_in_repo=".ddd.png")
    ... ],
    ... allow_patterns=["*.pdf"],
    ... ignore_patterns=[".*"],
    ... key=lambda x: x.repo_in_path
    ... ))
    [CommitOperationAdd(path_or_fileobj="/tmp/aaa.pdf", path_in_repo="aaa.pdf")]
    ```
    Nc                 S   s   g | ]}t |ƒ‘qS © ©Ú_add_wildcard_to_directories©Ú.0Úpr   r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/utils/_paths.pyÚ
<listcomp>m   s     z'filter_repo_objects.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r   o   s     )Úitemr   c                 S   s4   t | tƒr| S t | tƒr t| ƒS td| › d�ƒ‚d S )Nz9Please provide `key` argument in `filter_repo_objects`: `z` is not a string.)Ú
isinstanceÚstrr   Ú
ValueError)r   r   r   r   Ú	_identitys   s
    

z&filter_repo_objects.<locals>._identityc                 3   s   | ]}t ˆ |ƒV  qd S ©Nr   ©r   Úr©Úpathr   r   Ú	<genexpr>€   s     z&filter_repo_objects.<locals>.<genexpr>c                 3   s   | ]}t ˆ |ƒV  qd S r   r   r    r"   r   r   r$   „   s     )r   r   r   Úany)r   r   r   r   r   r   r   r"   r   Úfilter_repo_objects'   s$    ?

r&   )Úpatternr   c                 C   s   | d dkr| d S | S )Néÿÿÿÿú/Ú*r   )r'   r   r   r   r   Š   s    r   )Ú__doc__r   Úpathlibr   Útypingr   r   r   r   r	   r
   r   r   ZDEFAULT_IGNORE_PATTERNSZFORBIDDEN_FOLDERSr   r&   r   r   r   r   r   Ú<module>   s2   $øûúc