Your IP : 18.225.117.89
a
R�fsk � @ s d Z dZddlZddlZddlZddlZddlZddlZddlZddl m
Z
ddlmZm
Z
mZ ddlmZ ddlmZmZ dd lmZ d!dd�ZG d
d� de�Zdd� Zdd� Zdd� Zdd� Zdd� ZG dd� de�ZG dd� de�Z G dd� de�Z!G dd � 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_matcherTc C sT t | g g dg�}g }t�|j�D ].\}}}|�d�r |rD|dd� }|�|� q |S )zEReturn a sorted list of all available fix names in the given package.�*�fix_� N)�
__import__�pkgutilZiter_modules�__path__�
startswith�append)Z fixer_pkgZ
remove_prefixZpkgZ fix_names�finder�nameZispkg� r �5/opt/alt/python39/lib64/python3.9/lib2to3/refactor.py�get_all_fix_names s
r c @ s e Zd ZdS )�
_EveryNodeN��__name__�
__module__�__qualname__r r r r r + s r c C s� t | tjtjf�r(| jdu r t�| jhS t | tj�rH| jrDt| j�S t�t | tj �r�t
� }| jD ]}|D ]}|�t|�� qhq`|S td| ��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)
�
isinstancer ZNodePatternZLeafPattern�typer ZNegatedPatternZcontent�_get_head_typesZWildcardPattern�set�update� Exception)Zpat�r�p�xr r r r / s
r c C s� t �t�}g }| D ]v}|jrbzt|j�}W n tyF |�|� Y q�0 |D ]}|| �|� qLq|jdur~||j �|� q|�|� qtt 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_nodesZevery�fixerZheadsZ node_typer r r �_get_headnode_dictK s$
�r0 c s � fdd�t � d�D �S )zN
Return the fully qualified names for fixers in the package pkg_name.
c s g | ]}� d | �qS ��.r )�.0�fix_name�Zpkg_namer r �
<listcomp>h s �z+get_fixers_from_package.<locals>.<listcomp>F)r r5 r r5 r �get_fixers_from_packaged s
�r7 c C s | S �Nr )�objr r r � _identityk s r: c sV d}t �t�| �j�� � fdd�}ttjt jtj h�}t
� }z�|� \}}||v rTq>q>|tjkrl|rf�q6d}q>|tjk�r6|dk�r6|� \}}|tjks�|dkr��q6|� \}}|tjks�|dkrq6|� \}}|tj
kr�|dkr�|� \}}|tjk�r4|�|� |� \}}|tj
k�s.|d k�r"�q4|� \}}q�q>�q6q>W n t�yL Y n0 t|�S )
NFc s t � �} | d | d fS )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)�sourceZhave_docstringr? �ignore�features�tp�valuer r= r �_detect_future_featureso sB
rV c @ s e Zd ZdZdS )�
FixerErrorzA fixer could not be loaded.N)r r r �__doc__r r r r rW � s rW c @ s� e Zd Zdddd�ZdZdZd4dd�Zdd � Zd
d� Zdd
� Z dd� Z
dd� Zd5dd�Zd6dd�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�
exec_function�write_unchanged_filesZFixr Nc C sJ || _ |pg | _| j�� | _|dur0| j�|� tj�� | _| jd rR| jj d= n| jd rf| jj d= | j�
d�| _g | _t
�d�| _g | _d| _tj| jtj| jd �| _| �� \| _| _g | _t�� | _g | _g | _t| j| j�D ]H}|j r�| j�!|� q�|| jv �r| j�"|� q�|| jv r�| j�"|� q�t#| 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.
NrZ �printr[ �execr\ rY F)�convert�logger)&�fixers�explicit�_default_options�copy�optionsr! r r* �grammar�keywords�getr\ �errors�loggingZ getLoggerr` � fixer_log�wroter ZDriverr r_ �
get_fixers� pre_order�
post_order�files�bmZ
BottomMatcher�BMZ
bmi_pre_orderZbmi_post_orderr Z
BM_compatibleZ add_fixerr r0 �bmi_pre_order_heads�bmi_post_order_heads)�selfZfixer_namesre rb r/ r r r �__init__� sB
�
zRefactoringTool.__init__c C sV g }g }| j D �]}t|i i dg�}|�dd�d }|�| j�rR|t| j�d� }|�d�}| jd�dd � |D �� }zt ||�}W n$ t
y� td
||f �d�Y n0 || j| j
�} | jr�| jdur�|| jvr�| �d|� q| �d
|� | jdk�r|�| � q| jdk�r|�| � qtd| j ��qt�d�}
|j|
d� |j|
d� ||fS )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 r2 r ���N�_� c S s g | ]}|� � �qS r )�title)r3 r$ r r r r6 � � z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ run_order��key)ra r �rsplitr �FILE_PREFIX�len�split�CLASS_PREFIX�join�getattr�AttributeErrorrW re rk rb �log_message� log_debug�orderr �operator�
attrgetter�sort)ru Zpre_order_fixersZpost_order_fixersZfix_mod_path�modr4 �parts�
class_nameZ fix_classr/ Zkey_funcr r r rm � s:
�
zRefactoringTool.get_fixersc O s � dS )zCalled when an error occurs.Nr )ru �msg�args�kwdsr r r � log_error s zRefactoringTool.log_errorc G s |r|| }| j �|� dS )zHook to log a message.N)r` �info�ru r� r� r r r r� s zRefactoringTool.log_messagec G s |r|| }| j �|� d S r8 )r` �debugr� r r r r�
s zRefactoringTool.log_debugc C s dS )zTCalled with the old version, new version, and filename of a
refactored file.Nr )ru �old_text�new_text�filename�equalr r r �print_output s zRefactoringTool.print_outputc C s8 |D ].}t j�|�r$| �|||� q| �|||� qdS )z)Refactor a list of files and directories.N)�os�path�isdir�refactor_dir�
refactor_file)ru �items�write�
doctests_onlyZdir_or_filer r r �refactor s zRefactoringTool.refactorc
C s� t jd }t �|�D ]�\}}}| �d|� |�� |�� |D ]>}|�d�s>t j�|�d |kr>t j�||�} | � | ||� q>dd� |D �|dd�<