U
    A`                      @   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 )AzFix 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   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 )Nzimport_name< 'import' (module=%r
                                  | dotted_as_names< any* module=%r any* >) >
                  zimport_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   ./usr/lib/python3.8/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 )zTransform for the basic import case. Replaces the old
           import name with a comma separated list of its
           replacements.
        moduleNr   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}|rt|tr0|d }d}t|j D ]}|j|d kr>|d } q^q>|rv|t||d n| |d ng }i }	|d }
|
D ]}|j	t
jkr|jd	 j}|jd j}n
|j}d}|d
krt|j D ]B}||d kr|d |	kr||d  |	|d g | qqg }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 )zTransform for imports of specific module elements. Replaces
           the module to be imported from with the appropriate new
           module.
        
mod_membermemberr   Nr   r   !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/   r0   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firstr1   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 }t|j D ]}|j|d kr4|d } qTq4|rn|t||jd n| |d dS )z.Transform for calls to module members in code.bare_with_attrr*   Nr   r   r   r+   )	r   r2   r3   r   r   r"   r   r   r4   )r   r#   r$   Z
module_dotr*   r8   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?   r4   )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~   $                    !