B
    Khh0                 @   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   D/tmp/pip-install-vv81h98y/pathspec/pathspec/patterns/gitwildmatch.pyr	      s   r	   c               @   s`   e Zd ZdZdZeeeee ee	 f dddZ
eeedddZeeedd	d
ZdS )GitWildMatchPatternzX
	The :class:`GitWildMatchPattern` class represents a compiled Git
	wildmatch pattern.
	r   )patternreturnc             C   sz  t |trt}n*t |tr*t}|t}ntd|d|}|drR| }n| }|	drpd}d}n|dkrd}d}n|rL|	drd}|d	d }nd
}d}|
d}xFtt|d	 ddD ].}||d	  }	|| }
|	dkr|
dkr||= qW t|dkr2|d dkr2|d	 s2dt d}|d sD|d= nBt|d	ksjt|dkr|d	 s|d dkr|dd n |std||d st|d	krd|d< |dkrFdg}d}t|d	 }xRt|D ]D\}}
|
dkrj|dkr"||kr"|dt d nF|dkr<|d d}n,||krZ|dt d n|d d
}n|
dkr|r|d |d ||kr|dt d d
}nz|r|d y|| |
 W n4 tk
r } ztd||W dd}~X Y nX ||kr&|dt d d
}qW |d d|}n|}nd}d}|dk	rr|tkrr|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:z! is not a unicode or byte string.z\ #N/!F   Tr   z**r   z^.+(?P<z>/).*$zInvalid git pattern: ^z[^/]+(?:(?P<z>/).*)?z(?:.+/)?z(?P<z>/).*z(?:/.+)?*z[^/]+z(?:(?P<$ )
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_segs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 |  }}xn||k r| | }|d7 }|rLd}|t|7 }q|dkrZd}q|dkrl|d7 }q|d	kr~|d
7 }q|dkrv|}||k r| | dks| | dkr|d7 }||k r| | dkr|d7 }x ||k r| | dkr|d7 }qW ||k rl|d7 }d}| | dkr(|d7 }|d7 }n| | dkrF|d7 }|d7 }|| || dd7 }||7 }|}n|d7 }q|t|7 }qW |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   rA   r3   r5   r9   charjexprr   r   r   r-      sP    


 


z+GitWildMatchPattern._translate_segment_glob)sr   c                st   t | trt}| }n*t | tr.t}| t}ntd| dd d fdd|D }|tkrl|tS |S d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:z! is not a unicode or byte string.z[]!*#?r   c             3   s"   | ]}| krd | n|V  qdS )r<   Nr   ).0x)meta_charactersr   r   	<genexpr>z  s    z-GitWildMatchPattern.escape.<locals>.<genexpr>N)r   r   r   r   r    r!   r/   r0   )rF   r2   stringZ
out_stringr   )rI   r   rA   d  s    



zGitWildMatchPattern.escapeN)r
   r   r   r   	__slots__classmethodr   r   r   boolr;   staticmethodr   r-   rA   r   r   r   r   r   %   s     Bpr   Zgitwildmatchc                   sH   e Zd ZdZdd fddZed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.
	N)r   c                s   |    tt| j|| dS )z
		Warn about deprecation.
		N)_deprecatedsuperrP   __init__)selfargskw)	__class__r   r   rS     s    zGitIgnorePattern.__init__c               C   s   t jdtdd dS )z
		Warn about deprecation.
		z_GitIgnorePattern ('gitignore') is deprecated. Use GitWildMatchPattern ('gitwildmatch') instead.   )
stacklevelN)warningswarnDeprecationWarningr   r   r   r   rQ     s    zGitIgnorePattern._deprecatedc                s   |    tt| j||S )z
		Warn about deprecation.
		)rQ   rR   rP   r;   )r1   rU   rV   )rW   r   r   r;     s    z!GitIgnorePattern.pattern_to_regex)
r
   r   r   r   rS   rO   rQ   rM   r;   __classcell__r   r   )rW   r   rP     s
   	rP   Z	gitignore)r   r@   rZ   typingr   r   r   r   r   r   r   r    r)   r.   r	   r   Zregister_patternrP   r   r   r   r   <module>   s     ^!