
    Æe9                    x   d dl mZ d dlm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mZ erd dlmZmZ d dlmZ d dlmZ ed   Z	 d	 	 	 	 	 	 	 	 	 d d
ZddZddZ G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z!d Z"d Z# G d d      Z$ G d de$      Z%y	)!    )annotations)TYPE_CHECKING)changecodeanalyzeevaluate
exceptionslibutilspynames	pyobjectsworder)functionutilsimportutilssourceutilssuites)LiteralOptional)Project)Resource)variablefunctionclassmodulepackageNc                \    t         t        t        t        t        d}||    } |||||      S )z{A factory for creating `Generate` objects

    Used in https://github.com/python-rope/ropemode but not in Rope itself.
    )r   r   r   r   r   )goal_resource)GenerateClassGenerateFunctionGenerateModuleGeneratePackageGenerateVariable)kindprojectresourceoffsetr   dgenerates          7/usr/lib/python3/dist-packages/rope/contrib/generate.pycreate_generater(       s6     $ "$	A wHGXv]KK    c                    || j                   }|j                  d      }|}|dd D ]  }|j                  |      } |j                  |d   dz         S )z9Creates a module and returns a `rope.base.resources.File`N..py)rootsplit	get_childcreate_file)r"   namesourcefolderpackagesparentr   s         r'   create_moduler6   6   sb    ||zz#HFCR= +!!'*+hrlU233r)   c                    || j                   }|j                  d      }|}|dd D ]  }|j                  |      } |j                  |d         }|j	                  d       |S )z<Creates a package and returns a `rope.base.resources.Folder`Nr+   r,   __init__.py)r.   r/   r0   create_folderr1   )r"   r2   r3   r4   r5   r   made_packagess          r'   create_packager;   A   sp    ||zz#HFCR= +!!'*+(("6Mm,r)   c                  8    e Zd Zd	dZd Zd Zd Zd Zd Zd Z	y)
	_GenerateNc                    || _         || _        || _        | j                  |||      | _        | j                  j                         | _        | j                          y N)r"   r#   r   _generate_infoinfoget_namer2   _check_exceptional_conditions)selfr"   r#   r$   r   s        r'   __init__z_Generate.__init__O   sN     *''6B	II&&(	**,r)   c                F    t        |j                  ||| j                        S r?   )_GenerationInfopycorer   rD   r"   r#   r$   s       r'   r@   z_Generate._generate_infoW   s    w~~xASASTTr)   c                    | j                   j                         r"t        j                  d| j                  z        | j                   j                         s"t        j                  d| j                  z        y )NzElement <%s> already exists.z5Cannot determine the scope <%s> should be defined in.)rA   element_already_existsr   RefactoringErrorr2   primary_is_foundrD   s    r'   rC   z'_Generate._check_exceptional_conditionsZ   sg    99++---.:  yy))+--G$))S  ,r)   c                ,   t        j                  d| j                          d| j                   d      }| j                  j                         }| j                  j                         }t        j                  | j                         |      }d|d   z  |z   d|d   z  z   }| j                  j                         }| j                  j                         \  }}t        j                  |j                               }	|	j                  |||       |j                  t        j                   ||	j#                                      | j$                  rGt'        | j(                  | j*                  | j$                  | j                        }
|j                  |
       |S )Nz	Generate z <>
r      )r   	ChangeSet_get_element_kindr2   rA   get_scope_indentsget_blank_linesr   fix_indentation_get_elementget_insertion_resourceget_insertion_offsetsr   ChangeCollectorread
add_changeChangeContentsget_changedr   _add_relative_import_to_moduler"   r#   )rD   changesindentsblanksbase_definition
definitionr#   startend	collectorrelative_imports              r'   get_changesz_Generate.get_changesd   sG   ""Yt/E/E/G.H499+UV#WX))--/**,%55d6G6G6I7SF1I%7$:JJ
99335YY446
s//@	UC46009;P;P;RST<dmmT-?-?O /r)   c                j    | j                   j                         | j                   j                         fS r?   )rA   rY   get_insertion_linenorN   s    r'   get_locationz_Generate.get_locationx   s'    		002DII4R4R4TUUr)   c                    t               r?   NotImplementedErrorrN   s    r'   rT   z_Generate._get_element_kind{       !##r)   c                    t               r?   ro   rN   s    r'   rX   z_Generate._get_element~   rq   r)   r?   )
__name__
__module____qualname__rE   r@   rC   rj   rm   rT   rX    r)   r'   r=   r=   N   s'    -U(V$$r)   r=   c                      e Zd Zd Zd Zd Zy)r   c                0    t        |j                  ||      S r?   )_FunctionGenerationInforH   rI   s       r'   r@   zGenerateFunction._generate_info   s    &w~~xHHr)   c                   d}g }| j                   j                         rd}| j                   j                         s4| j                   j                         s| j                   j	                         r|j                  d       |j                  | j                   j                                dj                  || j                  dj                  |            }|S )N z@staticmethod
rD   z{}def {}({}):
    pass
z, )rA   is_static_method	is_methodis_constructoris_instanceappendextendget_passed_argsformatr2   join)rD   	decoratorargsre   s       r'   rX   zGenerateFunction._get_element   s    	99%%')III!yy'')yy$$&KKDII--/0077IIIIdO


 r)   c                     y)NFunctionrv   rN   s    r'   rT   z"GenerateFunction._get_element_kind       r)   N)rs   rt   ru   r@   rX   rT   rv   r)   r'   r   r      s    I&r)   r   c                      e Zd Zd Zd Zy)r    c                     d| j                   z  S )Nz
%s = None
r2   rN   s    r'   rX   zGenerateVariable._get_element   s    tyy((r)   c                     y)NVariablerv   rN   s    r'   rT   z"GenerateVariable._get_element_kind   r   r)   Nrs   rt   ru   rX   rT   rv   r)   r'   r    r       s    )r)   r    c                      e Zd Zd Zd Zy)r   c                     d| j                   z  S )Nzclass %s(object):
    pass
r   rN   s    r'   rX   zGenerateClass._get_element   s    .::r)   c                     y)NClassrv   rN   s    r'   rT   zGenerateClass._get_element_kind   s    r)   Nr   rv   r)   r'   r   r      s    ;r)   r   c                      e Zd Zd Zd Zy)r   c                   | j                   j                         }t        j                  d| j                  z        }| j
                  j                  |j                   d| j                   d      }|j                         r"t        j                  d|j                  z        |j                  t        j                  |             |j                  t        | j
                  | j                  |             |S )NzGenerate Module <%s>/r-   zModule <%s> already exists)rA   get_packager   rS   r2   r"   get_filepathexistsr   rL   r]   CreateResource_add_import_to_moduler#   )rD   r   ra   new_resources       r'   rj   zGenerateModule.get_changes   s    ))'')""#9DII#EF||,,~Qtyyk-MN --,|/@/@@  	600>?!$,,|L	
 r)   c                v    | j                   j                         }|j                  d| j                  z        dfS )Nz%s.pyrR   rA   r   r0   r2   )rD   r   s     r'   rm   zGenerateModule.get_location   s3    ))'')!!'DII"56::r)   Nrs   rt   ru   rj   rm   rv   r)   r'   r   r      s    ;r)   r   c                      e Zd Zd Zd Zy)r   c                   | j                   j                         }t        j                  d| j                  z        }| j
                  j                  |j                   d| j                         }|j                         r"t        j                  d|j                  z        |j                  t        j                  |             |j                  t        | j
                  | j                  |             | j
                  j                  |j                  dz   | j                  z         }|j                  t        j                  |d             |S )NzGenerate Package <%s>r   zPackage <%s> already existsr8   )rA   r   r   rS   r2   r"   
get_folderr   r   r   rL   r]   r   r   r#   
CreateFile)rD   r   ra   r   childs        r'   rj   zGeneratePackage.get_changes   s   ))'')""#:TYY#FG||..',,q/LM ---0A0AA  	600>?!$,,|L	
 ''s(:TYY(FG6,,UMBCr)   c                    | j                   j                         }|j                  | j                        }|j                  d      dfS )Nr8   rR   r   )rD   r   r   s      r'   rm   zGeneratePackage.get_location   s<    ))'')!!$)),.22r)   Nr   rv   r)   r'   r   r      s     3r)   r   c                4   | j                  |      }t        j                  |       }|j                  |      }t	        j
                  |      }t        j                  |d ff      }|j                  |       t        j                  ||j                               S r?   )get_pymoduler   ImportToolsmodule_importsr	   modnameNormalImport
add_importr   r^   get_changed_source)r"   r#   importedpymoduleimport_toolsr   module_name
new_imports           r'   r   r      s    ##H-H**73L!00:N""8,K))K+>*@AJj)  >+L+L+NOOr)   c                    | j                  |      }t        j                  |       }|j                  |      }|j	                  ||      }|j                  |       t        j                  ||j                               S r?   )	r   r   r   r   get_from_importr   r   r^   r   )r"   r#   r   r2   r   r   r   r   s           r'   r`   r`      sn    ##H-H**73L!00:N--h=Jj)  >+L+L+NOOr)   c                  b    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zy)rG   Nc                *   || _         || _        || _        || _        | j                   j                  j                  |      | _        t        j                  | j                        }|j                  |      \  | _
        | _        | j                          y r?   )rH   r#   r$   r   r"   r   source_pymoduler   ScopeNameFinderget_primary_and_pyname_atprimarypyname_init_fields)rD   rH   r#   r$   r   finders         r'   rE   z_GenerationInfo.__init__   sw     *#{{22??I))$*>*>?$*$D$DV$L!dkr)   c                    | j                         | _        | j                         | _        | j	                  | j                        | _        y r?   )_get_source_scopesource_scope_get_goal_scope
goal_scope_get_goal_modulegoal_pymodulerN   s    r'   r   z_GenerationInfo._init_fields   s:     224..0!224??Cr)   c                   | j                   Y| j                  r=| j                  j                  j	                  | j                        j                         S | j                         S | j                   j                         }t        |t        j                        r|j                         S t        |j                         t        j                        r|j                         j                         S y r?   )r   r   rH   r"   r   	get_scoper   
get_object
isinstancer   PyDefinedObjectget_typePyClassrD   pyobjects     r'   r   z_GenerationInfo._get_goal_scope   s    <<!!{{**778J8JKUUWW--//<<**,h	 9 9:%%''))+Y->->?$$&0022 @r)   c                j    |y |j                   |j                   }|j                   |j                  S r?   )r5   r   )rD   scopes     r'   r   z _GenerationInfo._get_goal_module  s3    =ll&LLE ll&~~r)   c                    | j                   j                         }| j                   j                  j                  | j                        }|j                  |      S r?   )r   r   linesget_line_numberr$   get_inner_scope_for_line)rD   module_scopelinenos      r'   r   z!_GenerationInfo._get_source_scope  sG    ++557%%++;;DKKH44V<<r)   c                   | j                   j                  }| j                  | j                  k(  r| j                   j                  }|j                  | j                        }|j                  |      d   }t        j                  | j                  j                  j                               }|j                  |      }t        j                  ||      }| j                         |k  rF|j!                         }t        j                  ||      }|j"                  }| j                         |k  rF|S t%        | j                  j'                         dz   |j)                               S )Nr   rR   )r   r   r   r   logical_linesr   r$   logical_line_inr   ast_suite_treer   get_ast
find_suiter   get_indentsrU   	get_startr5   minget_endlength)rD   r   line_finderr   r.   suiterb   s          r'   rl   z$_GenerationInfo.get_insertion_lineno  s   ""((??d///,,::K**4;;7F 008;F(()A)A)I)I)KLDOOF+E!--eV<G((*W4*%11%@ ((*W4 Mt..014ellnEEr)   c                6    | j                   j                         S r?   )r   get_resourcerN   s    r'   rY   z&_GenerationInfo.get_insertion_resource$  s    !!..00r)   c                b   | j                   j                         dk(  rZt        j                  | j                   j                        \  }}| j
                  j                  || j                         dk(  r||fS | j
                  j                  }|j                  | j                               }||fS )Nr   pass)r   get_kindr   get_body_regionr   r   source_codestripr   get_line_startrl   )rD   rf   rg   r   s       r'   rZ   z%_GenerationInfo.get_insertion_offsets'  s    ??##%0$44T__5M5MNJE3!!--eC8>>@FJcz!""(($$T%>%>%@Au~r)   c                    | j                   j                         dk(  ryt        j                  | j                  j
                  | j                   j                               dz   S )NModuler      )r   r   r   r   r   r   r   rN   s    r'   rU   z!_GenerationInfo.get_scope_indents0  sU    ??##%1##""(($//*C*C*E 	
r)   c                P   | j                   j                         dk(  r+d}| j                  j                  j	                         dk(  rd}| j                   j                         dk(  rd}| j                   j                         dk(  rd}| j                   | j
                  k(  rdfS dfS )Nr      r{   r   r   rR   r   )r   r   r   r   r   r   )rD   base_blankss     r'   rV   z_GenerationInfo.get_blank_lines:  s    ??##%1K!!--335;??##%0K??##%3K??d///{##Qr)   c                6   | j                   }| j                   '| j                  j                  j                         d   S t	        |j                         t        j                        r|j                         j                         S t        j                  d      )Nr   z2A module/package can be only created in a package.)r   rH   r"   get_source_foldersr   r   r   	PyPackager   r   rL   )rD   r   s     r'   r   z_GenerationInfo.get_packageG  sz    ,,<<;;&&99;A>>g((*I,?,?@%%'4466))@
 	
r)   c                    | j                   d uS r?   )r   rN   s    r'   rM   z _GenerationInfo.primary_is_foundQ  s    d**r)   c                    | j                   $t        | j                   t        j                        ry| j	                         | j
                  j                         v S NFr   r   r
   UnboundNamerB   r   get_defined_namesrN   s    r'   rK   z&_GenerationInfo.element_already_existsT  @    ;;*T[[':M:M"N}}$//"C"C"EEEr)   c                V    t        j                  | j                  | j                        S r?   )r   get_name_atr#   r$   rN   s    r'   rB   z_GenerationInfo.get_nameY  s    !!$--==r)   r?   )rs   rt   ru   rE   r   r   r   r   rl   rY   rZ   rU   rV   r   rM   rK   rB   rv   r)   r'   rG   rG      sK    D

3=
F"1
 
+F
>r)   rG   c                  B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)ry   c                L   | j                         r(| j                  j                         j                         S | j	                         r6| j                  j                         j                         j                         S | j                  | j                         S | j                  j                         }t        |t        j                        r|j                         S t        |j                         t        j                        r|j                         j                         S y r?   )r~   r   r   r   r   r   r   r   r   r   r   r   r   s     r'   r   z'_FunctionGenerationInfo._get_goal_scope^  s     ;;))+5577;;))+446@@BB<<))++<<**,h	 9 9:%%''))+Y->->?$$&0022 @r)   c                    | j                   $t        | j                   t        j                        ry| j	                         | j
                  j                         v S r   r   rN   s    r'   rK   z._FunctionGenerationInfo.element_already_existsk  r   r)   c                    | j                   d uxr2 t        | j                   j                         t        j                        S r?   )r   r   r   r   r   rN   s    r'   r|   z(_FunctionGenerationInfo.is_static_methodp  s7    ||4' 
JLL##%y'8'8-
 	
r)   c                    | j                   d uxr@ t        | j                   j                         j                         t        j
                        S r?   )r   r   r   r   r   r   rN   s    r'   r}   z!_FunctionGenerationInfo.is_methodu  s@    ||4' 
JLL##%..0)2C2C-
 	
r)   c                    | j                   d uxr2 t        | j                   j                         t        j                        S r?   )r   r   r   r   r   rN   s    r'   r~   z&_FunctionGenerationInfo.is_constructorz  s7    {{$& 
:KK""$i&7&7,
 	
r)   c                    | j                   y| j                   j                         }t        |j                         t        j
                        S r   )r   r   r   r   r   r   r   s     r'   r   z#_FunctionGenerationInfo.is_instance  s=    ;;;;))+(++-y/@/@AAr)   c                    | j                         ry| j                         ryt        j                  | j                  | j
                        S )NrE   __call__)r~   r   r   r   r#   r$   rN   s    r'   rB   z _FunctionGenerationInfo.get_name  s;     !!$--==r)   c                    g }| j                   j                  }t        j                  |      }|j	                  | j
                        r|j                  | j
                        \  }}|j                  |dz
        \  }}||| }t        j                  |d      }	|	j                         \  }
}|
D ]B  }| j                  |      r|j                  |       &|j                  dt        |      z         D |D ]  \  }}|j                  |        |S )NrR   Fzarg%d)r   r   r   Worderis_a_function_being_calledr$   get_primary_rangeget_word_parens_ranger   _FunctionParserget_parameters_is_idr   len)rD   resultsourcer   rf   rg   parens_start
parens_endcallparserr   keywordsargr2   values                  r'   r   z'_FunctionGenerationInfo.get_passed_args  s    %%11v&,,T[[911$++>JE3'-'C'CC!G'L$L*%
+D"224?F#224ND( 9;;s#MM#&MM'CK"78	9
  ( $ed#$r)   c                b    d }|D ]  } ||      r|j                         r y  ||d         S )Nc                0    | j                         xs | dk(  S )N_)isalpha)cs    r'   id_or_underlinez7_FunctionGenerationInfo._is_id.<locals>.id_or_underline  s    99;*!s(*r)   Fr   )isdigit)rD   r  r  r  s       r'   r  z_FunctionGenerationInfo._is_id  s<    	+  	A"1%aiik	 s1v&&r)   N)rs   rt   ru   r   rK   r|   r}   r~   r   rB   r   r  rv   r)   r'   ry   ry   ]  s1    3F






B>&'r)   ry   r?   )
r!   GenerateKindr"   r   r#   r   r$   intr   zOptional[Resource])&
__future__r   typingr   	rope.baser   r   r   r   r	   r
   r   r   rope.refactorr   r   r   r   r   r   rope.base.projectr   rope.base.resourcesr   r!  r(   r6   r;   r=   r   r    r   r   r   r   r`   rG   ry   rv   r)   r'   <module>r)     s    "  	 	 	 J I(),	L )-L
LL L 	L
 &L,4
1$ 1$hy 6y I ;Y ;(3i 3.PPo> o>dI'o I'r)   