Edit File: refactor.cpython-37.opt-1.pyc
B ����q}�ck������������������@���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���sX���t�|�g�g�dg�}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__�pkgutilZiter_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_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(|�jdkr t�|�jhS�t�|�tj�rH|�jrDt|�j�S�t�t�|�tj �r�t ��}x*|�jD�] }x|D�]}|�t|���qlW�qbW�|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�}x�|�D�]|}|jrjyt|j�}W�n�tk rJ���|�|��Y�q�X�xB|D�]}||��|��qRW�q|jdk r�||j��|��q|�|��qW�x,tt j j���t j j �D�]}||��|��q�W�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"���� r/���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)�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�����fdd�}ttjt�jtj h�}t ��}y�x�|��\}}||krVq@q@|tjkrl|rfP�d}q@|tjk�r.|dk�r.|��\}}|tjks�|dkr�P�|��\}}|tjks�|dkr�P�|��\}}|tj kr�|dkr�|��\}}xJ|tjk�r*|�|��|��\}}|tj k�s|d k�rP�|��\}}q�W�q@P�q@W�W�n�tk �rL���Y�nX�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���sD���� rR���c���������������@���s���e�Zd�ZdZdS�)� FixerErrorzA fixer could not be loaded.N)r���r���r����__doc__r���r���r���r���rS�������s���rS���c���������������@���s����e�Zd�Zddd�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�write_unchanged_filesZFixr���Nc�������������C���s2��||�_�|pg�|�_|�j���|�_|dk r0|�j�|��|�jd�rDtj|�_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�W�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. 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���ZDriverr���rX���� get_fixers� pre_order� post_order�files�bmZ BottomMatcher�BMZ bmi_pre_orderZbmi_post_orderr���Z BM_compatibleZ add_fixerr���r/����bmi_pre_order_heads�bmi_post_order_heads)�selfZfixer_namesr^���r[���r.���r���r���r����__init__����s<���� zRefactoringTool.__init__c���������� ���C���s^��g�}g�}�x(|�j�D��]}t|i�i�dg�}|�dd�d�}|�|�j�rV|t|�j�d��}|�d�}|�jd�dd ��|D����}yt ||�}W�n&�t k r����td ||f��d�Y�nX�||�j|�j �} | jr�|�jdk r�||�jkr�|��d|��q|��d |��| jdk�r |�| ��q| jdk�r"|�| ��qtd| j���qW�t�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���r0���r������N�_��c�������������S���s���g�|�]}|�����qS�r���)�title)r1���r#���r���r���r���r4�������s����z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal 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���Zpre_order_fixersZpost_order_fixersZfix_mod_path�modr2����parts� class_nameZ fix_classr.���Zkey_funcr���r���r���rf�������s8���� zRefactoringTool.get_fixersc��������������O���s�����dS�)zCalled when an error occurs.Nr���)rn����msg�args�kwdsr���r���r���� log_error����s����zRefactoringTool.log_errorc�������������G���s���|r||�}|�j��|��dS�)zHook to log a message.N)rY����info)rn���r����r����r���r���r���r}�����s����zRefactoringTool.log_messagec�������������G���s���|r||�}|�j��|��d�S�)N)rY����debug)rn���r����r����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���)rn����old_text�new_text�filename�equalr���r���r����print_output��s����zRefactoringTool.print_outputc�������������C���s<���x6|D�].}t�j�|�r&|��|||��q|��|||��qW�dS�)z)Refactor a list of files and directories.N)�os�path�isdir�refactor_dir� refactor_file)rn����items�write� doctests_onlyZdir_or_filer���r���r����refactor��s���� zRefactoringTool.refactorc������� ������C���s����t�jd�}x�t��|�D�]�\}}}|��d|��|����|����xF|D�]>}|�d�sBt�j�|�d�|krBt�j�||�} |�� | ||��qBW�dd��|D��|dd�<�qW�dS�)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 %sr0���r���c�������������S���s���g�|�]}|��d��s|�qS�)r0���)r���)r1���Zdnr���r���r���r4���.��s����z0RefactoringTool.refactor_dir.<locals>.<listcomp>N) r�����extsep�walkr~���r����r���r�����splitextrz���r����) rn���Zdir_namer����r����Zpy_ext�dirpathZdirnames� filenamesr����fullnamer���r���r���r������s���� zRefactoringTool.refactor_dirc���������� ���C���s����yt�|d�}W�n0�tk r>�}�z|��d||��dS�d}~X�Y�nX�zt�|j�d�}W�d|����X�tj�|d|dd��}|���|fS�Q�R�X�dS�) zG Do our best to decode a Python source file correctly. �rbzCan'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����|���|�\}}|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)rn���r����r����r�����inputr�����output�treer���r���r���r����@��s����zRefactoringTool.refactor_filec���������� ���C���s����t�|�}d|krtj|�j_zLy|�j�|�}W�n6�tk rb�}�z|��d||jj |��dS�d}~X�Y�nX�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���zCan't parse %s: %s: %sNzRefactoring %s) rR���r ���r_���r���r`���Zparse_stringr!���r����� __class__r����future_featuresr~���� refactor_tree)rn����datar���rO���r����r����r���r���r���r����W��s���� zRefactoringTool.refactor_stringc�������������C���s����t�j���}|rN|��d��|��|d�}|�js2||krB|��|d|��q�|��d��n:|��|d�}|�jsj|r~|jr~|��t |�d|��n |��d��d�S�)NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin) �sys�stdinr����r~���r����rW���r����r����r����r����)rn���r����r����r����r����r���r���r����refactor_stdinr��s���� zRefactoringTool.refactor_stdinc������� ��� ���C���s���x"t�|�j|�j�D�]}|�||��qW�|��|�j|�����|��|�j|�����|�j�|� ���}�xtt |�����rΐx^|�jjD��]P}||krv||�rv||�j tjjdd��|jr�||�j tjjd���xt||��D�]�}|||�kr�||��|��yt|��W�n�tk �r���w�Y�nX�|j�r&||jk�r&q�|�|�}|r�|�||�}|dk r�|�|��x,|���D�] }|j�sng�|_|j�|���q\W�|�j�|� ���}x2|D�]*} | |k�r�g�|| <�|| ��|| ����q�W�q�W�qvW�q\W�x$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)rt���N)r���rg���rh���Z start_tree�traverse_byrl���rm���rk����runZleaves�anyr*���rZ���r����r���ZBaseZdepthZkeep_line_orderZ get_linenor'����remover���� ValueErrorZfixers_applied�match� transform�replacer���r,���Zfinish_treer����) rn���r����r���r.���Z match_set�node�results�newZnew_matchesZfxrr���r���r���r�������sJ���� $zRefactoringTool.refactor_treec�������������C���s^���|sdS�xP|D�]H}xB||j��D�]4}|�|�}|r|�||�}|dk r|�|��|}qW�qW�dS�)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���� zRefactoringTool.traverse_byc�������������C���s����|�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. Nr���zNo changes to %szNot writing changes to %s)ri���r���r����r����r~���rW���� write_file)rn���r����r����r����r����r����r����r���r���r���r�������s����zRefactoringTool.processed_filec�������������C���s����yt�j|d|dd�}W�n0�tk rF�}�z|��d||��dS�d}~X�Y�nX�|�F�y|�|��W�n0�tk r��}�z|��d||��W�dd}~X�Y�nX�W�dQ�R�X�|��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)rA���r����r����r����r����r~���re���)rn���r����r����r����r�����fpr����r���r���r���r�������s����*zRefactoringTool.write_filez>>> z... c������� ��� ���C���s��g�}d}d}d}d}x�|j�dd�D�]�}|d7�}|����|�j�r�|dk r\|�|��||||���|}|g}|�|�j�} |d| ��}q"|dk r�|�||�j��s�|||�j����d�kr�|� |��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.) Nr���T)�keependsr���r����rr���)� splitlines�lstripr����PS1r,����refactor_doctest�find�PS2�rstripr���rz���) rn���r����r�����result�blockZblock_lineno�indent�lineno�line�ir���r���r���r������s:���� z"RefactoringTool.refactor_docstringc������� ��� ������s��y���||���}W�nf�tk rx�}�zH�j�tj�rRx|D�]}��d|�d���q8W���d|||j j |��|S�d}~X�Y�nX���||��rt|�j dd�}|d|d���||d�d���} }|d��d�s�|d��d7��<����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����r���rp���r���c����������������s���g�|�]}���j��|��qS�r���)r����)r1���r����)r����rn���r���r���r4���Z��s����z4RefactoringTool.refactor_doctest.<locals>.<listcomp>)�parse_blockr!���rY���ZisEnabledForrc����DEBUGr~���r����r����r����r���r����r����r�����endswithr�����pop) rn���r����r����r����r����r����r����r����r����Zclippedr���)r����rn���r���r����@��s$���� "z RefactoringTool.refactor_doctestc�������������C���s����|�j�rd}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�W�d�S�) N�werez need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:r���zThere was 1 error:zThere were %d errors:)re���ri���r}���rd���rb���rw���)rn���r�����file�messager����r����r����r���r���r���� summarize]��s$���� zRefactoringTool.summarizec�������������C���s"���|�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. )r���Zparse_tokens� wrap_toksrD���r����)rn���r����r����r����r����r���r���r���r����t��s����zRefactoringTool.parse_blockc�������������c���sh���t��|��||�j�}xN|D�]F\}}\}}\} } }||d�7�}| |d�7�} ||||f| | f|fV��qW�dS�)z;Wraps a tokenize stream to systematically modify start/end.r���N)r���r@���� gen_lines�__next__)rn���r����r����r����r+���r���rQ���Zline0Zcol0Zline1Zcol1Z line_textr���r���r���r����~��s ����zRefactoringTool.wrap_toksc�������������c���s����||�j��}||�j�}|}xV|D�]N}|�|�r@|t|�d��V��n(||���d�krXdV��ntd||f���|}qW�x dV��qrW�dS�)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����r���rw���r�����AssertionError)rn���r����r�����prefix1Zprefix2�prefixr����r���r���r���r�������s���� zRefactoringTool.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�Zd�ZdS�)�MultiprocessingUnsupportedN)r���r���r���r���r���r���r���r�������s���r����c�������������������sB���e�Zd�Z��fdd�Zd��fdd� Z��fdd�Z��fd d �Z���ZS�)�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��|dkrt�t���|||�S�ydd�l��W�n�tk r@���t�Y�nX��jd�k rTtd��������_��� ���_ ���fdd�t|�D��}z.x|D�]}|����q�W�t�t���|||��W�d��j� ���xt|�D�]}�j�d���q�W�x|D�]}|���r�|� ���q�W�d��_X�d�S�)Nr���r���z already doing multiple processesc����������������s���g�|�]}��j��jd���qS�))�target)ZProcess�_child)r1���r����)�multiprocessingrn���r���r���r4������s���z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r����r����r����r�����ImportErrorr����r�����RuntimeErrorZ JoinableQueueZLockr�����range�startrz����putZis_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|\}}ztt|��j||��W�d�|�j�����X�|�j����}qW�d�S�)N)r����ra���r����r����r����Z task_done)rn���Ztaskr����r����)r����r���r���r�������s���� z"MultiprocessRefactoringTool._childc����������������s2���|�j�d�k r|�j��||f��ntt|��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���Zpgen2r���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��� (����