o
    ”7?eâ  ã                   @   sx   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl	mZ d	d
„ Zdd„ Zdd„ ZdS )z Rename names in a python module.é    )Úabsolute_import)Údivision)Úprint_functionN)Úimport_utils)Ú	ast_utils)Úscopec                 C   sT  t  | ¡}||jvrdS d}i }g }|j| D ]}t|jtjƒrh| |j¡}t|tjƒrO||vrOt	||||ƒs9J ‚| 
dd¡d || 
dd¡d < | |¡ n||jjt|ƒd…  |j_|jjse|||< d}qt|jtjƒr–|j|vr–t	||j||ƒs~J ‚| 
dd¡d || 
dd¡d < | |j¡ d}qt |¡D ]\}	}
t|| |	|
ƒ qœ|S )a  Rename an imported name in a module.

  This will rewrite all import statements in `tree` that reference the old
  module as well as any names in `tree` which reference the imported name. This
  may introduce new import statements, but only if necessary.

  For example, to move and rename the module `foo.bar.utils` to `foo.bar_utils`:
  > rename_external(tree, 'foo.bar.utils', 'foo.bar_utils')

  - import foo.bar.utils
  + import foo.bar_utils

  - from foo.bar import utils
  + from foo import bar_utils

  - from foo.bar import logic, utils
  + from foo.bar import logic
  + from foo import bar_utils

  Arguments:
    t: (ast.Module) Module syntax tree to perform the rename in. This will be
      updated as a result of this function call with all affected nodes changed
      and potentially new Import/ImportFrom nodes added.
    old_name: (string) Fully-qualified path of the name to replace.
    new_name: (string) Fully-qualified path of the name to update to.

  Returns:
    True if any changes were made, False otherwise.
  FÚ.é   éÿÿÿÿNT)r   ZanalyzeZexternal_referencesÚ
isinstanceÚnodeÚastÚaliasÚparentÚ
ImportFromÚ_rename_name_in_importfromÚrsplitÚappendÚnameÚlenÚasnameÚsixÚ	iteritemsÚ_rename_reads)ÚtÚold_nameÚnew_nameÚscÚhas_changedÚrenamesZalready_changedÚrefr   Z
rename_oldZ
rename_new© r!   úU/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/pasta/augment/rename.pyÚrename_external   s6   

 
 €r#   c           	      C   sè   ||krdS |j  d¡}| d¡}| d¡}|d t|ƒ… |kr0d ||t|ƒd …  ¡|_ dS |jD ]}|j|d kr> qAq3dS |d |_||d d… krrt|jƒdkrht | ||¡}d |d d… ¡|_ dS d |d d… ¡|_ dS )NFr   Tr
   r	   )ÚmoduleÚsplitr   ÚjoinÚnamesr   r   Zsplit_import)	r   r   r   r   Úmodule_partsZ	old_partsZ	new_partsZalias_to_changeZ
new_importr!   r!   r"   r   ^   s*   


ÿ
þr   c           	      C   sš   |  d¡}z| j|d  }|dd… D ]}|j| }qW n
 ty&   Y dS w d}|jD ]}t|tjtjfƒrJt	 
|  |¡|t |¡jd j¡ d}q,|S )ap  Updates all locations in the module where the given name is read.

  Arguments:
    sc: (scope.Scope) Scope to work in. This should be the scope of `t`.
    t: (ast.AST) The AST to perform updates in.
    old_name: (string) Dotted name to update.
    new_name: (string) Dotted name to replace it with.

  Returns:
    True if any changes were made, False otherwise.
  r   r   r	   NFT)r%   r'   ÚattrsÚKeyErrorZreadsr   r   ÚNameÚ	Attributer   Zreplace_childr   ÚparseÚbodyÚvalue)	r   r   r   r   Z
name_partsr   Úpartr   Zref_noder!   r!   r"   r      s$   
ÿÿ
ÿ€r   )Ú__doc__Ú
__future__r   r   r   r   r   Zpasta.augmentr   Z
pasta.baser   r   r#   r   r   r!   r!   r!   r"   Ú<module>   s   A!