a
    lcm6                     @   s
  d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ dZe
d	  Zd
d Zdd Zdd Zd1ddZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z d'd( Z!d)d* Z"d+d, Z#d-d. Z$d/d0 Z%dS )2    )dedent)split_lines)debug)RefactoringError)RefactoringEXPRESSION_PARTS)indent_block)function_is_classmethodfunction_is_staticmethodZsuite
file_inputz_atom testlist_star_expr testlist test lambdef lambdef_nocond keyword name number string fstringc                 C   s\   t |||}td| t|\}}|s0t||d t| }	|t|||	|i}
t| |
S )NzExtracting nodes: %s = )_find_nodesr   Zdbg_is_expression_with_errorr   _expression_nodes_to_string_replacer   )inference_statepathmodule_nodenamepos	until_posnodesis_expressionmessageZgenerated_codefile_to_node_changes r   X/var/www/html/django/DPS/env/lib/python3.9/site-packages/jedi/api/refactoring/extract.pyextract_variable   s    r   c                 C   s:   t dd | D rdS | d jtvr6dd| d j fS dS )z8
    Returns a tuple (is_expression, error_string).
    c                 s   s    | ]}|j d ko| V  qdS )r   N)typeis_definition).0noder   r   r   	<genexpr>#       z,_is_expression_with_error.<locals>.<genexpr>)Fz,Cannot extract a name that defines somethingr   FzCannot extract a "%s")T )anyr   _VARIABLE_EXCTRACTABLEr   r   r   r   r      s
    r   c                 C   s*  | j |dd}|du r||jdkr>| }|dur>|j|kr>|}t|rL|j}|jjdkr`|jj}|jjtv rt|j}q`|g}n|j|kr| }|dur|}t|r|j}| j |dd}|j|kr| }|du rt	d|}|j|jk r|j}qt
|||}t|dkr&|jdv r&|d	 jd gS |S )
zd
    Looks up a module and tries to find the appropriate amount of nodes that
    are in there.
    T)Zinclude_prefixesNoperatortrailerz!Cannot extract anything from that   )return_stmtZ
yield_exprr   )Zget_leaf_for_positionr   get_next_leaf	start_pos_is_not_extractable_syntaxparentr   end_posZget_previous_leafr   !_remove_unwanted_expression_nodeslenchildren)r   r   r   Z
start_nodeZ	next_leafr   Zend_leafparent_noder   r   r   r   +   s>    


r   Nc                 C   s   t | d }|d u r| }| d  }t|jdd}||u rR|d urR||d d< t||d d g|dd< d|}	i }
||u r|	| |
| d < n>|d u r|j}n|t| d  }|| |
| d < |	|j |
|< | dd  D ]}d|
|< q|
S )Nr   Tkeepends
r%   r+   )_get_parent_definitionget_first_leafr   prefixr   join_get_indentationvalue)r   Zexpression_replacementZ	extractedr   insert_before_leafremaining_prefixZ
definitionZfirst_node_leaflinesZextracted_prefixreplacement_dctpr"   r   r   r   r   a   s*    

r   c                 C   s   d dd t| D S )Nr%   c                 s   s"   | ]\}}|j |d kdV  qdS )r   Zinclude_prefixNget_code)r!   inr   r   r   r#      r$   z._expression_nodes_to_string.<locals>.<genexpr>)r=   	enumerater(   r   r   r   r      s    r   c                 C   sV   | d }t | |d d \}}||jdd ddd | dd  D  }||fS )Nr   r+   FrE   r%   c                 s   s   | ]}|  V  qd S )NrF   )r!   rI   r   r   r   r#      r$   z)_suite_nodes_to_string.<locals>.<genexpr>)_split_prefix_atr;   rG   r=   )r   r   rI   r<   Zpart_of_codecoder   r   r   _suite_nodes_to_string   s    rM   c                 C   sD   | j d | }t| jdd}d|d|  d|| d fS )zU
    Returns a tuple of the leaf's prefix, split at the until_line
    position.
    r   Tr6   r%   N)r.   r   r<   r=   )leafZ
until_lineZsecond_line_countrB   r   r   r   rK      s    rK   c                 C   s   t |  jd S )Nr8   )r   r;   r<   r"   r   r   r   r>      s    r>   c                 C   s,   | dur | j jtv r| S | j } q tddS )z8
    Returns the statement where a node is defined.
    NzWe should never even get here)r0   r   _DEFINITION_SCOPESNotImplementedErrorrO   r   r   r   r:      s
    r:   c                 C   s  | j }|dv }|tv s|r| j}t|D ],\}}|j|kr*|}|j dkrR|d8 } qXq*ttt|D ]T\}}|j|k rh|}	|j dkr|	d7 }	||d D ]}
t|
r|	d7 }	q qq qqh|||	d  }|st	|d |||dd< t	|d |||dd< |S | gS )z~
    This function makes it so for `1 * 2 + 3` you can extract `2 + 3`, even
    though it is not part of the expression.
    r   r)   r+   Nr   r8   )
r   r   r4   rJ   r1   reversedlistr.   r/   r2   )r5   r   r   typZis_suite_partr   rH   rI   start_indexZ	end_indexZn2r   r   r   r2      s4    




r2   c                 C   s   | j dkp| j dko| jdvS )Nr)   keyword)NoneTrueFalse)r   r?   rO   r   r   r   r/      s    
r/   c                    sj  t |j||}t|sJ t|\}}||d }	|	 }
tt||	|\}}|	 r^d }nt	|	j|
}|
 }|rdt| d }d }d}nt|d }|s|rtt|	|d j|d j|p|d gng }t||\}}|d  }t||d \}}||7 }t|}|s,d|}|d| d 7 }t|r@|d d n| d}d  |
rt|	js|	  }t|r|d j  fdd	|D }t|	jrd
}n|d7 }d||d d u r|n g| t|f }d d u rdn d | d|f }|r|}n$|r"d| d }n|d | d }t||||||}|sX||j ||< ||i}t| |S )Nr   zreturn r9   Fr8   z, r%   c                    s   g | ]}| kr|qS r   r   )r!   rD   Z
self_paramr   r   
<listcomp>  r$   z$extract_function.<locals>.<listcomp>z@classmethod
z%sdef %s(%s):
%sz%s(%s).r   )r   Z	tree_noder3   r   Zcreate_contextis_bound_methodrS   _find_inputs_and_outputs	is_module_get_code_insertion_noder;   r   _is_node_ending_return_stmt_find_needed_output_variablesr0   r1   rM   r-   rK   r   r=   _check_for_non_extractablesr
   	get_valueZget_param_namesZstring_namer	   r   r   r?   r   )r   r   module_contextr   r   r   r   r   _contextr]   paramsreturn_variablesr@   r"   Z
code_blockrA   Zhas_ending_return_stmtZ
after_leaffirstsecondZoutput_var_str	decoratorZfunction_param_namesZfunction_codeZfunction_callreplacementrC   r   r   rZ   r   extract_function   s    	



rn   c              	   C   sX   | D ]N}z
|j }W n6 tyH   |jdkr2td|jdkrDtdY q0 t| qd S )Nreturnz:Can only extract return statements if they are at the end.yieldz Cannot extract yield statements.)r4   AttributeErrorr?   r   rc   )r   rI   r4   r   r   r   rc   (  s    


rc   c                 C   s\   |D ]R}|j dks|j s| | ur. dS |jd u sP||j  krN|k sn  dS qdS )NparamTF)Zapi_typeZparent_contextr_   Zget_root_contextr.   )re   namesrj   lastr   r   r   r   _is_name_input6  s    "ru   c           	      C   s   |d j }|d j}g }g }t|D ]X}| rF||vr|||j q$|j|vr$|||j }|rpt| |||r$||j q$||fS )Nr   r8   )r.   r1   _find_non_global_namesr    appendr?   Zgotoru   )	re   rg   r   rj   rt   inputsoutputsr   Zname_definitionsr   r   r   r^   @  s    


r^   c              	   c   sd   | D ]Z}z
|j }W n" ty4   |jdkr0|V  Y q0 |jdkrP|j d dkrPqt|E d H  qd S )Nr   r*   r   r\   )r4   rq   r   rv   )r   r"   r4   r   r   r   rv   U  s    

rv   c                 C   s8   |rt | r | jjdkr | j} q| jjdv r4| j} q | S )Nr   )Zasync_funcdefZ	decoratedZ
async_stmt)r
   r0   r   )r"   r]   r   r   r   r`   d  s    r`   c                 c   sZ   |j D ]N}|j|k rqt|}t|gD ]*}| s(|j|v r(||j |jV  q(qdS )z
    Searches everything after at_least_pos in a node and checks if any of the
    return_variables are used in there and returns those.
    N)r4   r.   setrv   r    r?   remove)rg   Zsearch_nodeZat_least_posri   r"   r   r   r   r   rb   n  s    

rb   c                 C   s$   | j }|dkrt| jd S |dkS )NZsimple_stmtr   r,   )r   ra   r4   )r"   tr   r   r   ra   ~  s    ra   )NN)&textwrapr   Zparsor   Zjedir   Zjedi.api.exceptionsr   Zjedi.api.refactoringr   r   Zjedi.commonr   Zjedi.parser_utilsr	   r
   rP   splitr'   r   r   r   r   r   rM   rK   r>   r:   r2   r/   rn   rc   ru   r^   rv   r`   rb   ra   r   r   r   r   <module>   s<   7 
!$W

