ó
†Å•Zc           @   sÆ   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 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d e d e e d	 „ Z d
 „  Z d S(   iÿÿÿÿN(   t
   exceptionst	   pyobjectst   pynamest
   taskhandlet   evaluatet   wordert   codeanalyzet   libutils(   t	   ChangeSett   ChangeContentst   MoveResource(   t   occurrencest   Renamec           B   sh   e  Z d  Z d d „ Z d „  Z d e d e d e j ƒ  d „ Z	 d „  Z
 d „  Z d „  Z d „  Z RS(	   s£   A class for performing rename refactoring

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

    c         C   s  | |  _  | |  _ | d k	 r t j |  j | ƒ |  _ |  j  j |  j ƒ } t j | | ƒ \ |  _	 |  _
 |  j
 d k rt j d ƒ ‚ qn‰ | j ƒ  rµ | j d k rµ | j } n  t j |  j  d ƒ } d |  _	 t j | d | ƒ|  _
 | j ƒ  r| j |  _ n | j d  |  _ d S(   s:   If `offset` is None, the `resource` itself will be renamedsH   Rename refactoring should be performed on resolvable python identifiers.s   __init__.pyt    t   resourceiýÿÿÿN(   t   projectR   t   NoneR   t   get_name_att   old_namet   get_pymoduleR   t   eval_location2t   old_instancet
   old_pynameR    t   RefactoringErrort	   is_foldert   namet   parentR   t   get_string_moduleR   t   ImportedModule(   t   selfR   R   t   offsett   this_pymodulet   dummy_pymodule(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   __init__   s$    			c         C   s   |  j  S(   N(   R   (   R   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   get_old_name*   s    c         C   sÞ  | t  t f k r= t j d t d d ƒ| d „ } | } n  | d k	 rw t j d t d d ƒ| rw |  j g } qw n  t |  j ƒ r• |  j g } n  | d k r³ |  j	 j
 ƒ  } n  t d |  j | f ƒ }	 t j |  j	 |  j |  j d | d | d	 |  j d
 | o|  j ƒ  ƒ}
 | j d t | ƒ ƒ } xb | D]Z } | j | j ƒ t |
 | d | ƒ} | d k	 r€|	 j t | | ƒ ƒ n  | j ƒ  q0W|  j ƒ  rÚ|  j j ƒ  j ƒ  } |  j | | ƒ rÚ|  j | | |	 ƒ qÚn  |	 S(   sº  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`\s 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.

        s@   unsure parameter should be a function that returns True or Falset
   stackleveli   c         S   s   |  S(   N(    (   t   value(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   unsure_funcJ   s    sA   `in_file` argument has been deprecated; use `resources` instead. s   Renaming <%s> to <%s>t   unsuret   docst   instancet   in_hierarchys   Collecting ChangesR   N(   t   Truet   Falset   warningst   warnt   DeprecationWarningR   R   t	   _is_localR   R   t   get_python_filesR   R   R   t   create_finderR   t	   is_methodt   create_jobsett   lent   started_jobt   patht   rename_in_modulet
   add_changeR	   t   finished_jobt   _is_renaming_a_modulet
   get_objectt   get_resourcet   _is_allowed_to_movet   _rename_module(   R   t   new_namet   in_fileR)   R&   R'   t	   resourcest   task_handleR%   t   changest   findert   job_sett   file_t   new_contentR   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   get_changes-   sF    		c         C   sL   | j  ƒ  r> y | j d ƒ | k SWqH t j k
 r: t SXn
 | | k Sd  S(   Ns   __init__.py(   R   t	   get_childR    t   ResourceNotFoundErrorR+   (   R   RA   R   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR=   k   s    c         C   s#   t  |  j j ƒ  t j ƒ r t St S(   N(   t
   isinstanceR   R;   R   t   AbstractModuleR*   R+   (   R   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR:   t   s    c         C   sL   |  j  } t | t j ƒ oK t | j ƒ  t j ƒ oK t | j ƒ  j t j ƒ S(   N(	   R   RK   R   t   DefinedNameR;   R   t
   PyFunctionR   t   PyClass(   R   t   pyname(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR2   y   s    	c         C   sb   | j  ƒ  s | d } n  | j j } | d k r: | } n | d | } | j t | | ƒ ƒ d  S(   Ns   .pyR   t   /(   R   R   R6   R8   R
   (   R   R   R?   RC   t   parent_patht   new_location(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR>      s    	N(   t   __name__t
   __module__t   __doc__R   R!   R"   R+   R   t   NullTaskHandleRH   R=   R:   R2   R>   (    (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR   	   s   		<				t   ChangeOccurrencesc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z e e e d „ Z RS(   sù  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         C   sa   | |  _  | |  _ | |  _ t j | | ƒ |  _ | j |  j ƒ |  _ t j	 |  j | ƒ |  _
 d  S(   N(   R   R   R   R   R   R   R   t   pymoduleR   t   eval_locationR   (   R   R   R   R   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR!   —   s    			c         C   s(   t  j |  j j ƒ  ƒ } | j |  j ƒ S(   N(   R   t   WorderR   t   readt   get_primary_atR   (   R   t   word_finder(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR"   Ÿ   s    c         C   sd   |  j  j } |  j  j ƒ  j | j |  j ƒ ƒ } | j | j ƒ  ƒ } | j | j	 ƒ  ƒ } | | f S(   N(
   RY   t   linest	   get_scopet   get_inner_scope_for_linet   get_line_numberR   t   get_line_startt	   get_startt   get_line_endt   get_end(   R   R_   t   scopet   startt   end(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   _get_scope_offset£   s    c   
      C   s·   t  d |  j | f ƒ } |  j ƒ  \ } } t j |  j |  j |  j d t d | ƒ} t | | d |  j	 d t
 d | | f d | d | ƒ}	 |	 d  k	 r³ | j t |  j |	 ƒ ƒ n  | S(	   Ns!   Changing <%s> occurrences to <%s>t   importst
   only_callsRY   t   replace_primaryt   regiont   readst   writes(   R   R   Rj   R   R1   R   R   R+   R7   RY   R*   R   R8   R	   R   (
   R   R?   Rl   Ro   Rp   RC   t   scope_startt	   scope_endRD   t   new_contents(    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyRH   «   s    (	   RT   RU   RV   R!   R"   Rj   R+   R*   RH   (    (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyRX   Š   s
   			c         C   s  | d k	 r | j ƒ  } n	 | j } t j | ƒ }	 xÒ |  j | | ƒ D]¾ }
 | rd |
 j ƒ  rd qF n  | r |
 j ƒ  \ } } n |
 j ƒ  \ } } | r¥ |
 j	 ƒ  sF | r¾ |
 j	 ƒ  r¾ qF n  | d k sî | d | k oé | d k  n rF |	 j
 | | | ƒ qF qF W|	 j ƒ  S(   s;   Returns the changed source or `None` if there is no changesi    i   N(   R   R\   t   source_codeR   t   ChangeCollectort   find_occurrencest   is_a_fixed_primaryt   get_primary_ranget   get_word_ranget
   is_writtenR8   t   get_changed(   t   occurrences_finderR?   R   RY   Rm   Rn   Ro   Rp   Rt   t   change_collectort
   occurrenceRh   Ri   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR7   º   s     	0c         C   s¡   |  j  ƒ  \ } } | d  k r" t S| j ƒ  j | ƒ } t |  t j ƒ rg | j ƒ  d k rg | j	 } n  | j ƒ  d k o  |  | j
 ƒ  j ƒ  k o  t |  t j ƒ S(   Nt   Functiont   Class(   R   R€   (   t   get_definition_locationR   R+   R`   Ra   RK   R   RM   t   get_kindR   t	   get_namest   valuest   AssignedName(   RP   t   modulet   linenoRg   (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyR/   Ò   s    (   R,   t	   rope.baseR    R   R   R   R   R   R   R   t   rope.base.changeR   R	   R
   t   rope.refactorR   t   objectR   RX   R   R+   R*   R7   R/   (    (    (    s3   lib/python2.7/site-packages/rope/refactor/rename.pyt   <module>   s   :0	