Edit File: refactor.cpython-36.opt-2.pyc
3 � \=m������������������@���s8��d�Z�ddlZddlZddlZddlZddlZddlZddlmZ�ddl m Z mZmZ�ddl mZ�ddlmZmZ�ddlmZ�d%d d�ZG�dd ��d e�Zdd��Zdd��Zdd��Zdd��Zejd&k�r�ddlZejZdd��Zdd��Z neZeZeZ dd��Z!G�dd��de�Z"G�dd ��d e#�Z$G�d!d"��d"e�Z%G�d#d$��d$e$�Z&dS�)'z#Guido van Rossum <guido@python.org>�����N)�chain����)�driver�tokenize�token)� find_root)�pytree�pygram)�btm_matcherTc�������������C���st���t�|�g�g�dg�}tjj|j�}g�}xLttj|��D�]:}|jd�r2|jd�r2|rZ|dd���}|j |d�d����q2W�|S�)N�*�fix_z.py�����������) � __import__�os�path�dirname�__file__�sorted�listdir� startswith�endswith�append)Z fixer_pkgZ remove_prefixZpkgZ fixer_dirZ fix_names�name��r����(/usr/lib64/python3.6/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(|�jd�kr t�|�jhS�t�|�tj�rH|�jrDt|�j�S�t�t�|�tj �r�t ��}x*|�jD�] }x|D�]}|jt|���qlW�qbW�|S�td|����d�S�)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�jt�}g�}x�|�D�]|}|jrjyt|j�}W�n�tk rJ���|j|��Y�q�X�xB|D�]}||�j|��qRW�q|jd�k r�||j�j|��q|j|��qW�x,tt j jj��t j j �D�]}||�j|��q�W�t|�S�)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"���� r5���c����������������s�����fdd�t���d�D��S�)Nc����������������s���g�|�]}��d��|��qS�)�.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�dd�S�)Nz � )�replace)�inputr���r���r����_from_system_newlinesr���s����rA���c�������������C���s ���t�jdkr|�jdt�j�S�|�S�d�S�)Nr>���)r����linesepr?���)r@���r���r���r����_to_system_newlinest���s���� rC���c����������������sT��d}t�jtj|��j�����fdd�}ttjt�jtj h�}t ��}y�x�|��\}}||krVq@q@|tjkrl|rfP�d}q@|tjko||dk�r,|��\}}|tjks�|dkr�P�|��\}}|tjks�|dkr�P�|��\}}|tj kr�|dkr�|��\}}xJ|tjk�r(|j|��|��\}}|tj k�s|d k�rP�|��\}}q�W�q@P�q@W�W�n�tk �rJ���Y�nX�t|�S�) NFc�����������������s���t����}�|�d�|�d�fS�)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)�sourceZhave_docstringrG����ignore�features�tp�valuer���)rF���r����_detect_future_features���sD���� r^���c���������������@���s���e�Zd�ZdS�)� FixerErrorN)r���r ���r!���r���r���r���r���r_�������s���r_���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�|�_|�jj��|�_|d�k r0|�jj|��|�jd�rDtj|�_ntj |�_|�jj d�|�_g�|�_t jd�|�_g�|�_d|�_tj|�jtj|�jd�|�_|�j��\|�_|�_g�|�_tj��|�_g�|�_g�|�_xXt|�j|�j�D�]F}|j r�|�jj!|��q�||�jkr�|�jj"|��q�||�jkr�|�jj"|��q�W�t#|�j�|�_$t#|�j�|�_%d�S�)Nra���rb���r`���F)�convert�logger)&�fixers�explicit�_default_options�copy�optionsr&���r ����!python_grammar_no_print_statement�grammarr/����getrb����errors�loggingZ getLoggerrd���� fixer_log�wroter���ZDriverr���rc���� get_fixers� pre_order� post_order�files�bmZ BottomMatcher�BMZ bmi_pre_orderZbmi_post_orderr���Z BM_compatibleZ add_fixerr���r5����bmi_pre_order_heads�bmi_post_order_heads)�selfZfixer_namesri���rf���r4���r���r���r����__init__����s<���� zRefactoringTool.__init__c�������������C���s\��g�}g�}�x&|�j�D��]}t|i�i�dg�}|jdd�d�}|j|�j�rV|t|�j�d���}|jd�}|�jdjdd��|D����}yt ||�}W�n$�t k r����td||f���Y�nX�||�j|�j �} | jr�|�jd k r�||�jkr�|�jd |��q|�jd|��| jdk�r|j| ��q| jd k�r |j| ��qtd| j���qW�tjd�} |j| d��|j| d��||fS�)Nr���r6���r����_��c�������������S���s���g�|�]}|j����qS�r���)�title)r7���r)���r���r���r���r:�������s����z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ run_order)�key���)re���r����rsplitr����FILE_PREFIX�len�split�CLASS_PREFIX�join�getattr�AttributeErrorr_���ri���ro���rf����log_message� log_debug�orderr����operator� attrgetter�sort)ry���Zpre_order_fixersZpost_order_fixersZfix_mod_path�modr8����parts� class_nameZ fix_classr4���Zkey_funcr���r���r���rq�������s8���� zRefactoringTool.get_fixersc�������������O���s�����d�S�)Nr���)ry����msg�args�kwdsr���r���r���� log_error��s����zRefactoringTool.log_errorc�������������G���s���|r||�}|�j�j|��d�S�)N)rd����info)ry���r����r����r���r���r���r������s����zRefactoringTool.log_messagec�������������G���s���|r||�}|�j�j|��d�S�)N)rd����debug)ry���r����r����r���r���r���r������s����zRefactoringTool.log_debugc�������������C���s���d�S�)Nr���)ry����old_text�new_text�filename�equalr���r���r����print_output��s����zRefactoringTool.print_outputc�������������C���s<���x6|D�].}t�jj|�r&|�j|||��q|�j|||��qW�d�S�)N)r���r����isdir�refactor_dir� refactor_file)ry����items�write� doctests_onlyZdir_or_filer���r���r����refactor#��s���� zRefactoringTool.refactorc������� ������C���s����t�jd�}x�t�j|�D�]�\}}}|�jd|��|j���|j���xH|D�]@}|jd��rBt�jj|�d�|krBt�jj||�} |�j | ||��qBW�dd��|D��|d�d��<�qW�d�S�)N�pyzDescending into %sr6���r���c�������������S���s���g�|�]}|j�d��s|�qS�)r6���)r���)r7���Zdnr���r���r���r:���>��s����z0RefactoringTool.refactor_dir.<locals>.<listcomp>) r����extsep�walkr����r����r���r����splitextr����r����) ry���Zdir_namer����r����Zpy_ext�dirpathZdirnames� filenamesr����fullnamer���r���r���r����,��s���� zRefactoringTool.refactor_dirc�������������C���s����yt�|d�}W�n.�tk r<�}�z|�jd||��dS�d�}~X�nX�ztj|j�d�}W�d�|j���X�t|d|d��}t|j ���|fS�Q�R�X�d�S�)N�rbzCan't open %s: %sr���r(���)�encoding)NN) �open�OSErrorr����r����detect_encodingrO����close�_open_with_encodingrA����read)ry���r�����f�errr����r���r���r����_read_python_source@��s���� z#RefactoringTool._read_python_sourcec�������������C���s����|�j�|�\}}|d�krd�S�|d7�}|rn|�jd|��|�j||�}|�jsL||kr`|�j|||||��q�|�jd|��nH|�j||�}|�js�|r�|jr�|�jt|�d�d��|||d��n|�jd|��d�S�)Nr>���zRefactoring doctests in %szNo doctest changes in %sr���)r����r����zNo changes in %sr���)r����r�����refactor_docstringrb����processed_file�refactor_string�was_changed�str)ry���r����r����r����r@���r�����output�treer���r���r���r����P��s����zRefactoringTool.refactor_filec�������������C���s����t�|�}d|krtj|�j_zJy|�jj|�}W�n4�tk r`�}�z|�jd||jj |��d�S�d�}~X�nX�W�d�|�j|�j_X�||_ |�jd|��|�j||��|S�)Nra���zCan't parse %s: %s: %szRefactoring %s) r^���r ���rj���r���rk���Zparse_stringr'���r����� __class__r����future_featuresr����� refactor_tree)ry����datar���r[���r����r����r���r���r���r����g��s���� zRefactoringTool.refactor_stringc�������������C���s����t�jj��}|rN|�jd��|�j|d�}|�js2||krB|�j|d|��q�|�jd��n:|�j|d�}|�jsj|r~|jr~|�jt |�d|��n |�jd��d�S�)NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin) �sys�stdinr����r����r����rb���r����r����r����r����)ry���r����r@���r����r����r���r���r����refactor_stdin���s���� zRefactoringTool.refactor_stdinc������� ������C���s���x"t�|�j|�j�D�]}|j||��qW�|�j|�j|j����|�j|�j|j����|�jj|j ���}�xvt |j����rАx`|�jjD��]R}||ko�||�rv||�j tjjdd��|jr�||�j tjjd���x t||��D���]�}|||�kr�||�j|��yt|��W�n�tk �r���w�Y�nX�|j�r(||jk�r(q�|j|�}|r�|j||�}|d�k r�|j|��x,|j��D�] }|j�spg�|_|jj|���q^W�|�jj|j ���}x2|D�]*} | |k�r�g�|| <�|| �j|| ����q�W�q�W�qvW�q\W�x$t�|�j|�j�D�]}|j||���q�W�|jS�)NT)r~����reverse)r~���)r���rr���rs���Z start_tree�traverse_byrw���rx���rv���ZrunZleaves�anyr0���re���r����r���ZBaseZdepthZkeep_line_orderZ get_linenor-����remover���� ValueErrorZfixers_applied�match� transformr?���r���r2���Zfinish_treer����) ry���r����r���r4���Z match_set�node�results�newZnew_matchesZfxrr���r���r���r�������sJ���� $zRefactoringTool.refactor_treec�������������C���s^���|sd�S�xP|D�]H}xB||j��D�]4}|j|�}|r|j||�}|d�k r|j|��|}qW�qW�d�S�)N)r#���r����r����r?���)ry���re���Z traversalr����r4���r����r����r���r���r���r�������s���� zRefactoringTool.traverse_byc�������������C���s����|�j�j|��|d�kr.|�j|�d�}|d�kr.d�S�||k}|�j||||��|r`|�jd|��|�js`d�S�|rv|�j||||��n|�jd|��d�S�)Nr���zNo changes to %szNot writing changes to %s)rt���r���r����r����r����rb���� write_file)ry���r����r����r����r����r����r����r���r���r���r�������s����zRefactoringTool.processed_filec����������%���C���s����yt�|d|d�}W�n.�tk r@�}�z|�jd||��d�S�d�}~X�nX�zHy|jt|���W�n0�tk r��}�z|�jd||��W�Y�d�d�}~X�nX�W�d�|j���X�|�jd|��d|�_d�S�)N�w)r����zCan't create %s: %szCan't write %s: %szWrote changes to %sT)r����r����r����r����rC���r����r����rp���)ry���r����r����r����r����r����r����r���r���r���r���� ��s����$ zRefactoringTool.write_filez>>> z... c������� ������C���s��g�}d�}d�}d�}d}x�|j�dd�D�]�}|d7�}|j��j|�j�r�|d�k r\|j|�j||||���|}|g}|j|�j�} |d�| ��}q"|d�k r�|j||�j��s�|||�jj���d�kr�|j |��q"|d�k r�|j|�j||||���d�}d�}|j |��q"W�|d�k �r|j|�j||||���dj |�S�)Nr���T)�keependsr���r>���r|���)� splitlines�lstripr����PS1r2����refactor_doctest�find�PS2�rstripr���r����) ry���r@���r�����result�blockZblock_lineno�indent�lineno�line�ir���r���r���r����%��s:���� z"RefactoringTool.refactor_docstringc������� ���������s��y�j�||���}W�nd�tk rv�}�zH�jjtj�rRx|D�]}�jd|jd���q8W��jd|||j j |��|S�d�}~X�nX��j||��r t|�j dd�}|d�|d���||d�d����} }|d �jd�s�|d��d7��<����j�|jd��g}|�r |���fdd �|D��7�}|S�)Nz Source: %sr>���z+Can't parse docstring in %s line %s: %s: %sT)r����r���r���c����������������s���g�|�]}���j��|��qS�r���)r����)r7���r����)r����ry���r���r���r:���j��s����z4RefactoringTool.refactor_doctest.<locals>.<listcomp>r���r���)�parse_blockr'���rd���ZisEnabledForrn����DEBUGr����r����r����r����r���r����r����r����r���r�����pop) ry���r����r����r����r����r����r����r����r����Zclippedr���)r����ry���r���r����P��s$���� "z RefactoringTool.refactor_doctestc�������������C���s����|�j�rd}nd}|�js$|�jd|��n&|�jd|��x|�jD�]}|�j|��q8W�|�jrt|�jd��x|�jD�]}|�j|��qbW�|�jr�t|�j�dkr�|�jd��n|�jdt|�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:)rp���rt���r����ro���rm���r����)ry���r�����file�messager����r����r����r���r���r���� summarizem��s$���� zRefactoringTool.summarizec�������������C���s"���|�j�j|�j|||��}t��|_|S�)N)r���Zparse_tokens� wrap_toksrP���r����)ry���r����r����r����r����r���r���r���r�������s����zRefactoringTool.parse_blockc�������������c���sh���t�j|�j||�j�}xN|D�]F\}}\}}\} } }||d�7�}| |d�7�} ||||f| | f|fV��qW�d�S�)Nr���)r���rL���� gen_lines�__next__)ry���r����r����r����r1���r#���r]���Zline0Zcol0Zline1Zcol1Z line_textr���r���r���r�������s ����zRefactoringTool.wrap_toksc�������������c���s����||�j��}||�j�}|}xV|D�]N}|j|�r@|t|�d���V��n(||j��d�krXdV��ntd||f���|}qW�x dV��qrW�d�S�)Nr>���zline=%r, prefix=%rr|���)r����r����r���r����r�����AssertionError)ry���r����r�����prefix1Zprefix2�prefixr����r���r���r���r�������s���� zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)r���r ���r!���rg���r����r����rz���rq���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���r`�������s:��� 4( O� + r`���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����rz����queue�output_lock)ry���r�����kwargs)r����r���r���rz������s����z$MultiprocessRefactoringTool.__init__Fr���c����������������s��|dkrt�t��j|||�S�ydd�l��W�n�tk r@���t�Y�nX��jd�k rTtd����j���_��j ���_ ���fdd�t|�D��}z.x|D�]}|j���q�W�t�t��j|||��W�d��jj ���xt|�D�]}�jjd���q�W�x|D�]}|j��r�|j ���q�W�d��_X�d�S�)Nr���r���z already doing multiple processesc����������������s���g�|�]}��j��jd���qS�))�target)ZProcess�_child)r7���r����)�multiprocessingry���r���r���r:������s���z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r����r����r����r�����ImportErrorr����r�����RuntimeErrorZ JoinableQueueZLockr�����range�startr�����putZis_alive)ry���r����r����r����Z num_processesZ processesr)���r����)r����)r����ry���r���r�������s2���� z$MultiprocessRefactoringTool.refactorc���������� ������sR���|�j�j��}xB|d�k rL|\}}ztt|��j||��W�d�|�j�j���X�|�j�j��}qW�d�S�)N)r����rl���r����r����r����Z task_done)ry���Ztaskr����r����)r����r���r���r�������s���� z"MultiprocessRefactoringTool._childc����������������s2���|�j�d�k r|�j�j||f��ntt|��j||�S�d�S�)N)r����r����r����r����r����)ry���r����r����)r����r���r���r�������s���� z)MultiprocessRefactoringTool.refactor_file)FFr���)r���r ���r!���rz���r����r����r����� __classcell__r���r���)r����r���r�������s ����r����)T)r���r���)'� __author__r���r����rn���r����r+���rM���� itertoolsr���Zpgen2r���r���r���Z fixer_utilr���r|���r���r ���r ���ru���r���r'���r���r$���r5���r;���r=����version_info�codecsr����r����rA���rC���r^���r_����objectr`���r����r����r���r���r���r����<module>���sD��� (����