
    mg6                         d Z ddlmZ ddlmZ ddlmZmZ ed   Zedef   Z G d d	      Z	 G d
 de	      Z
 G d de	      Z G d d      Z G d d      Z G d de      Z G d de      Zy)z-Pattern matching for principal and host names    )fnmatch)Union   )	IPAddress
ip_network)WildcardHostPatternCIDRHostPatternWildcardPatternc                   ,    e Zd ZdZdefdZdedefdZy)_BaseWildcardPatternz/A base class for matching '*' and '?' wildcardspatternc                 >    dj                  d |D              | _        y )N c              3   <   K   | ]  }|d k(  rdn|dk(  rdn|  yw)[z[[]]z[]]N ).0chs     0lib/python3.12/site-packages/asyncssh/pattern.py	<genexpr>z0_BaseWildcardPattern.__init__.<locals>.<genexpr>'   s/       6-4r *,s)+s "!#-4s   )join_patternselfr   s     r   __init__z_BaseWildcardPattern.__init__$   s!       6-4 6 6    valuereturnc                 .    t        || j                        S z2Return whether a wild card pattern matches a value)r   r   )r   r   s     r   _matchesz_BaseWildcardPattern._matches+   s     udmm,,r   N)__name__
__module____qualname____doc__strr   boolr"   r   r   r   r   r   !   s$    96 6-c -d -r   r   c                   ,     e Zd ZdZdedef fdZ xZS )r
   z+A pattern matcher for '*' and '?' wildcardsr   r   c                 "    t         |   |      S r!   )superr"   )r   r   	__class__s     r   matcheszWildcardPattern.matches4   s     w&&r   )r#   r$   r%   r&   r'   r(   r-   __classcell__r,   s   @r   r
   r
   1   s    5'S 'T ' 'r   c                   4     e Zd ZdZdedededef fdZ xZS )r   z7Match a host name or address against a wildcard patternhostaddr_ipr   c                 x    t        |      xr t        | 	  |      xs t        |      xr t        | 	  |      S )zAReturn whether a host or address matches a wild card host pattern)r(   r+   r"   )r   r1   r2   r3   r,   s       r   r-   zWildcardHostPattern.matches=   s;     T
5uw/5 7T
5uw/5	7r   )	r#   r$   r%   r&   r'   r   r(   r-   r.   r/   s   @r   r   r   :   s,    A7C 7s 7 7t 7 7r   r   c                   4    e Zd ZdZdefdZdedededefdZy	)
r	   z7Match IPv4/v6 address against CIDR-style subnet patternr   c                 $    t        |      | _        y N)r   _networkr   s     r   r   zCIDRHostPattern.__init__G   s    "7+r   _host_addripr   c                 8    t        |      xr || j                  v S )z;Return whether an IP address matches a CIDR address pattern)r(   r8   )r   r9   r:   r;   s       r   r-   zCIDRHostPattern.matchesJ   s     Bx/B$--//r   N)	r#   r$   r%   r&   r'   r   r   r(   r-   r   r   r   r	   r	   D   s2    A, ,0S 0 0) 0 0r   r	   c                   8    e Zd ZdZdefdZdedefdZdefdZ	y)	_PatternLista  Match against a list of comma-separated positive and negative patterns

       This class is a base class for building a pattern matcher that
       takes a set of comma-separated positive and negative patterns,
       returning `True` if one or more positive patterns match and
       no negative ones do.

       The pattern matching is done by objects returned by the
       build_pattern method. The arguments passed in when a match
       is performed will vary depending on what class build_pattern
       returns.

    patternsc                    g | _         g | _        |j                  d      D ]g  }|j                  d      rd}|dd  }nd}| j	                  |      }|r| j                  j                  |       M| j                   j                  |       i y )N,!Tr   F)_pos_patterns_neg_patternssplit
startswithbuild_patternappend)r   r?   r   negatematchers        r   r   z_PatternList.__init___   s    ~~c*G!!#&!!"+((1G""))'2""))'2 +r   r   r   c                     t         )z)Abstract method to build a pattern object)NotImplementedErrorr   s     r   rG   z_PatternList.build_patternq   s
     "!r   c                     t        fd| j                  D              }t        fd| j                  D              }|xr | S )z?Match a set of values against positive & negative pattern listsc              3   <   K   | ]  } |j                      y wr7   r-   r   pargss     r   r   z'_PatternList.matches.<locals>.<genexpr>y        E2DQ			4(2D   c              3   <   K   | ]  } |j                      y wr7   rO   rP   s     r   r   z'_PatternList.matches.<locals>.<genexpr>z   rS   rT   )anyrC   rD   )r   rR   	pos_match	neg_matchs    `  r   r-   z_PatternList.matchesv   s>     E$2D2DEE	E$2D2DEE	*]*r   N)
r#   r$   r%   r&   r'   r   _AnyPatternrG   r(   r-   r   r   r   r>   r>   P   s2    3 3$"S "[ "
+ +r   r>   c                        e Zd ZdZdedefdZy)WildcardPatternListz%Match names against wildcard patternsr   r   c                     t        |      S )zBuild a wild card pattern)r
   r   s     r   rG   z!WildcardPatternList.build_pattern   s     w''r   N)r#   r$   r%   r&   r'   r
   rG   r   r   r   r[   r[      s    /(S (_ (r   r[   c                        e Zd ZdZdedefdZy)HostPatternListz?Match host names & addresses against wildcard and CIDR patternsr   r   c                 N    	 t        |      S # t        $ r t        |      cY S w xY w)z.Build a CIDR address or wild card host pattern)r	   
ValueErrorr   r   s     r   rG   zHostPatternList.build_pattern   s,    	0"7++ 	0&w//	0s   
 $$N)r#   r$   r%   r&   r'   _HostPatternrG   r   r   r   r^   r^      s    I0S 0\ 0r   r^   N)r&   r   typingr   miscr   r   ra   rY   r   r
   r   r	   r>   r[   r^   r   r   r   <module>rd      s   * 4   ' =>%|34- - '* '7. 7	0 	0,+ ,+^(, (	0l 	0r   