U
    d                     @   s:   d dl Z d dlmZmZ eeee f ZG dd dZdS )    N)IterableUnionc                   @   sv   e Zd ZdZdddeeedddZdd	 Zd
d Zee	dddZ
edeedddZedeedddZdS )	GlobGroupa  A set of patterns that candidate strings will be matched against.

    A candidate is composed of a list of segments separated by ``separator``, e.g. "foo.bar.baz".

    A pattern contains one or more segments. Segments can be:
        - A literal string (e.g. "foo"), which matches exactly.
        - A string containing a wildcard (e.g. "torch*", or "foo*baz*"). The wildcard matches
          any string, including the empty string.
        - A double wildcard ("**"). This matches against zero or more complete segments.

    Examples:
        ``torch.**``: matches ``torch`` and all its submodules, e.g. ``torch.nn`` and ``torch.nn.functional``.
        ``torch.*``: matches ``torch.nn`` or ``torch.functional``, but not ``torch.nn.functional``.
        ``torch*.**``: matches ``torch``, ``torchvision``, and all their submodules.

    A candidates will match the ``GlobGroup`` if it matches any of the ``include`` patterns and
    none of the ``exclude`` patterns.

    Args:
        include (Union[str, Iterable[str]]): A string or list of strings,
            each representing a pattern to be matched against. A candidate
            will match if it matches *any* include pattern
        exclude (Union[str, Iterable[str]]): A string or list of strings,
            each representing a pattern to be matched against. A candidate
            will be excluded from matching if it matches *any* exclude pattern.
        separator (str): A string that delimits segments in candidates and
            patterns. By default this is "." which corresponds to how modules are
            named in Python. Another common value for this is "/", which is
            the Unix path separator.
     .)exclude	separator)includer   r   c                C   s:   d| d| d| _ t||| _t||| _|| _d S )NzGlobGroup(include=z
, exclude=))_dbgr   
_glob_listr	   r   r   )selfr	   r   r   r   r   </tmp/pip-unpacked-wheel-ua33x9lu/torch/package/glob_group.py__init__'   s    zGlobGroup.__init__c                 C   s   | j S Nr   r   r   r   r   __str__/   s    zGlobGroup.__str__c                 C   s   | j S r   r   r   r   r   r   __repr__2   s    zGlobGroup.__repr__)	candidatereturnc                    s:   | j    t fdd| jD o8t fdd| jD S )Nc                 3   s   | ]}|  V  qd S r   	fullmatch.0pr   r   r   	<genexpr>7   s     z$GlobGroup.matches.<locals>.<genexpr>c                 3   s   | ]}|   V  qd S r   r   r   r   r   r   r   7   s    )r   anyr	   allr   )r   r   r   r   r   matches5   s    
$zGlobGroup.matcheselemsr   c                    s.   t | trt|  gS  fdd| D S d S )Nc                    s   g | ]}t | qS r   )r   _glob_to_re)r   er   r   r   
<listcomp>@   s     z(GlobGroup._glob_list.<locals>.<listcomp>)
isinstancestrr   r#   r!   r   r%   r   r   ;   s    
zGlobGroup._glob_list)patternr   c                    s4   fdd d  fdd| D }t|S )Nc                    sd   d| kr4| dkr*dt   d   d S tdn,t  d  d dd | d	D  S d S )
Nz**(z[^z]+)*z,** can only appear as an entire path segmentz]*c                 s   s   | ]}t |V  qd S r   )reescape)r   xr   r   r   r   M   s    zAGlobGroup._glob_to_re.<locals>.component_to_re.<locals>.<genexpr>*)r+   r,   
ValueErrorjoinsplit)	componentr%   r   r   component_to_reF   s    
z.GlobGroup._glob_to_re.<locals>.component_to_re c                 3   s   | ]} |V  qd S r   r   )r   c)r3   r   r   r   Q   s     z(GlobGroup._glob_to_re.<locals>.<genexpr>)r0   r1   r+   compile)r)   r   resultr   )r3   r   r   r#   B   s    zGlobGroup._glob_to_reN)r   )r   )__name__
__module____qualname____doc__GlobPatternr(   r   r   r   boolr    staticmethodr   r#   r   r   r   r   r      s       r   )r+   typingr   r   r(   r<   r   r   r   r   r   <module>   s   