B
    Ç S]¡   ã               @   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ddddd	d
ddgfddddddddddddddddgfddgfgdd	dd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5gfdd6d7gfgd8œZed9  ed: d; ¡ d<d=„ ZG d>d?„ d?eƒZd@S )Az¾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.requestZ	URLopenerZFancyURLopenerZurlretrieveZ
_urlopenerZurlopenZ
urlcleanupZpathname2urlZurl2pathnamezurllib.parseZquoteZ
quote_plusZunquoteZunquote_plusZ	urlencodeZ	splitattrZ	splithostZ
splitnportZsplitpasswdZ	splitportZ
splitqueryZsplittagZ	splittypeZ	splituserZ
splitvaluezurllib.errorZContentTooShortErrorZinstall_openerZbuild_openerZRequestZOpenerDirectorZBaseHandlerZHTTPDefaultErrorHandlerZHTTPRedirectHandlerZHTTPCookieProcessorZProxyHandlerZHTTPPasswordMgrZHTTPPasswordMgrWithDefaultRealmZAbstractBasicAuthHandlerZHTTPBasicAuthHandlerZProxyBasicAuthHandlerZAbstractDigestAuthHandlerZHTTPDigestAuthHandlerZProxyDigestAuthHandlerZHTTPHandlerZHTTPSHandlerZFileHandlerZ
FTPHandlerZCacheFTPHandlerZUnknownHandlerZURLErrorZ	HTTPError)ÚurllibÚurllib2r   r   é   c              c   s~   t ƒ } xrt ¡ D ]f\}}x\|D ]T}|\}}t|ƒ}d||f V  d|||f V  d| V  d| V  d||f V  qW qW d S )Nzimport_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   ú)lib/python3.7/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   sz   |  d¡}|j}g }x6t|j dd… D ] }| t|d |dtƒ g¡ q(W | 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)	Úgetr   r   ÚvalueÚextendr   r   ÚappendÚreplace)r   Ú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}x*t|j D ]}|j|d kr@|d }P q@W |rx| t||d¡ n|  |d¡ n°g }i }	|d }
x–|
D ]Ž}|j	t
jkrÈ|jd	 j}|jd j}n
|j}d}|d
kržxPt|j D ]B}||d kræ|d |	kr| |d ¡ |	 |d g ¡ |¡ qæW qžW g }t|ƒ}d}dd„ }x–|D ]Ž}|	| }g }x2|dd… D ]"}| |||ƒ¡ | tƒ ¡ qlW | ||d |ƒ¡ t||ƒ}|rÆ|jj |¡rÌ||_| |¡ d}qNW |r,g }x&|dd… D ]}| |tƒ g¡ qøW | |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   )r   z!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   )r   r   r*   )Útyper
   Úimport_as_namer   Úchildrenr   Zcloner	   )r&   r   Zkidsr   r   r   Úhandle_nameŒ   s    z/FixUrllib.transform_member.<locals>.handle_namer   FzAll module elements are invalid)r   r   Ú
isinstanceÚlistr   r   r!   r   Úcannot_convertr,   r
   r-   r.   r    Ú
setdefaultr   r   r   r   ÚparentÚendswithr   )r   r"   r#   r(   r$   r)   Únew_namer   ÚmodulesZmod_dictr   Zas_nameÚmember_nameZ	new_nodesZindentationÚfirstr/   r   Zeltsr%   ZeltÚnewZnodesZnew_noder   r   r   Útransform_member\   sh    







zFixUrllib.transform_memberc             C   s€   |  d¡}|  d¡}d}t|tƒr*|d }x*t|j D ]}|j|d kr6|d }P q6W |rp| t||jd¡ n|  |d¡ dS )z.Transform for calls to module members in code.Úbare_with_attrr)   Nr   r   )r   z!This is an invalid module element)	r   r0   r1   r   r   r!   r   r   r2   )r   r"   r#   Z
module_dotr)   r6   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 )Nr   r(   r<   Zmodule_starzCannot handle star imports.Z	module_asz#This module is now multiple modules)r   r'   r;   r=   r2   )r   r"   r#   r   r   r   Ú	transform¹   s    




zFixUrllib.transformN)Ú__name__Ú
__module__Ú__qualname__r   r'   r;   r=   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@   $
