
    Æe$                         d dl Z d dl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lmZ  G d d      Z G d d      Z	 	 	 	 	 	 dd	Zd
 Zy)    N)codeanalyzeevaluate
exceptionslibutilspynames	pyobjects
taskhandleworder)ChangeContents	ChangeSetMoveResource)occurrencesc                   ^    e Zd ZdZddZd Zdddddej                  fdZd Z	d Z
d	 Zd
 Zy)RenamezA class for performing rename refactoring

    It can rename everything: classes, functions, modules, packages,
    methods, variables and keyword arguments.

    Nc                    || _         || _        |t        j                  | j                  |      | _        | j                   j                  | j                        }t        j                  ||      \  | _        | _	        | j                  t        j                  d      y|j                         s|j                  dk(  r|j                  }t        j                   | j                   d      }d| _        t#        j$                  ||      | _	        |j                         r|j                  | _        y|j                  dd | _        y)z:If `offset` is None, the `resource` itself will be renamedNzHRename refactoring should be performed on resolvable python identifiers.__init__.py resource)projectr   r
   get_name_atold_nameget_pymoduler   eval_location2old_instance
old_pynamer   RefactoringError	is_foldernameparentr   get_string_moduler   ImportedModule)selfr   r   offsetthis_pymoduledummy_pymodules         6/usr/lib/python3/dist-packages/rope/refactor/rename.py__init__zRename.__init__   s    "..t}}fEDM LL55dmmDM191H1Hv2.Dt & 119  ' %%'HMM],J#??%77bIN $D%44^hWDO!!# ( (cr 2    c                     | j                   S N)r   r$   s    r(   get_old_namezRename.get_old_name3   s    }}r*   Fc           
         |dv r#t        j                  dt        d       |fd}|}|+t        j                  dt        d       |r| j                  g}t	        | j
                        r| j                  g}|| j                  j                         }t        d| j                   d| d	      }	t        j                  | j                  | j                  | j
                  ||| j                  |xr | j                         
      }
|j                  dt        |            }|D ]X  }|j!                  |j"                         t%        |
||      }||	j'                  t)        ||             |j+                          Z | j-                         rM| j
                  j/                         j1                         }| j3                  ||      r| j5                  |||	       |	S )a  Get the changes needed for this refactoring

        Parameters:

        - `in_hierarchy`: when renaming a method this keyword forces
          to rename all matching methods in the hierarchy
        - `docs`: when `True` rename refactoring will rename
          occurrences in comments and strings where the name is
          visible.  Setting it will make renames faster, too.
        - `unsure`: decides what to do about unsure occurrences.
          If `None`, they are ignored.  Otherwise `unsure` is
          called with an instance of `occurrence.Occurrence` as
          parameter.  If it returns `True`, the occurrence is
          considered to be a match.
        - `resources` can be a list of `rope.base.resources.File` to
          apply this refactoring on.  If `None`, the restructuring
          will be applied to all python files.
        - `in_file`: this argument has been deprecated; use
          `resources` instead.

        )TFz@unsure parameter should be a function that returns True or False   )
stacklevelc                     | S r,    )values    r(   unsure_funcz'Rename.get_changes.<locals>.unsure_func\   s    r*   zA`in_file` argument has been deprecated; use `resources` instead. z
Renaming <z> to <>)unsuredocsinstancein_hierarchyzCollecting Changesr   )warningswarnDeprecationWarningr   	_is_localr   r   get_python_filesr   r   r   create_finderr   	is_methodcreate_jobsetlenstarted_jobpathrename_in_module
add_changer   finished_job_is_renaming_a_module
get_objectget_resource_is_allowed_to_move_rename_module)r$   new_namein_filer:   r7   r8   	resourcestask_handler5   changesfinderjob_setfile_new_contentr   s                  r(   get_changeszRename.get_changes6   s   > ]"MMU" #)  !FMMV"
 !]]O	T__%I557IjvhZqIJ**LLMMOO&&%:$..*:
 ++,@#i.Q 	#E

+*68eLK&"">%#EF  "	# %%'113@@BH''	8<##Hh@r*   c                     |j                         r	 |j                  d      |v S ||v S # t        j                  $ r Y yw xY w)Nr   F)r   	get_childr   ResourceNotFoundError)r$   rP   r   s      r(   rL   zRename._is_allowed_to_move   sP    ))-8IEE y(( 33 s   ) ??c                 f    t        | j                  j                         t        j                        S r,   )
isinstancer   rJ   r   AbstractModuler-   s    r(   rI   zRename._is_renaming_a_module   s"    $//446	8P8PQQr*   c                 
   | j                   }t        |t        j                        xr\ t        |j	                         t
        j                        xr2 t        |j	                         j                  t
        j                        S r,   )	r   r\   r   DefinedNamerJ   r   
PyFunctionr!   PyClass)r$   pynames     r(   rA   zRename.is_method   sd    vw223 J6,,.	0D0DEJ6,,.55y7H7HI	
r*   c                     |j                         s|dz   }|j                  j                  }|dk(  r|}n|dz   |z   }|j                  t	        ||             y )Nz.pyr   /)r   r!   rE   rG   r   )r$   r   rN   rR   parent_pathnew_locations         r(   rM   zRename._rename_module   sX    !!#%'Hoo**"#L&,x7L<,?@r*   r,   )__name__
__module____qualname____doc__r)   r.   r	   DEFAULT_TASK_HANDLErW   rL   rI   rA   rM   r3   r*   r(   r   r      sH    34 22KZ)R
Ar*   r   c                   *    e Zd ZdZd Zd Zd ZddZy)ChangeOccurrencesa  A class for changing the occurrences of a name in a scope

    This class replaces the occurrences of a name.  Note that it only
    changes the scope containing the offset passed to the constructor.
    What's more it does not have any side-effects.  That is for
    example changing occurrences of a module does not rename the
    module; it merely replaces the occurrences of that module in a
    scope with the given expression.  This class is useful for
    performing many custom refactorings.

    c                     || _         || _        || _        t        j                  ||      | _        |j                  | j                        | _        t        j                  | j                  |      | _
        y r,   )r   r   r%   r
   r   r   r   pymoduler   eval_locationr   )r$   r   r   r%   s       r(   r)   zChangeOccurrences.__init__   sY     **8V<,,T]];"00Gr*   c                     t        j                  | j                  j                               }|j	                  | j
                        S r,   )r
   Worderr   readget_primary_atr%   )r$   word_finders     r(   r.   zChangeOccurrences.get_old_name   s2    mmDMM$6$6$89))$++66r*   c                     | j                   j                         j                  | j                        }|j	                         S r,   )ro   	get_scopeget_inner_scope_for_offsetr%   
get_region)r$   scopes     r(   _get_scope_offsetz#ChangeOccurrences._get_scope_offset   s2    '')DDT[[Q!!r*   c           	      ^   t        d| j                   d| d      }| j                         \  }}t        j                  | j
                  | j                  | j                  d|      }t        ||| j                  d||f||      }	|	%|j                  t        | j                  |	             |S )Nz
Changing <z> occurrences to <r6   F)imports
only_callsT)ro   replace_primaryregionreadswrites)r   r   r{   r   r@   r   r   rF   ro   rG   r   r   )
r$   rN   r~   r   r   rR   scope_start	scope_endrS   new_contentss
             r(   rW   zChangeOccurrences.get_changes   s    j7I(STUV!%!7!7!9Y**LLMMOO!
 (]] +
 #~dmm\JKr*   N)FTT)rg   rh   ri   rj   r)   r.   r{   rW   r3   r*   r(   rm   rm      s    
H7"r*   rm   c                    ||j                         }n|j                  }t        j                  |      }	| j	                  ||      D ]  }
|r|
j                         r|r|
j                         \  }}n|
j                         \  }}|s|
j                         r|s|
j                         rd||d   |cxk  r	|d   k  sxn {|	j                  |||        |	j                         S )z;Returns the changed source or `None` if there is no changesr      )rs   source_coder   ChangeCollectorfind_occurrencesis_a_fixed_primaryget_primary_rangeget_word_range
is_writtenrG   get_changed)occurrences_finderrN   r   ro   r   r   r   r   r   change_collector
occurrencestartends                r(   rF   rF      s     mmo**"22;?(99(HM >
z<<>#557JE3#224JE3j335:002>VAY%;&);''sH=> ''))r*   c                 ~   | j                         \  }}|y|j                         j                  |      }t        | t        j
                        r|j                         dv r|j                  }|j                         dk(  xr< | |j                         j                         v xr t        | t        j                        S )NF)FunctionClassr   )get_definition_locationrw   get_inner_scope_for_liner\   r   r_   get_kindr!   	get_namesvaluesAssignedName)rb   modulelinenorz   s       r(   r>   r>      s    335NFF~77?E&'--.5>>3C H 4 J& 	5eoo'..00	5vw334r*   )NNFNTT)r;   	rope.baser   r   r   r   r   r   r	   r
   rope.base.changer   r   r   rope.refactorr   r   rm   rF   r>   r3   r*   r(   <module>r      s\    	 	 	 E D %NA NAb2 2p 
*@r*   