o
    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   Z/var/www/html/magazine_api/magazine_env/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   Z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   Zportsr
   Z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   r-   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   r4   	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   r/   )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   r0   r   r1   r3   r   r   r   r6      r7   z1Validator.require_presence_of.<locals>.<listcomp>r8   c                 S   r9   r:   r   r;   r   r   r   r=      r>   z1Validator.require_presence_of.<locals>.<dictcomp>)r?   r@   rA   r   rB   rC   r   r   r   require_presence_of   rF   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   r4   r<   requiredr   r   r   r6          z&Validator.validate.<locals>.<listcomp>c                 S   rH   r   r   rI   r   r   r   r6      rK   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__	frozensetr?   r   r   r"   r)   r,   r.   rE   rG   rR   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   ZPasswordForbidden)rQ   r   credentialsr   r   r   rL      s   
rL   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   ZUnpermittedComponentError)Zallowed_valuesrQ   	attributevaluer   r   r   rP      s   
rP   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;   rQ   r   r   r6     s
    z4ensure_required_components_exist.<locals>.<listcomp>N)sortedr   ZMissingComponentError)rQ   r   Zmissing_componentsr   r_   r   rN     s   
rN   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)r^   Zmatcherrequirer   r   r   is_valid  s   
rc   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)rc   r   ZSUBAUTHORITY_MATCHERhost_is_valid)	authorityr	   rb   	validatedr   r   r   authority_is_valid!  s   
rg   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)rc   r   ZHOST_MATCHERZIPv4_MATCHERra   valid_ipv4_host_addressZIPv6_MATCHERZIPv6_NO_RFC4007_MATCHER)r	   rb   rf   r   r   r   rd   5  s   rd   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
    )rc   r   ZSCHEME_MATCHER)r   rb   r   r   r   scheme_is_validI     rj   c                 C   ri   )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
    )rc   r   ZPATH_MATCHER)r   rb   r   r   r   path_is_validX  rk   rl   c                 C   ri   )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
    )rc   r   ZQUERY_MATCHER)r   rb   r   r   r   query_is_validg  rk   rm   c                 C   ri   )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
    )rc   r   ZFRAGMENT_MATCHER)r   rb   r   r   r   fragment_is_validv  rk   rn   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(   )r4   byter   r   r   r6     s   , z+valid_ipv4_host_address.<locals>.<listcomp>.)allrY   )r	   r   r   r   rh     s   rh   )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'   )Zauthority_infor   ZInvalidAuthorityrd   r(   	TypeError)rQ   r<   Zsubauthority_dictr
   r   r   r   subauthority_component_is_valid  s   rt   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_VALIDATORSrt   r   _COMPONENT_VALIDATORSr\   r   ZInvalidComponentsError)rQ   r   Zinvalid_componentsr<   	validatorr   r   r   rO     s   


rO   )NF)F)rV    r   r   r   objectr   rL   rP   rN   rc   rg   rd   rj   rl   rm   rn   rh   rv   r   ru   rt   rO   r   r   r   r   <module>   s2    Y





