B
    v9a¸  ã               @   sZ   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
mZ dd„ ZG dd	„ d	ejƒZd
S )zÞFixer for import statements.
If spam is being imported from the local directory, this import:
    from spam import eggs
Becomes:
    from .spam import eggs

And this import:
    import spam
Becomes:
    from . import spam
é   )Ú
fixer_baseé    )ÚdirnameÚjoinÚexistsÚsep)Ú
FromImportÚsymsÚtokenc             c   s¢   | g}x–|rœ|  ¡ }|jtjkr*|jV  q|jtjkrPd dd„ |jD ƒ¡V  q|jtj	krn| 
|jd ¡ q|jtjkr’| |jddd… ¡ qtdƒ‚qW dS )zF
    Walks over all the names imported in a dotted_as_names node.
    Ú c             S   s   g | ]
}|j ‘qS © )Úvalue)Ú.0Zchr   r   ú /usr/lib/python3.7/fix_import.pyú
<listcomp>   s    z$traverse_imports.<locals>.<listcomp>r   Néþÿÿÿzunknown node type)ÚpopÚtyper
   ÚNAMEr   r	   Zdotted_namer   ÚchildrenZdotted_as_nameÚappendZdotted_as_namesÚextendÚAssertionError)ÚnamesZpendingÚnoder   r   r   Útraverse_imports   s    
r   c                   s4   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )	Ú	FixImportTzj
    import_from< 'from' imp=any 'import' ['('] any [')'] >
    |
    import_name< 'import' imp=any >
    c                s"   t t| ƒ ||¡ d|jk| _d S )NZabsolute_import)Úsuperr   Ú
start_treeZfuture_featuresÚskip)ÚselfZtreeÚname)Ú	__class__r   r   r   /   s    zFixImport.start_treec             C   s¼   | j r
d S |d }|jtjkrZxt|dƒs6|jd }q W |  |j¡r¸d|j |_| ¡  n^d}d}x$t	|ƒD ]}|  |¡r€d}qld}qlW |r |rœ|  
|d¡ d S td|gƒ}|j|_|S d S )NÚimpr   r   Ú.FTz#absolute and local imports together)r   r   r	   Zimport_fromÚhasattrr   Úprobably_a_local_importr   Zchangedr   Zwarningr   Úprefix)r    r   Úresultsr#   Z
have_localZhave_absoluteZmod_nameÚnewr   r   r   Ú	transform3   s,    

zFixImport.transformc             C   sv   |  d¡rdS | dd¡d }t| jƒ}t||ƒ}ttt|ƒdƒƒsHdS x(dtddd	d
gD ]}t|| ƒrZdS qZW dS )Nr$   Fé   r   z__init__.pyz.pyz.pycz.soz.slz.pydT)Ú
startswithÚsplitr   Úfilenamer   r   r   )r    Zimp_nameÚ	base_pathZextr   r   r   r&   U   s    


z!FixImport.probably_a_local_import)	Ú__name__Ú
__module__Ú__qualname__ZBM_compatibleZPATTERNr   r*   r&   Ú__classcell__r   r   )r"   r   r   &   s
   "r   N)Ú__doc__r   r   Zos.pathr   r   r   r   Z
fixer_utilr   r	   r
   r   ZBaseFixr   r   r   r   r   Ú<module>   s
   