Your IP : 3.144.25.130


Current Path : /proc/self/root/opt/alt/python37/lib64/python3.7/lib2to3/__pycache__/
Upload File :
Current File : //proc/self/root/opt/alt/python37/lib64/python3.7/lib2to3/__pycache__/refactor.cpython-37.opt-2.pyc

B

YI~dk�@s
dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZmZddlmZd d
d�ZGdd
�d
e�Zdd�Zdd�Zdd�Zdd�Zdd�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!dS)!z#Guido van Rossum <guido@python.org>�N)�chain�)�driver�tokenize�token)�	find_root)�pytree�pygram)�btm_matcherTcCsXt|ggdg�}g}x>t�|j�D].\}}}|�d�r"|rF|dd�}|�|�q"W|S)N�*�fix_�)�
__import__�pkgutilZiter_modules�__path__�
startswith�append)Z	fixer_pkgZ
remove_prefixZpkgZ	fix_names�finder�nameZispkg�r�5/opt/alt/python37/lib64/python3.7/lib2to3/refactor.py�get_all_fix_namess
rc@seZdZdS)�
_EveryNodeN)�__name__�
__module__�__qualname__rrrrr+srcCs�t|tjtjf�r(|jdkr t�|jhSt|tj�rH|jrDt|j�St�t|tj	�r�t
�}x*|jD] }x|D]}|�t|��qlWqbW|Std|��dS)Nz$Oh no! I don't understand pattern %s)
�
isinstancerZNodePatternZLeafPattern�typerZNegatedPatternZcontent�_get_head_typesZWildcardPattern�set�update�	Exception)Zpat�r�p�xrrrr/s


rc	Cs�t�t�}g}x�|D]|}|jrjyt|j�}Wntk
rJ|�|�Yq�XxB|D]}||�|�qRWq|jdk	r�||j�|�q|�|�qWx,tt	j
j��t	j
j
�D]}||�|�q�Wt|�S)N)�collections�defaultdict�list�patternrrrZ_accept_typerr	�python_grammarZ
symbol2number�values�tokens�extend�dict)Z
fixer_listZ
head_nodesZevery�fixerZheadsZ	node_typerrr�_get_headnode_dictKs"



r/cs�fdd�t�d�D�S)Ncsg|]}�d|�qS)�.r)�.0�fix_name)�pkg_namerr�
<listcomp>hsz+get_fixers_from_package.<locals>.<listcomp>F)r)r3r)r3r�get_fixers_from_packageds
r5cCs|S)Nr)�objrrr�	_identityksr7csVd}t�t�|�j���fdd�}ttjtjtj	h�}t
�}y�x�|�\}}||krVq@q@|tjkrl|rfPd}q@|tjk�r.|dk�r.|�\}}|tjks�|dkr�P|�\}}|tjks�|dkr�P|�\}}|tj
kr�|dkr�|�\}}xJ|tjk�r*|�|�|�\}}|tj
k�s|d	k�rP|�\}}q�Wq@Pq@WWntk
�rLYnXt|�S)
NFcst��}|d|dfS)Nrr)�next)�tok)�genrr�advancersz(_detect_future_features.<locals>.advanceT�fromZ
__future__�import�(�,)r�generate_tokens�io�StringIO�readline�	frozensetr�NEWLINE�NL�COMMENTr�STRING�NAME�OP�add�
StopIteration)�sourceZhave_docstringr;�ignore�features�tp�valuer)r:r�_detect_future_featuresosD








rRc@seZdZdS)�
FixerErrorN)rrrrrrrrS�srSc@s�eZdZddd�ZdZdZd4dd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zd5dd�Zd6dd�Z
dd�Zd7dd�Zdd�Zd8dd�Zdd�Zd d!�Zd9d"d#�Zd:d$d%�Zd&Zd'Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�ZdS);�RefactoringToolF)�print_function�write_unchanged_filesZFixrNcCs2||_|pg|_|j��|_|dk	r0|j�|�|jdrDtj|_ntj	|_|j�
d�|_g|_t
�d�|_g|_d|_tj|jtj|jd�|_|��\|_|_g|_t��|_g|_g|_xXt|j|j�D]F}|j r�|j�!|�q�||jkr�|j�"|�q�||jkr�|j�"|�q�Wt#|j�|_$t#|j�|_%dS)NrUrVrTF)�convert�logger)&�fixers�explicit�_default_options�copy�optionsr r	�!python_grammar_no_print_statement�grammarr)�getrV�errors�loggingZ	getLoggerrX�	fixer_log�wroterZDriverrrW�
get_fixers�	pre_order�
post_order�files�bmZ
BottomMatcher�BMZ
bmi_pre_orderZbmi_post_orderrZ
BM_compatibleZ	add_fixerrr/�bmi_pre_order_heads�bmi_post_order_heads)�selfZfixer_namesr]rZr.rrr�__init__�s<





zRefactoringTool.__init__c	Cs^g}g}�x(|jD�]}t|iidg�}|�dd�d}|�|j�rV|t|j�d�}|�d�}|jd�dd�|D��}yt	||�}Wn&t
k
r�td	||f�d�YnX||j|j
�}	|	jr�|jd
k	r�||jkr�|�d|�q|�d|�|	jd
k�r
|�|	�q|	jdk�r"|�|	�qtd|	j��qWt�d�}
|j|
d�|j|
d�||fS)Nrr0r����_�cSsg|]}|���qSr)�title)r1r#rrrr4�sz.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ	run_order)�key)rYr�rsplitr�FILE_PREFIX�len�split�CLASS_PREFIX�join�getattr�AttributeErrorrSr]rcrZ�log_message�	log_debug�orderr�operator�
attrgetter�sort)rmZpre_order_fixersZpost_order_fixersZfix_mod_path�modr2�parts�
class_nameZ	fix_classr.Zkey_funcrrrre�s8


zRefactoringTool.get_fixerscOs�dS)Nr)rm�msg�args�kwdsrrr�	log_error�szRefactoringTool.log_errorcGs|r||}|j�|�dS)N)rX�info)rmr�r�rrrr|szRefactoringTool.log_messagecGs|r||}|j�|�dS)N)rX�debug)rmr�r�rrrr}	szRefactoringTool.log_debugcCsdS)Nr)rm�old_text�new_text�filename�equalrrr�print_outputszRefactoringTool.print_outputcCs<x6|D].}tj�|�r&|�|||�q|�|||�qWdS)N)�os�path�isdir�refactor_dir�
refactor_file)rm�items�write�
doctests_onlyZdir_or_filerrr�refactors
zRefactoringTool.refactorc
Cs�tjd}x�t�|�D]�\}}}|�d|�|��|��xF|D]>}|�d�sBtj�|�d|krBtj�||�}	|�	|	||�qBWdd�|D�|dd�<qWdS)N�pyzDescending into %sr0rcSsg|]}|�d�s|�qS)r0)r)r1Zdnrrrr4.sz0RefactoringTool.refactor_dir.<locals>.<listcomp>)
r��extsep�walkr}r�rr��splitextryr�)
rmZdir_namer�r�Zpy_ext�dirpathZdirnames�	filenamesr�fullnamerrrr�s


zRefactoringTool.refactor_dirc
Cs�yt|d�}Wn0tk
r>}z|�d||�dSd}~XYnXzt�|j�d}Wd|��Xtj|d|dd��}|��|fSQRXdS)N�rbzCan't open %s: %s)NNrr"rq)�encoding�newline)	�open�OSErrorr�r�detect_encodingrC�closerA�read)rmr��f�errr�rrr�_read_python_source0s
z#RefactoringTool._read_python_sourcecCs�|�|�\}}|dkrdS|d7}|rn|�d|�|�||�}|jsL||kr`|�|||||�q�|�d|�nH|�||�}|js�|r�|jr�|jt|�dd�|||d�n|�d|�dS)N�
zRefactoring doctests in %szNo doctest changes in %sro)r�r�zNo changes in %s)r�r}�refactor_docstringrV�processed_file�refactor_string�was_changed�str)rmr�r�r��inputr��output�treerrrr�@szRefactoringTool.refactor_filec
Cs�t|�}d|krtj|j_zLy|j�|�}Wn6tk
rb}z|�d||jj	|�dSd}~XYnXWd|j|j_X||_
|�d|�|�||�|S)NrUzCan't parse %s: %s: %szRefactoring %s)
rRr	r^rr_Zparse_stringr!r��	__class__r�future_featuresr}�
refactor_tree)rm�datarrOr�r�rrrr�Ws
zRefactoringTool.refactor_stringcCs�tj��}|rN|�d�|�|d�}|js2||krB|�|d|�q�|�d�n:|�|d�}|jsj|r~|jr~|�t	|�d|�n
|�d�dS)NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin)
�sys�stdinr�r}r�rVr�r�r�r�)rmr�r�r�r�rrr�refactor_stdinrs

zRefactoringTool.refactor_stdinc

Cs�x"t|j|j�D]}|�||�qW|�|j|���|�|j|���|j�|�	��}�xtt
|����rΐx^|jjD�]P}||krv||rv||j
tjjdd�|jr�||j
tjjd��xt||�D]�}|||kr�||�|�yt|�Wntk
�rw�YnX|j�r&||jk�r&q�|�|�}|r�|�||�}|dk	r�|�|�x,|��D] }|j�sng|_|j�|��q\W|j�|�	��}x2|D]*}	|	|k�r�g||	<||	�||	��q�Wq�WqvWq\Wx$t|j|j�D]}|�||��q�W|jS)NT)rs�reverse)rs)rrfrgZ
start_tree�traverse_byrkrlrj�runZleaves�anyr*rYr�rZBase�depthZkeep_line_orderZ
get_linenor'�remover�
ValueErrorZfixers_applied�match�	transform�replacerr,Zfinish_treer�)
rmr�rr.Z	match_set�node�results�newZnew_matchesZfxrrrrr��sJ



$zRefactoringTool.refactor_treecCs^|sdSxP|D]H}xB||jD]4}|�|�}|r|�||�}|dk	r|�|�|}qWqWdS)N)rr�r�r�)rmrYZ	traversalr�r.r�r�rrrr��s


zRefactoringTool.traverse_bycCs�|j�|�|dkr.|�|�d}|dkr.dS||k}|�||||�|r`|�d|�|js`dS|rv|�||||�n|�d|�dS)NrzNo changes to %szNot writing changes to %s)rhrr�r�r}rV�
write_file)rmr�r�r�r�r�r�rrrr��szRefactoringTool.processed_filecCs�ytj|d|dd�}Wn0tk
rF}z|�d||�dSd}~XYnX|�Fy|�|�Wn0tk
r�}z|�d||�Wdd}~XYnXWdQRX|�d|�d|_dS)N�wrq)r�r�zCan't create %s: %szCan't write %s: %szWrote changes to %sT)rAr�r�r�r�r}rd)rmr�r�r�r��fpr�rrrr��s*zRefactoringTool.write_filez>>> z... c
	Csg}d}d}d}d}x�|jdd�D]�}|d7}|���|j�r�|dk	r\|�|�||||��|}|g}|�|j�}	|d|	�}q"|dk	r�|�||j�s�|||j��dkr�|�	|�q"|dk	r�|�|�||||��d}d}|�	|�q"W|dk	�r|�|�||||��d�
|�S)NrT)�keependsrr�rq)�
splitlines�lstripr�PS1r,�refactor_doctest�find�PS2�rstriprry)
rmr�r��result�blockZblock_lineno�indent�lineno�line�irrrr�s:



z"RefactoringTool.refactor_docstringc

sy��||��}Wnftk
rx}zH�j�tj�rRx|D]}��d|�d��q8W��d|||j	j
|�|Sd}~XYnX��||��rt|�j
dd�}|d|d�||dd�}	}|d�d�s�|dd7<��j|�d�g}|�r|��fd	d
�|D�7}|S)Nz
Source: %sr�z+Can't parse docstring in %s line %s: %s: %sT)r�rrorcsg|]}��j|�qSr)r�)r1r�)r�rmrrr4Zsz4RefactoringTool.refactor_doctest.<locals>.<listcomp>)�parse_blockr!rXZisEnabledForrb�DEBUGr}r�r�r�rr�r�r��endswithr��pop)
rmr�r�r�r�r�r�r�r�Zclippedr)r�rmrr�@s$
"z RefactoringTool.refactor_doctestcCs�|jrd}nd}|js$|�d|�n&|�d|�x|jD]}|�|�q8W|jrt|�d�x|jD]}|�|�qbW|jr�t|j�dkr�|�d�n|�dt|j��x&|jD]\}}}|j|f|�|�q�WdS)	N�werez
need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:rzThere was 1 error:zThere were %d errors:)rdrhr|rcrarv)rmr��file�messager�r�r�rrr�	summarize]s$
zRefactoringTool.summarizecCs"|j�|�|||��}t�|_|S)N)rZparse_tokens�	wrap_toksrDr�)rmr�r�r�r�rrrr�tszRefactoringTool.parse_blockccsht�|�||�j�}xN|D]F\}}\}}\}	}
}||d7}|	|d7}	||||f|	|
f|fVqWdS)Nr)rr@�	gen_lines�__next__)rmr�r�r�r+rrQZline0Zcol0Zline1Zcol1Z	line_textrrrr�~s
zRefactoringTool.wrap_toksccs�||j}||j}|}xV|D]N}|�|�r@|t|�d�Vn(||��dkrXdVntd||f��|}qWx
dVqrWdS)Nr�zline=%r, prefix=%rrq)r�r�rrvr��AssertionError)rmr�r��prefix1Zprefix2�prefixr�rrrr��s



zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrr[rxrurnrer�r|r}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrT�s:
4(
	


O

+
rTc@seZdZdS)�MultiprocessingUnsupportedN)rrrrrrrr��sr�csBeZdZ�fdd�Zd�fdd�	Z�fdd�Z�fd	d
�Z�ZS)�MultiprocessRefactoringToolcs"tt|�j||�d|_d|_dS)N)�superr�rn�queue�output_lock)rmr��kwargs)r�rrrn�sz$MultiprocessRefactoringTool.__init__Frc
s|dkrtt���|||�Syddl�Wntk
r@t�YnX�jdk	rTtd������_��	��_
��fdd�t|�D�}z.x|D]}|��q�Wtt���|||�Wd�j�
�xt|�D]}�j�d�q�Wx|D]}|��r�|�
�q�Wd�_XdS)Nrrz already doing multiple processescsg|]}�j�jd��qS))�target)ZProcess�_child)r1r�)�multiprocessingrmrrr4�sz8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r�r�r�r��ImportErrorr�r��RuntimeErrorZ
JoinableQueueZLockr��range�startry�putZis_alive)rmr�r�r�Z
num_processesZ	processesr#r�)r�)r�rmrr��s2









z$MultiprocessRefactoringTool.refactorcsR|j��}xB|dk	rL|\}}ztt|�j||�Wd|j��X|j��}qWdS)N)r�r`r�r�r�Z	task_done)rmZtaskr�r�)r�rrr��s


z"MultiprocessRefactoringTool._childcs2|jdk	r|j�||f�ntt|�j||�SdS)N)r�r�r�r�r�)rmr�r�)r�rrr��s

z)MultiprocessRefactoringTool.refactor_file)FFr)rrrrnr�r�r��
__classcell__rr)r�rr��s
r�)T)"�
__author__rAr�rr�rbrr%�	itertoolsrZpgen2rrrZ
fixer_utilrrqrr	r
rirr!rrr/r5r7rRrS�objectrTr�r�rrrr�<module>s6
(	

?>