Current File : //usr/lib64/python3.6/site-packages/borg/__pycache__/patterns.cpython-36.pyc
3

y��b�4�@s�ddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZddl
mZddl
mZdd	�Zd5d
d�Zdd
�ZGdd�dej�ZGdd�dej�ZGdd�de�ZGdd�d�Zdd�ZGdd�d�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d!�d!e�ZGd"d#�d#e�ZeeeeehZe d$d%�eD��Z!ed&d'�Z"Gd(d)�d)e
�Z#d*d+�Z$d,d-�Z%ed.fd/d0�Z&efd1d2�Z'efd3d4�Z(dS)6�N)�
namedtuple)�Enum�)�shellpattern)�clean_lines)�ErrorcCsHt||d�}|jtjkr&|j|j�n|jtjkr:|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_commandsrZ
ie_command�r� /usr/lib64/python3.6/patterns.py�parse_patternfile_lines
rcCs0|dkrt}xt|�D]}t||||�}qWdS)N)�ShellPatternrr)�fileobjrrrrrrr�load_pattern_filesrcCs$xt|�D]}|jt|��q
WdS)N)rr
�parse_exclude_pattern)r�patternsZ
patternstrrrr�load_exclude_file#srcs(eZdZd�fdd�	Zddd�Z�ZS)	�ArgparsePatternActionrcst�jfd|i|��dS)N�nargs)�super�__init__)�selfr�kw)�	__class__rrr)szArgparsePatternAction.__init__NcCst|d|j|jt�dS)Nr)r�pathsrr)r �parser�args�values�
option_stringrrr�__call__,szArgparsePatternAction.__call__)r)N)�__name__�
__module__�__qualname__rr(�
__classcell__rr)r"rr(srcs0eZdZd	�fdd�	Zd
dd�Zdd�Z�ZS)�ArgparsePatternFileActionrcst�jfd|i|��dS)Nr)rr)r rr!)r"rrr1sz"ArgparsePatternFileAction.__init__NcCs`|d}y$t|��}|j||�WdQRXWn.tk
rZ}ztt|���WYdd}~XnXdS)z�Load and parse patterns from a file.
        Lines empty or starting with '#' after stripping whitespace on both line ends are ignored.
        rN)�open�parse�FileNotFoundErrorr�str)r r$r%r&r'�filename�f�errrr(4s
z"ArgparsePatternFileAction.__call__cCst||j|j�dS)N)rr#r)r �fobjr%rrrr/?szArgparsePatternFileAction.parse)r)N)r)r*r+rr(r/r,rr)r"rr-0s
r-c@seZdZdd�ZdS)�ArgparseExcludeFileActioncCst||j�dS)N)rr)r r5r%rrrr/DszArgparseExcludeFileAction.parseN)r)r*r+r/rrrrr6Csr6c@sReZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�PatternMatcherz�Represents a collection of pattern objects to match paths against.

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

    NcCs@g|_||_i|_d|_d|_g|_tjdtjdtj	di|_
dS)NTF)�_itemsr�_path_full_patterns�recurse_dir�recurse_dir_default�include_patternsr�Exclude�ExcludeNoRecurse�Include�is_include_cmd)r rrrrrNszPatternMatcher.__init__cCst|j�ot|j�S)N)�lenr8r9)r rrr�emptyiszPatternMatcher.emptycCs0t|t�r|j}||j|<n|jj||f�dS)z%*cmd* is an IECommand value.
        N)�
isinstance�PathFullPattern�patternr9r8r
)r rEr
�keyrrr�_addls
zPatternMatcher._addcCsx|D]}|j||�qWdS)z�Add 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 rr
rErrr�addus
zPatternMatcher.addcCs.dd�|D�}|j|tj�||_||_dS)zHUsed to add inclusion-paths from args.paths (from commandline).
        cSsg|]}t|t��qSr)�
parse_pattern�PathPrefixPattern)�.0�prrr�
<listcomp>�sz3PatternMatcher.add_includepaths.<locals>.<listcomp>N)rHrr?rr<)r Z
include_pathsr<rrr�add_includepaths}szPatternMatcher.add_includepathscCsdd�|jD�S)zBNote that this only returns patterns added via *add_includepaths*.cSsg|]}|jdkr|�qS)r)�match_count)rKrLrrrrM�szAPatternMatcher.get_unmatched_include_patterns.<locals>.<listcomp>)r<)r rrr�get_unmatched_include_patterns�sz-PatternMatcher.get_unmatched_include_patternscCs"x|D]\}}|j||�qWdS)zBAdd list of patterns (of type CmdTuple) to internal list.
        N)rG)r rrEr
rrr�add_inclexcl�szPatternMatcher.add_inclexclcCszt|�}t�}|jj||�}||k	r8t|�|_|j|Sx2|jD](\}}|j|dd�r@|j|_|j|Sq@W|j	|_|j
S)z�Return 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�valuerEr
rrrrW�s

zPatternMatcher.match)N)r)r*r+�__doc__rrBrGrHrNrPrQrWrrrrr7Hs
	r7cCstjdkrtjd|�S|S)z=normalize paths for MacOS (but do nothing on other platforms)�darwinZNFD)�sys�platform�unicodedatarR)rXrrrrS�srSc@sHeZdZdZeZddd�Zddd�Zdd	�Zd
d�Z	dd
�Z
dd�ZdS)�PatternBasez3Shared logic for inclusion/exclusion patterns.
    FcCs(||_d|_t|�}|j|�||_dS)Nr)�pattern_origrOrS�_preparer:)r rEr:rrrr�s

zPatternBase.__init__TcCs,|rt|�}|j|�}|r(|jd7_|S)aReturn 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 rXrRZmatchesrrrrW�s
zPatternBase.matchcCsdt|�|jfS)Nz%s(%s))�typerE)r rrr�__repr__�szPatternBase.__repr__cCs|jS)N)r`)r rrr�__str__�szPatternBase.__str__cCst�dS)z$Should set the value of self.patternN)�NotImplementedError)r rErrrra�szPatternBase._preparecCst�dS)N)rf)r rXrrrrb�szPatternBase._matchN)F)T)r)r*r+rZ�NotImplemented�PREFIXrrWrdrerarbrrrrr_�s


r_c@s$eZdZdZdZdd�Zdd�ZdS)rDzFull match of a path.ZpfcCstjj|�|_dS)N)�osrX�normpathrE)r rErrrra�szPathFullPattern._preparecCs
||jkS)N)rE)r rXrrrrb�szPathFullPattern._matchN)r)r*r+rZrhrarbrrrrrD�srDc@s$eZdZdZdZdd�Zdd�ZdS)rJz�Literal 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.
    ZppcCs$tjj|�jtjj�tjj|_dS)N)rirXrj�rstrip�seprE)r rErrrra�szPathPrefixPattern._preparecCs|tjjj|j�S)N)rirXrl�
startswithrE)r rXrrrrb�szPathPrefixPattern._matchN)r)r*r+rZrhrarbrrrrrJ�srJc@s$eZdZdZdZdd�Zdd�ZdS)�FnmatchPatternz�Shell glob patterns to exclude.  A trailing slash means to
    exclude the contents of a directory, but not the directory itself.
    ZfmcCsp|jtjj�r:tjj|�jtjj�tjjdtjj}ntjj|�tjjd}||_tjt	j
|j��|_dS)N�*)�endswithrirXrlrjrkrE�re�compile�fnmatch�	translate�regex)r rErrrra�s
,zFnmatchPattern._preparecCs|jj|tjj�dk	S)N)rurWrirXrl)r rXrrrrb
szFnmatchPattern._matchN)r)r*r+rZrhrarbrrrrrn�srnc@s$eZdZdZdZdd�Zdd�ZdS)rz�Shell glob patterns to exclude.  A trailing slash means to
    exclude the contents of a directory, but not the directory itself.
    ZshcCsttjj}|j|�r:tjj|�j|�|d|d|}ntjj|�|d|d}||_tjt	j
|j��|_dS)Nz**ro)rirXrlrprjrkrErqrrrrtru)r rErlrrrras
(zShellPattern._preparecCs|jj|tjj�dk	S)N)rurWrirXrl)r rXrrrrbszShellPattern._matchN)r)r*r+rZrhrarbrrrrrsrc@s$eZdZdZdZdd�Zdd�ZdS)�RegexPatternz#Regular expression to exclude.
    rqcCs||_tj|�|_dS)N)rErqrrru)r rErrrra(szRegexPattern._preparecCs,tjjdkr|jtjjd�}|jj|�dk	S)N�/)rirXrl�replaceru�search)r rXrrrrb,szRegexPattern._matchN)r)r*r+rZrhrarbrrrrrv#srvccs|]}|j|fVqdS)N)rh)rK�irrr�	<genexpr><sr{�CmdTuplezval cmdc@s$eZdZdZdZdZdZdZdZdS)rz8A command that an InclExcl file line can represent.
    r����N)	r)r*r+rZrrr?r=r>rrrrrAsrcCs|tjgkS)N)rr>)r
rrrrVKsrVcCs2yt|Stk
r,tdj|��d�YnXdS)NzUnknown pattern style: {})�_PATTERN_CLASS_BY_PREFIX�KeyError�
ValueError�format)�prefixrrr�get_pattern_classPsr�TcCsZt|�dkrL|ddkrL|dd�j�rL|dd�|dd�}}t|�}n|}|||�S)z^Read pattern from string and return an instance of the appropriate implementation class.

    r}�:Nr~)rA�isalnumr�)rErr:Zstyle�clsrrrrIWs
(
rIcCst||dd�}t|tj�S)z]Read pattern from string and return an instance of the appropriate implementation class.
    F)r:)rIr|rr>)Zpattern_strrZepattern_objrrrrcsrcCs�tjtjtjtjtjtjtjd�}|s0tjd��|j|d�}|dkrZtjddj	|���|dd�j
�}|sxtjd��|tjkr�|}nR|tjkr�yt|�}Wq�tk
r�tjd	j
|���Yq�Xnt|�}t|||�}t||�S)
zHRead a --patterns-from command from string and return a CmdTuple object.)�-�!�+�R�r�PrLz$A pattern/command must not be empty.rNz/A pattern/command must start with anyone of: %sz, rz)A pattern/command must have a value part.zInvalid pattern style: {})rr=r>r?rr�argparseZArgumentTypeErrorrU�join�lstripr�r�r�rVrIr|)Zcmd_line_strrZcmd_prefix_mapr
Z
remainder_strrr:rrrr	js4




r	)N))r�rsZos.pathrirqr\r^�collectionsr�enumr�rZhelpersrrrrrZActionrr-r6r7rSr_rDrJrnrrvZ_PATTERN_CLASSES�dictr�r|rrVr�rIrr	rrrr�<module>sJ
c'