B
    v9ag;              
   @   s  d dl mZ d dlmZmZ d dlmZ d dlm	Z	 dd Z
dd Zd	d
 Zdd ZdVddZdd Zdd Zdd Ze e fddZdWddZdd Zdd ZdXddZd d! ZdYd"d#ZdZd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1d2d3d4d5d6d7d8d9h
Zd:d; Z d<a!d=a"d>a#d?a$d@dA Z%dBdC Z&dDdE Z'dFdG Z(dHdI Z)dJdK Z*dLdM Z+dNdO Z,ej-ej.hZ/d[dPdQZ0ej.ej-ej1hZ2dRdS Z3d\dTdUZ4dS )]   )token)LeafNode)python_symbols)patcompc             C   s   t tj| ttjd|gS )N=)r   symsargumentr   r   EQUAL)keywordvalue r    /usr/lib/python3.7/fixer_util.py
KeywordArg   s    r   c               C   s   t tjdS )N()r   r   LPARr   r   r   r   LParen   s    r   c               C   s   t tjdS )N))r   r   RPARr   r   r   r   RParen   s    r   c             C   sH   t | ts| g} t |ts&d|_|g}ttj| ttjdddg | S )N r   )prefix)	
isinstancelistr   r   r   atomr   r   r
   )targetsourcer   r   r   Assign   s    

r   Nc             C   s   t tj| |dS )N)r   )r   r   NAME)namer   r   r   r   Name$   s    r    c             C   s   | t tjt |ggS )N)r   r   trailerDot)objattrr   r   r   Attr(   s    r%   c               C   s   t tjdS )N,)r   r   COMMAr   r   r   r   Comma,   s    r(   c               C   s   t tjdS )N.)r   r   DOTr   r   r   r   r"   0   s    r"   c             C   s4   t tj| | g}| r0|dt tj|  |S )Nr   )r   r   r!   cloneinsert_childarglist)argsZlparenZrparennoder   r   r   ArgList4   s    r0   c             C   s&   t tj| t|g}|d k	r"||_|S )N)r   r   powerr0   r   )Z	func_namer.   r   r/   r   r   r   Call;   s    r2   c               C   s   t tjdS )N
)r   r   NEWLINEr   r   r   r   NewlineB   s    r5   c               C   s   t tjdS )N )r   r   r4   r   r   r   r   	BlankLineF   s    r7   c             C   s   t tj| |dS )N)r   )r   r   NUMBER)nr   r   r   r   NumberJ   s    r:   c             C   s"   t tjttjd| ttjdgS )N[])r   r   r!   r   r   LBRACERBRACE)Z
index_noder   r   r   	SubscriptM   s    r?   c             C   s   t tj| |dS )N)r   )r   r   STRING)stringr   r   r   r   StringS   s    rB   c       	      C   s   d| _ d|_ d|_ ttjd}d|_ ttjd}d|_ ||||g}|rtd|_ ttjd}d|_ |ttj||g ttj| ttj	|g}ttj
ttjd|ttjdgS )Nr6   r   forinifr;   r<   )r   r   r   r   appendr   r   Zcomp_ifZ	listmakerZcomp_forr   r=   r>   )	ZxpfpitZtestZfor_leafZin_leafZ
inner_argsZif_leafinnerr   r   r   ListCompW   s$    
rJ   c             C   sZ   x|D ]}|   qW ttjdttj| ddttjdddttj|g}ttj|}|S )Nfromr   )r   import)remover   r   r   r   r   import_as_namesimport_from)Zpackage_nameZ
name_leafsZleafchildrenimpr   r   r   
FromImporto   s    

rR   c          	   C   s   |d   }|jtjkr"|  }nttj|  g}|d }|rNdd |D }ttjtt|d t|d ttj|d   ||d   gg | }| j	|_	|S )	Nr#   afterc             S   s   g | ]}|  qS r   )r+   ).0r9   r   r   r   
<listcomp>   s    z!ImportAndCall.<locals>.<listcomp>    r   ZlparZrpar)
r+   typer   r-   r   r1   r%   r    r!   r   )r/   resultsnamesr#   Z
newarglistrS   newr   r   r   ImportAndCall   s    
Dr[   c             C   s   t | tr | jt t gkr dS t | tot| jdkot | jd tot | jd tot | jd to| jd jdko| jd jdkS )NT   rV   r      r   r   )r   r   rP   r   r   lenr   r   )r/   r   r   r   is_tuple   s    
r_   c             C   sX   t | toVt| jdkoVt | jd toVt | jd toV| jd jdkoV| jd jdkS )Nr   rV   r;   r<   )r   r   r^   rP   r   r   )r/   r   r   r   is_list   s    
ra   c             C   s   t tjt | t gS )N)r   r   r   r   r   )r/   r   r   r   parenthesize   s    rb   sortedr   setanyalltuplesumminmax	enumeratec             c   s(   t | |}x|r"|V  t ||}qW d S )N)getattr)r#   r$   nextr   r   r   
attr_chain   s    
rn   zefor_stmt< 'for' any 'in' node=any ':' any* >
        | comp_for< 'for' any 'in' node=any any* >
     z
power<
    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' |
      'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) )
    trailer< '(' node=any ')' >
    any*
>
z`
power<
    ( 'sorted' | 'enumerate' )
    trailer< '(' arglist<node=any any*> ')' >
    any*
>
Fc             C   sr   t s&ttattattada tttg}x<t|t| dD ](\}}i }|||rB|d | krBdS qBW dS )NTparentr/   F)	
pats_builtr   Zcompile_patternp0p1p2ziprn   match)r/   Zpatternspatternro   rX   r   r   r   in_special_context   s    



rw   c             C   s   | j }|d k	r|jtjkrdS | j}|jtjtjfkr:dS |jtjkrX|j	d | krXdS |jtj
ks|jtjkr|d k	r|jtjks|j	d | krdS dS )NFrV   T)Zprev_siblingrW   r   r*   ro   r   funcdefclassdef	expr_stmtrP   Z
parametersZtypedargslistr'   )r/   prevro   r   r   r   is_probably_builtin   s    r|   c             C   sN   xH| d k	rH| j tjkr@t| jdkr@| jd }|j tjkr@|jS | j} qW dS )Nr]   r   r6   )	rW   r   suiter^   rP   r   INDENTr   ro   )r/   indentr   r   r   find_indentation  s    


r   c             C   s>   | j tjkr| S |  } | jd  }| _ttj| g}||_|S )N)rW   r   r}   r+   ro   r   )r/   ro   r}   r   r   r   
make_suite  s    r   c             C   s(   x"| j tjkr"| j} | stdqW | S )Nz,root found before file_input node was found.)rW   r   Z
file_inputro   
ValueError)r/   r   r   r   	find_root&  s
    r   c             C   s   t |t|| }t|S )N)find_bindingr   bool)packager   r/   Zbindingr   r   r   does_tree_import/  s    r   c             C   s   | j tjtjfkS )N)rW   r   import_namerO   )r/   r   r   r   	is_import7  s    r   c             C   s4  dd }t |}t| ||r d S d }}xTt|jD ]F\}}||sFq4x(t|j|d  D ]\}}||sZP qZW || }P q4W |dkrxDt|jD ]6\}}|jtjkr|jr|jd jtjkr|d }P qW | d krt	tj
ttjdttj|ddg}	nt| ttj|ddg}	|	t g}
||t	tj|
 d S )Nc             S   s    | j tjko| jot| jd S )NrV   )rW   r   simple_stmtrP   r   )r/   r   r   r   is_import_stmt>  s    z$touch_import.<locals>.is_import_stmtrV   r   rL   r   )r   )r   r   rk   rP   rW   r   r   r   r@   r   r   r   r   rR   r5   r,   )r   r   r/   r   rootZ
insert_posoffsetidxZnode2import_rP   r   r   r   touch_import;  s4    

r   c             C   s  x|j D ]}d }|jtjkrVt| |j d r4|S t| t|j d |}|rR|}n4|jtjtjfkrt| t|j d |}|r|}n |jtj	krt| t|j d |}|r|}nXxt
|j dd  D ]@\}}|jtjkr|jdkrt| t|j |d  |}|r|}qW nx|jtkr6|j d j| kr6|}nTt|| |rJ|}n@|jtjkrft| ||}n$|jtjkrt| |j d r|}|r
|s|S t|r
|S q
W d S )Nr   r`   r]   r\   :   rV   )rP   rW   r   Zfor_stmt_findr   r   Zif_stmtZ
while_stmtZtry_stmtrk   r   COLONr   	_def_syms_is_import_bindingr   rz   r   )r   r/   r   childretr9   iZkidr   r   r   r   i  sH       
r   c             C   sX   |g}xL|rR|  }|jdkr6|jtkr6||j q|jtjkr|j| kr|S qW d S )N   )poprW   _block_symsextendrP   r   r   r   )r   r/   Znodesr   r   r   r     s    r   c             C   s  | j tjkr|s| jd }|j tjkrtx|jD ]@}|j tjkrT|jd j|krn| S q.|j tjkr.|j|kr.| S q.W nL|j tjkr|jd }|j tjkr|j|kr| S n|j tjkr|j|kr| S n| j tj	kr|rt
| jd  |krd S | jd }|rtd|rd S |j tjkr,t||r,| S |j tjkrd|jd }|j tjkr|j|kr| S n6|j tjkr|j|kr| S |r|j tjkr| S d S )Nr   r]   r`   r\   as)rW   r   r   rP   Zdotted_as_namesZdotted_as_namer   r   r   rO   strstripr   rN   Zimport_as_nameSTAR)r/   r   r   rQ   r   Zlastr9   r   r   r   r     s@    




r   )N)NN)N)N)N)N)N)5Zpgen2r   Zpytreer   r   Zpygramr   r   r6   r   r   r   r   r   r    r%   r(   r"   r0   r2   r5   r7   r:   r?   rB   rJ   rR   r[   r_   ra   rb   Zconsuming_callsrn   rq   rr   rs   rp   rw   r|   r   r   r   r   r   r   ry   rx   r   r   r!   r   r   r   r   r   r   r   <module>   sX   





		-
*
