B
    	Q[:                 @   s~   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ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 )zD
Fixer for:
(a,)* *b (,c)* [,] = s
for (a,)* *b (,c)* [,] in d: ...
    )
fixer_base)count)
AssignCommaCallNewlineNameNumbertokensymsNodeLeaf)indentationsuitify	commatizec             C   s  g }t | }t |}| dkrttjt|ttjttjdttj	ttj
dt|gttjdgg}|| |ttjddd ttjttjdddt|ttjttjdttj	| dkrt|ntdd	ttj
d|dkrttjttjd
t|gntdd	gttjdttjdgg}|| |dkr|ttjddd ttjt|ddttjttjdttj	ttjttjd
t|gttj
dgttjdgg}	||	 ttj|}
|
S )z
    Accepts num_pre and num_post, which are counts of values
    before and after the starg (not including the starg)
    Returns a source fit for Assign() from fixer_util
    r   [:]+ )prefix    -)Zunicoder   r   Zpowerr   Ztrailerr   r
   LSQBZ	subscriptCOLONr	   RSQBappendPLUSZfactorMINUSZ
arith_expr)Znum_preZnum_postLISTNAMEITERNAMEchildrenprepostZpre_partZ	main_partZ	post_partsource r&   @lib/python3.7/site-packages/libpasteurize/fixes/fix_unpacking.pyassignment_source   s    J


b
r(   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	FixUnpackinga9  
    expl=expr_stmt< testlist_star_expr<
        pre=(any ',')*
            star_expr< '*' name=NAME >
        post=(',' any)* [','] > '=' source=any > |
    impl=for_stmt< 'for' lst=exprlist<
        pre=(any ',')*
            star_expr< '*' name=NAME >
        post=(',' any)* [','] > 'in' it=any ':' suite=any>c       
         s    fdddD \}}}}dd |D }d|_ dd |D }dd t|| g | D }|t  d	|_ tt| jttd
| g}t|t	t
|t
|| j| j}	||	fS )Nc             3   s   | ]}  |V  qd S )N)get).0n)resultsr&   r'   	<genexpr>3   s    z4FixUnpacking.fix_explicit_context.<locals>.<genexpr>)r#   namer$   r%   c             S   s    g | ]}|j tjkr| qS r&   )typer
   NAMEclone)r+   r,   r&   r&   r'   
<listcomp>4   s    z5FixUnpacking.fix_explicit_context.<locals>.<listcomp>r   c             S   s    g | ]}|j tjkr| qS r&   )r0   r
   r1   r2   )r+   r,   r&   r&   r'   r3   6   s    c             S   s   g | ]}|  qS r&   )r2   )r+   r,   r&   r&   r'   r3   7   s    r   list)r   r   r2   r   r   r   r   r    r   r(   lenr!   )
selfnoder-   r#   r/   r$   r%   target
setup_line
power_liner&   )r-   r'   fix_explicit_context2   s       z!FixUnpacking.fix_explicit_contextc                s    fdddD \}}}}dd |D }d|_ dd |D }dd t|| g | D }|t  | }d	|_ tt| jttd
t| j	g}	t|t
t|t|| j| j	}
|	|
fS )z_
        Only example of the implicit context is
        a for loop, so only fix that.
        c             3   s   | ]}  |V  qd S )N)r*   )r+   r,   )r-   r&   r'   r.   E   s    z4FixUnpacking.fix_implicit_context.<locals>.<genexpr>)r#   r/   r$   itc             S   s    g | ]}|j tjkr| qS r&   )r0   r
   r1   r2   )r+   r,   r&   r&   r'   r3   F   s    z5FixUnpacking.fix_implicit_context.<locals>.<listcomp>r   c             S   s    g | ]}|j tjkr| qS r&   )r0   r
   r1   r2   )r+   r,   r&   r&   r'   r3   H   s    c             S   s   g | ]}|  qS r&   )r2   )r+   r,   r&   r&   r'   r3   I   s    r   r4   )r   r   r2   r   r   r   r   r    r   r!   r(   r5   )r6   r7   r-   r#   r/   r$   r<   r8   r%   r9   r:   r&   )r-   r'   fix_implicit_context@   s     " z!FixUnpacking.fix_implicit_contextc       
      C   s@  |  d| _|  d| _|d|d }}|dk	r| ||\}}|j|_t|j|_|t	  |j}|
 }||| ||| n|dk	r<| ||\}}t| dd |jD d }	d	|_|	jd
 j|_t|	jd |	jd _|	dt	  |	d| |	dt	  |	d| |dt| jdd dS )a  
        a,b,c,d,e,f,*g,h,i = range(100) changes to
        _3to2list = list(range(100))
        a,b,c,d,e,f,g,h,i, = _3to2list[:6] + [_3to2list[6:-2]] + _3to2list[-2:]

        and

        for a,b,*c,d,e in iter_of_iters: do_stuff changes to
        for _3to2iter in iter_of_iters:
            _3to2list = list(_3to2iter)
            a,b,c,d,e, = _3to2list[:2] + [_3to2list[2:-2]] + _3to2list[-2:]
            do_stuff
        Z	_3to2listZ	_3to2iterexplimplNc             S   s   g | ]}|j tjkr|qS r&   )r0   r   suite)r+   kr&   r&   r'   r3   p   s    z*FixUnpacking.transform.<locals>.<listcomp>r   r   r      Zlstr   )r   )new_namer    r!   r*   r;   r   r   parentZappend_childr   removeZinsert_childr=   r   r"   valuereplacer   )
r6   r7   r-   r>   r?   r9   r:   rD   ir@   r&   r&   r'   	transformS   s0    
zFixUnpacking.transformN)__name__
__module____qualname__ZPATTERNr;   r=   rI   r&   r&   r&   r'   r)   &   s   
r)   N)__doc__Zlib2to3r   	itertoolsr   Zlib2to3.fixer_utilr   r   r   r   r   r	   r
   r   r   r   Zlibfuturize.fixer_utilr   r   r   r(   ZBaseFixr)   r&   r&   r&   r'   <module>   s   0