a
    ]9a                     @   sl   d dl Zd dlZd dlmZ d dl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 deZeZdS )    N)libutils)
taskhandleevaluate)	ChangeSetChangeContents)renameoccurrencessourceutilsimportutilsc                   @   sh   e Zd Zdd Zdde 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dS )IntroduceFactoryc                 C   s   || _ || _| j |}t||| _| jd u sFt| j tj	j
jsTtj	jd| j  | _| j  | _| j | _d S )Nz1Introduce factory should be performed on a class.)projectoffsetZget_pymoduler   Zeval_location
old_pyname
isinstance
get_objectropebaseZ	pyobjectsZPyClass
exceptionsRefactoringErrorget_nameold_nameZ
get_modulepymoduleZget_resourceresource)selfr   r   r   Zthis_pymodule r   >lib/python3.9/site-packages/rope/refactor/introduce_factory.py__init__
   s    zIntroduceFactory.__init__FNc                 C   sD   |du r| j  }td| }|dt|}| ||||| |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.

        NzIntroduce factory method <%s>zCollecting Changes)r   Zget_python_filesr   Zcreate_jobsetlen_change_module)r   factory_nameglobal_factory	resourcesZtask_handlechangesjob_setr   r   r   get_changes   s    
zIntroduceFactory.get_changesc                 C   s   | j S )zReturn the name of the classr   )r   r   r   r   r   3   s    zIntroduceFactory.get_namec                 C   s   |rd| }n|  ||}|D ]}||j || jkrP| ||| |  q| |||}|d ur|rt| j	|| j}	t
| j}
t| j	|	|
|\}}|||}|t|| |  qd S )Nz__rope_factory_%s_)_new_function_nameZstarted_jobpathr   _change_resourceZfinished_job_rename_occurrencesr   get_string_moduler   modnamer
   Z
add_importreplace
add_changer   )r   r!   r"   r   global_r#   Zreplacementfile_Zchanged_codeZnew_pymoduler+   Zimportedr   r   r   r   7   s,    



zIntroduceFactory._change_modulec           	      C   s   | j   }| | j| |||}|d u r8| jj}ntj	| j
|| jd| _| jj}| ||}|d | }|| ||||7 }|||d  7 }|t| j| d S )N)r   )r   r   Z	get_scoper)   r   r&   r   source_coder   r*   r   lines_get_insertion_offset_get_factory_methodr-   r   )	r   r"   r   r.   class_scoper0   r1   startresultr   r   r   r(   Q   s    

z!IntroduceFactory._change_resourcec                 C   s2   |  }| r | d   }||d }|S )N   )Zget_endZ
get_scopesZget_line_end)r   r4   r1   Z
start_liner5   r   r   r   r2   c   s
    z&IntroduceFactory._get_insertion_offsetc                 C   s   dt | j }|rB| ||dkr2tjjdd||| jf S d| d|| jf  }| ||t | j }dt 	|| S )N r   z5Cannot make global factory method for nested classes.z3
def %s(*args, **kwds):
%sreturn %s(*args, **kwds)
z%@staticmethod
def %s(*args, **kwds):
z%sreturn %s(*args, **kwds)

)
r	   Z
get_indentr   _get_scope_indentsr   r   r   r   r   Zindent_lines)r   r1   r4   r   r.   Zunit_indentsZunindented_factoryindentsr   r   r   r3   j   s&    z$IntroduceFactory._get_factory_methodc                 C   s   t || S )N)r	   Zget_indentsZ	get_start)r   r1   Zscoper   r   r   r;      s    z#IntroduceFactory._get_scope_indentsc                 C   s   |r|S | j d | S d S )N.r%   )r   r   r.   r   r   r   r&      s    z#IntroduceFactory._new_function_namec                 C   s.   t j| j| j| jdd}tj||||d}|S )NT)Z
only_calls)r   Zreplace_primary)r   Zcreate_finderr   r   r   r   Zrename_in_module)r   r/   Zchanged_namer    finderr6   r   r   r   r)      s    z$IntroduceFactory._rename_occurrences)__name__
__module____qualname__r   r   ZNullTaskHandler$   r   r   r(   r2   r3   r;   r&   r)   r   r   r   r   r   	   s   
r   )Zrope.base.exceptionsr   Zrope.base.pyobjectsZ	rope.baser   r   r   Zrope.base.changer   r   Zrope.refactorr   r   r	   r
   objectr   ZIntroduceFactoryRefactoringr   r   r   r   <module>   s    
