£Á°èZ¨Ä…–K§‚«“ô4“ÒÙ´dîfUÙÃÅ WKbyÊ¦•êŽ…È®FÒ¿ÊÎóCozá¬S@6{Í:›œêZÌ:Š•_%:¢¾¾~;‘Ã~èŠ©ÊÇí`ÔÑ©úë™µ'5I¿fš×WO%ø9¾«¾DK|€ùÍD”Ýs]nHÕ¶ê×Ó¼ãžªéUWŸÈË%DÒÕ¬ï‘]/Åcx  ‰ï2ß]ä6G[]S£ÔÏ¯rs{úëóµmÒï#UQxo·õÞCe]"±/aÙ&Eã4ú9Jé_ÞåëdãöKë)AÞ                  ¯¹ægƒÛowÐø^d™ý½ßB7áyMä9ÜÖUã
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<html>
B
     fk                 @   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 )"zRefactoring 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   sX   t | g g dg}g }x>t|jD ].\}}}|dr"|rF|dd }|| q"W |S )zEReturn a sorted list of all available fix names in the given package.*fix_   N)
__import__pkgutilZiter_modules__path__
startswithappend)Z	fixer_pkgZremove_prefixZpkgZ	fix_namesfindernameZispkg r   5/opt/alt/python37/lib64/python3.7/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kr t| jhS t | tjrH| jrDt| jS tt | tj	rt
 }x*| jD ] }x|D ]}|t| qlW qbW |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)Zpatrpxr   r   r   r   /   s    


r   c          	   C   s   t t}g }x| D ]|}|jrjyt|j}W n tk
rJ   || Y qX xB|D ]}|| | qRW q|jdk	r||j | q|| qW x,tt	j
j t	j
jD ]}|| | qW 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"    



r/   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   )r3   r   )r3   r   get_fixers_from_packaged   s    
r5   c             C   s   | S )Nr   )objr   r   r   	_identityk   s    r7   c                sV  d}t t| j  fdd}ttjt jtj	h}t
 }yx| \}}||krVq@q@|tjkrl|rfP d}q@|tjkr.|dkr.| \}}|tjks|dkrP | \}}|tjks|dkrP | \}}|tjkr|dkr| \}}xJ|tjkr*|| | \}}|tjks|d	krP | \}}qW q@P q@W W n tk
rL   Y nX 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_docstringr;   ignorefeaturestpvaluer   )r:   r   _detect_future_featureso   sD    








rR   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   rS      s   rS   c               @   s   e Zd Z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write_unchanged_filesZFixr   Nc             C   s2  || _ |pg | _| j | _|dk	r0| j| | jd rDtj| _ntj	| _| j
d| _g | _td| _g | _d| _tj| jtj| jd| _|  \| _| _g | _t | _g | _g | _xXt| j| jD ]F}|j r| j!| q|| jkr| j"| q|| jkr| j"| qW t#| j| _$t#| j| _%dS )zInitializer.

        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.
        NrV   rW   rU   F)convertlogger)&fixersexplicit_default_optionscopyoptionsr    r	   !python_grammar_no_print_statementgrammarr)   getrW   errorsloggingZ	getLoggerrY   	fixer_logwroter   ZDriverr   rX   
get_fixers	pre_order
post_orderfilesbmZBottomMatcherBMZbmi_pre_orderZbmi_post_orderr   ZBM_compatibleZ	add_fixerr   r/   bmi_pre_order_headsbmi_post_order_heads)selfZfixer_namesr^   r[   r.   r   r   r   __init__   s<    





zRefactoringTool.__init__c          	   C   s^  g }g }x(| j D ]}t|i i dg}|ddd }|| jrV|t| jd }|d}| jddd	 |D  }yt	||}W n& t
k
r   td
||f dY nX || j| j}	|	jr| jdk	r|| jkr| d| q| d| |	jdkr
||	 q|	jdkr"||	 qtd|	j qW 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   r0   r   N_ c             S   s   g | ]}|  qS r   )title)r1   r#   r   r   r   r4      s    z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ	run_order)key)rZ   r   rsplitr   FILE_PREFIXlensplitCLASS_PREFIXjoingetattrAttributeErrorrS   r^   rd   r[   log_message	log_debugorderr   operator
attrgettersort)rn   Zpre_order_fixersZpost_order_fixersZfix_mod_pathmodr2   parts
class_nameZ	fix_classr.   Zkey_funcr   r   r   rf      s8    


zRefactoringTool.get_fixersc              O   s    dS )zCalled when an error occurs.Nr   )rn   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)rY   info)rn   r   r   r   r   r   r}     s    zRefactoringTool.log_messagec             G   s   |r|| }| j | d S )N)rY   debug)rn   r   r   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   )rn   old_textnew_textfilenameequalr   r   r   print_output  s    zRefactoringTool.print_outputc             C   s<   x6|D ].}t j|r&| ||| q| ||| qW dS )z)Refactor a list of files and directories.N)ospathisdirrefactor_dirrefactor_file)rn   itemswritedoctests_onlyZdir_or_filer   r   r   refactor  s    
zRefactoringTool.refactorc       
      C   s   t jd }xt |D ]\}}}| d| |  |  xF|D ]>}|dsBt j|d |krBt j||}	| 	|	|| qBW dd |D |dd< qW dS )zDescends 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 %sr0   r   c             S   s   g | ]}| d s|qS )r0   )r   )r1   Zdnr   r   r   r4   .  s    z0RefactoringTool.refactor_dir.<locals>.<listcomp>N)
r   extsepwalkr~   r   r   r   splitextrz   r   )
rn   Zdir_namer   r   Zpy_extdirpathZdirnames	filenamesr   fullnamer   r   r   r     s    


zRefactoringTool.refactor_dirc          
   C   s   yt |d}W n0 tk
r> } z| d|| dS d}~X Y nX zt|jd }W d|  X tj |d|dd}| |fS Q R X dS )	zG
        Do our best to decode a Python source file correctly.
        rbzCan't open %s: %s)NNNr   r"   rr   )encodingnewline)	openOSErrorr   r   detect_encodingrC   closerA   read)rn   r   ferrr   r   r   r   _read_python_source0  s    
z#RefactoringTool._read_python_sourcec             C   s   |  |\}}|dk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 %srp   )r   r   zNo changes in %s)r   r~   refactor_docstringrW   processed_filerefactor_stringwas_changedstr)rn   r   r   r   inputr   outputtreer   r   r   r   @  s    zRefactoringTool.refactor_filec          
   C   s   t |}d|krtj| j_zLy| j|}W n6 tk
rb } z| d||jj	| dS d}~X Y nX W d| j| j_X ||_
| 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.
        rV   zCan't parse %s: %s: %sNzRefactoring %s)rR   r	   r_   r   r`   Zparse_stringr!   r   	__class__r   future_featuresr~   refactor_tree)rn   datar   rO   r   r   r   r   r   r   W  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   rW   r   r   r   r   )rn   r   r   r   r   r   r   r   refactor_stdinr  s    

zRefactoringTool.refactor_stdinc       
   
   C   s  x"t | j| jD ]}||| qW | | j|  | | j|  | j|	 }xtt
| rΐx^| jjD ]P}||krv|| rv|| jtjjdd |jr|| jtjjd xt|| D ]}||| kr|| | yt| W n tk
r   wY nX |jr&||jkr&q||}|r|||}|dk	r|| x,| D ] }|jsng |_|j| q\W | j|	 }x2|D ]*}	|	|krg ||	< ||	 ||	  qW qW qvW q\W x$t | j| jD ]}||| qW |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)rt   reverse)rt   N)r   rg   rh   Z
start_treetraverse_byrl   rm   rk   runZleavesanyr*   rZ   r   r   ZBaseZdepthZkeep_line_orderZ
get_linenor'   remover   
ValueErrorZfixers_appliedmatch	transformreplacer   r,   Zfinish_treer   )
rn   r   r   r.   Z	match_setnoderesultsnewZnew_matchesZfxrr   r   r   r     sJ    



$zRefactoringTool.refactor_treec             C   s^   |sdS xP|D ]H}xB||j  D ]4}||}|r|||}|dk	r|| |}qW qW 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   )rn   rZ   Z	traversalr   r.   r   r   r   r   r   r     s    


zRefactoringTool.traverse_byc             C   s   | j | |dkr.| |d }|dk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)ri   r   r   r   r~   rW   
write_file)rn   r   r   r   r   r   r   r   r   r   r     s    zRefactoringTool.processed_filec             C   s   yt j|d|dd}W n0 tk
rF } z| d|| dS d}~X Y nX |F y|| W n0 tk
r } z| d|| W dd}~X Y nX W dQ R X | d| d| _dS )	zWrites 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.
        wrr   )r   r   zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rA   r   r   r   r   r~   re   )rn   r   r   r   r   fpr   r   r   r   r     s    *zRefactoringTool.write_filez>>> z... c       
   	   C   s  g }d}d}d}d}x|j ddD ]}|d7 }| | jr|dk	r\|| |||| |}|g}|| j}	|d|	 }q"|dk	r||| j s||| j  d kr|	| q"|dk	r|| |||| d}d}|	| q"W |dk	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   rr   )
splitlineslstripr   PS1r,   refactor_doctestfindPS2rstripr   rz   )
rn   r   r   resultblockZblock_linenoindentlinenolineir   r   r   r     s:    



z"RefactoringTool.refactor_docstringc       
   
      s  y || }W nf tk
rx } zHjtjrRx|D ]}d|d q8W d|||j	j
| |S d}~X Y nX ||rt|jdd}|d|d  ||d d  }	}|d ds|d  d7  <  j |d	 g}|r| fd
d|D 7 }|S )zRefactors 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: %sNT)r   r   rp   r   c                s   g | ]} j  | qS r   )r   )r1   r   )r   rn   r   r   r4   Z  s    z4RefactoringTool.refactor_doctest.<locals>.<listcomp>)parse_blockr!   rY   ZisEnabledForrc   DEBUGr~   r   r   r   r   r   r   r   endswithr   pop)
rn   r   r   r   r   r   r   r   r   Zclippedr   )r   rn   r   r   @  s$    
"z RefactoringTool.refactor_doctestc             C   s   | j rd}nd}| js$| d| n&| d| x| jD ]}| | q8W | jrt| d x| jD ]}| | qbW | jrt| jdkr| d n| dt| j x&| jD ]\}}}| j|f|| qW 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:)re   ri   r}   rd   rb   rw   )rn   r   filemessager   r   r   r   r   r   	summarize]  s$    
zRefactoringTool.summarizec             C   s"   | j | |||}t |_|S )zParses 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_toksrD   r   )rn   r   r   r   r   r   r   r   r   t  s    zRefactoringTool.parse_blockc             c   sh   t | ||j}xN|D ]F\}}\}}\}	}
}||d 7 }|	|d 7 }	||||f|	|
f|fV  qW dS )z;Wraps a tokenize stream to systematically modify start/end.r   N)r   r@   	gen_lines__next__)rn   r   r   r   r+   r   rQ   Zline0Zcol0Zline1Zcol1Z	line_textr   r   r   r   ~  s
    zRefactoringTool.wrap_toksc             c   s   || j  }|| j }|}xV|D ]N}||r@|t|d V  n(|| d krXdV  ntd||f |}qW x
dV  qrW dS )zGenerates 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=%rrr   )r   r   r   rw   r   AssertionError)rn   r   r   prefix1Zprefix2prefixr   r   r   r   r     s    



zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)r   r   r   r\   ry   rv   ro   rf   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   rU      s:   
4(
	


O 

+
rU   c               @   s   e Zd ZdS )MultiprocessingUnsupportedN)r   r   r   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|| d | _d | _d S )N)superr   ro   queueoutput_lock)rn   r   kwargs)r   r   r   ro     s    z$MultiprocessRefactoringTool.__init__Fr   c          
      s  |dkrt t|||S ydd l W n tk
r@   tY nX jd k	rTtd  _ 	 _
 fddt|D }z.x|D ]}|  qW t t||| W d j  xt|D ]}jd  qW x|D ]}| r|  qW d _X d S )Nr   r   z already doing multiple processesc                s   g | ]} j jd qS ))target)ZProcess_child)r1   r   )multiprocessingrn   r   r   r4     s   z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r   r   r   r   ImportErrorr   r   RuntimeErrorZJoinableQueueZLockr   rangestartrz   putZis_alive)rn   r   r   r   Znum_processesZ	processesr#   r   )r   )r   rn   r   r     s2    









z$MultiprocessRefactoringTool.refactorc                sR   | j  }xB|d k	rL|\}}ztt| j|| W d | j   X | j  }qW d S )N)r   ra   r   r   r   Z	task_done)rn   Ztaskr   r   )r   r   r   r     s    


z"MultiprocessRefactoringTool._childc                s2   | j d k	r| j ||f ntt| j||S d S )N)r   r   r   r   r   )rn   r   r   )r   r   r   r     s    

z)MultiprocessRefactoringTool.refactor_file)FFr   )r   r   r   ro   r   r   r   __classcell__r   r   )r   r   r     s
    r   )T)#rT   
__author__rA   r   r   r   rc   r   r%   	itertoolsr   Zpgen2r   r   r   Z
fixer_utilr   rr   r   r	   r
   rj   r   r!   r   r   r/   r5   r7   rR   rS   objectrU   r   r   r   r   r   r   <module>	   s8   
(    	