
\[\                  @   sv  d  Z  d d l m Z m Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z i 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  6d d
 d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 g f d d8 d9 g f g d: 6Z e d: j e d  d;  d< d=   Z Gd> 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)
fixer_base)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urllibZ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	HTTPErrorZurllib2   c              c   s   t    }  x t j   D]w \ } } xh | D]` } | \ } } t |  } d | | f Vd | | | f Vd | Vd | Vd | | f Vq) Wq Wd  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.4/lib2to3/fixes/fix_urllib.pybuild_pattern1   s    			r   c               @   sL   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 j  t    S)N|)joinr   )selfr   r   r   r   J   s    zFixUrllib.build_patternc             C   s   | j  d  } | j } g  } xE t | j d d  D], } | j t | d d | t   g  q6 W| j t t | j d d d |  | j |  d S)zTransform for the basic import case. Replaces the old
           import name with a comma separated list of its
           replacements.
        moduleNr   r   prefixr   )	getr   r   valueextendr   r   appendreplace)r   noderesultsZ
import_modprefnamesnamer   r   r   transform_importM   s    	*(zFixUrllib.transform_importc             C   s  | j  d  } | j } | j  d  } | r t | t  rI | d } n  d } x6 t | j D]' } | j | d k r] | d } Pq] q] W| r | j t | d |  q|  j | d  n>g  } i  }	 | d }
 x |
 D] } | j	 t
 j k r| j d	 j } | j d j } n | j } d } | d
 k r xl t | j D]Z } | | d k r>| d |	 k rx| j | d  n  |	 j | d g   j |  q>q>Wq q Wg  } t |  } d } d d   } x | D] } |	 | } g  } x> | d d  D], } | j | | |   | j t    qW| j | | d |   t | |  } | sj| j j j |  rv| | _ n  | j |  d } qW| rg  } x. | d d  D] } | j | t   g  qW| j | d  | j |  n |  j | 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   z!This is an invalid module elementr      ,Tc             S   sz   |  j  t j k rd t |  j d j d | |  j d j   |  j d j   g } t t j |  g St |  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_nameFzAll module elements are invalidr   r   r   r   )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_nameZ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   | j  d  } | j  d  } d } t | t  r@ | d } n  x6 t | j D]' } | j | d k rN | d } PqN qN W| r | j t | d | j  n |  j | 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   r/   r0   r   r   r    r   r   r1   )r   r!   r"   Z
module_dotr(   r5   r   r   r   r   transform_dot   s    
zFixUrllib.transform_dotc             C   s   | j  d  r" |  j | |  n | j  d  rD |  j | |  nf | j  d  rf |  j | |  nD | j  d  r |  j | d  n" | j  d  r |  j | d  n  d  S)Nr   r'   r:   Zmodule_starzCannot handle star imports.Z	module_asz#This module is now multiple modules)r   r&   r9   r;   r1   )r   r!   r"   r   r   r   	transform   s    zFixUrllib.transformN)__name__
__module____qualname__r   r&   r9   r;   r<   r   r   r   r   r   H   s
   Lr   N)__doc__Zlib2to3.fixes.fix_importsr   r   Zlib2to3r   Zlib2to3.fixer_utilr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   <module>   sD   4						