a
    ´isk  ã                   @   s  d Z 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d„ZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd „ d e ƒZ"dS )"zÛRefactoring framework.

Used as a main program, this can refactor any number of files and/or
recursively descend down directories.  Imported as a module, this
provides infrastructure to write your own refactoring tool.
z#Guido van Rossum <guido@python.org>é    N)Úchainé   )ÚdriverÚtokenizeÚtoken)Ú	find_root)ÚpytreeÚpygram)Úbtm_matcherTc                 C   sT   t | g g dgƒ}g }t |j¡D ].\}}}| d¡r |rD|dd… }| |¡ q |S )zEReturn a sorted list of all available fix names in the given package.Ú*Úfix_é   N)Ú
__import__ÚpkgutilÚiter_modulesÚ__path__Ú
startswithÚappend)Z	fixer_pkgZremove_prefixÚpkgZ	fix_namesÚfinderÚnameÚispkg© r   ú(/usr/lib64/python3.9/lib2to3/refactor.pyÚget_all_fix_names   s    
r   c                   @   s   e Zd ZdS )Ú
_EveryNodeN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   +   s   r   c                 C   s’   t | tjtjfƒr(| jdu r t‚| jhS t | tjƒrH| jrDt| jƒS t‚t | tj	ƒr‚t
ƒ }| jD ]}|D ]}| t|ƒ¡ qhq`|S td|  ƒ‚dS )zf Accepts a pytree Pattern Node and returns a set
        of the pattern types which will match first. Nz$Oh no! I don't understand pattern %s)Ú
isinstancer   ZNodePatternZLeafPatternÚtyper   ZNegatedPatternZcontentÚ_get_head_typesZWildcardPatternÚsetÚupdateÚ	Exception)ÚpatÚrÚpÚxr   r   r   r"   /   s    


r"   c              	   C   s¼   t  t¡}g }| D ]v}|jrbzt|jƒ}W n tyF   | |¡ Y qˆ0 |D ]}||  |¡ qLq|jdur~||j  |¡ q| |¡ qtt	j
j ¡ t	j
jƒD ]}||  |¡ q t|ƒS )z^ Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  N)ÚcollectionsÚdefaultdictÚlistÚpatternr"   r   r   Z_accept_typer   r	   Úpython_grammarZsymbol2numberÚvaluesÚtokensÚextendÚdict)Z
fixer_listZ
head_nodesZeveryÚfixerZheadsZ	node_typer   r   r   Ú_get_headnode_dictK   s$    

ÿr4   c                    s   ‡ fdd„t ˆ dƒD ƒS )zN
    Return the fully qualified names for fixers in the package pkg_name.
    c                    s   g | ]}ˆ d  | ‘qS ©Ú.r   )Ú.0Úfix_name©Úpkg_namer   r   Ú
<listcomp>h   s   ÿz+get_fixers_from_package.<locals>.<listcomp>F)r   r9   r   r9   r   Úget_fixers_from_packaged   s    
ÿr<   c                 C   s   | S ©Nr   )Úobjr   r   r   Ú	_identityk   s    r?   c                    sV  d}t  t | ¡j¡‰ ‡ fdd„}ttjt jtj	hƒ}t
ƒ }zü|ƒ \}}||v rTq>q>|tjkrl|rfq6d}q>|tjkr6|dkr6|ƒ \}}|tjksž|dkr¢q6|ƒ \}}|tjks¾|dkrÂq6|ƒ \}}|tjkrè|dkrè|ƒ \}}|tjkr4| |¡ |ƒ \}}|tjks.|d	kr"q4|ƒ \}}qèq>q6q>W n tyL   Y n0 t|ƒS )
NFc                     s   t ˆ ƒ} | d | d fS )Nr   r   )Únext)Útok©Úgenr   r   Úadvancer   s    z(_detect_future_features.<locals>.advanceTÚfromZ
__future__Úimportú(ú,)r   Úgenerate_tokensÚioÚStringIOÚreadlineÚ	frozensetr   ÚNEWLINEÚNLÚCOMMENTr#   ÚSTRINGÚNAMEÚOPÚaddÚStopIteration)ÚsourceZhave_docstringrD   ÚignoreÚfeaturesÚtpÚvaluer   rB   r   Ú_detect_future_featureso   sB    








r[   c                   @   s   e Zd ZdZdS )Ú
FixerErrorzA fixer could not be loaded.N)r   r   r   Ú__doc__r   r   r   r   r\   —   s   r\   c                   @   sæ   e Zd ZddddœZdZdZd4dd„Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zd5dd„Zd6dd„Zdd„ Zd7dd„Zdd„ Zd8dd„Zdd„ Zd d!„ Zd9d"d#„Zd:d$d%„Zd&Zd'Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ ZdS );ÚRefactoringToolF)Úprint_functionÚexec_functionÚwrite_unchanged_filesZFixr   Nc                 C   sJ  || _ |pg | _| j ¡ | _|dur0| j |¡ tj ¡ | _| jd rR| jj	d= n| jd rf| jj	d= | j 
d¡| _g | _t d¡| _g | _d| _tj| jtj| jd	| _|  ¡ \| _| _g | _t ¡ | _g | _g | _t| j| jƒD ]H}|j rü| j !|¡ qä|| jv r| j "|¡ qä|| jv rä| j "|¡ qät#| jƒ| _$t#| jƒ| _%dS )
zÑInitializer.

        Args:
            fixer_names: a list of fixers to import
            options: a dict with configuration.
            explicit: a list of fixers to run even if they are explicit.
        Nr_   Úprintr`   Úexecra   r^   F)ÚconvertÚlogger)&ÚfixersÚexplicitÚ_default_optionsÚcopyÚoptionsr$   r	   r.   ÚgrammarÚkeywordsÚgetra   ÚerrorsÚloggingÚ	getLoggerre   Ú	fixer_logÚwroter   ZDriverr   rd   Ú
get_fixersÚ	pre_orderÚ
post_orderÚfilesÚbmZBottomMatcherÚBMZbmi_pre_orderZbmi_post_orderr   ZBM_compatibleZ	add_fixerr   r4   Úbmi_pre_order_headsÚbmi_post_order_heads)ÚselfZfixer_namesrj   rg   r3   r   r   r   Ú__init__¤   sB    



þ

zRefactoringTool.__init__c              	   C   sV  g }g }| j D ]}t|i i dgƒ}| dd¡d }| | j¡rR|t| jƒd… }| d¡}| jd dd	„ |D ƒ¡ }zt	||ƒ}W n$ t
y¨   td
||f ƒd‚Y n0 || j| jƒ}	|	jrà| jdurà|| jvrà|  d|¡ q|  d|¡ |	jdkr| |	¡ q|	jdkr| |	¡ qtd|	j ƒ‚qt d¡}
|j|
d |j|
d ||fS )a  Inspects the options to load the requested patterns and handlers.

        Returns:
          (pre_order, post_order), where pre_order is the list of fixers that
          want a pre-order AST traversal, and post_order is the list that want
          post-order traversal.
        r   r6   r   éÿÿÿÿNÚ_Ú c                 S   s   g | ]}|  ¡ ‘qS r   )Útitle)r7   r(   r   r   r   r;   ë   ó    z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sÚpreZpostzIllegal fixer order: %rZ	run_order©Úkey)rf   r   Úrsplitr   ÚFILE_PREFIXÚlenÚsplitÚCLASS_PREFIXÚjoinÚgetattrÚAttributeErrorr\   rj   rq   rg   Úlog_messageÚ	log_debugÚorderr   ÚoperatorÚ
attrgetterÚsort)r{   Zpre_order_fixersZpost_order_fixersZfix_mod_pathÚmodr8   ÚpartsÚ
class_nameZ	fix_classr3   Zkey_funcr   r   r   rs   Û   s:    
ÿ
zRefactoringTool.get_fixersc                  O   s   ‚ dS )zCalled when an error occurs.Nr   )r{   ÚmsgÚargsÚkwdsr   r   r   Ú	log_error  s    zRefactoringTool.log_errorc                 G   s   |r|| }| j  |¡ dS )zHook to log a message.N)re   Úinfo©r{   r–   r—   r   r   r   r     s    zRefactoringTool.log_messagec                 G   s   |r|| }| j  |¡ d S r=   )re   Údebugr›   r   r   r   rŽ     s    zRefactoringTool.log_debugc                 C   s   dS )zTCalled with the old version, new version, and filename of a
        refactored file.Nr   )r{   Úold_textÚnew_textÚfilenameÚequalr   r   r   Úprint_output  s    zRefactoringTool.print_outputc                 C   s8   |D ].}t j |¡r$|  |||¡ q|  |||¡ qdS )z)Refactor a list of files and directories.N)ÚosÚpathÚisdirÚrefactor_dirÚrefactor_file)r{   ÚitemsÚwriteÚdoctests_onlyZdir_or_filer   r   r   Úrefactor  s    zRefactoringTool.refactorc           
      C   sš   t jd }t  |¡D ]€\}}}|  d|¡ | ¡  | ¡  |D ]>}| d¡s>t j |¡d |kr>t j ||¡}	|  	|	||¡ q>dd„ |D ƒ|dd…< qdS )zÄDescends down a directory and refactor every Python file found.

        Python files are assumed to have a .py extension.

        Files and subdirectories starting with '.' are skipped.
        ÚpyzDescending into %sr6   r   c                 S   s   g | ]}|  d ¡s|‘qS r5   )r   )r7   Zdnr   r   r   r;   2  r   z0RefactoringTool.refactor_dir.<locals>.<listcomp>N)
r¢   ÚextsepÚwalkrŽ   r’   r   r£   ÚsplitextrŠ   r¦   )
r{   Zdir_namer¨   r©   Zpy_extÚdirpathÚdirnamesÚ	filenamesr   Úfullnamer   r   r   r¥      s    

ÿzRefactoringTool.refactor_dirc              
   C   s®   zt |dƒ}W n4 tyB } z|  d||¡ W Y d}~dS d}~0 0 zt |j¡d }W | ¡  n
| ¡  0 tj |d|dd}| ¡ |fW  d  ƒ S 1 s 0    Y  dS )	zG
        Do our best to decode a Python source file correctly.
        ÚrbzCan't open %s: %sN)NNr   r'   r   ©ÚencodingÚnewline)	ÚopenÚOSErrorr™   r   Údetect_encodingrL   ÚcloserJ   Úread)r{   rŸ   ÚfÚerrrµ   r   r   r   Ú_read_python_source4  s    z#RefactoringTool._read_python_sourcec                 C   sº   |   |¡\}}|du rdS |d7 }|rn|  d|¡ |  ||¡}| jsL||kr`|  |||||¡ q¶|  d|¡ nH|  ||¡}| jsŠ|rª|jrª| jt|ƒdd… |||d n|  d|¡ dS )zRefactors a file.NÚ
zRefactoring doctests in %szNo doctest changes in %sr}   )r¨   rµ   zNo changes in %s)r¾   rŽ   Úrefactor_docstringra   Úprocessed_fileÚrefactor_stringÚwas_changedÚstr)r{   rŸ   r¨   r©   Úinputrµ   ÚoutputÚtreer   r   r   r¦   D  s     ÿzRefactoringTool.refactor_filec              
   C   s°   t |ƒ}d|v rtj| j_zfz| j |¡}W nF tyr } z.|  d||jj	|¡ W Y d}~W | j| j_dS d}~0 0 W | j| j_n| j| j_0 ||_
|  d|¡ |  ||¡ |S )aF  Refactor a given input string.

        Args:
            data: a string holding the code to be refactored.
            name: a human-readable name for use in error/log messages.

        Returns:
            An AST corresponding to the refactored input stream; None if
            there were errors during the parse.
        r_   zCan't parse %s: %s: %sNzRefactoring %s)r[   r	   Z!python_grammar_no_print_statementr   rk   Zparse_stringr%   r™   Ú	__class__r   Úfuture_featuresrŽ   Úrefactor_tree)r{   Údatar   rX   rÇ   r½   r   r   r   rÂ   [  s"    

ÿ
þzRefactoringTool.refactor_stringc                 C   sŒ   t j ¡ }|rN|  d¡ |  |d¡}| js2||krB|  |d|¡ qˆ|  d¡ n:|  |d¡}| jsj|r~|jr~|  t	|ƒd|¡ n
|  d¡ d S )NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin)
ÚsysÚstdinr»   rŽ   rÀ   ra   rÁ   rÂ   rÃ   rÄ   )r{   r©   rÅ   rÆ   rÇ   r   r   r   Úrefactor_stdinv  s    

zRefactoringTool.refactor_stdinc           
   
   C   sÚ  t | j| jƒD ]}| ||¡ q|  | j| ¡ ¡ |  | j| ¡ ¡ | j | 	¡ ¡}t
| ¡ ƒr²| jjD ]B}||v rj|| rj|| jtjjdd |jr°|| jtjjd t|| ƒD ]ð}||| v rÚ||  |¡ zt|ƒ W n t yþ   Y q¼Y n0 |jr||jv rq¼| |¡}|r¼| ||¡}|dur¼| |¡ | ¡ D ] }|js\g |_|j |¡ qJ| j | 	¡ ¡}|D ]*}	|	|vr–g ||	< ||	  ||	 ¡ q€q¼qjqTt | j| jƒD ]}| ||¡ qÀ|jS )aÏ  Refactors a parse tree (modifying the tree in place).

        For compatible patterns the bottom matcher module is
        used. Otherwise the tree is traversed node-to-node for
        matches.

        Args:
            tree: a pytree.Node instance representing the root of the tree
                  to be refactored.
            name: a human-readable name for this tree.

        Returns:
            True if the tree was modified, False otherwise.
        T)r„   Úreverserƒ   N)r   rt   ru   Z
start_treeÚtraverse_byry   rz   rx   ÚrunZleavesÚanyr/   rf   r’   r   ZBaseÚdepthZkeep_line_orderZ
get_linenor,   Úremover   Ú
ValueErrorZfixers_appliedÚmatchÚ	transformÚreplacer   r1   Zfinish_treerÃ   )
r{   rÇ   r   r3   Z	match_setÚnodeÚresultsÚnewZnew_matchesZfxrr   r   r   rÊ   †  sJ    



zRefactoringTool.refactor_treec                 C   sV   |sdS |D ]D}||j  D ]4}| |¡}|r| ||¡}|dur| |¡ |}qqdS )a  Traverse an AST, applying a set of fixers to each node.

        This is a helper method for refactor_tree().

        Args:
            fixers: a list of fixer instances.
            traversal: a generator that yields AST nodes.

        Returns:
            None
        N)r!   rÖ   r×   rØ   )r{   rf   Z	traversalrÙ   r3   rÚ   rÛ   r   r   r   rÐ   Õ  s    

zRefactoringTool.traverse_byc                 C   s†   | j  |¡ |du r.|  |¡d }|du r.dS ||k}|  ||||¡ |r`|  d|¡ | js`dS |rv|  ||||¡ n|  d|¡ dS )zR
        Called when a file has been refactored and there may be changes.
        Nr   zNo changes to %szNot writing changes to %s)rv   r   r¾   r¡   rŽ   ra   Ú
write_file)r{   rž   rŸ   r   r¨   rµ   r    r   r   r   rÁ   ì  s    zRefactoringTool.processed_filec                 C   sÈ   zt j|d|dd}W n4 tyJ } z|  d||¡ W Y d}~dS d}~0 0 |R z| |¡ W n2 ty’ } z|  d||¡ W Y d}~n
d}~0 0 W d  ƒ n1 s¨0    Y  |  d|¡ d| _dS )	zÑWrites a string to a file.

        It first shows a unified diff between the old text and the new text, and
        then rewrites the file; the latter is only done if the write option is
        set.
        Úwr   r´   zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rJ   r·   r¸   r™   r¨   rŽ   rr   )r{   rž   rŸ   r   rµ   Úfpr½   r   r   r   rÜ     s    BzRefactoringTool.write_filez>>> z... c           
   	   C   s  g }d}d}d}d}|j ddD ]È}|d7 }| ¡  | j¡r~|durZ| |  ||||¡¡ |}|g}| | j¡}	|d|	… }q |dur¸| || j ¡s¬||| j ¡  d kr¸| 	|¡ q |durÖ| |  ||||¡¡ d}d}| 	|¡ q |dur
| |  ||||¡¡ d 
|¡S )aË  Refactors a docstring, looking for doctests.

        This returns a modified version of the input string.  It looks
        for doctests, which start with a ">>>" prompt, and may be
        continued with "..." prompts, as long as the "..." is indented
        the same as the ">>>".

        (Unfortunately we can't use the doctest module's parser,
        since, like most parsers, it is not geared towards preserving
        the original source.)
        Nr   T©Úkeependsr   r¿   r   )Ú
splitlinesÚlstripr   ÚPS1r1   Úrefactor_doctestÚfindÚPS2Úrstripr   rŠ   )
r{   rÅ   rŸ   ÚresultÚblockZblock_linenoÚindentÚlinenoÚlineÚir   r   r   rÀ     sD    ÿÿþÿ
ÿz"RefactoringTool.refactor_docstringc           
   
      s.  zˆ  ||ˆ ¡}W nh tyz } zPˆj tj¡rL|D ]}ˆ d| d¡¡ q4ˆ d|||j	j
|¡ |W  Y d}~S d}~0 0 ˆ ||¡r*t|ƒjdd}|d|d … ||d d…  }	}|	dg|d  ksÖJ |	ƒ‚|d  d¡ sö|d  d7  < ˆ ˆj | d	¡ g}|r*|‡ ‡fd
d„|D ƒ7 }|S )zÞRefactors one doctest.

        A doctest is given as a block of lines, the first of which starts
        with ">>>" (possibly indented), while the remaining lines start
        with "..." (identically indented).

        z
Source: %sr¿   z+Can't parse docstring in %s line %s: %s: %sNTrß   r   r}   r   c                    s   g | ]}ˆ ˆj  | ‘qS r   )ræ   )r7   rì   ©rê   r{   r   r   r;   ^  r   z4RefactoringTool.refactor_doctest.<locals>.<listcomp>)Úparse_blockr%   re   ÚisEnabledForro   ÚDEBUGrŽ   rç   r™   rÈ   r   rÊ   rÄ   rá   Úendswithrã   Úpop)
r{   ré   rë   rê   rŸ   rÇ   r½   rì   rÛ   Zclippedr   rî   r   rä   D  s(    ÿ"z RefactoringTool.refactor_doctestc                 C   sÌ   | j rd}nd}| js$|  d|¡ n"|  d|¡ | jD ]}|  |¡ q6| jrl|  d¡ | jD ]}|  |¡ q\| jrÈt| jƒdkrŒ|  d¡ n|  dt| jƒ¡ | jD ]"\}}}| j|g|¢R i |¤Ž q¤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:)rr   rv   r   rq   rn   r‡   )r{   rô   ÚfileÚmessager–   r—   r˜   r   r   r   Ú	summarizea  s$    


zRefactoringTool.summarizec                 C   s"   | j  |  |||¡¡}tƒ |_|S )z³Parses a block into a tree.

        This is necessary to get correct line number / offset information
        in the parser diagnostics and embedded into the parse tree.
        )r   Zparse_tokensÚ	wrap_toksrM   rÉ   )r{   ré   rë   rê   rÇ   r   r   r   rï   x  s    zRefactoringTool.parse_blockc                 c   sd   t  |  ||¡j¡}|D ]F\}}\}}\}	}
}||d 7 }|	|d 7 }	||||f|	|
f|fV  qdS )z;Wraps a tokenize stream to systematically modify start/end.r   N)r   rI   Ú	gen_linesÚ__next__)r{   ré   rë   rê   r0   r!   rZ   Zline0Zcol0Zline1Zcol1Z	line_textr   r   r   rø   ‚  s
    zRefactoringTool.wrap_toksc                 c   sx   || j  }|| j }|}|D ]N}| |¡r>|t|ƒd… V  n(|| ¡ d krVdV  ntd||f ƒ‚|}qdV  qldS )z–Generates lines as expected by tokenize from a list of lines.

        This strips the first len(indent + self.PS1) characters off each line.
        Nr¿   zline=%r, prefix=%rr   )rã   ræ   r   r‡   rç   ÚAssertionError)r{   ré   rê   Úprefix1Zprefix2Úprefixrì   r   r   r   rù     s    


zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)r   r   r   rh   r‰   r†   r|   rs   r™   r   rŽ   r¡   rª   r¥   r¾   r¦   rÂ   rÎ   rÊ   rÐ   rÁ   rÜ   rã   ræ   rÀ   rä   r÷   rï   rø   rù   r   r   r   r   r^   ›   s@   þ
7(
	


O  ÿ

+
r^   c                   @   s   e Zd ZdS )ÚMultiprocessingUnsupportedNr   r   r   r   r   rþ   ¤  s   rþ   c                       sB   e Zd Z‡ fdd„Zd‡ fdd„	Z‡ fdd„Z‡ fd	d
„Z‡  ZS )ÚMultiprocessRefactoringToolc                    s&   t t| ƒj|i |¤Ž d | _d | _d S r=   )Úsuperrÿ   r|   ÚqueueÚoutput_lock©r{   r—   Úkwargs©rÈ   r   r   r|   ª  s    z$MultiprocessRefactoringTool.__init__Fr   c              
      s>  |dkrt tˆƒ |||¡S zdd l‰ W n ty>   t‚Y n0 ˆjd urRtdƒ‚ˆ  ¡ ˆ_ˆ  	¡ ˆ_
‡ ‡fdd„t|ƒD ƒ}zn|D ]}| ¡  q„t tˆƒ |||¡ W ˆj ¡  t|ƒD ]}ˆj d ¡ qº|D ]}| ¡ rÐ| ¡  qÐd ˆ_nLˆj ¡  t|ƒD ]}ˆj d ¡ q |D ]}| ¡ r| ¡  qd ˆ_0 d S )Nr   r   z already doing multiple processesc                    s   g | ]}ˆ j ˆjd ‘qS ))Útarget)ÚProcessÚ_child)r7   rí   ©Úmultiprocessingr{   r   r   r;   ¼  s   ÿz8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r   rÿ   rª   r
  ÚImportErrorrþ   r  ÚRuntimeErrorÚJoinableQueueÚLockr  ÚrangeÚstartrŠ   ÚputÚis_alive)r{   r§   r¨   r©   Znum_processesÚ	processesr(   rí   r  r	  r   rª   ¯  sF    
ÿ



ÿ
ÿ

ú

z$MultiprocessRefactoringTool.refactorc                    s\   | j  ¡ }|d urX|\}}z$tt| ƒj|i |¤Ž W | j  ¡  n| j  ¡  0 | j  ¡ }q
d S r=   )r  rm   r   rÿ   r¦   Ú	task_done)r{   Ztaskr—   r  r  r   r   r  Ì  s    

ÿÿz"MultiprocessRefactoringTool._childc                    s6   | j d ur| j  ||f¡ ntt| ƒj|i |¤ŽS d S r=   )r  r  r   rÿ   r¦   r  r  r   r   r¦   ×  s    

ÿÿz)MultiprocessRefactoringTool.refactor_file)FFr   )r   r   r   r|   rª   r  r¦   Ú__classcell__r   r   r  r   rÿ   ¨  s     ÿrÿ   )T)#r]   Ú
__author__rJ   r¢   r   rÌ   ro   r   r*   Ú	itertoolsr   Zpgen2r   r   r   Z
fixer_utilr   r   r   r	   r
   rw   r   r%   r   r"   r4   r<   r?   r[   r\   Úobjectr^   rþ   rÿ   r   r   r   r   Ú<module>   s8   
(    