
    	al5                         d Z ddlmZ ddlmZ ddlmZ  G d d      Zd Zd Zd	 Zd
 Z	ddZ
ddZddZddZddZddZd ZeeeedZh dZd Zd Zy)z3Module containing the validation logic for rfc3986.   )
exceptions)misc)normalizersc                   Z    e Zd ZdZ eg d      Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zy)	Validatora  Object used to configure validation of all objects in rfc3986.

    .. versionadded:: 1.0

    Example usage::

         >>> from rfc3986 import api, validators
         >>> uri = api.uri_reference('https://github.com/')
         >>> validator = validators.Validator().require_presence_of(
         ...    'scheme', 'host', 'path',
         ... ).allow_schemes(
         ...    'http', 'https',
         ... ).allow_hosts(
         ...    '127.0.0.1', 'github.com',
         ... )
         >>> validator.validate(uri)
         >>> invalid_uri = rfc3986.uri_reference('imap://mail.google.com')
         >>> validator.validate(invalid_uri)
         Traceback (most recent call last):
         ...
         rfc3986.exceptions.MissingComponentError: ('path was required but
         missing', URIReference(scheme=u'imap', authority=u'mail.google.com',
         path=None, query=None, fragment=None), ['path'])

    schemeuserinfohostportpathqueryfragmentc                     t               | _        t               | _        t               | _        d| _        dddddddd| _        | j
                  j                         | _        y)z#Initialize our default validations.TFr   N)setallowed_schemesallowed_hostsallowed_portsallow_passwordrequired_componentscopyvalidated_componentsselfs    4/usr/lib/python3/dist-packages/rfc3986/validators.py__init__zValidator.__init__3   s`    "u U U"$
  %)$<$<$A$A$C!    c                 p    |D ]0  }| j                   j                  t        j                  |             2 | S )a	  Require the scheme to be one of the provided schemes.

        .. versionadded:: 1.0

        :param schemes:
            Schemes, without ``://`` that are allowed.
        :returns:
            The validator instance.
        :rtype:
            Validator
        )r   addr   normalize_scheme)r   schemesr	   s      r   allow_schemeszValidator.allow_schemesD   s9      	KF  $$[%A%A&%IJ	Kr   c                 p    |D ]0  }| j                   j                  t        j                  |             2 | S )zRequire the host to be one of the provided hosts.

        .. versionadded:: 1.0

        :param hosts:
            Hosts that are allowed.
        :returns:
            The validator instance.
        :rtype:
            Validator
        )r   r   r   normalize_host)r   hostsr   s      r   allow_hostszValidator.allow_hostsT   s9      	ED"";#=#=d#CD	Er   c                     |D ]9  }t        |d      }d|cxk  rdk  sn | j                  j                  |       ; | S )zRequire the port to be one of the provided ports.

        .. versionadded:: 1.0

        :param ports:
            Ports that are allowed.
        :returns:
            The validator instance.
        :rtype:
            Validator
        
   base      )intr   r   )r   portsr   port_ints       r   allow_portszValidator.allow_portsd   sG      	-D4b)HH%%""&&t,	- r   c                     d| _         | S )zAllow passwords to be present in the URI.

        .. versionadded:: 1.0

        :returns:
            The validator instance.
        :rtype:
            Validator
        Tr   r   s    r   allow_use_of_passwordzValidator.allow_use_of_passwordv   s     #r   c                     d| _         | S )zPrevent passwords from being included in the URI.

        .. versionadded:: 1.0

        :returns:
            The validator instance.
        :rtype:
            Validator
        Fr2   r   s    r   forbid_use_of_passwordz Validator.forbid_use_of_password   s     $r   c                     |D cg c]  }|j                          }}|D ]  }|| j                  vst        d| d       | j                  j	                  |D ci c]  }|d c}       | S c c}w c c}w )aA  Check the validity of the components provided.

        This can be specified repeatedly.

        .. versionadded:: 1.1

        :param components:
            Names of components from :attr:`Validator.COMPONENT_NAMES`.
        :returns:
            The validator instance.
        :rtype:
            Validator
        "" is not a valid componentT)lowerCOMPONENT_NAMES
ValueErrorr   updater   
componentsc	components       r   check_validity_ofzValidator.check_validity_of   s     *44Aaggi4
4# 	LI 4 44 1YK/I!JKK	L 	!!((.89Y_9	
  5
 :   A.
A3c                     |D cg c]  }|j                          }}|D ]  }|| j                  vst        d| d       | j                  j	                  |D ci c]  }|d c}       | S c c}w c c}w )a3  Require the components provided.

        This can be specified repeatedly.

        .. versionadded:: 1.0

        :param components:
            Names of components from :attr:`Validator.COMPONENT_NAMES`.
        :returns:
            The validator instance.
        :rtype:
            Validator
        r7   r8   T)r9   r:   r;   r   r<   r=   s       r   require_presence_ofzValidator.require_presence_of   s     *44Aaggi4
4# 	LI 4 44 1YK/I!JKK	L 	  ''.89Y_9	
  5
 :rB   c                    | j                   st        |       | j                  j                         D cg c]	  \  }}|r| }}}| j                  j                         D cg c]	  \  }}|r| }}}|rt        ||       |rt        ||       t        | j                  |d       t        | j                  |d       t        | j                  |d       yc c}}w c c}}w )a  Check a URI for conditions specified on this validator.

        .. versionadded:: 1.0

        :param uri:
            Parsed URI to validate.
        :type uri:
            rfc3986.uri.URIReference
        :raises MissingComponentError:
            When a required component is missing.
        :raises UnpermittedComponentError:
            When a component is not one of those allowed.
        :raises PasswordForbidden:
            When a password is present in the userinfo component but is
            not permitted by configuration.
        :raises InvalidComponentsError:
            When a component was found to be invalid.
        r	   r   r   N)r   check_passwordr   itemsr    ensure_required_components_existensure_components_are_validensure_one_ofr   r   r   )r   urir@   requiredr   r   s         r   validatezValidator.validate   s    & ""3 (,'?'?'E'E'G
#	8 
 
 (,'@'@'F'F'H 
#	8  
  

 ,S2EF'-ABd**C:d((#v6d((#v6#

 
s   C#CN)__name__
__module____qualname____doc__	frozensetr:   r   r"   r&   r0   r3   r5   rA   rD   rM    r   r   r   r      sD    4  KOD"  $..'7r   r   c                     | j                   }|sy|j                  dd      }t        |      dk  ryt        j                  |       )z4Assert that there is no password present in the uri.N:r   )r
   splitlenr   PasswordForbidden)rK   r
   credentialss      r   rF   rF      sB    ||H..a(K
;1

&
&s
++r   c                 ^    t        ||      }|| r|| vrt        j                  |||       yyy)z=Assert that the uri's attribute is one of the allowed values.N)getattrr   UnpermittedComponentError)allowed_valuesrK   	attributevalues       r   rJ   rJ      sD    C#E^^0K22
 	
 1L^r   c                 ^     t         fd|D              }|rt        j                   g| y)z;Assert that all required components are present in the URI.c              3   :   K   | ]  }t        |      |  y w)N)r[   ).0r@   rK   s     r   	<genexpr>z3ensure_required_components_exist.<locals>.<genexpr>   s&       3	"* 	 s   N)sortedr   MissingComponentError)rK   r   missing_componentss   `  r   rH   rH      s<      ,  
 ..sH5GHH r   c                 b    |r| duxr |j                  |       S | du xs |j                  |       S )a  Determine if a value is valid based on the provided matcher.

    :param str value:
        Value to validate.
    :param matcher:
        Compiled regular expression to use to validate the value.
    :param require:
        Whether or not the value is required.
    N)match)r_   matcherrequires      r   is_validrk   	  s:     D 9W]]5%99 D=0GMM%00r   Nc                 \    t        | t        j                  |      }|r|t        ||      S |S )an  Determine if the authority string is valid.

    :param str authority:
        The authority to validate.
    :param str host:
        (optional) The host portion of the authority to validate.
    :param bool require:
        (optional) Specify if authority must not be None.
    :returns:
        ``True`` if valid, ``False`` otherwise
    :rtype:
        bool
    )rk   r   SUBAUTHORITY_MATCHERhost_is_valid)	authorityr   rj   	validateds       r   authority_is_validrq     s3     D$=$=wGIT%T7++r   c                     t        | t        j                  |      }|r,| *t        j                  j	                  |       rt        |       S |rB| @t        j                  j	                  |       r!t        j                  j	                  |       duS |S )a  Determine if the host string is valid.

    :param str host:
        The host to validate.
    :param bool require:
        (optional) Specify if host must not be None.
    :returns:
        ``True`` if valid, ``False`` otherwise
    :rtype:
        bool
    N)rk   r   HOST_MATCHERIPv4_MATCHERrh   valid_ipv4_host_addressIPv6_MATCHERIPv6_NO_RFC4007_MATCHER)r   rj   rp   s      r   rn   rn   .  s{     t00':IT%$*;*;*A*A$*G&t,,	t'D,=,=,C,CD,I++11$7tCCr   c                 8    t        | t        j                  |      S )a+  Determine if the scheme is valid.

    :param str scheme:
        The scheme string to validate.
    :param bool require:
        (optional) Set to ``True`` to require the presence of a scheme.
    :returns:
        ``True`` if the scheme is valid. ``False`` otherwise.
    :rtype:
        bool
    )rk   r   SCHEME_MATCHER)r	   rj   s     r   scheme_is_validrz   B  s     FD//99r   c                 8    t        | t        j                  |      S )a+  Determine if the path component is valid.

    :param str path:
        The path string to validate.
    :param bool require:
        (optional) Set to ``True`` to require the presence of a path.
    :returns:
        ``True`` if the path is valid. ``False`` otherwise.
    :rtype:
        bool
    )rk   r   PATH_MATCHER)r   rj   s     r   path_is_validr}   Q  s     D$++W55r   c                 8    t        | t        j                  |      S )a0  Determine if the query component is valid.

    :param str query:
        The query string to validate.
    :param bool require:
        (optional) Set to ``True`` to require the presence of a query.
    :returns:
        ``True`` if the query is valid. ``False`` otherwise.
    :rtype:
        bool
    )rk   r   QUERY_MATCHER)r   rj   s     r   query_is_validr   `  s     E4--w77r   c                 8    t        | t        j                  |      S )a?  Determine if the fragment component is valid.

    :param str fragment:
        The fragment string to validate.
    :param bool require:
        (optional) Set to ``True`` to require the presence of a fragment.
    :returns:
        ``True`` if the fragment is valid. ``False`` otherwise.
    :rtype:
        bool
    )rk   r   FRAGMENT_MATCHER)r   rj   s     r   fragment_is_validr   o  s     Hd33W==r   c           
          t        | j                  d      D cg c]  }dt        |d      cxk  xr dk  nc  c}      S c c}w )z4Determine if the given host is a valid IPv4 address..r+   r(   r)      )allrV   r-   )r   bytes     r   ru   ru   ~  s6     4::c?K4SB'.3.KLLKs   "A)r	   r   r   r   >   r   r   r
   c                     	 | j                         }|dk(  rt        |d         S |dk7  ry	 t	        |d         }d|cxk  xr dk  S c S # t        j                  $ r Y yw xY w# t
        $ r Y yw xY w)z4Determine if the userinfo, host, and port are valid.Fr   r   Tr+   r,   )authority_infor   InvalidAuthorityrn   r-   	TypeError)rK   r@   subauthority_dictr   s       r   subauthority_component_is_validr     s    ..0 F.v677	f	$V,- # &&    s"   A
 A# 
A A #	A/.A/c                     t               }|D ]U  }|t        v rt        | |      s|j                  |       )t        |   } |t        | |            rE|j                  |       W |rt        j                  | g| y)z0Assert that all components are valid in the URI.N)r   _SUBAUTHORITY_VALIDATORSr   r   _COMPONENT_VALIDATORSr[   r   InvalidComponentsError)rK   r   invalid_componentsr@   	validators        r   rI   rI     s    ) .	0023	B"&&y1 ))4	i01""9-. //I6HII r   )NF)F)rQ    r   r   r   r   rF   rJ   rH   rk   rq   rn   rz   r}   r   r   ru   r   r   r   rI   rS   r   r   <module>r      s    :   Q7 Q7h,
I1"((:68>M !	  8 0Jr   