B
    b*                 @   sB  d dl Z d dlZ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 d dlmZ dd	d
ddddddddddddddddhZdZdFddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdGd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdHd6d7ZeeZd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"e"dBZ#e"dCZ$e"dDdEZ%dS )I    N)literal_eval)cleandoc)WeakKeyDictionary)tree)parser_cache)split_linesfuncdefclassdefZimport_fromZimport_nameZtestZor_testZand_testZnot_testZ
comparisonexprZxor_exprZand_exprZ
shift_exprZ
arith_expr	atom_exprZtermZfactorZpoweratom)	tryexceptfinallyelseifelifwithforwhileFc             C   s  g }| j }|dkrB|  }|dkr@| jj dkr@|dkr@||  n|dkrv||  x| jD ]}|t|dd7 }q\W n|dkr| jd	 d
kr| jd } | dkr|t| 7 }nTy
| j}W n tk
r   Y n6X | j tkr|s||  x|D ]}|t||7 }qW |S )z
    For static analysis.
    nameFparam=	expr_stmtT)
last_added	decorator)()typeget_next_leafparentappendchildrenget_executable_nodesAttributeError_EXECUTE_NODES)noder   resulttypZ	next_leafchildr$    r,   0lib/python3.7/site-packages/jedi/parser_utils.pyr%      s0    




r%   c             c   sZ   | V  | j d }xD|jdkr*|j d V  n|jdkr<|V  n|jdksHP |j d }qW d S )Ncomp_for   sync_comp_forZcomp_if)r$   r    )r/   Zlastr,   r,   r-   get_sync_comp_fors:   s    



r2   c             C   s   | j d jdkS )z
    Returns True if only one name is returned: ``for x in y``.
    Returns False if the for loop is more complicated: ``for x, z in y``.

    :returns: bool
    r0   r   )r$   r    )for_stmtr,   r,   r-   for_stmt_defines_one_nameG   s    r4   c             C   sj   |j }| j |  k r| jks(n tdd }x8t| jD ]*\}}||j k rN|S | }|tkr8|}q8W d S )Nz!The node is not part of the flow.)	start_posend_pos
ValueError	enumerater$   get_first_leaf_FLOW_KEYWORDS)Z	flow_noder(   r5   keywordir+   Z
first_leafr,   r,   r-   get_flow_branch_keywordQ   s    
r=   c             C   s"   |   }|dk	rtt|jS dS )z3 Returns a cleaned version of the docstring token. N )Zget_doc_noder   safe_literal_evalvalue)Z
scope_noder(   r,   r,   r-   clean_scope_docstring`   s    rA   c             C   sP   | j dkrL| j} |  }|d k	rL|j dkrL|jd }|j dkrLtt|jS dS )Nr   Zsimple_stmtr   stringr>   )r    r"   Zget_next_siblingr$   r   r?   r@   )Z	tree_nodeZmaybe_stringr,   r,   r-   find_statement_documentationl   s    



rC   c             C   s0   | d d   }|d dks$|dkr(dS t| S )N   r   f)frZrfr>   )lowerr   )r@   Z	first_twor,   r,   r-   r?   x   s    r?   H   c       	      C   s   |dkr | j dkrd}n| jj}|  }|r8|dd }dddd |D   d	 }td
d|}| jr|sd| j	  }nd}|| | }dt
||S )z
    Generate a string signature of a function.

    :param width: Fold lines if a line is longer than this value.
    :type width: int
    :arg func_name: Override function name when given.
    :type func_name: str

    :rtype: str
    Nlambdefz<lambda>r0   r   r>   c             s   s   | ]}|  V  qd S )N)get_code).0r   r,   r,   r-   	<genexpr>   s    z get_signature.<locals>.<genexpr>r   z\s+ z ->
)r    r   r@   Z
get_paramsjoinstripresubZ
annotationrJ   textwrapwrap)	r   widthZcall_stringZomit_first_paramZomit_return_annotationparamspZrtypecoder,   r,   r-   get_signature   s    
 
rY   c             C   sJ   y
| j }W n" tk
r,   |  j|7  _Y nX x|D ]}t|| q4W dS )z$
    Move the `Node` start_pos.
    N)r$   r&   linemove)r(   Zline_offsetr$   cr,   r,   r-   r[      s    

r[   c             C   s   yj| j dkr| jd  j}nJ| j dkr:| jd  j}n.| j dkrZ| jd   j}n|   j}W n& tk
r~   dS  tk
r   dS X d|krdS ||dd }d	|kr|d|d	 }d
|kr|d|d
 }|S )zl
    returns (as string) any comment that appears on the same line,
    after the node, including the #
    r3      Z	with_stmt   r      N#rN   )	r    r$   r9   prefixr!   Zget_last_leafr&   r7   index)r(   Z
whitespacecommentr,   r,   r-   get_following_comment_same_line   s(    


re   c             C   s&   | j }|dkr| jd j dkS |dkS )Nr/   r0   r1   )Z
file_inputr	   r   rI   r1   )r    r$   )r(   tr,   r,   r-   is_scope   s    rg   c                s   t   d fdd	}|S )NFc                st   | d kr||S y |  }W n  t k
r>   i  } | < Y nX y|| S  t k
rn   || }||< |S X d S )N)KeyError)Zparso_cache_noder(   include_flowsZ
for_moduler)   )cachefuncr,   r-   wrapper   s    
z(_get_parent_scope_cache.<locals>.wrapper)F)r   )rk   rl   r,   )rj   rk   r-   _get_parent_scope_cache   s    rm   c                s    j }|dkrdS xt|r|jdkr|jd}|j| j jkr j jdkr^ j j kr^n& j jdkr| j jd  kr|n|j }q|S |rt|tj	r|jdkrt
 fdd	| D s|S |j }qW dS )
z'
    Returns the underlying scope.
    N)r	   r   rI   :r   Ztfpdefr   Zif_stmtc             3   s,   | ]$}|j  j   ko|jk n  V  qd S )N)r5   r6   )rK   n)r(   r,   r-   rL     s   z#get_parent_scope.<locals>.<genexpr>)r"   rg   r    r$   rc   r5   r   
isinstancer   ZFlowanyZget_test_nodes)r(   ri   Zscoperc   r,   )r(   r-   get_parent_scope   s*    

rr   c             C   s   t | |jS )z
    Basically access the cached code lines in parso. This is not the nicest way
    to do this, but we avoid splitting all the lines again.
    )get_parso_cache_nodelines)grammarpathr,   r,   r-   get_cached_code_lines  s    rw   c             C   s   t | j | S )z
    This is of course not public. But as long as I control parso, this
    shouldn't be a problem. ~ Dave

    The reason for this is mostly caching. This is obviously also a sign of a
    broken caching architecture.
    )r   Z_hashed)ru   rv   r,   r,   r-   rs     s    rs   c             C   sj   t | jddd|d | j d  }|d }| j|d krD|| j8 }|sLdS |d d| |d< d|S )z3
    Cuts of the value of the leaf at position
    T)keependsNr   r0   r>   r.   )r   r@   rZ   columnrO   )ZleafZpositionrt   ry   r,   r,   r-   cut_value_at_position"  s    $
rz   c             C   s   | j dkr8t| jdkr4| jd dkr4t| jd S dS | j dkr| j}|d dkrXdS t|d shdS td	d
 |dd D S | j dkS )zP
    Checks if a path looks like `name` or `name.foo.bar` and not `name()`.
    r   r^   r   r   r0   Fr   awaitc             s   s   | ]}|j d  dkV  qdS )r   .N)r$   )rK   r\   r,   r,   r-   rL   ?  s    z!expr_is_dotted.<locals>.<genexpr>Nr   )r    lenr$   expr_is_dottedall)r(   r$   r,   r,   r-   r~   0  s    

r~   c                 s    fdd}|S )Nc                s0   x*|   D ]}|jd }|  kr
dS q
W dS )z
        This is a heuristic. It will not hold ALL the times, but it will be
        correct pretty much for anyone that doesn't try to beat it.
        staticmethod/classmethod are builtins and unless overwritten, this will
        be correct.
        r0   TF)Zget_decoratorsr$   rJ   )Zfunction_noder   Zdotted_name)method_namesr,   r-   rl   D  s
    
z&_function_is_x_method.<locals>.wrapperr,   )r   rl   r,   )r   r-   _function_is_x_methodC  s    r   staticmethodclassmethodpropertyZcached_property)F)rH   NFF)F)&rQ   rS   Zastr   inspectr   weakrefr   Zparso.pythonr   Zparso.cacher   Zparsor   r'   r:   r%   r2   r4   r=   rA   rC   r?   rY   r[   re   rg   rm   rr   Zget_cached_parent_scoperw   rs   rz   r~   r   Zfunction_is_staticmethodZfunction_is_classmethodZfunction_is_propertyr,   r,   r,   r-   <module>   sF   


%

 
 	
 