o
    Dh6                     @   s   d Z ddlmZ ddlmZ ddlmZ G dd deZdd Zd	d
 Zdd Z	dd Z
d%ddZd&ddZd&ddZd&ddZd&ddZd&ddZdd ZeeeedZeg d Zd!d" Zd#d$ ZdS )'z3Module containing the validation logic for rfc3986.   )
exceptions)misc)normalizersc                   @   sd   e Zd ZdZeg dZ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d ZdS )	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                 C   sD   t  | _t  | _t  | _d| _dddddddd| _| j | _dS )z#Initialize our default validations.TFr   N)setallowed_schemesallowed_hostsallowed_portsallow_passwordrequired_componentscopyvalidated_componentsself r   M/var/www/html/env_mimamsha/lib/python3.10/site-packages/rfc3986/validators.py__init__4   s   	zValidator.__init__c                 G       |D ]}| j t| q| 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   r   r   r   allow_schemesE      zValidator.allow_schemesc                 G   r   )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	   r   r   r   allow_hostsU   r    zValidator.allow_hostsc                 G   s>   |D ]}t |dd}d|  krdkrn q| j| q| 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_intr   r   r   allow_portse   s   zValidator.allow_portsc                 C   
   d| _ | S )zAllow passwords to be present in the URI.

        .. versionadded:: 1.0

        :returns:
            The validator instance.
        :rtype:
            Validator
        Tr   r   r   r   r   allow_use_of_passwordw      
zValidator.allow_use_of_passwordc                 C   r-   )zPrevent passwords from being included in the URI.

        .. versionadded:: 1.0

        :returns:
            The validator instance.
        :rtype:
            Validator
        Fr.   r   r   r   r   forbid_use_of_password   r0   z Validator.forbid_use_of_passwordc                 G   J   dd |D }|D ]}|| j vrtd|q	| jdd |D  | S )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
        c                 S      g | ]}|  qS r   lower.0cr   r   r   
<listcomp>       z/Validator.check_validity_of.<locals>.<listcomp>"{}" is not a valid componentc                 S      i | ]}|d qS Tr   r7   	componentr   r   r   
<dictcomp>       z/Validator.check_validity_of.<locals>.<dictcomp>)COMPONENT_NAMES
ValueErrorformatr   updater   
componentsr?   r   r   r   check_validity_of      
zValidator.check_validity_ofc                 G   r2   )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
        c                 S   r3   r   r4   r6   r   r   r   r9      r:   z1Validator.require_presence_of.<locals>.<listcomp>r;   c                 S   r<   r=   r   r>   r   r   r   r@      rA   z1Validator.require_presence_of.<locals>.<dictcomp>)rB   rC   rD   r   rE   rF   r   r   r   require_presence_of   rI   zValidator.require_presence_ofc                 C   s   | j st| dd | j D }dd | j D }|r"t|| |r)t|| t| j|d t| j	|d t| j
|d dS )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.
        c                 S      g | ]\}}|r|qS r   r   r7   r?   requiredr   r   r   r9          z&Validator.validate.<locals>.<listcomp>c                 S   rK   r   r   rL   r   r   r   r9      rN   r   r	   r
   N)r   check_passwordr   itemsr    ensure_required_components_existensure_components_are_validensure_one_ofr   r   r   )r   urir   r   r   r   r   validate   s   

zValidator.validateN)__name__
__module____qualname____doc__	frozensetrB   r   r   r#   r,   r/   r1   rH   rJ   rU   r   r   r   r   r      s    r   c                 C   s4   | j }|sdS |dd}t|dkrdS t| )z4Assert that there is no password present in the uri.N:r   )r   splitlenr   PasswordForbidden)rT   r   credentialsr   r   r   rO      s   
rO   c                 C   s8   t ||}|dur| r|| vrt||| dS dS dS )z=Assert that the uri's attribute is one of the allowed values.N)getattrr   UnpermittedComponentError)allowed_valuesrT   	attributevaluer   r   r   rS      s   
rS   c                    s0   t  fdd|D }|rtj g|R  dS )z;Assert that all required components are present in the URI.c                    s   g | ]}t  |d u r|qS )N)r`   r>   rT   r   r   r9     s
    z4ensure_required_components_exist.<locals>.<listcomp>N)sortedr   MissingComponentError)rT   r   missing_componentsr   re   r   rQ     s   
rQ   c                 C   s(   |r| duo
| | S | du p| | 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)rd   matcherrequirer   r   r   is_valid  s   
rl   NFc                 C   s(   t | tj|}|r|du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
    N)rl   r   SUBAUTHORITY_MATCHERhost_is_valid)	authorityr	   rk   	validatedr   r   r   authority_is_valid!  s   
rq   c                 C   sZ   t | tj|}|r| durtj| rt| S |r+| dur+tj| r+t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)rl   r   HOST_MATCHERIPv4_MATCHERri   valid_ipv4_host_addressIPv6_MATCHERIPv6_NO_RFC4007_MATCHER)r	   rk   rp   r   r   r   rn   5  s   rn   c                 C      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
    )rl   r   SCHEME_MATCHER)r   rk   r   r   r   scheme_is_validI     ry   c                 C   rw   )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
    )rl   r   PATH_MATCHER)r   rk   r   r   r   path_is_validX  rz   r|   c                 C   rw   )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
    )rl   r   QUERY_MATCHER)r   rk   r   r   r   query_is_validg  rz   r~   c                 C   rw   )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
    )rl   r   FRAGMENT_MATCHER)r   rk   r   r   r   fragment_is_validv  rz   r   c                 C   s   t dd | dD S )z4Determine if the given host is a valid IPv4 address.c                 S   s,   g | ]}d t |dd  kodkn  qS )r'   r$   r%      )r)   )r7   byter   r   r   r9     s   , z+valid_ipv4_host_address.<locals>.<listcomp>.)allr\   )r	   r   r   r   rt     s   rt   )r   r   r   r   )r   r	   r
   c                 C   s   z|   }W n tjy   Y dS w |dkrt|d S |dkr"dS zt|d }W n
 ty4   Y dS w d|  ko>dkS   S )z4Determine if the userinfo, host, and port are valid.Fr	   r
   Tr'   r(   )authority_infor   InvalidAuthorityrn   r)   	TypeError)rT   r?   subauthority_dictr
   r   r   r   subauthority_component_is_valid  s   r   c                 C   sj   t g }|D ]!}|tv rt| |s|| qt| }|t| |s'|| q|r3tj| g|R  dS )z0Assert that all components are valid in the URI.N)r   _SUBAUTHORITY_VALIDATORSr   r   _COMPONENT_VALIDATORSr`   r   InvalidComponentsError)rT   r   invalid_componentsr?   	validatorr   r   r   rR     s   


rR   )NF)F)rY    r   r   r   objectr   rO   rS   rQ   rl   rq   rn   ry   r|   r~   r   rt   r   r   r   r   rR   r   r   r   r   <module>   s2    Y





