
    1Vh                     r    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	 Zd
 Zd Zy)z Rename names in a python module.    )absolute_import)division)print_functionN)import_utils)	ast_utils)scopec                    t        j                  |       }||j                  vryd}i }g }|j                  |   D ]  }t        |j                  t
        j                        r|j                  |j                        }t        |t
        j                        rQ||vrMt        ||||      sJ |j                  dd      d   ||j                  dd      d   <   |j                  |       nO||j                  j                  t        |      d z   |j                  _        |j                  j                  s|||<   d}t        |j                  t
        j                        s'|j                  |vs7t        ||j                  ||      sJ |j                  dd      d   ||j                  dd      d   <   |j                  |j                         d} t        j                   |      D ]  \  }	}
t#        || |	|
        |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   analyze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renamesalready_changedrefr   
rename_old
rename_news              D/home/dcms/DCMS/lib/python3.12/site-packages/pasta/augment/rename.pyrename_externalr)      s   < }}Q"R++++'/##H- c#((CII&yy"f 
FCNN	+o0M)"fhIII/7sA/Fr/JQ'+,v& 388==X#@@xx&'(
k	CHHcnn	-		()"chh(KKK/7sA/Fr/JQ'+,sxx(), !$g 6 1j*"aZ01	    c                 ,   ||k(  ry|j                   j                  d      }|j                  d      }|j                  d      }|d t        |       |k(  r&dj                  ||t        |      d  z         |_         y|j                  D ]  }|j
                  |d   k(  s n y|d   |_        ||d d k7  rbt        |j                        dkD  r1t        j                  | ||      }dj                  |d d       |_         ydj                  |d d       |_         y)NFr
   Tr   r   )modulesplitr   joinnamesr   r   split_import)	r!   r   r   r    module_parts	old_parts	new_partsalias_to_change
new_imports	            r(   r   r   ^   s   ""3',nnS!)nnS!) /3y>"i/((9|C	NO'DDEDK  oy}, "2/ Ys^#
4::,,RGj((9Sb>2j 
 HHYs^,dk	r*   c                    |j                  d      }	 | j                  |d      }|dd D ]  }|j                  |   } 	 d}|j                  D ]  }t        |t        j                  t        j                  f      s.t        j                  | j                  |      |t        j                  |      j                  d   j                         d} |S # t        $ r Y yw xY w)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KeyErrorreadsr   r   Name	Attributer   replace_childr   parsebodyvalue)	r!   r   r   r    
name_partsr   partr"   ref_nodes	            r(   r   r      s     ~~c"*88JqM"D12 ZZd
 +** h(SXXs}}56bii18!ii166q9??Ak	 
 
 s   +C 	CC)__doc__
__future__r   r   r   r   r   pasta.augmentr   
pasta.baser   r   r)   r   r    r*   r(   <module>rH      s3    & '  % 
 
 &   >BBr*   