
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
yb4                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 ddlmZ ddlmZ ddlmZ dd	 Zd5d
dZdd ZG dd de jZG dd de jZG dd deZG dd dZdd ZG dd dZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZeeeeehZe d$d% eD Z!ed&d'Z"G d(d) d)e
Z#d*d+ Z$d,d- Z%ed.fd/d0Z&efd1d2Z'efd3d4Z(dS )6    N)
namedtuple)Enum   )shellpattern)clean_lines)Errorc             C   sH   t | |d}|jtjkr&|j|j n|jtjkr:|j}n
|j| |S )zKParse a pattern-file line and act depending on which command it represents.)fallback)parse_inclexcl_commandcmd	IECommandRootPathappendvalPatternStyle)linerootsie_commandsr   Z
ie_command r    /usr/lib64/python3.6/patterns.pyparse_patternfile_line   s    
r   c             C   s0   |d krt }xt| D ]}t||||}qW d S )N)ShellPatternr   r   )fileobjr   r   r   r   r   r   r   load_pattern_file   s    r   c             C   s$   xt | D ]}|jt| q
W d S )N)r   r   parse_exclude_pattern)r   patternsZ
patternstrr   r   r   load_exclude_file#   s    r   c                   s(   e Zd Zd fdd	ZdddZ  ZS )	ArgparsePatternActionr   c                s   t  jf d|i| d S )Nnargs)super__init__)selfr   kw)	__class__r   r   r   )   s    zArgparsePatternAction.__init__Nc             C   s   t |d |j|jt d S )Nr   )r   pathsr   r   )r    parserargsvaluesoption_stringr   r   r   __call__,   s    zArgparsePatternAction.__call__)r   )N)__name__
__module____qualname__r   r(   __classcell__r   r   )r"   r   r   (   s   r   c                   s0   e Zd Zd	 fdd	Zd
ddZdd Z  ZS )ArgparsePatternFileActionr   c                s   t  jf d|i| d S )Nr   )r   r   )r    r   r!   )r"   r   r   r   1   s    z"ArgparsePatternFileAction.__init__Nc             C   s`   |d }y$t |}| j|| W dQ R X W n. tk
rZ } ztt|W Y dd}~X nX dS )zLoad and parse patterns from a file.
        Lines empty or starting with '#' after stripping whitespace on both line ends are ignored.
        r   N)openparseFileNotFoundErrorr   str)r    r$   r%   r&   r'   filenamefer   r   r   r(   4   s    
z"ArgparsePatternFileAction.__call__c             C   s   t ||j|j d S )N)r   r#   r   )r    fobjr%   r   r   r   r/   ?   s    zArgparsePatternFileAction.parse)r   )N)r)   r*   r+   r   r(   r/   r,   r   r   )r"   r   r-   0   s   
r-   c               @   s   e Zd Zdd ZdS )ArgparseExcludeFileActionc             C   s   t ||j d S )N)r   r   )r    r5   r%   r   r   r   r/   D   s    zArgparseExcludeFileAction.parseN)r)   r*   r+   r/   r   r   r   r   r6   C   s   r6   c               @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )PatternMatcherzRepresents a collection of pattern objects to match paths against.

    *fallback* is a boolean value that *match()* returns if no matching patterns are found.

    Nc             C   s@   g | _ || _i | _d | _d| _g | _tjdtjdtj	di| _
d S )NTF)_itemsr   _path_full_patternsrecurse_dirrecurse_dir_defaultinclude_patternsr   ExcludeExcludeNoRecurseIncludeis_include_cmd)r    r   r   r   r   r   N   s    zPatternMatcher.__init__c             C   s   t | j ot | j S )N)lenr8   r9   )r    r   r   r   emptyi   s    zPatternMatcher.emptyc             C   s0   t |tr|j}|| j|< n| jj||f dS )z%*cmd* is an IECommand value.
        N)
isinstancePathFullPatternpatternr9   r8   r   )r    rE   r
   keyr   r   r   _addl   s    
zPatternMatcher._addc             C   s   x|D ]}| j || qW dS )zAdd list of patterns to internal list. *cmd* indicates whether the
        pattern is an include/exclude pattern, and whether recursion should be
        done on excluded folders.
        N)rG   )r    r   r
   rE   r   r   r   addu   s    
zPatternMatcher.addc             C   s.   dd |D }| j |tj | | _|| _dS )zHUsed to add inclusion-paths from args.paths (from commandline).
        c             S   s   g | ]}t |tqS r   )parse_patternPathPrefixPattern).0pr   r   r   
<listcomp>   s    z3PatternMatcher.add_includepaths.<locals>.<listcomp>N)rH   r   r?   r   r<   )r    Zinclude_pathsr<   r   r   r   add_includepaths}   s    zPatternMatcher.add_includepathsc             C   s   dd | j D S )zBNote that this only returns patterns added via *add_includepaths*.c             S   s   g | ]}|j d kr|qS )r   )match_count)rK   rL   r   r   r   rM      s    zAPatternMatcher.get_unmatched_include_patterns.<locals>.<listcomp>)r<   )r    r   r   r   get_unmatched_include_patterns   s    z-PatternMatcher.get_unmatched_include_patternsc             C   s"   x|D ]\}}| j || qW dS )zBAdd list of patterns (of type CmdTuple) to internal list.
        N)rG   )r    r   rE   r
   r   r   r   add_inclexcl   s    zPatternMatcher.add_inclexclc             C   sz   t |}t }| jj||}||k	r8t|| _| j| S x2| jD ](\}}|j|ddr@|j| _| j| S q@W | j	| _| j
S )zReturn True or False depending on whether *path* is matched.

        If no match is found among the patterns in this matcher, then the value
        in self.fallback is returned (defaults to None).

        F)	normalize)normalize_pathobjectr9   getcommand_recurses_dirr:   r@   r8   matchr;   r   )r    pathZnon_existentvaluerE   r
   r   r   r   rW      s    

zPatternMatcher.match)N)r)   r*   r+   __doc__r   rB   rG   rH   rN   rP   rQ   rW   r   r   r   r   r7   H   s   
	r7   c             C   s   t jdkrtjd| S | S )z=normalize paths for MacOS (but do nothing on other platforms)darwinZNFD)sysplatformunicodedatarR   )rX   r   r   r   rS      s    rS   c               @   sH   e Zd ZdZeZdddZdddZdd	 Zd
d Z	dd Z
dd ZdS )PatternBasez3Shared logic for inclusion/exclusion patterns.
    Fc             C   s(   || _ d| _t|}| j| || _d S )Nr   )pattern_origrO   rS   _preparer:   )r    rE   r:   r   r   r   r      s
    
zPatternBase.__init__Tc             C   s,   |rt |}| j|}|r(|  jd7  _|S )a   Return a boolean indicating whether *path* is matched by this pattern.

        If normalize is True (default), the path will get normalized using normalize_path(),
        otherwise it is assumed that it already is normalized using that function.
        r   )rS   _matchrO   )r    rX   rR   Zmatchesr   r   r   rW      s    
zPatternBase.matchc             C   s   dt | | jf S )Nz%s(%s))typerE   )r    r   r   r   __repr__   s    zPatternBase.__repr__c             C   s   | j S )N)r`   )r    r   r   r   __str__   s    zPatternBase.__str__c             C   s   t dS )z$Should set the value of self.patternN)NotImplementedError)r    rE   r   r   r   ra      s    zPatternBase._preparec             C   s   t d S )N)rf   )r    rX   r   r   r   rb      s    zPatternBase._matchN)F)T)r)   r*   r+   rZ   NotImplementedPREFIXr   rW   rd   re   ra   rb   r   r   r   r   r_      s   

r_   c               @   s$   e Zd ZdZdZdd Zdd ZdS )rD   zFull match of a path.Zpfc             C   s   t jj|| _d S )N)osrX   normpathrE   )r    rE   r   r   r   ra      s    zPathFullPattern._preparec             C   s
   || j kS )N)rE   )r    rX   r   r   r   rb      s    zPathFullPattern._matchN)r)   r*   r+   rZ   rh   ra   rb   r   r   r   r   rD      s   rD   c               @   s$   e Zd ZdZdZdd Zdd ZdS )rJ   zLiteral files or directories listed on the command line
    for some operations (e.g. extract, but not create).
    If a directory is specified, all paths that start with that
    path match as well.  A trailing slash makes no difference.
    Zppc             C   s$   t jj|jt jjt jj | _d S )N)ri   rX   rj   rstripseprE   )r    rE   r   r   r   ra      s    zPathPrefixPattern._preparec             C   s   |t jj j| jS )N)ri   rX   rl   
startswithrE   )r    rX   r   r   r   rb      s    zPathPrefixPattern._matchN)r)   r*   r+   rZ   rh   ra   rb   r   r   r   r   rJ      s   rJ   c               @   s$   e Zd ZdZdZdd Zdd ZdS )FnmatchPatternzShell glob patterns to exclude.  A trailing slash means to
    exclude the contents of a directory, but not the directory itself.
    Zfmc             C   sp   |j tjjr:tjj|jtjjtjj d tjj }ntjj|tjj d }|| _tjt	j
| j| _d S )N*)endswithri   rX   rl   rj   rk   rE   recompilefnmatch	translateregex)r    rE   r   r   r   ra      s
    ,zFnmatchPattern._preparec             C   s   | j j|tjj d k	S )N)ru   rW   ri   rX   rl   )r    rX   r   r   r   rb   
  s    zFnmatchPattern._matchN)r)   r*   r+   rZ   rh   ra   rb   r   r   r   r   rn      s   rn   c               @   s$   e Zd ZdZdZdd Zdd ZdS )r   zShell glob patterns to exclude.  A trailing slash means to
    exclude the contents of a directory, but not the directory itself.
    Zshc             C   st   t jj}|j|r:t jj|j|| d | d | }nt jj|| d | d }|| _tjt	j
| j| _d S )Nz**ro   )ri   rX   rl   rp   rj   rk   rE   rq   rr   r   rt   ru   )r    rE   rl   r   r   r   ra     s    
(zShellPattern._preparec             C   s   | j j|tjj d k	S )N)ru   rW   ri   rX   rl   )r    rX   r   r   r   rb     s    zShellPattern._matchN)r)   r*   r+   rZ   rh   ra   rb   r   r   r   r   r     s   r   c               @   s$   e Zd ZdZdZdd Zdd ZdS )RegexPatternz#Regular expression to exclude.
    rq   c             C   s   || _ tj|| _d S )N)rE   rq   rr   ru   )r    rE   r   r   r   ra   (  s    zRegexPattern._preparec             C   s,   t jjdkr|jt jjd}| jj|d k	S )N/)ri   rX   rl   replaceru   search)r    rX   r   r   r   rb   ,  s    zRegexPattern._matchN)r)   r*   r+   rZ   rh   ra   rb   r   r   r   r   rv   #  s   rv   c             c   s   | ]}|j |fV  qd S )N)rh   )rK   ir   r   r   	<genexpr><  s    r{   CmdTuplezval cmdc               @   s$   e Zd ZdZdZdZdZdZdZdS )r   z8A command that an InclExcl file line can represent.
    r               N)	r)   r*   r+   rZ   r   r   r?   r=   r>   r   r   r   r   r   A  s   r   c             C   s   | t jgkS )N)r   r>   )r
   r   r   r   rV   K  s    rV   c             C   s2   yt |  S  tk
r,   tdj| d Y nX d S )NzUnknown pattern style: {})_PATTERN_CLASS_BY_PREFIXKeyError
ValueErrorformat)prefixr   r   r   get_pattern_classP  s    r   Tc             C   sZ   t | dkrL| d dkrL| dd j rL| dd | dd  }} t|}n|}|| |S )z^Read pattern from string and return an instance of the appropriate implementation class.

    r}   :Nr~   )rA   isalnumr   )rE   r   r:   Zstyleclsr   r   r   rI   W  s
    (
rI   c             C   s   t | |dd}t|tjS )z]Read pattern from string and return an instance of the appropriate implementation class.
    F)r:   )rI   r|   r   r>   )Zpattern_strr   Zepattern_objr   r   r   r   c  s    r   c             C   s   t jt jt jt jt jt jt jd}| s0tjd|j| d }|dkrZtjddj	| | dd j
 }|sxtjd|t jkr|}nR|t jkryt|}W q tk
r   tjd	j|Y qX nt|}t|||}t||S )
zHRead a --patterns-from command from string and return a CmdTuple object.)-!+RrPrL   z$A pattern/command must not be empty.r   Nz/A pattern/command must start with anyone of: %sz, r   z)A pattern/command must have a value part.zInvalid pattern style: {})r   r=   r>   r?   r   r   argparseZArgumentTypeErrorrU   joinlstripr   r   r   rV   rI   r|   )Zcmd_line_strr   Zcmd_prefix_mapr
   Zremainder_strr   r:   r   r   r   r	   j  s4    




r	   )N))r   rs   Zos.pathri   rq   r\   r^   collectionsr   enumr    r   Zhelpersr   r   r   r   r   ZActionr   r-   r6   r7   rS   r_   rD   rJ   rn   r   rv   Z_PATTERN_CLASSESdictr   r|   r   rV   r   rI   r   r	   r   r   r   r   <module>   sJ   
c'

