o
    €Ýh®  ã                   @   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   ú[/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/rfc3986/normalizers.pyÚnormalize_scheme   s   r	   c                 C   sF   | \}}}d}|r|t |ƒd 7 }|r|t|ƒ7 }|r!|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   ó
   t  | ¡S )z<Normalize a username to make it safe to include in userinfo.©r   Zurlquote)Úusernamer   r   r   Únormalize_username(   ó   
r   c                 C   r   )z/Normalize a password to make safe for userinfo.r   )Úpasswordr   r   r   Únormalize_password-   r   r   c                 C   s‚   t j | ¡r=|  d¡}|dkr=|  d¡}|dks(||k s(||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 t | ƒS )zNormalize the query string.©r   )Úqueryr   r   r   Únormalize_queryR   ó   r*   c                 C   r'   )zNormalize the fragment string.r(   )Úfragmentr   r   r   Únormalize_fragmentY   r+   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 ]}|dkrq	|dkr| |¡ q	|r | ¡  q	|  d¡r2|r,|d r2| dd¡ |  d¡r<| 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)r3   ÚsegmentsÚoutputÚsegmentr   r   r   r$   o   s   
€


r$   c           	      C   s¾   | du r| S t t t | |¡¡ƒ}t | |¡}|| d¡k}tƒ }tdt |ƒƒD ]1}|||d … }t	|ƒ}|r<|dksG|dk rM| 
¡ tjv rM| |¡ q(| d |¡ ¡  ¡ ¡ q(| 
|¡S )z7Encode the specific component in the provided encoding.Nó   %r   r   é€   z%{0:02x})r!   r/   r0   r   Zto_strÚto_bytesÚcountÚ	bytearrayÚrangeÚordÚdecoder   ZNON_PCT_ENCODEDÚextendÚformatÚencoder2   )	Zuri_componentÚencodingÚpercent_encodingsÚ	uri_bytesÚis_percent_encodedZencoded_uriÚiÚbyteÚbyte_ordr   r   r   Úencode_component‘   s"   ÿ

rT   )Ú__doc__Úrer
   r   r   r	   r   r   r   r   r&   r*   r-   Úcompiler/   r   r$   rT   r   r   r   r   Ú<module>   s    	
"