U
    :xg                     @  sJ  d dl mZ d dlZd dlZd dl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 d dlmZ dd	lmZ d
ddddZede ddddddZeedd- ZedeZeded Zeded Zeded Zdddd d!Zddd"d#d$Zd%ejjkr&ejjd% ddd&d'd(Zd)dd*d+d,Z dS ).    )annotationsN)quote)unquote)	urlencode)urlsplit)
urlunsplit   )iter_multi_itemsUnicodeErrorztuple[str, int])ereturnc                 C  s$   t | j| j| j dd}|| jfS )zRUsed in :func:`uri_to_iri` after unquoting to re-quote any
    invalid bytes.
     safe)r   objectstartend)r   out r   1/tmp/pip-unpacked-wheel-5ifiwx3z/werkzeug/urls.py_codec_error_url_quote   s    r   werkzeug.url_quotestrzt.Callable[[str], str])namecharsr   c                   sR   d dd t|D }td| dtj ddd fdd	}d
|  |_|S )zCreate a function that unquotes all percent encoded characters except those
    given. This allows working with unquoted characters if possible while not changing
    the meaning of a given part of a URL.
    |c                 s  s   | ]}t |d V  qdS )Z02XN)ord).0cr   r   r   	<genexpr>"   s     z%_make_unquote_part.<locals>.<genexpr>z((?:%(?:z))+)r   )valuer   c                   sH   t  | }g }|D ]&}|t|dd |t|d qd|S )Nzutf-8r   r   )itersplitappendr   nextjoin)r    partsr   partpatternr   r   _unquote_partial%   s    z,_make_unquote_part.<locals>._unquote_partialZ	_unquote_)r%   sortedrecompileI__name__)r   r   choicesr*   r   r(   r   _make_unquote_part   s
    
r1   !   %      fragmentqueryz&=+#pathz/?#userz:@/?#)urir   c                 C  s   t | }t|j}t|j}t|j}|jr8t|j}nd}d|krPd| d}|j	rf| d|j	 }|j
rt|j
}|jrt|j}| d| }| d| }t|j||||fS )a-  Convert a URI to an IRI. All valid UTF-8 characters are unquoted,
    leaving all reserved and invalid characters quoted. If the URL has
    a domain, it is decoded from Punycode.

    >>> uri_to_iri("http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF")
    'http://\u2603.net/p\xe5th?q=\xe8ry%DF'

    :param uri: The URI to convert.

    .. versionchanged:: 3.0
        Passing a tuple or bytes, and the ``charset`` and ``errors`` parameters,
        are removed.

    .. versionchanged:: 2.3
        Which characters remain quoted is specific to each part of the URL.

    .. versionchanged:: 0.15
        All reserved and invalid characters remain quoted. Previously,
        only some reserved characters were preserved, and invalid bytes
        were replaced instead of left quoted.

    .. versionadded:: 0.6
    r   :[]@)r   _unquote_pathr7   _unquote_queryr6   _unquote_fragmentr5   hostname_decode_idnaportusername_unquote_userpasswordr   scheme)r9   r&   r7   r6   r5   netlocauthrF   r   r   r   
uri_to_iri=   s$    




rJ   )irir   c                 C  s   t | }t|jdd}t|jdd}t|jdd}|jrL|jdd}nd}d|krdd	| d
}|jrz| d|j }|j	rt|j	dd}|j
rt|j
dd}| d| }| d| }t|j||||fS )a,  Convert an IRI to a URI. All non-ASCII and unsafe characters are
    quoted. If the URL has a domain, it is encoded to Punycode.

    >>> iri_to_uri('http://\u2603.net/p\xe5th?q=\xe8ry%DF')
    'http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF'

    :param iri: The IRI to convert.

    .. versionchanged:: 3.0
        Passing a tuple or bytes, the ``charset`` and ``errors`` parameters,
        and the ``safe_conversion`` parameter, are removed.

    .. versionchanged:: 2.3
        Which characters remain unquoted is specific to each part of the URL.

    .. versionchanged:: 0.15
        All reserved characters remain unquoted. Previously, only some reserved
        characters were left unquoted.

    .. versionchanged:: 0.9.6
       The ``safe_conversion`` parameter was added.

    .. versionadded:: 0.6
    z%!$&'()*+,/:;=@r   z%!$&'()*+,/:;=?@z%!#$&'()*+,/:;=?@idnaasciir   r:   r;   r<   z%!$&'()*+,;=r=   )r   r   r7   r6   r5   rA   encodedecoderC   rD   rF   r   rG   )rK   r&   r7   r6   r5   rH   rI   rF   r   r   r   
iri_to_uriq   s$    rP   zitms-services)domainr   c              	   C  s   z|  d}W n tk
r&   |  Y S X z|dW S  tk
rH   Y nX g }|dD ]>}z||d W qX tk
r   ||d Y qXX qXd|S )NrM   rL      ..)rN   UnicodeEncodeErrorrO   UnicodeDecodeErrorr"   r#   r%   )rQ   datar&   r'   r   r   r   rB      s    
rB   z1t.Mapping[str, str] | t.Iterable[tuple[str, str]])r6   r   c                 C  s   dd t | D }t|ddS )Nc                 S  s   g | ]}|d  dk	r|qS )r   Nr   )r   xr   r   r   
<listcomp>   s      z_urlencode.<locals>.<listcomp>z!$'()*,/:;?@r   )r	   r   )r6   itemsr   r   r   
_urlencode   s    rZ   )r3   r4   )!
__future__r   codecsr,   typingturllib.parseurllibr   r   r   r   r   Zdatastructuresr	   r   register_errorr1   bytesrangerO   Z_always_unsafer@   r?   r>   rE   rJ   rP   parseuses_netlocr#   rB   rZ   r   r   r   r   <module>   s0   

4: