U
    Ê¼µb®  ã                   @   sŠ   d Z ddlZddlmZ ddlm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e d¡Zdd„ Zdd„ Zdd„ ZdS )z.Module with functions to normalize components.é    Né   )Úcompat)Úmiscc                 C   s   |   ¡ S )zNormalize the scheme component.)Úlower)Úscheme© r   ú7/tmp/pip-unpacked-wheel-9bk_1qnx/rfc3986/normalizers.pyÚnormalize_scheme   s    r	   c                 C   sF   | \}}}d}|r"|t |ƒd 7 }|r2|t|ƒ7 }|rB|d| 7 }|S )z)Normalize an authority tuple to a string.Ú ú@ú:)Únormalize_percent_charactersÚnormalize_host)Ú	authorityÚuserinfoÚhostÚportÚresultr   r   r   Únormalize_authority   s    
r   c                 C   s
   t  | ¡S )z<Normalize a username to make it safe to include in userinfo.©r   Zurlquote)Úusernamer   r   r   Únormalize_username(   s    r   c                 C   s
   t  | ¡S )z/Normalize a password to make safe for userinfo.r   )Úpasswordr   r   r   Únormalize_password-   s    r   c                 C   s‚   t j | ¡rz|  d¡}|dkrz|  d¡}|dksP||k sP||kr^|t| ƒd kr^|  ddd¡} | d|…  ¡ | |d…  S |  ¡ S )zNormalize a host string.ú%éÿÿÿÿz%25é   r   N)r   ZIPv6_MATCHERÚmatchÚfindÚlenÚreplacer   )r   ÚpercentZ
percent_25r   r   r   r   2   s    

ÿþýýr   c                 C   s   | s| S t | ƒ} t| ƒS )zNormalize the path string.)r   Úremove_dot_segments)Úpathr   r   r   Únormalize_pathI   s    r$   c                 C   s   | s| S t | ƒS )zNormalize the query string.©r   )Úqueryr   r   r   Únormalize_queryR   s    r'   c                 C   s   | s| S t | ƒS )zNormalize the fragment string.r%   )Úfragmentr   r   r   Únormalize_fragmentY   s    r)   z%[A-Fa-f0-9]{2}c                 C   s4   t t | ¡ƒ}|D ]}| ¡ s|  || ¡ ¡} q| S )zAll percent characters should be upper-cased.

    For example, ``"%3afoo%DF%ab"`` should be turned into ``"%3Afoo%DF%AB"``.
    )ÚsetÚPERCENT_MATCHERÚfindallÚisupperr    Úupper)ÚsÚmatchesÚmr   r   r   r   c   s
    r   c                 C   s„   |   d¡}g }|D ]0}|dkr"qq|dkr6| |¡ q|r| ¡  q|  d¡rf|rZ|d rf| dd¡ |  d¡rz| d¡ d |¡S )zURemove dot segments from the string.

    See also Section 5.2.4 of :rfc:`3986`.
    ú/Ú.z..r   r
   )z/.z/..)ÚsplitÚappendÚpopÚ
startswithÚinsertÚendswithÚjoin)r/   ÚsegmentsÚoutputÚsegmentr   r   r   r"   o   s    



r"   c           	      C   s¾   | dkr| S t t t | |¡¡ƒ}t | |¡}|| d¡k}tƒ }tdt |ƒƒD ]b}|||d … }t	|ƒ}|rx|dksŽ|dk rš| 
¡ tjkrš| |¡ qP| d |¡ ¡  ¡ ¡ qP| 
|¡S )z7Encode the specific component in the provided encoding.Nó   %r   r   é€   z%{0:02x})r   r+   r,   r   Zto_strÚto_bytesÚcountÚ	bytearrayÚrangeÚordÚdecoder   ZNON_PCT_ENCODEDÚextendÚformatÚencoder.   )	Zuri_componentÚencodingÚpercent_encodingsÚ	uri_bytesÚis_percent_encodedZencoded_uriÚiÚbyteÚbyte_ordr   r   r   Úencode_component‘   s(    ÿÿÿ
rP   )Ú__doc__Úrer
   r   r   r	   r   r   r   r   r$   r'   r)   Úcompiler+   r   r"   rP   r   r   r   r   Ú<module>   s   	
"