U
    ʼb3                     @   sP   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ G dd	 d	eZ	dS )
z;Module containing the implementation of the URIMixin class.    N   )
exceptions)misc)normalizers)
validatorsc                   @   s   e Zd ZdZejZdd Zdd Zedd Z	edd	 Z
e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 Zd)ddZdd  Zejejejejejfd!d"Zd#S )*URIMixinz0Mixin with all shared methods for URIs and IRIs.c                 C   sz   | j sddddS |  }|dkr6t| j | j| }|d}|rvtj	
|rvt|svt| j | j|S )a  Return a dictionary with the ``userinfo``, ``host``, and ``port``.

        If the authority is not valid, it will raise a
        :class:`~rfc3986.exceptions.InvalidAuthority` Exception.

        :returns:
            ``{'userinfo': 'username:password', 'host': 'www.example.com',
            'port': '80'}``
        :rtype: dict
        :raises rfc3986.exceptions.InvalidAuthority:
            If the authority is not ``None`` and can not be parsed.
        N)userinfohostportr	   )	authority_match_subauthorityexcInvalidAuthorityencodeencoding	groupdictgetr   ZIPv4_MATCHERmatchr   Zvalid_ipv4_host_address)selfr   matchesr	    r   2/tmp/pip-unpacked-wheel-9bk_1qnx/rfc3986/_mixin.pyauthority_info   s    

zURIMixin.authority_infoc                 C   s   t j| jS N)r   ZSUBAUTHORITY_MATCHERr   r   r   r   r   r   r   8   s    zURIMixin._match_subauthorityc                 C   s.   z|   }W n tjk
r$   Y dS X |d S )z+If present, a string representing the host.Nr	   r   r   r   r   r   r   r   r   r	   ;   s
    zURIMixin.hostc                 C   s.   z|   }W n tjk
r$   Y dS X |d S )z2If present, the port extracted from the authority.Nr
   r   r   r   r   r   r
   D   s
    zURIMixin.portc                 C   s.   z|   }W n tjk
r$   Y dS X |d S )z6If present, the userinfo extracted from the authority.Nr   r   r   r   r   r   r   M   s
    zURIMixin.userinfoc                 C   s   t tj|  S )zDetermine if this URI Reference is an absolute URI.

        See http://tools.ietf.org/html/rfc3986#section-4.3 for explanation.

        :returns: ``True`` if it is an absolute URI, ``False`` otherwise.
        :rtype: bool
        )boolr   ZABSOLUTE_URI_MATCHERr   unsplitr   r   r   r   is_absoluteV   s    zURIMixin.is_absolutec              	   K   sr   t dt | j|ddf| j|ddf| j|ddf| j|ddf| j|ddfg}t	dd	 |D S )
ab  Determine if the URI is valid.

        .. deprecated:: 1.1.0

            Use the :class:`~rfc3986.validators.Validator` object instead.

        :param bool require_scheme: Set to ``True`` if you wish to require the
            presence of the scheme component.
        :param bool require_authority: Set to ``True`` if you wish to require
            the presence of the authority component.
        :param bool require_path: Set to ``True`` if you wish to require the
            presence of the path component.
        :param bool require_query: Set to ``True`` if you wish to require the
            presence of the query component.
        :param bool require_fragment: Set to ``True`` if you wish to require
            the presence of the fragment component.
        :returns: ``True`` if the URI is valid. ``False`` otherwise.
        :rtype: bool
        XPlease use rfc3986.validators.Validator instead. This method will be eventually removed.Zrequire_schemeFZrequire_authorityZrequire_pathZrequire_queryZrequire_fragmentc                 s   s   | ]\}}||V  qd S r   r   ).0vrr   r   r   	<genexpr>   s     z$URIMixin.is_valid.<locals>.<genexpr>)
warningswarnDeprecationWarningscheme_is_validr   authority_is_validpath_is_validquery_is_validfragment_is_validall)r   kwargsr   r   r   r   is_valid`   s    zURIMixin.is_validFc                 C   sF   t dt z|   W n tjk
r0   Y dS X tj| j| j	|dS )a  Determine if the authority component is valid.

        .. deprecated:: 1.1.0

            Use the :class:`~rfc3986.validators.Validator` object instead.

        :param bool require:
            Set to ``True`` to require the presence of this component.
        :returns:
            ``True`` if the authority is valid. ``False`` otherwise.
        :rtype:
            bool
        r    F)r	   require)
r%   r&   r'   r   r   r   r   r)   r   r	   r   r0   r   r   r   r)      s    zURIMixin.authority_is_validc                 C   s   t dt t| j|S )a`  Determine if the scheme component is valid.

        .. deprecated:: 1.1.0

            Use the :class:`~rfc3986.validators.Validator` object instead.

        :param str require: Set to ``True`` to require the presence of this
            component.
        :returns: ``True`` if the scheme is valid. ``False`` otherwise.
        :rtype: bool
        r    )r%   r&   r'   r   r(   schemer1   r   r   r   r(      s
    zURIMixin.scheme_is_validc                 C   s   t dt t| j|S )a\  Determine if the path component is valid.

        .. deprecated:: 1.1.0

            Use the :class:`~rfc3986.validators.Validator` object instead.

        :param str require: Set to ``True`` to require the presence of this
            component.
        :returns: ``True`` if the path is valid. ``False`` otherwise.
        :rtype: bool
        r    )r%   r&   r'   r   r*   pathr1   r   r   r   r*      s
    zURIMixin.path_is_validc                 C   s   t dt t| j|S )a^  Determine if the query component is valid.

        .. deprecated:: 1.1.0

            Use the :class:`~rfc3986.validators.Validator` object instead.

        :param str require: Set to ``True`` to require the presence of this
            component.
        :returns: ``True`` if the query is valid. ``False`` otherwise.
        :rtype: bool
        r    )r%   r&   r'   r   r+   queryr1   r   r   r   r+      s
    zURIMixin.query_is_validc                 C   s   t dt t| j|S )aG  Determine if the fragment component is valid.

        .. deprecated:: 1.1.0

            Use the Validator object instead.

        :param str require: Set to ``True`` to require the presence of this
            component.
        :returns: ``True`` if the fragment is valid. ``False`` otherwise.
        :rtype: bool
        r    )r%   r&   r'   r   r,   fragmentr1   r   r   r   r,      s
    zURIMixin.fragment_is_validc                 C   s   t |  t | kS )a  Compare this URIReference to another URIReference.

        :param URIReference other_ref: (required), The reference with which
            we're comparing.
        :returns: ``True`` if the references are equal, ``False`` otherwise.
        :rtype: bool
        )tuple	normalize)r   Z	other_refr   r   r   normalized_equality   s    zURIMixin.normalized_equalityc                 C   s  t |tst| |}| s*t|| }| }|sR|j|jkrR|j	dd}|jdk	rr|j	t
|jd}n|jdk	r|j	|jt
|jd}n|jdkr|jdk	r|j}n|j}|j	|j|j|j|d}nF|jdrt
|j}nt
t||j}|j	|j|j||jd}|S )a  Use an absolute URI Reference to resolve this relative reference.

        Assuming this is a relative reference that you would like to resolve,
        use the provided base URI to resolve it.

        See http://tools.ietf.org/html/rfc3986#section-5 for more information.

        :param base_uri: Either a string or URIReference. It must be an
            absolute URI or it will raise an exception.
        :returns: A new URIReference which is the result of resolving this
            reference using ``base_uri``.
        :rtype: :class:`URIReference`
        :raises rfc3986.exceptions.ResolutionError:
            If the ``base_uri`` is not an absolute URI.
        N)r2   )r3   )r2   r3   )r2   r   r3   r4   /)
isinstancer   typeZfrom_stringr   r   ResolutionErrorr7   r2   	copy_withr   normalize_pathr3   r   r4   
startswithr   Zmerge_paths)r   Zbase_uristrictZ	resolvingtargetr4   r3   r   r   r   resolve_with   sN    







zURIMixin.resolve_withc                 C   s   g }| j r|| j dg | jr0|d| jg | jrB|| j | jdk	r\|d| jg | jdk	rv|d| jg d|S )zCreate a URI string from the components.

        :returns: The URI Reference reconstituted as a string.
        :rtype: str
        :z//N?# )r2   extendr   r3   appendr4   r5   join)r   Zresult_listr   r   r   r   <  s    

zURIMixin.unsplitc           
      C   sN   |||||d}t | D ]\}}|tjkr||= q| jf |}	| j|	_|	S )a  Create a copy of this reference with the new components.

        :param str scheme:
            (optional) The scheme to use for the new reference.
        :param str authority:
            (optional) The authority to use for the new reference.
        :param str path:
            (optional) The path to use for the new reference.
        :param str query:
            (optional) The query to use for the new reference.
        :param str fragment:
            (optional) The fragment to use for the new reference.
        :returns:
            New URIReference with provided components.
        :rtype:
            URIReference
        )r2   r   r3   r4   r5   )listitemsr   UseExisting_replacer   )
r   r2   r   r3   r4   r5   
attributeskeyvalueurir   r   r   r=   P  s    
zURIMixin.copy_withN)F)F)F)F)F)F)__name__
__module____qualname____doc__r6   __hash__r   r   propertyr	   r
   r   r   r/   r)   r(   r*   r+   r,   r8   rB   r   r   rL   r=   r   r   r   r   r   
   s4   )



"






Fr   )
rU   r%   rF   r   r   r   r   r   objectr   r   r   r   r   <module>   s   