
    ;esk                     >   	 d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZmZ ddlmZ dd
Z G d de          Zd Zd Zd Zd Zd Z G d de          Z G d de          Z G d de          Z  G d de          Z!dS )z#Guido van Rossum <guido@python.org>    N)chain   )drivertokenizetoken)	find_root)pytreepygram)btm_matcherTc                     	 t          | g g dg          }g }t          j        |j                  D ]<\  }}}|                    d          r!|r
|dd          }|                    |           =|S )N*fix_   )
__import__pkgutiliter_modules__path__
startswithappend)	fixer_pkgremove_prefixpkg	fix_namesfindernameispkgs          '/usr/lib/python3.11/lib2to3/refactor.pyget_all_fix_namesr      s    O
YB
.
.CI&3CLAA # #e??6"" 	#  ABBxT"""    c                       e Zd ZdS )
_EveryNodeN__name__
__module____qualname__ r   r   r!   r!   +           Dr   r!   c                    	 t          | t          j        t          j        f          r| j        t
          | j        hS t          | t          j                  r"| j        rt          | j                  S t
          t          | t          j	                  rAt                      }| j        D ])}|D ]$}|                    t          |                     %*|S t          d| z            )Nz$Oh no! I don't understand pattern %s)
isinstancer	   NodePatternLeafPatterntyper!   NegatedPatterncontent_get_head_typesWildcardPatternsetupdate	Exception)patrpxs       r   r/   r/   /   s    9 #*F,>?@@  8z#v,-- ; 	0"3;///#v-.. EE 	- 	-A - -++,,,,-
:SA
B
BBr   c                 \   	 t          j        t                    }g }| D ]}|j        r[	 t	          |j                  }|D ]}||                             |           ?# t          $ r |                    |           Y `w xY w|j        !||j                                     |           |                    |           t          t          j
        j                                        t          j
        j                  D ]}||                             |           t          |          S N)collectionsdefaultdictlistpatternr/   r   r!   _accept_typer   r
   python_grammarsymbol2numbervaluestokensextenddict)
fixer_list
head_nodeseveryfixerheads	node_types         r   _get_headnode_dictrK   K   sO   /(..JE $ $= 	$8'66 "' 8 8Iy)0077778  $ $ $U#####$ !-5-.55e<<<<U####60>EEGG!079 9 , ,	9$$U++++
s   AB ?B c                 >     	  fdt           d          D             S )Nc                      g | ]
}d z   |z   S .r&   ).0fix_namepkg_names     r   
<listcomp>z+get_fixers_from_package.<locals>.<listcomp>h   s8     @ @ @ sNX% @ @ @r   F)r   )rR   s   `r   get_fixers_from_packagerT   d   sE    @ @ @ @-h>>@ @ @ @r   c                     | S r9   r&   )objs    r   	_identityrW   k   s    Jr   c                 h   d}t          j        t          j        |           j                  fd}t          t          j        t           j        t          j	        h          }t                      }	 	  |            \  }}||v r|t          j        k    r|rnd}n|t          j        k    r|dk    r |            \  }}|t          j        k    s|dk    rn |            \  }}|t          j        k    s|dk    rn |            \  }}|t          j        k    r|dk    r |            \  }}|t          j        k    rV|                    |            |            \  }}|t          j        k    s|dk    rn |            \  }}|t          j        k    Vnn n# t          $ r Y nw xY wt          |          S )	NFc                  B    t                    } | d         | d         fS )Nr   r   )next)tokgens    r   advancez(_detect_future_features.<locals>.advancer   s     3ii1vs1v~r   Tfrom
__future__import(,)r   generate_tokensioStringIOreadline	frozensetr   NEWLINENLCOMMENTr1   STRINGNAMEOPaddStopIteration)sourcehave_docstringr]   ignorefeaturestpvaluer\   s          @r   _detect_future_featuresrv   o   s   N

"2;v#6#6#?
@
@C     x{EMBCCFuuH			IBV||u|##! !%uz!!evoo#GII	E##u'<'<#GII	E##u'8'8#GII	E>>esll '		IBEJ&&LL''' '		IBUX~~# '		IB EJ&& 3	4    Xs   3D!F 
F"!F"c                       e Zd ZdS )
FixerErrorNr"   r&   r   r   rx   rx      s        &&r   rx   c                       e Zd ZddddZdZdZddZd Zd Zd	 Z	d
 Z
d ZddZddZd ZddZd Zd dZd Zd Z	 	 d!dZd"dZdZdZd Zd Zd Zd Zd Zd ZdS )#RefactoringToolF)print_functionexec_functionwrite_unchanged_filesFixr   Nc                 R   	 || _         |pg | _        | j                                        | _        || j                            |           t          j                                        | _        | j        d         r| j        j	        d= n| j        d         r| j        j	        d= | j        
                    d          | _        g | _        t          j        d          | _        g | _        d| _        t%          j        | j        t(          j        | j                  | _        |                                 \  | _        | _        g | _        t5          j                    | _        g | _        g | _        t?          | j        | j                  D ]k}|j         r| j        !                    |           $|| j        v r| j        "                    |           H|| j        v r| j        "                    |           ltG          | j                  | _$        tG          | j                  | _%        d S )	Nr{   printr|   execr}   rz   F)convertlogger)&fixersexplicit_default_optionscopyoptionsr2   r
   r?   grammarkeywordsgetr}   errorslogging	getLoggerr   	fixer_logwroter   Driverr	   r   
get_fixers	pre_order
post_orderfilesbmBottomMatcherBMbmi_pre_orderbmi_post_orderr   BM_compatible	add_fixerr   rK   bmi_pre_order_headsbmi_post_order_heads)selffixer_namesr   r   rH   s        r   __init__zRefactoringTool.__init__   s   	 " B,1133L(((,1133<() 	.%g..\/* 	.%f-
 &*\%5%56M%N%N"'(9::
mDL,2N+/;8 8 8 +///*;*;' 
"$$ 4?DN;; 	2 	2E" 2!!%(((( $.(("))%0000$/))#**5111#5d6H#I#I $6t7J$K$K!!!r   c                    	 g }g }| j         D ]}t          |i i dg          }|                    dd          d         }|                    | j                  r|t          | j                  d          }|                    d          }| j        d                    d |D                       z   }	 t          ||          }n$# t          $ r t          d|d|          d w xY w || j        | j                  }	|	j        r*| j        d	ur!|| j        vr|                     d
|           !|                     d|           |	j        dk    r|                    |	           Y|	j        dk    r|                    |	           {t          d|	j        z            t'          j        d          }
|                    |
           |                    |
           ||fS )Nr   rO   r   _ c                 6    g | ]}|                                 S r&   )title)rP   r6   s     r   rS   z.RefactoringTool.get_fixers.<locals>.<listcomp>   s     5O5O5OAaggii5O5O5Or   zCan't find TzSkipping optional fixer: %szAdding transformation: %sprepostzIllegal fixer order: %r	run_orderkey)r   r   rsplitr   FILE_PREFIXlensplitCLASS_PREFIXjoingetattrAttributeErrorrx   r   r   r   log_message	log_debugorderr   operator
attrgettersort)r   pre_order_fixerspost_order_fixersfix_mod_pathmodrQ   parts
class_name	fix_classrH   key_funcs              r   r   zRefactoringTool.get_fixers   s'   	  K 	J 	JL\2rC599C#**32226H""4#344 <#C(8$9$9$:$:;NN3''E*RWW5O5O5O5O5O-P-PPJX#C44		! X X X jxxx!LMMSWWXIdlDN;;E~ $-t";"; 55  !>IIINN6AAA{e## ''....&&!((//// !:U[!HIII&{33(+++8,,, "344s   2C!C$c                      	  r9   r&   )r   msgargskwdss       r   	log_errorzRefactoringTool.log_error  s    *r   c                 J    	 |r||z  }| j                             |           d S r9   )r   infor   r   r   s      r   r   zRefactoringTool.log_message  s2    $ 	*Cr   c                 H    |r||z  }| j                             |           d S r9   )r   debugr   s      r   r   zRefactoringTool.log_debug  s/     	*C#r   c                     	 d S r9   r&   )r   old_textnew_textfilenameequals        r   print_outputzRefactoringTool.print_output  s    	r   c                     	 |D ]P}t           j                            |          r|                     |||           9|                     |||           Qd S r9   )ospathisdirrefactor_dirrefactor_file)r   itemswritedoctests_onlydir_or_files        r   refactorzRefactoringTool.refactor  so    7  	F 	FKw}}[)) F!!+umDDDD"";}EEEE		F 	Fr   c                    	 t           j        dz   }t          j        |          D ]\  }}}|                     d|           |                                 |                                 |D ]w}|                    d          s`t           j                            |          d         |k    r7t           j                            ||          }	| 	                    |	||           xd |D             |d d <   d S )NpyzDescending into %srO   r   c                 <    g | ]}|                     d           |S rN   )r   )rP   dns     r   rS   z0RefactoringTool.refactor_dir.<locals>.<listcomp>2  s)    KKK"c8J8JK2KKKr   )
r   extsepwalkr   r   r   r   splitextr   r   )
r   dir_namer   r   py_extdirpathdirnames	filenamesr   fullnames
             r   r   zRefactoringTool.refactor_dir   s   	 T!,.GH,=,= 
	L 
	L(GXyNN/999MMOOONN! G G,, GG$$T**1-77!w||GT::H&&xFFFKKKKKHQQQKK
	L 
	Lr   c                    	 	 t          |d          }n/# t          $ r"}|                     d||           Y d }~dS d }~ww xY w	 t          j        |j                  d         }|                                 n# |                                 w xY wt          j         |d|d          5 }|                                |fcd d d            S # 1 swxY w Y   d S )NrbzCan't open %s: %sNNr   r5   r   encodingnewline)	openOSErrorr   r   detect_encodingrf   closerd   read)r   r   ferrr   s        r   _read_python_sourcez#RefactoringTool._read_python_source4  s'   		Xt$$AA 	 	 	NN.#>>>:::::		/
;;A>HGGIIIIAGGIIIIWXsXrBBB 	&a6688X%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s0    
A ;A A8 8B)CCCc                    	 |                      |          \  }}|d S |dz  }|rl|                     d|           |                     ||          }| j        s||k    r|                     |||||           d S |                     d|           d S |                     ||          }| j        s	|r7|j        r0|                     t          |          d d         |||           d S |                     d|           d S )N
zRefactoring doctests in %szNo doctest changes in %sr   )r   r   zNo changes in %s)r   r   refactor_docstringr}   processed_filerefactor_stringwas_changedstr)r   r   r   r   inputr   outputtrees           r   r   zRefactoringTool.refactor_fileD  sC   228<<x=F 	=NN7BBB,,UH==F) EVu__##FHeUHMMMMM98DDDDD''x88D) =d =t7G =##CIIcrcNH*/( $ D D D D D 18<<<<<r   c                    	 t          |          }d|v rt          j        | j        _        	 | j                            |          }nK# t          $ r>}|                     d||j        j	        |           Y d }~| j        | j        _        d S d }~ww xY w	 | j        | j        _        n# | j        | j        _        w xY w||_
        |                     d|           |                     ||           |S )Nr{   zCan't parse %s: %s: %szRefactoring %s)rv   r
   !python_grammar_no_print_statementr   r   parse_stringr3   r   	__class__r#   future_featuresr   refactor_tree)r   datar   rs   r  r   s         r   r   zRefactoringTool.refactor_string[  s   		 +400x''"("JDK	/;++D11DD 	 	 	NN3!7> > >FFF"&,DK	  #',DK$,DK....''...4&&&s/   A B% 
B"B
3B% 
BB% %B8c                    t           j                                        }|rh|                     d           |                     |d          }| j        s||k    r|                     |d|           d S |                     d           d S |                     |d          }| j        s	|r-|j        r&|                     t          |          d|           d S |                     d           d S )NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin)
sysstdinr   r   r   r}   r   r   r   r   )r   r   r   r  r  s        r   refactor_stdinzRefactoringTool.refactor_stdinv  s    	   	6NN:;;;,,UI>>F) >Vu__##FIu=====<=====''y99D) 6d 6t7G 6##CIIy%@@@@@455555r   c                 
   	 t          | j        | j                  D ]}|                    ||           |                     | j        |                                           |                     | j        |                                           | j                            |	                                          }t          |                                          r| j        j        D ]}||v r||         r||                             t          j        j        d           |j        r+||                             t          j        j                   t'          ||                   D ]8}|||         v r||                             |           	 t+          |           n# t,          $ r Y Ew xY w|j        r
||j        v rZ|                    |          }|r|                    ||          }||                    |           |                                D ]*}|j        sg |_        |j                            |           +| j                            |	                                          }|D ],}	|	|vrg ||	<   ||	                             ||	                    -:t          |                                          t          | j        | j                  D ]}|                    ||           |j        S )NT)r   reverser   )r   r   r   
start_treetraverse_byr   r   r   runleavesanyrA   r   r   r	   Basedepthkeep_line_order
get_linenor<   remover   
ValueErrorfixers_appliedmatch	transformreplacer   rC   finish_treer   )
r   r  r   rH   	match_setnoderesultsnewnew_matchesfxrs
             r   r  zRefactoringTool.refactor_tree  s*   	 4>4?;; 	) 	)ET4(((( 	14>>3C3CDDD2DOO4E4EFFF GKK..	)""$$%% /	L .L .LI%%)E*:%e$))fk.?)NNN, J "%(--&+2H-III $Yu%5 6 6 $L $L9U#333%e,33D999%%dOOOO) % % % %H%
  . %5D<O3O3O$"'++d"3"3" L"'//$"@"@C" $S 1 1 1,/NN,<,< !F !FD ,0+> %A>@(;$($7$>$>u$E$E$E$E /3gkk#**,,.G.G+6 !L !LC+.)+;+;79	#$-cN$9$9+c:J$K$K$K$K_ )""$$%% /	Lb 4>4?;; 	* 	*EdD))))s   F&&
F32F3c                     	 |sd S |D ]X}||j                  D ]H}|                    |          }|r/|                    ||          }||                    |           |}IYd S r9   )r,   r  r  r  )r   r   	traversalr!  rH   r"  r#  s          r   r  zRefactoringTool.traverse_by  s    
	  	F 	# 	#D	* # #++d++ #//$88CS)))"#	# 	#r   c                 `   	 | j                             |           ||                     |          d         }|d S ||k    }|                     ||||           |r|                     d|           | j        sd S |r|                     ||||           d S |                     d|           d S )Nr   zNo changes to %szNot writing changes to %s)r   r   r   r   r   r}   
write_file)r   r   r   r   r   r   r   s          r   r   zRefactoringTool.processed_file  s    	 	
(###//99!<HH$(Hh>>> 	NN-x888-  	BOOHh(CCCCCNN6AAAAAr   c                    	 	 t          j        |d|d          }n/# t          $ r"}|                     d||           Y d }~d S d }~ww xY w|5  	 |                    |           n.# t          $ r!}|                     d||           Y d }~nd }~ww xY wd d d            n# 1 swxY w Y   |                     d|           d| _        d S )Nwr   r   zCan't create %s: %szCan't write %s: %szWrote changes to %sT)rd   r   r   r   r   r   r   )r   r   r   r   r   fpr   s          r   r)  zRefactoringTool.write_file  s]   		32FFFBB 	 	 	NN0(C@@@FFFFF	  	D 	DD"""" D D D3XsCCCCCCCCD	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D
 	,h777


sP    
AAABA%$B%
B/BBBBB#&B#z>>> z... c           	         	 g }d }d }d }d}|                     d          D ])}|dz  }|                                                    | j                  rW|+|                    |                     ||||                     |}|g}|                    | j                  }	|d |	         }|V|                    || j        z             s#||| j                                        z   dz   k    r|	                    |           |+|                    |                     ||||                     d }d }|	                    |           +|+|                    |                     ||||                     d
                    |          S )Nr   Tkeependsr   r   r   )
splitlineslstripr   PS1rC   refactor_doctestfindPS2rstripr   r   )
r   r   r   resultblockblock_linenoindentlinenolineis
             r   r   z"RefactoringTool.refactor_docstring  s   
	 $$d$33 	$ 	$DaKF{{}}''11 $$MM$"7"7|8>#J #J K K K%IIdh''bqb$??6DH#455 %6DHOO$5$55<<<T""""$MM$"7"7|8>#J #J K K Kd####MM$//|06B B C C Cwwvr   c                     	 	                       ||          }n# t          $ r} j                            t          j                  r.|D ]+}                     d|                    d                     ,                     d|||j	        j
        |           |cY d }~S d }~ww xY w                     ||          rt          |                              d          }|d |dz
           ||dz
  d          }}	|d                             d          s|dxx         dz  cc<    j        z   |                    d          z   g}|r| fd	|D             z  }|S )
Nz
Source: %sr   z+Can't parse docstring in %s line %s: %s: %sTr.  r   r   r   c                 *    g | ]}j         z   |z   S r&   )r5  )rP   r<  r:  r   s     r   rS   z4RefactoringTool.refactor_doctest.<locals>.<listcomp>^  s%    CCCt&48+d2CCCr   )parse_blockr3   r   isEnabledForr   DEBUGr   r6  r   r  r#   r  r   r0  endswithr2  pop)
r   r8  r;  r:  r   r  r   r<  r#  clippeds
   `  `      r   r3  z RefactoringTool.refactor_doctestD  s   		##E66::DD 	 	 	{''66 D! D DDNN<T1B1BCCCCNNH#VS]-CSJ J JLLLLLL	 dH-- 		Dd))&&&55Cyqy>3vaxyy>SGr7##D))  B4dh&34E DCCCCCsCCCCs    
B(A6B#B(#B(c                 6   | j         rd}nd}| j        s|                     d|           n5|                     d|           | j        D ]}|                     |           | j        r4|                     d           | j        D ]}|                     |           | j        rut          | j                  dk    r|                     d           n(|                     dt          | j                             | j        D ]\  }}} | j        |g|R i | d S d S )	Nwerez
need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:r   zThere was 1 error:zThere were %d errors:)r   r   r   r   r   r   )r   rG  filemessager   r   r   s          r   	summarizezRefactoringTool.summarizea  sk   : 	 DDDz 	'4d;;;;6===
 ' '  &&&&> 	*CDDD> * *  )))); 	54;1$$  !56666  !8#dk:J:JKKK#'; 5 5T4  4t444t4444	5 	5
5 5r   c                     	 | j                             |                     |||                    }t                      |_        |S r9   )r   parse_tokens	wrap_toksrg   r  )r   r8  r;  r:  r  s        r   r@  zRefactoringTool.parse_blockx  s?    	
 {''uff(M(MNN({{r   c              #      K   	 t          j        |                     ||          j                  }|D ]+\  }}\  }}\  }	}
}||dz
  z  }|	|dz
  z  }	||||f|	|
f|fV  ,d S )Nr   )r   rc   	gen_lines__next__)r   r8  r;  r:  rB   r,   ru   line0col0line1col1	line_texts               r   rM  zRefactoringTool.wrap_toks  s      I)$..*G*G*PQQDJ 	G 	G@D%%yVaZEVaZE t}udmYFFFFF	G 	Gr   c              #     K   	 || j         z   }|| j        z   }|}|D ]h}|                    |          r|t          |          d          V  n5||                                dz   k    rdV  nt          d|d|          |}i	 dV  )Nr   zline=z	, prefix=Tr   )r2  r5  r   r   r6  AssertionError)r   r8  r:  prefix1prefix2prefixr<  s          r   rO  zRefactoringTool.gen_lines  s      	 48#48# 	 	Dv&& L3v;;<<(((((4///



$nTTT66%JKKKFF	HHH	r   r   )FF)F)NFNr9   )r#   r$   r%   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r)  r2  r5  r   r3  rJ  r@  rM  rO  r&   r   r   rz   rz      s       +0).279 9 LK3L 3L 3L 3Ln&5 &5 &5P      
  
F F F FL L L L(& & & = = = =.  66 6 6 6 M  M  M ^# # #. GL $B B B B*   * C
C) ) )V  :5 5 5.  G G G    r   rz   c                       e Zd ZdS )MultiprocessingUnsupportedNr"   r&   r   r   r\  r\    r'   r   r\  c                   B     e Zd Z fdZ	 	 d fd	Z fdZ fdZ xZS )MultiprocessRefactoringToolc                 d     t          t          |           j        |i | d | _        d | _        d S r9   )superr^  r   queueoutput_lockr   r   kwargsr  s      r   r   z$MultiprocessRefactoringTool.__init__  s;    9)40094J6JJJ
r   Fr   c                     |dk    r*t          t                                         |||          S 	 dd ln# t          $ r t
          w xY w j        t          d                                           _        	                                 _
         fdt          |          D             }	 |D ]}|                                 t          t                                         |||            j                                         t          |          D ]} j                            d            |D ]*}|                                r|                                 +d  _        d S #  j                                         t          |          D ]} j                            d            |D ]*}|                                r|                                 +d  _        w xY w)Nr   r   z already doing multiple processesc                 F    g | ]}                     j                   S ))target)Process_child)rP   r=  multiprocessingr   s     r   rS   z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>  s<     4 4 4 %,,DK,@@ 4 4 4r   )r`  r^  r   rj  ImportErrorr\  ra  RuntimeErrorJoinableQueueLockrb  rangestartr   putis_alive)
r   r   r   r   num_processes	processesr6   r=  rj  r  s
   `       @r   r   z$MultiprocessRefactoringTool.refactor  s/   A4d;;DDum- - -	-""""" 	- 	- 	-,,	-:!ABBB$2244
*//114 4 4 4 4#M224 4 4		  				-t44==eU>KM M M JOO=)) % %
t$$$$  ::<< FFHHHDJJJ JOO=)) % %
t$$$$  ::<< FFHHHDJs   : A4AE2 2A;G-c                 4   | j                                         }|{|\  }}	  t          t          |           j        |i | | j                                          n# | j                                          w xY w| j                                         }|yd S d S r9   )ra  r   r`  r^  r   	task_done)r   taskr   rd  r  s       r   ri  z"MultiprocessRefactoringTool._child  s    z~~LD&'F1488F%#% % % 
$$&&&&
$$&&&&:>>##D s    A A8c                     | j         | j                             ||f           d S  t          t          |           j        |i |S r9   )ra  rq  r`  r^  r   rc  s      r   r   z)MultiprocessRefactoringTool.refactor_file  sV    :!JNND&>*****I54d;;I!! ! !r   )FFr   )r#   r$   r%   r   r   ri  r   __classcell__)r  s   @r   r^  r^    s                 
 :?      :	$ 	$ 	$ 	$ 	$! ! ! ! ! ! ! ! !r   r^  )T)"
__author__rd   r   r   r  r   r   r:   	itertoolsr   pgen2r   r   r   
fixer_utilr   r   r	   r
   r   r   r   r3   r!   r/   rK   rT   rW   rv   rx   objectrz   r\  r^  r&   r   r   <module>r     s   3
 
			 				  



             + * * * * * * * * * ! ! ! ! ! !              	 	 	 		 	 	 	 	 	 	 	C C C8  2@ @ @  % % %P' ' ' ' ' ' ' 'F F F F Ff F F FR	 	 	 	 	 	 	 	4! 4! 4! 4! 4!/ 4! 4! 4! 4! 4!r   