a
    ´i¯   ã                   @   s¤   d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZ dg d¢fdg d¢fdd	gfgdg d
¢fdddgfgdœZed  ed d ¡ dd„ ZG dd„ deƒZdS )z¾Fix changes imports of urllib which are now incompatible.
   This is rather similar to fix_imports, but because of the more
   complex nature of the fixing for urllib, it has its own fixer.
é    )Ú
alternatesÚ
FixImports)ÚNameÚCommaÚ
FromImportÚNewlineÚfind_indentationÚNodeÚsymszurllib.request)	Z	URLopenerZFancyURLopenerÚurlretrieveZ
_urlopenerÚurlopenÚ
urlcleanupÚpathname2urlÚurl2pathnameÚ
getproxieszurllib.parse)ÚquoteÚ
quote_plusÚunquoteÚunquote_plusÚ	urlencodeÚ	splitattrÚ	splithostÚ
splitnportÚsplitpasswdÚ	splitportÚ
splitqueryÚsplittagÚ	splittypeÚ	splituserÚ
splitvaluezurllib.errorÚContentTooShortError)r   Zinstall_openerZbuild_openerZRequestZOpenerDirectorZBaseHandlerZHTTPDefaultErrorHandlerZHTTPRedirectHandlerZHTTPCookieProcessorZProxyHandlerZHTTPPasswordMgrZHTTPPasswordMgrWithDefaultRealmZAbstractBasicAuthHandlerZHTTPBasicAuthHandlerZProxyBasicAuthHandlerZAbstractDigestAuthHandlerZHTTPDigestAuthHandlerZProxyDigestAuthHandlerZHTTPHandlerZHTTPSHandlerÚFileHandlerZ
FTPHandlerZCacheFTPHandlerZUnknownHandlerÚURLErrorÚ	HTTPError)ÚurllibÚurllib2r%   r$   é   c                  c   sv   t ƒ } t ¡ D ]b\}}|D ]T}|\}}t|ƒ}d||f V  d|||f V  d| V  d| V  d||f V  qqd S )Nz�import_name< 'import' (module=%r
                                  | dotted_as_names< any* module=%r any* >) >
                  zÆimport_from< 'from' mod_member=%r 'import'
                       ( member=%s | import_as_name< member=%s 'as' any > |
                         import_as_names< members=any*  >) >
                  zIimport_from< 'from' module_star=%r 'import' star='*' >
                  ztimport_name< 'import'
                                  dotted_as_name< module_as=%r 'as' any > >
                  zKpower< bare_with_attr=%r trailer< '.' member=%s > any* >
                  )ÚsetÚMAPPINGÚitemsr   )ZbareZ
old_moduleZchangesÚchangeZ
new_moduleÚmembers© r,   ú0/usr/lib64/python3.9/lib2to3/fixes/fix_urllib.pyÚbuild_pattern0   s(    þýÿþÿr.   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú	FixUrllibc                 C   s   d  tƒ ¡S )Nú|)Újoinr.   )Úselfr,   r,   r-   r.   I   s    zFixUrllib.build_patternc                 C   sv   |  d¡}|j}g }t|j dd… D ] }| t|d |d�tƒ g¡ q&| tt|j d d |d�¡ | |¡ dS )z‘Transform for the basic import case. Replaces the old
           import name with a comma separated list of its
           replacements.
        ÚmoduleNéÿÿÿÿr   ©Úprefix)	Úgetr6   r(   ÚvalueÚextendr   r   ÚappendÚreplace)r2   ÚnodeÚresultsZ
import_modÚprefÚnamesÚnamer,   r,   r-   Útransform_importL   s    
 zFixUrllib.transform_importc                 C   s&  |  d¡}|j}|  d¡}|r†t|tƒr0|d }d}t|j D ]}|j|d v r>|d } q^q>|rv| t||d�¡ n|  |d¡ �nœg }i }	|d }
|
D ]Š}|j	t
jkrÄ|jd	 j}|jd j}n
|j}d}|d
kršt|j D ]B}||d v rà|d |	v�r| |d ¡ |	 |d g ¡ |¡ qàqšg }t|ƒ}d}dd„ }|D ]Š}|	| }g }|dd… D ]"}| |||ƒ¡ | tƒ ¡ �q^| ||d |ƒ¡ t||ƒ}|�r¶|jj |¡�r¼||_| |¡ d}�qB|�rg }|dd… D ]}| |tƒ g¡ �qä| |d ¡ | |¡ n|  |d¡ dS )zšTransform for imports of specific module elements. Replaces
           the module to be imported from with the appropriate new
           module.
        Ú
mod_memberÚmemberr   Nr&   r5   ú!This is an invalid module elementr+   é   ú,Tc                 S   sX   | j tjkrHt| jd j|d�| jd  ¡ | jd  ¡ g}ttj|ƒgS t| j|d�gS )Nr   r5   r&   rE   )Útyper
   Úimport_as_namer   Úchildrenr8   Zcloner	   )r@   r6   Zkidsr,   r,   r-   Úhandle_nameŒ   s    þz/FixUrllib.transform_member.<locals>.handle_namer4   FzAll module elements are invalid)r7   r6   Ú
isinstanceÚlistr(   r8   r;   r   Úcannot_convertrG   r
   rH   rI   r:   Ú
setdefaultr   r9   r   r   ÚparentÚendswithr   )r2   r<   r=   rB   r>   rC   Únew_namer*   ÚmodulesZmod_dictr+   Zas_nameÚmember_nameZ	new_nodesZindentationÚfirstrJ   r3   Úeltsr?   ÚeltÚnewZnodesZnew_noder,   r,   r-   Útransform_member\   sh    




zFixUrllib.transform_memberc                 C   s~   |  d¡}|  d¡}d}t|tƒr*|d }t|j D ]}|j|d v r4|d } qTq4|rn| t||jd�¡ n|  |d¡ dS )z.Transform for calls to module members in code.Úbare_with_attrrC   Nr   r&   r5   rD   )	r7   rK   rL   r(   r8   r;   r   r6   rM   )r2   r<   r=   Z
module_dotrC   rQ   r*   r,   r,   r-   Útransform_dot¨   s    


ÿ
zFixUrllib.transform_dotc                 C   sz   |  d¡r|  ||¡ n^|  d¡r0|  ||¡ nF|  d¡rH|  ||¡ n.|  d¡r`|  |d¡ n|  d¡rv|  |d¡ d S )Nr3   rB   rY   Zmodule_starzCannot handle star imports.Z	module_asz#This module is now multiple modules)r7   rA   rX   rZ   rM   )r2   r<   r=   r,   r,   r-   Ú	transform¹   s    




zFixUrllib.transformN)Ú__name__Ú
__module__Ú__qualname__r.   rA   rX   rZ   r[   r,   r,   r,   r-   r/   G   s
   Lr/   N)Ú__doc__Zlib2to3.fixes.fix_importsr   r   Zlib2to3.fixer_utilr   r   r   r   r   r	   r
   r(   r:   r.   r/   r,   r,   r,   r-   Ú<module>   s.   $ÿÿÿöÿÿñô!