
    Æe                     `    d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
mZmZmZ  G d d      ZeZy)    )evaluate
exceptionslibutils	pyobjects
taskhandle)ChangeContents	ChangeSet)importutilsoccurrencesrenamesourceutilsc                   d    e Zd Zd Zddej
                  fdZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)IntroduceFactoryc                 $   || _         || _        | j                   j                  |      }t        j                  ||      | _        | j
                  2t        | j
                  j                         t        j                        st        j                  d      | j
                  j                         j                         | _        | j
                  j                         j                         | _        | j                  j!                         | _        y )Nz1Introduce factory should be performed on a class.)projectoffsetget_pymoduler   eval_location
old_pyname
isinstance
get_objectr   PyClassr   RefactoringErrorget_nameold_name
get_modulepymoduleget_resourceresource)selfr   r   r   this_pymodules        A/usr/lib/python3/dist-packages/rope/refactor/introduce_factory.py__init__zIntroduceFactory.__init__   s    11(;"00G??"*OO&&()*;*;+
 --C  224==?224??A224    FNc                     || j                   j                         }t        d|z        }|j                  dt	        |            }| j                  |||||       |S )a  Get the changes this refactoring makes

        `factory_name` indicates the name of the factory function to
        be added.  If `global_factory` is `True` the factory will be
        global otherwise a static method is added to the class.

        `resources` can be a list of `rope.base.resource.File` that
        this refactoring should be applied on; if `None` all python
        files in the project are searched.

        zIntroduce factory method <%s>zCollecting Changes)r   get_python_filesr	   create_jobsetlen_change_module)r    factory_nameglobal_factory	resourcestask_handlechangesjob_sets          r"   get_changeszIntroduceFactory.get_changes   s^    $ 557I;lJK++,@#i.QIwngVr$   c                     | j                   S )zReturn the name of the classr   )r    s    r"   r   zIntroduceFactory.get_name0   s    }}r$   c                 l   |rd|z  }n| j                  ||      }|D ]  }|j                  |j                         || j                  k(  r$| j	                  |||       |j                          R| j                  |||      }||rt        j                  | j                  || j                        }	t        j                  | j                        }
t        j                  | j                  |	|
|      \  }}|j                  ||      }|j                  t        ||             |j                           y )Nz__rope_factory_%s_)_new_function_namestarted_jobpathr   _change_resourcefinished_job_rename_occurrencesr   get_string_moduler   modnamer
   
add_importreplace
add_changer   )r    r,   r.   r*   global_r/   replacementfile_changed_codenew_pymoduler;   importeds               r"   r)   zIntroduceFactory._change_module4   s   .=K11,HK 	#E

+%%%g|WE$$&33E;PL'#+#=#=lDMM$L '..t}}=G-8-C-ClG\.*L( $0#7#7X#NL"">%#FG  "%	#r$   c                 $   | j                   j                         j                         }| j                  | j                  | j                  ||      |      }|| j                  j                  }n1t        j                  | j                  || j                        | _        | j                  j                  }| j                  ||      }|d | }|| j                  ||||      z  }|||d  z  }|j                  t        | j                  |             y )N)r   )r   r   	get_scoper9   r   r4   r   source_coder   r:   r   lines_get_insertion_offset_get_factory_methodr>   r   )	r    r.   r*   r?   class_scoperG   rH   startresults	            r"   r7   z!IntroduceFactory._change_resourceN   s    oo002<<>..MM422<I7
 --33K$66kDMMDM ##**;>Ve$$**5+|WUU+ef%%>$--@Ar$   c                     |j                         }|j                         r!|j                         d   j                         }|j                  |      dz   }|S )N   )get_end
get_scopesget_line_end)r    rK   rH   
start_linerL   s        r"   rI   z&IntroduceFactory._get_insertion_offset`   sR     ((*
!!#$//1"5==?J"":.2r$   c                    dt        j                  | j                        z  }|rG| j                  ||      dkD  rt	        j
                  d      dj                  ||| j                        S d|z  | d| j                   dz   }| j                  ||      t        j                  | j                        z   }dt        j                  ||      z   S )	N r   z5Cannot make global factory method for nested classes.z3
def {}(*args, **kwds):
{}return {}(*args, **kwds)
z%@staticmethod
def %s(*args, **kwds):
zreturn z(*args, **kwds)

)	r   
get_indentr   _get_scope_indentsr   r   formatr   indent_lines)r    rH   rK   r*   r?   unit_indentsunindented_factoryindentss           r"   rJ   z$IntroduceFactory._get_factory_methodg   s    [33DLLAA&&uk:Q> 11K  LRR  6Dgdmm_4EFG 	 ))%=@V@VLLA
 
 k../A7KKKr$   c                 J    t        j                  ||j                               S )N)r   get_indents	get_start)r    rH   scopes      r"   rY   z#IntroduceFactory._get_scope_indents|   s    &&ueoo.?@@r$   c                 .    |r|S | j                   dz   |z   S )N.r2   )r    r*   r?   s      r"   r4   z#IntroduceFactory._new_function_name   s    ==3&55r$   c                     t        j                  | j                  | j                  | j                  d      }t        j                  ||||      S )NT)
only_calls)r   replace_primary)r   create_finderr   r   r   r   rename_in_module)r    rA   changed_namer+   finders        r"   r9   z$IntroduceFactory._rename_occurrences   sE    **LL$--T
 &&L5.
 	
r$   )__name__
__module____qualname__r#   r   DEFAULT_TASK_HANDLEr0   r   r)   r7   rI   rJ   rY   r4   r9    r$   r"   r   r      sH    5& 222#4B$L*A6
r$   r   N)	rope.baser   r   r   r   r   rope.base.changer   r	   rope.refactorr
   r   r   r   r   IntroduceFactoryRefactoringrp   r$   r"   <module>ru      s)    K K 6 G GE
 E
P / r$   