B
    ¢èåS&  ã               @   s   d dl Zd dlZd dlZd dlmZmZmZ G dd„ deƒZ	G dd„ de	ƒZ
G dd„ de	ƒZG d	d
„ d
e	ƒZG dd„ deƒZG dd„ de	ƒZdS )é    N)ÚastÚ
exceptionsÚutilsc               @   s¸   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	j
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e	j
dd „ ƒZeeƒZeeƒZeeƒZd!d"„ Zd#S )$ÚScopec             C   s   || _ || _|| _d S )N)ÚpycoreÚpyobjectÚparent)Úselfr   r   Úparent_scope© r   ú1lib/python3.7/site-packages/rope/base/pyscopes.pyÚ__init__	   s    zScope.__init__c             C   s
   | j  ¡ S )z2Return the names defined or imported in this scope)r   Úget_attributes)r	   r   r   r   Ú	get_names   s    zScope.get_namesc             C   s
   | j  ¡ S )z&Return the names defined in this scope)r   Z_get_structural_attributes)r	   r   r   r   Úget_defined_names   s    zScope.get_defined_namesc             C   s&   ||   ¡ krt d| ¡‚|   ¡ | S )z*Return name `PyName` defined in this scopezname %s not found)r   r   ÚNameNotFoundError)r	   Únamer   r   r   Úget_name   s    zScope.get_namec             C   s
   |   |¡S )zThe same as ``get_name(key)``)r   )r	   Úkeyr   r   r   Ú__getitem__   s    zScope.__getitem__c             C   s   ||   ¡ kS )z'The same as ``key in self.get_names()``)r   )r	   r   r   r   r   Ú__contains__    s    zScope.__contains__c             C   s   |   ¡ S )zsReturn the subscopes of this scope

        The returned scopes should be sorted by the order they appear.
        )Ú_create_scopes)r	   r   r   r   Ú
get_scopes$   s    zScope.get_scopesc             C   s2   ||   ¡ kr|   ¡ | S | jd k	r.| j |¡S d S )N)r   r   Ú_propagated_lookup)r	   r   r   r   r   Úlookup,   s
    
zScope.lookupc             C   s   |   ¡ S )a#  Return the visible names of this scope

        Return the names defined in this scope that are visible from
        scopes containing this scope.  This method returns the same
        dictionary returned by `get_names()` except for `ClassScope`
        which returns an empty dict.
        )r   )r	   r   r   r   Úget_propagated_names3   s    zScope.get_propagated_namesc             C   s2   ||   ¡ kr|   ¡ | S | jd k	r.| j |¡S d S )N)r   r   r   )r	   r   r   r   r   r   =   s
    
zScope._propagated_lookupc             C   s   dd„ | j  ¡ D ƒS )Nc             S   s   g | ]}|  ¡ ‘qS r   )Ú	get_scope)Ú.0Ú	pydefinedr   r   r   ú
<listcomp>E   s   z(Scope._create_scopes.<locals>.<listcomp>)r   Z_get_defined_objects)r	   r   r   r   r   D   s    zScope._create_scopesc             C   s   | }x|j d k	r|j }qW |S )N)r   )r	   Zcurrentr   r   r   Ú_get_global_scopeH   s    
zScope._get_global_scopec             C   s   | j  ¡ jS )N)r   Úget_astÚlineno)r	   r   r   r   Ú	get_startN   s    zScope.get_startc             C   s"   | j  ¡ j}|r|d jS |  ¡ S )Nr   )r   r!   Úbodyr"   r#   )r	   r$   r   r   r   Úget_body_startQ   s    
zScope.get_body_startc             C   s   |   ¡ j}|j | j¡d S )Né   )r    r   Úlogical_linesÚlogical_line_inÚlogical_end)r	   Úpymoduler   r   r   Úget_endW   s    
zScope.get_endc             C   s   |   ¡ }|j | ¡S )N)r    Ú_scope_finderÚfind_scope_end)r	   Zglobal_scoper   r   r   Úget_logical_end[   s    zScope.get_logical_endc             C   s   d S )Nr   )r	   r   r   r   Úget_kindd   s    zScope.get_kindN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   Úsaveitr   r   r   r   r   r    r#   r%   r+   r.   ÚpropertyÚstartÚendr)   r/   r   r   r   r   r      s(   
r   c                   sp   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡ fd	d
„Zddd„Zdd„ Z	e
ejdd„ ƒƒZe
dd„ ƒZ‡  ZS )ÚGlobalScopec                s"   t t| ƒ ||d ¡ | ¡ | _d S )N)Úsuperr7   r   Z_get_concluded_dataÚnames)r	   r   Úmodule)Ú	__class__r   r   r   j   s    zGlobalScope.__init__c             C   s   dS )Nr&   r   )r	   r   r   r   r#   n   s    zGlobalScope.get_startc             C   s   dS )NÚModuler   )r	   r   r   r   r/   q   s    zGlobalScope.get_kindc             C   sH   y
| j | S  tjk
rB   || jkr0| j| S t d| ¡‚Y nX d S )Nzname %s not found)r   r   ZAttributeNotFoundErrorÚbuiltin_namesr   )r	   r   r   r   r   r   t   s    


zGlobalScope.get_namec                sB   | j  ¡ d kr8t| jƒ}| tt| ƒ ¡ ¡ | j  |¡ | j  ¡ S )N)	r9   ÚgetÚdictr=   Úupdater8   r7   r   Úset)r	   Úresult)r;   r   r   r   |   s
    
zGlobalScope.get_namesNc             C   s   | j  | ||¡S )N)r,   Úget_holding_scope)r	   r"   Úindentsr   r   r   Úget_inner_scope_for_lineƒ   s    z$GlobalScope.get_inner_scope_for_linec             C   s   | j  | |¡S )N)r,   Úget_holding_scope_for_offset)r	   Úoffsetr   r   r   Úget_inner_scope_for_offset†   s    z&GlobalScope.get_inner_scope_for_offsetc             C   s
   t | jƒS )N)Ú_HoldingScopeFinderr   )r	   r   r   r   r,   ‰   s    zGlobalScope._scope_finderc             C   s   t jjj ¡ S )N)ÚropeÚbaseÚbuiltinsr   )r	   r   r   r   r=   Ž   s    zGlobalScope.builtin_names)N)r0   r1   r2   r   r#   r/   r   r   rE   rH   r4   r   r3   r,   r=   Ú__classcell__r   r   )r;   r   r7   h   s   
r7   c                   s\   e Zd Z‡ f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‡  ZS )ÚFunctionScopec                s<   t t| ƒ |||j ¡ ¡ d | _d | _d | _d | _|| _	d S )N)
r8   rN   r   r   r   r9   Úreturned_astsÚis_generatorÚdefinedsÚvisitor)r	   r   r   rR   )r;   r   r   r   •   s    zFunctionScope.__init__c             C   s   | j d kr|  ¡  | j S )N)r9   Ú_visit_function)r	   r   r   r   Ú
_get_namesž   s    
zFunctionScope._get_namesc             C   sv   | j d krr|  | j| j¡}x$t | j ¡ ¡D ]}t ||¡ q,W |j | _ | j  | j 	¡ ¡ |j
| _
|j| _|j| _d S )N)r9   rR   r   r   r   Zget_child_nodesr!   Úwalkr@   Zget_parametersrO   Ú	generatorrP   rQ   )r	   Znew_visitorÚnr   r   r   rS   £   s    
zFunctionScope._visit_functionc             C   s   | j d kr|  ¡  | jS )N)r9   rS   rO   )r	   r   r   r   Ú_get_returned_asts®   s    
z FunctionScope._get_returned_astsc             C   s   | j d kr|  ¡  | j S )N)rP   rX   )r	   r   r   r   Ú_is_generator³   s    
zFunctionScope._is_generatorc             C   s   |   ¡ S )N)rT   )r	   r   r   r   r   ¸   s    zFunctionScope.get_namesc             C   s"   | j d kr|  ¡  dd„ | j D ƒS )Nc             S   s   g | ]}|  ¡ ‘qS r   )r   )r   r   r   r   r   r   ¾   s    z0FunctionScope._create_scopes.<locals>.<listcomp>)rQ   rS   )r	   r   r   r   r   »   s    
zFunctionScope._create_scopesc             C   s   dS )NZFunctionr   )r	   r   r   r   r/   À   s    zFunctionScope.get_kindc             C   s<   x6|   ¡  ¡ D ]&}t|tjjjtjjjfƒr| ¡  qW d S )N)	r   ÚvaluesÚ
isinstancerJ   rK   ZpynamesZAssignedNameZEvaluatedNameZ
invalidate)r	   Zpynamer   r   r   Úinvalidate_dataÃ   s    zFunctionScope.invalidate_data)r0   r1   r2   r   rT   rS   rX   rY   r   r   r/   r\   rM   r   r   )r;   r   rN   “   s   	rN   c                   s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú
ClassScopec                s   t t| ƒ |||j ¡ ¡ d S )N)r8   r]   r   r   r   )r	   r   r   )r;   r   r   r   Ì   s    zClassScope.__init__c             C   s   dS )NZClassr   )r	   r   r   r   r/   Ð   s    zClassScope.get_kindc             C   s   i S )Nr   )r	   r   r   r   r   Ó   s    zClassScope.get_propagated_names)r0   r1   r2   r   r/   r   rM   r   r   )r;   r   r]   Ê   s   r]   c               @   sr   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
edd„ ƒZedd„ ƒZedd„ ƒZdS )rI   c             C   s
   || _ d S )N)r*   )r	   r*   r   r   r   r   Ù   s    z_HoldingScopeFinder.__init__c             C   s   t jj | j |¡¡S )N)rJ   rK   ZcodeanalyzeZcount_line_indentsÚlinesÚget_line)r	   r"   r   r   r   Úget_indentsÜ   s    z_HoldingScopeFinder.get_indentsc             C   s   |   | ¡ ¡S )N)r`   r#   )r	   Úscoper   r   r   Ú_get_scope_indentsà   s    z&_HoldingScopeFinder._get_scope_indentsNc             C   sž   |d kr|   |¡}|}|}x~|d k	r˜| ¡ dks>|  |¡|kr˜|}| ¡ |kr^| ¡ dkr^|S d }x2| ¡ D ]&}| ¡ |kr|| ¡ kr’|}P qlP qlW qW |S )Nr<   )r`   r/   rb   r#   r   r+   )r	   Zmodule_scoper"   Zline_indentsZcurrent_scopeZ	new_scopera   r   r   r   rC   ã   s&    


z%_HoldingScopeFinder.get_holding_scopec             C   s&   | j  |¡}| ¡ dkp$| ¡  d¡S )NÚ ú#)r^   r_   ÚstripÚlstripÚ
startswith)r	   r"   Úliner   r   r   Ú_is_empty_lineù   s    z"_HoldingScopeFinder._is_empty_linec             C   s   |   | ¡ ¡S )N)r`   r%   )r	   ra   r   r   r   Ú_get_body_indentsý   s    z%_HoldingScopeFinder._get_body_indentsc             C   s   |   || j |¡¡S )N)rC   r^   Zget_line_number)r	   ra   rG   r   r   r   rF      s    z0_HoldingScopeFinder.get_holding_scope_for_offsetc             C   s®   |j s| j ¡ S |j ¡ jd j}| jj 	|j
¡}|d |krN|  |¡d }n
|  |¡}xP| j t|d | j ¡ ƒ| j ¡ d ¡D ]$}|  |¡s‚|  |¡|k r¢|S |}q‚W |S )Néÿÿÿÿr&   é   )r   r^   Úlengthr   r!   r$   r"   r*   r'   r(   r5   rb   rj   Zgenerate_startsÚminri   r`   )r	   ra   r6   Zscope_startZbody_indentsÚlr   r   r   r-     s    

&
z"_HoldingScopeFinder.find_scope_endc             C   s   | j jS )N)r*   r^   )r	   r   r   r   r^     s    z_HoldingScopeFinder.linesc             C   s   | j jS )N)r*   Zsource_code)r	   r   r   r   Úcode  s    z_HoldingScopeFinder.codec             C   s   | j jS )N)r*   r'   )r	   r   r   r   r'     s    z!_HoldingScopeFinder.logical_lines)N)r0   r1   r2   r   r`   rb   rC   ri   rj   rF   r-   r4   r^   rp   r'   r   r   r   r   rI   ×   s   
rI   c                   s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚTemporaryScopezžCurrently used for list comprehensions and generator expressions

    These scopes do not appear in the `get_scopes()` method of their
    parent scopes.
    c                s    t t| ƒ ||j|¡ || _d S )N)r8   rq   r   r   r9   )r	   r   r
   r9   )r;   r   r   r   +  s    
zTemporaryScope.__init__c             C   s   | j S )N)r9   )r	   r   r   r   r   0  s    zTemporaryScope.get_namesc             C   s   | j S )N)r9   )r	   r   r   r   r   3  s    z TemporaryScope.get_defined_namesc             C   s   g S )Nr   )r	   r   r   r   r   6  s    zTemporaryScope._create_scopesc             C   s   dS )NZ	Temporaryr   )r	   r   r   r   r/   9  s    zTemporaryScope.get_kind)
r0   r1   r2   Ú__doc__r   r   r   r   r/   rM   r   r   )r;   r   rq   $  s   rq   )Zrope.base.builtinsrJ   Zrope.base.codeanalyzeZrope.base.pynamesZ	rope.baser   r   r   Úobjectr   r7   rN   r]   rI   rq   r   r   r   r   Ú<module>   s   a+7M