3
i^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   s|   | j sddddS | j }|dkr6tj| j j| j|j }|jd}|rxtj	j
|rxtj| rxtj| j 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   B/tmp/pip-build-8app2_gc/urllib3/urllib3/packages/rfc3986/_mixin.pyauthority_info   s    
zURIMixin.authority_infoc             C   s   t jj| jS )N)r   ZSUBAUTHORITY_MATCHERr   r   )r   r   r   r   r   5   s    zURIMixin._match_subauthorityc             C   s,   y| j  }W n tjk
r"   dS X |d S )z+If present, a string representing the host.Nr	   )r   r   r   )r   r   r   r   r   r	   8   s
    zURIMixin.hostc             C   s,   y| j  }W n tjk
r"   dS X |d S )z2If present, the port extracted from the authority.Nr
   )r   r   r   )r   r   r   r   r   r
   A   s
    zURIMixin.portc             C   s,   y| j  }W n tjk
r"   dS X |d S )z6If present, the userinfo extracted from the authority.Nr   )r   r   r   )r   r   r   r   r   r   J   s
    zURIMixin.userinfoc             C   s   t tjj| 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   unsplit)r   r   r   r   is_absoluteS   s    zURIMixin.is_absolutec             K   sr   t jdt | j|jddf| j|jddf| j|jddf| j|jddf| j|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
        zXPlease 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 )Nr   ).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   sD   t jdt y| j  W n tjk
r.   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
        zXPlease use rfc3986.validators.Validator instead. This method will be eventually removed.F)r	   require)
r    r!   r"   r   r   r   r   r$   r   r	   )r   r+   r   r   r   r$   }   s    zURIMixin.authority_is_validc             C   s   t jdt tj| 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
        zXPlease use rfc3986.validators.Validator instead. This method will be eventually removed.)r    r!   r"   r   r#   scheme)r   r+   r   r   r   r#      s    zURIMixin.scheme_is_validc             C   s   t jdt tj| 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
        zXPlease use rfc3986.validators.Validator instead. This method will be eventually removed.)r    r!   r"   r   r%   path)r   r+   r   r   r   r%      s    zURIMixin.path_is_validc             C   s   t jdt tj| 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
        zXPlease use rfc3986.validators.Validator instead. This method will be eventually removed.)r    r!   r"   r   r&   query)r   r+   r   r   r   r&      s    zURIMixin.query_is_validc             C   s   t jdt tj| 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
        zXPlease use rfc3986.validators.Validator instead. This method will be eventually removed.)r    r!   r"   r   r'   fragment)r   r+   r   r   r   r'      s    zURIMixin.fragment_is_validc             C   s   t | j t |j 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| j|}|j s*tj||j }| }| rT|j|jkrT|j	dd}|jdk	rt|j	t
j|jd}n|jdk	r|j	|jt
j|jd}n|jdkr|jdk	r|j}n|j}|j	|j|j|j|d}nF|jjdrt
j|j}nt
jtj||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)r,   )r-   )r,   r-   )r,   r   r-   r.   /)
isinstancer   typeZfrom_stringr   r   ResolutionErrorr1   r,   	copy_withr   normalize_pathr-   r   r.   
startswithr   Zmerge_paths)r   Zbase_uristrictZ	resolvingtargetr.   r-   r   r   r   resolve_with   sD    







zURIMixin.resolve_withc             C   s   g }| j r|j| j dg | jr0|jd| jg | jrB|j| j | jdk	r\|jd| jg | jdk	rv|jd| jg dj|S )zCreate a URI string from the components.

        :returns: The URI Reference reconstituted as a string.
        :rtype: str
        :z//N?# )r,   extendr   r-   appendr.   r/   join)r   Zresult_listr   r   r   r   -  s    

zURIMixin.unsplitc       
      C   sR   |||||d}x(t |j D ]\}}|tjkr||= qW | 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
        )r,   r   r-   r.   r/   )listitemsr   UseExisting_replacer   )
r   r,   r   r-   r.   r/   
attributeskeyvalueurir   r   r   r7   A  s    

zURIMixin.copy_withN)F)F)F)F)F)F)__name__
__module____qualname____doc__r0   __hash__r   r   propertyr	   r
   r   r   r*   r$   r#   r%   r&   r'   r2   r<   r   r   rF   r7   r   r   r   r   r   
   s(   &			
 






Fr   )
rO   r    r@   r   r   r   r   r   objectr   r   r   r   r   <module>   s   