B
    Khh:%                 @   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mZm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 ed	d
dZG dd
 d
eZdS )zR
This module provides an object oriented interface for pattern matching of files.
    )
Collection)zip_longest)	AnyStrCallabler   IterableIteratorOptionalTypeTypeVarUnion   )util)Pattern)StrPath	TreeEntry_filter_patterns_is_iterable
match_filenormalize_fileSelfPathSpec)Zboundc               @   s|  e Zd ZdZee ddddZeedddZ	e
d	d
dZed edddZed edddZeee eeeegef f ee edddZd#ddee eee  ee ee dddZeeZd$eeee  edddZd%ddee eee  ee ee dddZd&ddeee ee ee ee ddd Z d'ddeee ee ee ee dd!d"Z!e!Z"dS )(r   zd
	The :class:`PathSpec` class is a wrapper around a list of compiled
	:class:`.Pattern` instances.
	N)patternsreturnc             C   s   t |tr|nt|| _dS )z
		Initializes the :class:`PathSpec` instance.

		*patterns* (:class:`~collections.abc.Collection` or :class:`~collections.abc.Iterable`)
		yields each compiled pattern (:class:`.Pattern`).
		N)
isinstanceCollectionTypelistr   )selfr    r   7/tmp/pip-install-vv81h98y/pathspec/pathspec/pathspec.py__init__,   s    zPathSpec.__init__)otherr   c             C   s2   t |tr*t| j|j}tdd |D S tS dS )z
		Tests the equality of this path-spec with *other* (:class:`PathSpec`)
		by comparing their :attr:`~PathSpec.patterns` attributes.
		c             s   s   | ]\}}||kV  qd S )Nr   ).0abr   r   r   	<genexpr>A   s    z"PathSpec.__eq__.<locals>.<genexpr>N)r   r   r   r   allNotImplemented)r   r    Zpaired_patternsr   r   r   __eq__:   s    
zPathSpec.__eq__)r   c             C   s
   t | jS )zW
		Returns the number of compiled patterns this path-spec contains
		(:class:`int`).
		)lenr   )r   r   r   r   __len__E   s    zPathSpec.__len__)r   r    r   c             C   s$   t |tr| | j|j S tS dS )z]
		Combines the :attr:`Pathspec.patterns` patterns from two
		:class:`PathSpec` instances.
		N)r   r   	__class__r   r&   )r   r    r   r   r   __add__L   s    
zPathSpec.__add__c             C   s&   t |tr|  j|j7  _| S tS dS )zi
		Adds the :attr:`Pathspec.patterns` patterns from one :class:`PathSpec`
		instance to this instance.
		N)r   r   r   r&   )r   r    r   r   r   __iadd__V   s    
zPathSpec.__iadd__)clspattern_factorylinesr   c                s^   t  trt  t s,td dt|sDtd|d fdd|D }| |S )ai  
		Compiles the pattern lines.

		*pattern_factory* can be either the name of a registered pattern factory
		(:class:`str`), or a :class:`~collections.abc.Callable` used to compile
		patterns. It must accept an uncompiled pattern (:class:`str`) and return the
		compiled pattern (:class:`.Pattern`).

		*lines* (:class:`~collections.abc.Iterable`) yields each uncompiled pattern
		(:class:`str`). This simply has to yield each line so that it can be a
		:class:`io.TextIOBase` (e.g., from :func:`open` or :class:`io.StringIO`) or
		the result from :meth:`str.splitlines`.

		Returns the :class:`PathSpec` instance.
		zpattern_factory:z is not callable.zlines:z is not an iterable.c                s   g | ]}|r |qS r   r   )r!   line)r.   r   r   
<listcomp>   s    z'PathSpec.from_lines.<locals>.<listcomp>)r   strr   Zlookup_patterncallable	TypeErrorr   )r-   r.   r/   r   r   )r.   r   
from_linesa   s    

zPathSpec.from_lines)negate)entries
separatorsr6   r   c            c   s`   t |std|dt| j}x8|D ]0}t|j|}| ||}|rN| }|r(|V  q(W dS )a  
		Matches the entries to this path-spec.

		*entries* (:class:`~collections.abc.Iterable` of :class:`~util.TreeEntry`)
		contains the entries to be matched against :attr:`self.patterns <PathSpec.patterns>`.

		*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
		:data:`None`) optionally contains the path separators to normalize. See
		:func:`~pathspec.util.normalize_file` for more information.

		*negate* (:class:`bool` or :data:`None`) is whether to negate the match
		results of the patterns. If :data:`True`, a pattern matching a file will
		exclude the file rather than include it. Default is :data:`None` for
		:data:`False`.

		Returns the matched entries (:class:`~collections.abc.Iterator` of
		:class:`~util.TreeEntry`).
		zentries:z is not an iterable.N)r   r4   r   r   r   path_match_file)r   r7   r8   r6   use_patternsentry	norm_fileis_matchr   r   r   match_entries   s    

zPathSpec.match_entries)filer8   r   c             C   s   t j||d}| | j|S )a  
		Matches the file to this path-spec.

		*file* (:class:`str` or :class:`os.PathLike[str]`) is the file path to be
		matched against :attr:`self.patterns <PathSpec.patterns>`.

		*separators* (:class:`~collections.abc.Collection` of :class:`str`)
		optionally contains the path separators to normalize. See
		:func:`~pathspec.util.normalize_file` for more information.

		Returns :data:`True` if *file* matched; otherwise, :data:`False`.
		)r8   )r   r   r:   r   )r   r@   r8   r=   r   r   r   r      s    zPathSpec.match_file)filesr8   r6   r   c            c   s^   t |std|dt| j}x6|D ].}t||}| ||}|rL| }|r(|V  q(W dS )a  
		Matches the files to this path-spec.

		*files* (:class:`~collections.abc.Iterable` of :class:`str` or
		:class:`os.PathLike[str]`) contains the file paths to be matched against
		:attr:`self.patterns <PathSpec.patterns>`.

		*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
		:data:`None`) optionally contains the path separators to normalize. See
		:func:`~pathspec.util.normalize_file` for more information.

		*negate* (:class:`bool` or :data:`None`) is whether to negate the match
		results of the patterns. If :data:`True`, a pattern matching a file will
		exclude the file rather than include it. Default is :data:`None` for
		:data:`False`.

		Returns the matched files (:class:`~collections.abc.Iterator` of
		:class:`str` or :class:`os.PathLike[str]`).
		zfiles:z is not an iterable.N)r   r4   r   r   r   r:   )r   rA   r8   r6   r;   Z	orig_filer=   r>   r   r   r   match_files   s    


zPathSpec.match_files)rooton_errorfollow_linksr6   r   c            c   s(   t j|||d}| j||dE dH  dS )a  
		Walks the specified root path for all files and matches them to this
		path-spec.

		*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
		search.

		*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally
		is the error handler for file-system exceptions. See
		:func:`~pathspec.util.iter_tree_entries` for more information.

		*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
		symbolic links that resolve to directories. See
		:func:`~pathspec.util.iter_tree_files` for more information.

		*negate* (:class:`bool` or :data:`None`) is whether to negate the match
		results of the patterns. If :data:`True`, a pattern matching a file will
		exclude the file rather than include it. Default is :data:`None` for
		:data:`False`.

		Returns the matched files (:class:`~collections.abc.Iterator` of
		:class:`.TreeEntry`).
		)rD   rE   )r6   N)r   Ziter_tree_entriesr?   )r   rC   rD   rE   r6   r7   r   r   r   match_tree_entries   s    zPathSpec.match_tree_entriesc            c   s(   t j|||d}| j||dE dH  dS )a  
		Walks the specified root path for all files and matches them to this
		path-spec.

		*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
		search for files.

		*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally
		is the error handler for file-system exceptions. See
		:func:`~pathspec.util.iter_tree_files` for more information.

		*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
		symbolic links that resolve to directories. See
		:func:`~pathspec.util.iter_tree_files` for more information.

		*negate* (:class:`bool` or :data:`None`) is whether to negate the match
		results of the patterns. If :data:`True`, a pattern matching a file will
		exclude the file rather than include it. Default is :data:`None` for
		:data:`False`.

		Returns the matched files (:class:`~collections.abc.Iterable` of
		:class:`str`).
		)rD   rE   )r6   N)r   Ziter_tree_filesrB   )r   rC   rD   rE   r6   rA   r   r   r   match_tree_files  s    zPathSpec.match_tree_files)N)N)N)NN)NN)#__name__
__module____qualname____doc__r   r   r   objectboolr'   intr)   r   r+   r,   classmethodr	   r   r2   r   r   r5   r   r   r   r   r?   staticmethodr   r:   r   rB   rF   rG   Z
match_treer   r   r   r   r   &   s:   
$%  N)rK   collections.abcr   r   	itertoolsr   typingr   r   r   r   r   r	   r
   r    r   patternr   r   r   r   r   r   r   r   rL   r   r   r   r   r   <module>   s   , 