AlkantarClanX12

Your IP : 3.133.152.189


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

B

� fk�@sdZdZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZm
Z
mZddlmZddlmZmZdd	lmZd!dd�ZGd
d�de�Zdd�Zdd�Zdd�Zdd�Zdd�ZGdd�de�ZGdd�de�Z Gdd�de�Z!Gdd �d e �Z"dS)"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_matcherTcCsXt|ggdg�}g}x>t�|j�D].\}}}|�d�r"|rF|dd�}|�|�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_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)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)
�
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)z^ Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  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)zN
    Return the fully qualified names for fixers in the package pkg_name.
    csg|]}�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@seZdZdZdS)�
FixerErrorzA fixer could not be loaded.N)rrr�__doc__rrrrrS�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)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.
        NrVrWrUF)�convert�logger)&�fixers�explicit�_default_options�copy�optionsr r	�!python_grammar_no_print_statement�grammarr)�getrW�errors�loggingZ	getLoggerrY�	fixer_log�wroterZDriverrrX�
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^r[r.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�|jdk	r�||jkr�|�d|�q|�d
|�|	jdk�r
|�|	�q|	jdk�r"|�|	�qtd|	j��qWt�d�}
|j|
d�|j|
d�||fS)aInspects 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.
        rr0r���N�_�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)rZr�rsplitr�FILE_PREFIX�len�split�CLASS_PREFIX�join�getattr�AttributeErrorrSr^rdr[�log_message�	log_debug�orderr�operator�
attrgetter�sort)rnZpre_order_fixersZpost_order_fixersZfix_mod_path�modr2�parts�
class_nameZ	fix_classr.Zkey_funcrrrrf�s8


zRefactoringTool.get_fixerscOs�dS)zCalled when an error occurs.Nr)rn�msg�args�kwdsrrr�	log_error�szRefactoringTool.log_errorcGs|r||}|j�|�dS)zHook to log a message.N)rY�info)rnr�r�rrrr}szRefactoringTool.log_messagecGs|r||}|j�|�dS)N)rY�debug)rnr�r�rrrr~	szRefactoringTool.log_debugcCsdS)zTCalled with the old version, new version, and filename of a
        refactored file.Nr)rn�old_text�new_text�filename�equalrrr�print_outputszRefactoringTool.print_outputcCs<x6|D].}tj�|�r&|�|||�q|�|||�qWdS)z)Refactor a list of files and directories.N)�os�path�isdir�refactor_dir�
refactor_file)rn�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)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.
        �pyzDescending into %sr0rcSsg|]}|�d�s|�qS)r0)r)r1Zdnrrrr4.sz0RefactoringTool.refactor_dir.<locals>.<listcomp>N)
r��extsep�walkr~r�rr��splitextrzr�)
rnZdir_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)	zG
        Do our best to decode a Python source file correctly.
        �rbzCan't open %s: %s)NNNrr"rr)�encoding�newline)	�open�OSErrorr�r�detect_encodingrC�closerA�read)rnr��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)zRefactors a file.N�
zRefactoring doctests in %szNo doctest changes in %srp)r�r�zNo changes in %s)r�r~�refactor_docstringrW�processed_file�refactor_string�was_changed�str)rnr�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)aFRefactor 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.
        rVzCan't parse %s: %s: %sNzRefactoring %s)
rRr	r_rr`Zparse_stringr!r��	__class__r�future_featuresr~�
refactor_tree)rn�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�rWr�r�r�r�)rnr�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)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)rtN)rrgrhZ
start_tree�traverse_byrlrmrk�runZleaves�anyr*rZr�rZBaseZdepthZkeep_line_orderZ
get_linenor'�remover�
ValueErrorZfixers_applied�match�	transform�replacerr,Zfinish_treer�)
rnr�rr.Z	match_set�node�results�newZnew_matchesZfxrrrrr��sJ



$zRefactoringTool.refactor_treecCs^|sdSxP|D]H}xB||jD]4}|�|�}|r|�||�}|dk	r|�|�|}qWqWdS)aTraverse 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)rr�r�r�)rnrZZ	traversalr�r.r�r�rrrr��s


zRefactoringTool.traverse_bycCs�|j�|�|dkr.|�|�d}|dkr.dS||k}|�||||�|r`|�d|�|js`dS|rv|�||||�n|�d|�dS)zR
        Called when a file has been refactored and there may be changes.
        NrzNo changes to %szNot writing changes to %s)rirr�r�r~rW�
write_file)rnr�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)	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.
        �wrr)r�r�zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rAr�r�r�r�r~re)rnr�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)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.)
        NrT)�keependsrr�rr)�
splitlines�lstripr�PS1r,�refactor_doctest�find�PS2�rstriprrz)
rnr�r��result�blockZblock_lineno�indent�lineno�line�irrrr�s:



z"RefactoringTool.refactor_docstringc

s*y��||��}Wnftk
rx}zH�j�tj�rRx|D]}��d|�d��q8W��d|||j	j
|�|Sd}~XYnX��||��r&t|�j
dd�}|d|d�||dd�}	}|	dg|dks�t|	��|d�d�s�|dd7<��j|�d	�g}|�r&|��fd
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: %sNT)r�rrprcsg|]}��j|�qSr)r�)r1r�)r�rnrrr4Zsz4RefactoringTool.refactor_doctest.<locals>.<listcomp>)�parse_blockr!rYZisEnabledForrc�DEBUGr~r�r�r�rr�r�r��AssertionError�endswithr��pop)
rnr�r�r�r�r�r�r�r�Zclippedr)r�rnrr�@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:)rerir}rdrbrw)rnr��file�messager�r�r�rrr�	summarize]s$
zRefactoringTool.summarizecCs"|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.
        )rZparse_tokens�	wrap_toksrDr�)rnr�r�r�r�rrrr�tszRefactoringTool.parse_blockccsht�|�||�j�}xN|D]F\}}\}}\}	}
}||d7}|	|d7}	||||f|	|
f|fVqWdS)z;Wraps a tokenize stream to systematically modify start/end.rN)rr@�	gen_lines�__next__)rnr�r�r�r+rrQZline0Zcol0Zline1Zcol1Z	line_textrrrr�~s
zRefactoringTool.wrap_toksccs�||j}||j}|}xV|D]N}|�|�r@|t|�d�Vn(||��dkrXdVntd||f��|}qWx
dVqrWdS)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�zline=%r, prefix=%rrr)r�r�rrwr�r�)rnr�r��prefix1Zprefix2�prefixr�rrrr��s



zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrr\ryrvrorfr�r}r~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrU�s:
4(
	


O

+
rUc@seZdZdS)�MultiprocessingUnsupportedN)rrrrrrrr��sr�csBeZdZ�fdd�Zd�fdd�	Z�fdd�Z�fd	d
�Z�ZS)�MultiprocessRefactoringToolcs"tt|�j||�d|_d|_dS)N)�superr�ro�queue�output_lock)rnr��kwargs)r�rrro�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�)�multiprocessingrnrrr4�sz8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r�r�r�r��ImportErrorr�r��RuntimeErrorZ
JoinableQueueZLockr��range�startrz�putZis_alive)rnr�r�r�Z
num_processesZ	processesr#r�)r�)r�rnrr��s2









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


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

z)MultiprocessRefactoringTool.refactor_file)FFr)rrrror�r�r��
__classcell__rr)r�rr��s
r�)T)#rT�
__author__rAr�rr�rcr�r%�	itertoolsrZpgen2rrrZ
fixer_utilrrrrr	r
rjrr!rrr/r5r7rRrS�objectrUr�r�rrrr�<module>	s8
(