U
    Ê¼µbà3  ã                   @   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 )zéDetermine 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¡rìt
 |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 )z…Create 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   