o
    yAf3                     @   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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)CheckResultStrPathTStrPath	TreeEntry_filter_check_patterns_is_iterablenormalize_fileSelfPathSpec)boundc                   @   sb  e Zd ZdZdee ddfddZdedefdd	Z	de
fd
dZdedd defddZdedd defddZ	d/dedeee  dee fddZ	d/dee deee  deee  fddZ		d0dedeeegdf  dee deee  fddZedee deeeegef f dee defd d!Z	d/dd"d#ee  deee  d$ee dee  fd%d&Z!e"e#j$Z%	 	d/dedeee  defd'd(Z&	d/dd"dee deee  d$ee dee fd)d*Z'		d0dd"dedeeegdf  dee d$ee dee  f
d+d,Z(		d0dd"dedeeegdf  dee d$ee dee f
d-d.Z)e)Z*dS )1r   zd
	The :class:`PathSpec` class is a wrapper around a list of compiled
	:class:`.Pattern` instances.
	patternsreturnNc                 C   s   t |ts	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   6/usr/lib/python3.10/site-packages/pathspec/pathspec.py__init__-   s   
zPathSpec.__init__otherc                 C   s.   t |trt| j|j}tdd |D S t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>D   s    z"PathSpec.__eq__.<locals>.<genexpr>)r   r   r   r   allNotImplemented)r   r"   Zpaired_patternsr   r   r    __eq__=   s   
zPathSpec.__eq__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__H   s   
zPathSpec.__len__r   c                 C   s    t |tr| | j|j S tS )z]
		Combines the :attr:`Pathspec.patterns` patterns from two
		:class:`PathSpec` instances.
		)r   r   	__class__r   r)   r   r"   r   r   r    __add__O   s   
zPathSpec.__add__c                 C   s"   t |tr|  j|j7  _| S tS )zi
		Adds the :attr:`Pathspec.patterns` patterns from one :class:`PathSpec`
		instance to this instance.
		)r   r   r   r)   r.   r   r   r    __iadd__Y   s   
zPathSpec.__iadd__file
separatorsc                 C   s,   t ||}| t| j|\}}t|||S )a  
		Check the files against this path-spec.

		*file* (:class:`str` or :class:`os.PathLike`) is the file path 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.

		Returns the file check result (:class:`~pathspec.util.CheckResult`).
		)r   _match_file	enumerater   r   )r   r1   r2   	norm_fileincludeindexr   r   r    
check_filed   s   
zPathSpec.check_filefilesc                 c   sZ    t |std|dt| j}|D ]}t||}| ||\}}t|||V  qdS )a.  
		Check the files against this path-spec.

		*files* (:class:`~collections.abc.Iterable` of :class:`str` or
		:class:`os.PathLike`) contains the file paths to be checked 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.

		Returns an :class:`~collections.abc.Iterator` yielding each file check
		result (:class:`~pathspec.util.CheckResult`).
		files: is not an iterable.N)r   	TypeErrorr   r   r   r3   r   )r   r9   r2   use_patterns	orig_filer5   r6   r7   r   r   r    check_filesy   s   

zPathSpec.check_filesrooton_errorfollow_linksc                 c   s&    t j|||d}| |E dH  dS )a  
		Walks the specified root path for all files and checks them against this
		path-spec.

		*root* (:class:`str` or :class:`os.PathLike`) 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. It will be called with the
		exception (:exc:`OSError`). Reraise the exception to abort the walk. Default
		is :data:`None` to ignore file-system exceptions.

		*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
		symbolic links that resolve to directories. Default is :data:`None` for
		:data:`True`.

		*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 an :class:`~collections.abc.Iterator` yielding each file check
		result (:class:`~pathspec.util.CheckResult`).
		rA   rB   N)r   iter_tree_filesr?   )r   r@   rA   rB   r9   r   r   r    check_tree_files   s   zPathSpec.check_tree_filesclspattern_factorylinesc                    s^   t  tr
t  t std dt|s"t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:r;   c                    s   g | ]}|r |qS r   r   )r$   linerG   r   r    
<listcomp>   s    z'PathSpec.from_lines.<locals>.<listcomp>)r   strr   Zlookup_patterncallabler<   r   )rF   rG   rH   r   r   rJ   r    
from_lines   s   

zPathSpec.from_linesnegateentriesrP   c          	      c   sb    t |std|dt| j}|D ]}t|j|}| ||\}}|r)| }|r.|V  qdS )a  
		Matches the entries to this path-spec.

		*entries* (:class:`~collections.abc.Iterable` of :class:`~pathspec.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:`~pathspec.util.TreeEntry`).
		zentries:r;   N)r   r<   r   r   r   pathr3   )	r   rQ   r2   rP   r=   entryr5   r6   _indexr   r   r    match_entries   s   
zPathSpec.match_entriesc                 C   s(   t ||}| t| j|\}}t|S )a  
		Matches the file to this path-spec.

		*file* (:class:`str` or :class:`os.PathLike`) 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`.
		)r   r3   r4   r   bool)r   r1   r2   r5   r6   rT   r   r   r    
match_file  s   
zPathSpec.match_filec          	      c   s`    t |std|dt| j}|D ]}t||}| ||\}}|r(| }|r-|V  qdS )a  
		Matches the files to this path-spec.

		*files* (:class:`~collections.abc.Iterable` of :class:`str` or
		:class:`os.PathLike`) 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`).
		r:   r;   N)r   r<   r   r   r   r3   )	r   r9   r2   rP   r=   r>   r5   r6   rT   r   r   r    match_files  s   

zPathSpec.match_filesc                c   *    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`) is the root directory to
		search.

		*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally
		is the error handler for file-system exceptions. It will be called with the
		exception (:exc:`OSError`). Reraise the exception to abort the walk. Default
		is :data:`None` to ignore file-system exceptions.

		*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
		symbolic links that resolve to directories. Default is :data:`None` for
		:data:`True`.

		*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`).
		rC   rO   N)r   Ziter_tree_entriesrU   )r   r@   rA   rB   rP   rQ   r   r   r    match_tree_entriesB      zPathSpec.match_tree_entriesc                c   rY   )a  
		Walks the specified root path for all files and matches them to this
		path-spec.

		*root* (:class:`str` or :class:`os.PathLike`) 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. It will be called with the
		exception (:exc:`OSError`). Reraise the exception to abort the walk. Default
		is :data:`None` to ignore file-system exceptions.

		*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
		symbolic links that resolve to directories. Default is :data:`None` for
		:data:`True`.

		*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`).
		rC   rO   N)r   rD   rX   )r   r@   rA   rB   rP   r9   r   r   r    match_tree_filese  r[   zPathSpec.match_tree_filesr#   )NN)+__name__
__module____qualname____doc__r   r   r!   objectrV   r*   intr,   r   r/   r0   r   r   r   rL   r   r8   r   r?   r   r   OSErrorrE   classmethodr	   r   r   rN   r   rU   staticmethodr   Zcheck_match_filer3   rW   rX   rZ   r\   Z
match_treer   r   r   r    r   '   s    







!#


'	



+
&
%N)r`   collections.abcr   r   	itertoolsr   typingr   r   r   r   r   r	   r
   r    r   patternr   r   r   r   r   r   r   r   r   ra   r   r   r   r   r    <module>   s    ,$	