o
    ?Hh                     @   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 }z
t | }t |}W n ty   t| }t|}Y nw | dkrWt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rwt|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    -)unicode	NameErrorstrr   r   powerr   trailerr   r
   LSQB	subscriptCOLONr	   RSQBappendPLUSfactorMINUS
arith_expr)num_prenum_postLISTNAMEITERNAMEchildrenprepostpre_part	main_part	post_partsource r4   a/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/libpasteurize/fixes/fix_unpacking.pyassignment_source   s(   J

b
r6   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       | ]}  |V  qd S Nget.0nresultsr4   r5   	<genexpr>7       z4FixUnpacking.fix_explicit_context.<locals>.<genexpr>)r.   namer/   r3   c                 S       g | ]}|j tjkr| qS r4   typer
   NAMEcloner<   r4   r4   r5   
<listcomp>8        z5FixUnpacking.fix_explicit_context.<locals>.<listcomp>r   c                 S   rD   r4   rE   r<   r4   r4   r5   rI   :   rJ   c                 S      g | ]}|  qS r4   rH   r<   r4   r4   r5   rI   ;       r   list)r   r   rH   r$   r   r   r   r+   r   r6   lenr,   )
selfnoder@   r.   rC   r/   r3   target
setup_line
power_liner4   r?   r5   fix_explicit_context6   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   r8   r9   r:   r<   r?   r4   r5   rA   I   rB   z4FixUnpacking.fix_implicit_context.<locals>.<genexpr>)r.   rC   r/   itc                 S   rD   r4   rE   r<   r4   r4   r5   rI   J   rJ   z5FixUnpacking.fix_implicit_context.<locals>.<listcomp>r   c                 S   rD   r4   rE   r<   r4   r4   r5   rI   L   rJ   c                 S   rK   r4   rL   r<   r4   r4   r5   rI   M   rM   r   rN   )r   r   rH   r$   r   r   r   r+   r   r,   r6   rO   )rP   rQ   r@   r.   rC   r/   rV   rR   r3   rS   rT   r4   r?   r5   fix_implicit_contextD   s    " z!FixUnpacking.fix_implicit_contextc           
      C   sD  |  d| _|  d| _|d|d}}|durH| ||\}}|j|_t|j|_|t	  |j}|
 }||| ||| dS |du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 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
        	_3to2list	_3to2iterexplimplNc                 S   s   g | ]
}|j tjkr|qS r4   )rF   r   suite)r=   kr4   r4   r5   rI   t   s    z*FixUnpacking.transform.<locals>.<listcomp>r   r   r      lstr   r   )new_namer+   r,   r;   rU   r   r   parentappend_childr   removeinsert_childrW   r   r-   valuereplacer   )
rP   rQ   r@   rZ   r[   rS   rT   ra   ir\   r4   r4   r5   	transformW   s2   zFixUnpacking.transformN)__name__
__module____qualname__PATTERNrU   rW   rh   r4   r4   r4   r5   r7   *   s
    
r7   N)__doc__lib2to3r   	itertoolsr   lib2to3.fixer_utilr   r   r   r   r   r	   r
   r   r   r   libfuturize.fixer_utilr   r   r   r6   BaseFixr7   r4   r4   r4   r5   <module>   s    0