???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3

  \=m                 @   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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ejd'k rddlZejZdd Z dd Z!neZeZ e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   st   t | g g dg}tjj|j}g }xLttj|D ]:}|jdr2|jdr2|rZ|dd }|j	|dd  q2W |S )zEReturn a sorted list of all available fix names in the given package.*fix_z.py   N   )

__import__ospathdirname__file__sortedlistdir
startswithendswithappend)Z	fixer_pkgZremove_prefixZpkgZ	fixer_dirZ	fix_namesname r   (/usr/lib64/python3.6/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 ]}|j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 jt}g }x| D ]|}|jrjyt|j}W n tk
rJ   |j| Y qX xB|D ]}|| j| qRW q|jdk	r||j j| q|j| qW x,tt	j
jj t	j
jD ]}|| j| 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"    



r5   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=   r   c             C   s   | j ddS )Nz

)replace)inputr   r   r   _from_system_newlinesr   s    rA   c             C   s    t jdkr| jdt jS | S d S )Nr>   )r   linesepr?   )r@   r   r   r   _to_system_newlinest   s    
rC   c                sT  d}t jtj| 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o||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(|j| | \}}|tjks|d	krP | \}}qW q@P q@W W n tk
rJ   Y nX t|S )
NFc                 s   t  } | d | d fS )Nr   r   )next)tok)genr   r   advance   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_docstringrG   ignorefeaturestpvaluer   )rF   r   _detect_future_features   sD    








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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j | _|dk	r0| jj| | jd rDtj| _ntj	| _| jj
d| _g | _tjd| _g | _d| _tj| jtj| jd| _| j \| _| _g | _tj | _g | _g | _xXt| j| jD ]F}|j r| jj!| q|| jkr| jj"| q|| jkr| j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.
        Nrb   rc   ra   F)convertlogger)&fixersexplicit_default_optionscopyoptionsr&   r	   !python_grammar_no_print_statementgrammarr/   getrc   errorsloggingZ	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   r5   bmi_pre_order_headsbmi_post_order_heads)selfZfixer_namesrj   rg   r4   r   r   r   __init__   s<    





zRefactoringTool.__init__c             C   s\  g }g }x&| j D ]}t|i i dg}|jddd }|j| jrV|t| jd }|jd}| jdjdd |D  }yt	||}W n$ t
k
r   td	||f Y nX || j| j}	|	jr| jd
k	r|| jkr| jd| q| jd| |	jdkr|j|	 q|	jdkr |j|	 qtd|	j qW tj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 | ]}|j  qS r   )title)r7   r)   r   r   r   r:      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)rf   r   rsplitr   FILE_PREFIXlensplitCLASS_PREFIXjoingetattrAttributeErrorr_   rj   rp   rg   log_message	log_debugorderr   operator
attrgettersort)rz   Zpre_order_fixersZpost_order_fixersZfix_mod_pathmodr8   parts
class_nameZ	fix_classr4   Zkey_funcr   r   r   rr      s8    


zRefactoringTool.get_fixersc             O   s    dS )zCalled when an error occurs.Nr   )rz   msgargskwdsr   r   r   	log_error  s    zRefactoringTool.log_errorc             G   s   |r|| }| j j| dS )zHook to log a message.N)re   info)rz   r   r   r   r   r   r     s    zRefactoringTool.log_messagec             G   s   |r|| }| j j| d S )N)re   debug)rz   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   )rz   old_textnew_textfilenameequalr   r   r   print_output  s    zRefactoringTool.print_outputc             C   s<   x6|D ].}t jj|r&| j||| q| j||| qW dS )z)Refactor a list of files and directories.N)r   r   isdirrefactor_dirrefactor_file)rz   itemswritedoctests_onlyZdir_or_filer   r   r   refactor#  s    
zRefactoringTool.refactorc       
      C   s   t jd }xt j|D ]\}}}| jd| |j  |j  xH|D ]@}|jd rBt jj|d |krBt jj||}	| 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 %sr6   r   c             S   s   g | ]}|j d s|qS )r6   )r   )r7   Zdnr   r   r   r:   >  s    z0RefactoringTool.refactor_dir.<locals>.<listcomp>N)
r   extsepwalkr   r   r   r   splitextr   r   )
rz   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 n. tk
r< } z| jd|| dS d}~X nX ztj|jd }W d|j  X t|d|d}t|j	 |fS Q R X dS )zG
        Do our best to decode a Python source file correctly.
        rbzCan't open %s: %sNr   r(   )encoding)NN)
openOSErrorr   r   detect_encodingrO   close_open_with_encodingrA   read)rz   r   ferrr   r   r   r   _read_python_source@  s    
z#RefactoringTool._read_python_sourcec             C   s   | j |\}}|dkrdS |d7 }|rn| jd| | j||}| jsL||kr`| j||||| q| jd| nH| j||}| js|r|jr| jt|dd |||d n| jd| dS )	zRefactors a file.Nr>   zRefactoring doctests in %szNo doctest changes in %sr   )r   r   zNo changes in %sr   )r   r   refactor_docstringrc   processed_filerefactor_stringwas_changedstr)rz   r   r   r   r@   r   outputtreer   r   r   r   P  s    zRefactoringTool.refactor_filec             C   s   t |}d|krtj| j_zJy| jj|}W n4 tk
r` } z| jd||jj	| dS d}~X nX W d| j| j_X ||_
| jd| | j|| |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.
        rb   zCan't parse %s: %s: %sNzRefactoring %s)r^   r	   rk   r   rl   Zparse_stringr'   r   	__class__r   future_featuresr   refactor_tree)rz   datar   r[   r   r   r   r   r   r   g  s    
zRefactoringTool.refactor_stringc             C   s   t jj }|rN| jd | j|d}| js2||krB| j|d| q| jd n:| j|d}| jsj|r~|jr~| jt	|d| n
| jd d S )NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin)
sysstdinr   r   r   rc   r   r   r   r   )rz   r   r@   r   r   r   r   r   refactor_stdin  s    

zRefactoringTool.refactor_stdinc       
      C   s  x"t | j| jD ]}|j|| qW | j| j|j  | j| j|j  | jj|j	 }xvt
|j rАx`| jjD ]R}||ko|| rv|| jtjjdd |jr|| jtjjd x
t|| D  ]}||| kr|| j| yt| W n tk
r   wY nX |jr(||jkr(q|j|}|r|j||}|dk	r|j| x,|j D ] }|jspg |_|jj| q^W | jj|j	 }x2|D ]*}	|	|krg ||	< ||	 j||	  qW qW qvW q\W x$t | j| jD ]}|j|| 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)r   reverse)r   N)r   rs   rt   Z
start_treetraverse_byrx   ry   rw   ZrunZleavesanyr0   rf   r   r   ZBaseZdepthZkeep_line_orderZ
get_linenor-   remover   
ValueErrorZfixers_appliedmatch	transformr?   r   r2   Zfinish_treer   )
rz   r   r   r4   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}|j|}|r|j||}|dk	r|j| |}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?   )rz   rf   Z	traversalr   r4   r   r   r   r   r   r     s    


zRefactoringTool.traverse_byc             C   s   | j j| |dkr.| j|d }|dkr.dS ||k}| j|||| |r`| jd| | js`dS |rv| j|||| n| j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)ru   r   r   r   r   rc   
write_file)rz   r   r   r   r   r   r   r   r   r   r     s    zRefactoringTool.processed_filec          %   C   s   yt |d|d}W n. tk
r@ } z| jd|| dS d}~X nX zHy|jt| W n0 tk
r } z| jd|| W Y dd}~X nX W d|j  X | j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.
        w)r   zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)r   r   r   r   rC   r   r   rq   )rz   r   r   r   r   r   r   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 j| jr|dk	r\|j| j|||| |}|g}|j| j}	|d|	 }q"|dk	r|j|| j s||| jj  d kr|j	| q"|dk	r|j| j|||| d}d}|j	| q"W |dk	r|j| j|||| dj
|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   PS1r2   refactor_doctestfindPS2rstripr   r   )
rz   r@   r   resultblockZblock_linenoindentlinenolineir   r   r   r   %  s:    



z"RefactoringTool.refactor_docstringc       
         s(  yj || }W nd tk
rv } zHjjtjrRx|D ]}jd|jd q8W jd|||j	j
| |S d}~X nX j||r$t|jdd}|d|d  ||d d  }	}|	dg|d  kst|	|d jds|d  d7  <  j |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   r   c                s   g | ]} j  | qS r   )r   )r7   r   )r   rz   r   r   r:   j  s    z4RefactoringTool.refactor_doctest.<locals>.<listcomp>r   r   )parse_blockr'   re   ZisEnabledForro   DEBUGr   r   r   r   r   r   r   r   AssertionErrorr   r   pop)
rz   r   r   r   r   r   r   r   r   Zclippedr   )r   rz   r   r   P  s&    
"z RefactoringTool.refactor_doctestc             C   s   | j rd}nd}| js$| jd| n&| jd| x| jD ]}| j| q8W | jrt| jd x| jD ]}| j| qbW | jrt| jdkr| jd n| j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:)rq   ru   r   rp   rn   r   )rz   r   filemessager   r   r   r   r   r   	summarizem  s$    
zRefactoringTool.summarizec             C   s"   | j j| 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_toksrP   r   )rz   r   r   r   r   r   r   r   r     s    zRefactoringTool.parse_blockc             c   sh   t j| j||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   rL   	gen_lines__next__)rz   r   r   r   r1   r#   r]   Zline0Zcol0Zline1Zcol1Z	line_textr   r   r   r     s
    zRefactoringTool.wrap_toksc             c   s   || j  }|| j }|}xV|D ]N}|j|r@|t|d V  n(||j 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=%rr}   )r   r   r   r   r   r   )rz   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{   rr   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   ra      s:   
4(
	


O 

+
ra   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   r{   queueoutput_lock)rz   r   kwargs)r   r   r   r{     s    z$MultiprocessRefactoringTool.__init__Fr   c                s  |dkrt tj|||S ydd l W n tk
r@   tY nX jd k	rTtd j _ j	 _
 fddt|D }z.x|D ]}|j  qW t tj||| W d jj  xt|D ]}jjd  qW x|D ]}|j r|j  qW d _X d S )Nr   r   z already doing multiple processesc                s   g | ]} j jd qS ))target)ZProcess_child)r7   r   )multiprocessingrz   r   r   r:     s   z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r   r   r   r   ImportErrorr   r   RuntimeErrorZJoinableQueueZLockr   rangestartr   putZis_alive)rz   r   r   r   Znum_processesZ	processesr)   r   )r   )r   rz   r   r     s2    









z$MultiprocessRefactoringTool.refactorc          
      sR   | j j }xB|d k	rL|\}}ztt| j|| W d | j j  X | j j }qW d S )N)r   rm   r   r   r   Z	task_done)rz   Ztaskr   r   )r   r   r   r     s    


z"MultiprocessRefactoringTool._childc                s2   | j d k	r| j j||f ntt| j||S d S )N)r   r   r   r   r   )rz   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   r   )(r`   
__author__r   r   ro   r   r+   rM   	itertoolsr   Zpgen2r   r   r   Z
fixer_utilr   r}   r   r	   r
   rv   r   r'   r   r$   r5   r;   r=   version_infocodecsr   r   rA   rC   r^   r_   objectra   r   r   r   r   r   r   <module>	   sF   


(    	