o
    yAfO1                     @   s   d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 dZ	 dZ	 G d	d
 d
eZG dd de
Zede G dd deZede dS )z
This module implements Git's wildmatch pattern matching which itself is derived
from Rsync's wildmatch. Git uses wildmatch for its ".gitignore" files.
    N)AnyStrOptionalTuple   )util)RegexPatternlatin1Zps_dc                   @   s   e Zd ZdZdS )GitWildMatchPatternErrorzW
	The :class:`GitWildMatchPatternError` indicates an invalid git wild match
	pattern.
	N)__name__
__module____qualname____doc__ r   r   C/usr/lib/python3.10/site-packages/pathspec/patterns/gitwildmatch.pyr	      s    r	   c                   @   sf   e Zd ZdZdZededeee ee	 f fddZ
ededefddZed	edefd
dZdS )GitWildMatchPatternzX
	The :class:`GitWildMatchPattern` class represents a compiled Git wildmatch
	pattern.
	r   patternreturnc                 C   sb  t |trt}nt |trt}|t}ntd|d|}|dr)| }n| }|	dr8d}d}nf|dkrBd}d}n\|r|	drSd}|d	d }nd
}d}|
d}|d  }tt|d	 ddD ]}	||	d	  }
||	 }|
dkr|dkr||	= qkt|dkr|d dkr|d	 sdt d}|d s|d= nt|d	kst|dkr|d	 s|d dkr|dd n	 |std||d st|d	krd|d< |du rdg}d}t|d	 }t|D ]\}	}|dkr0|	dkr|	|kr|d q|	dkr|d d}q|	|kr(|r"|dt d q|d q|d d
}q|dkrS|r=|d |d |	|krP|dt d d
}q|r[|d z
|| | W n tyz } ztd||d}~ww |	|kr|dt d d
}q|d d|}n|}nd}d}|dur|tu r|t}||fS )a  
		Convert the pattern into a regular expression.

		*pattern* (:class:`str` or :class:`bytes`) is the pattern to convert into a
		regular expression.

		Returns the uncompiled regular expression (:class:`str`, :class:`bytes`, or
		:data:`None`); and whether matched files should be included (:data:`True`),
		excluded (:data:`False`), or if it is a null-operation (:data:`None`).
		zpattern:! is not a unicode or byte string.z\ #N/!F   Tr   z**r   z^.+(?P<z>/).*$zInvalid git pattern: ^z[^/]+(?:/.*)?z(?:.+/)?z(?P<z>/).*z/.*z(?:/.+)?*z[^/]+z(?:(?P<z>/).*)?$ )
isinstancestrbytesdecode_BYTES_ENCODING	TypeErrorendswithlstripstrip
startswithsplitrangelen	_DIR_MARKinsertr	   	enumerateappend_translate_segment_glob
ValueErrorjoinencode)clsr   return_typeZoriginal_patternregexincludeZoverride_regexZpattern_segsZis_dir_patterniprevsegoutputZ
need_slashender   r   r   pattern_to_regex-   s   







  













z$GitWildMatchPattern.pattern_to_regexc                 C   s  d}d}dt | }}||k r| | }|d7 }|r#d}|t|7 }n|dkr*d}n|dkr3|d7 }n|d	kr<|d
7 }n|dkr|}||k rV| | dksR| | dkrV|d7 }||k rd| | dkrd|d7 }||k r|| | dkr||d7 }||k r|| | dksn||k r|d7 }d}| | dkr|d7 }|d7 }n| | dkr|d7 }|d7 }|| || dd7 }||7 }|}n|d7 }n|t|7 }||k s|rtd| |S )a  
		Translates the glob pattern to a regular expression. This is used in the
		constructor to translate a path segment glob pattern to its corresponding
		regular expression.

		*pattern* (:class:`str`) is the glob pattern.

		Returns the regular expression (:class:`str`).
		Fr   r   r   \Tr   z[^/]*?z[^/][r   r   ]z\\z\[z9Escape character found with no next character to escape: )r)   reescapereplacer/   )r   rB   r4   r6   r:   charjexprr   r   r   r.      sT   

 

Yz+GitWildMatchPattern._translate_segment_globsc                    sp   t | tr
t}| }nt | trt}| t}ntd| dd d fdd|D }|tu r6|tS |S )z
		Escape special characters in the given string.

		*s* (:class:`str` or :class:`bytes`) a filename or a string that you want to
		escape, usually before adding it to a ".gitignore".

		Returns the escaped string (:class:`str` or :class:`bytes`).
		zs:r   z[]!*#?r   c                 3   s$    | ]}| v rd | n|V  qdS )r=   Nr   ).0xZmeta_charactersr   r   	<genexpr>z  s   " z-GitWildMatchPattern.escape.<locals>.<genexpr>)r   r   r   r    r!   r"   r0   r1   )rG   r3   stringZ
out_stringr   rJ   r   rB   d  s   



zGitWildMatchPattern.escapeN)r
   r   r   r   	__slots__classmethodr   r   r   boolr<   staticmethodr   r.   rB   r   r   r   r   r   $   s     Hnr   Zgitwildmatchc                       s@   e Zd ZdZd
 fddZed
ddZe fdd	Z  Z	S )GitIgnorePatternz
	The :class:`GitIgnorePattern` class is deprecated by :class:`GitWildMatchPattern`.
	This class only exists to maintain compatibility with v0.4.
	r   Nc                    s"   |    tt| j|i | dS )
		Warn about deprecation.
		N)_deprecatedsuperrQ   __init__)selfargskw	__class__r   r   rU     s   zGitIgnorePattern.__init__c                   C   s   t jdtdd dS )rR   z_GitIgnorePattern ('gitignore') is deprecated. Use GitWildMatchPattern ('gitwildmatch') instead.   )
stacklevelN)warningswarnDeprecationWarningr   r   r   r   rS     s   
zGitIgnorePattern._deprecatedc                    s   |    tt| j|i |S )rR   )rS   rT   rQ   r<   )r2   rW   rX   rY   r   r   r<     s   z!GitIgnorePattern.pattern_to_regex)r   N)
r
   r   r   r   rU   rP   rS   rN   r<   __classcell__r   r   rY   r   rQ     s    	rQ   Z	gitignore)r   rA   r]   typingr   r   r   r   r   r   r   r!   r*   r/   r	   r   Zregister_patternrQ   r   r   r   r   <module>   s"      _!