관리-도구
편집 파일: refactor.cpython-39.pyc
a �����DOgsk����������������������@���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__�pkgutil�iter_modules�__path__� startswith�append)Z fixer_pkgZ remove_prefix�pkgZ fix_names�finder�name�ispkg��r����(/usr/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)�pat�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$���� �r4���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���r9���r���r9���r����get_fixers_from_packaged���s���� �r<���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_docstringrD����ignore�features�tp�valuer���rB���r����_detect_future_featureso���sB���� r[���c�������������������@���s���e�Zd�ZdZdS�)� FixerErrorzA fixer could not be loaded.N)r���r���r����__doc__r���r���r���r���r\�������s���r\���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. Nr_����printr`����execra���r^���F)�convert�logger)&�fixers�explicit�_default_options�copy�optionsr$���r ���r.����grammar�keywords�getra����errors�logging� getLoggerre���� fixer_log�wroter���ZDriverr���rd���� get_fixers� pre_order� post_order�files�bmZ BottomMatcher�BMZ bmi_pre_orderZbmi_post_orderr���Z BM_compatibleZ add_fixerr���r4����bmi_pre_order_heads�bmi_post_order_heads)�selfZfixer_namesrj���rg���r3���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���r6���r������N�_��c�����������������S���s���g�|�]}|�����qS�r���)�title)r7���r(���r���r���r���r;������������z.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %s�preZpostzIllegal fixer order: %rZ run_order��key)rf���r����rsplitr����FILE_PREFIX�len�split�CLASS_PREFIX�join�getattr�AttributeErrorr\���rj���rq���rg����log_message� log_debug�orderr����operator� attrgetter�sort)r{���Zpre_order_fixersZpost_order_fixersZfix_mod_path�modr8����parts� class_nameZ fix_classr3���Zkey_funcr���r���r���rs�������s:���� � zRefactoringTool.get_fixersc������������������O���s�����dS�)zCalled when an error occurs.Nr���)r{����msg�args�kwdsr���r���r���� log_error��s����zRefactoringTool.log_errorc�����������������G���s���|r||�}|�j��|��dS�)zHook to log a message.N)re����info�r{���r����r����r���r���r���r������s����zRefactoringTool.log_messagec�����������������G���s���|r||�}|�j��|��d�S�r=���)re����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���)r{����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)r{����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�<�qdS�)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 %sr6���r���c�����������������S���s���g�|�]}|��d��s|�qS�r5���)r���)r7���Zdnr���r���r���r;���2��r����z0RefactoringTool.refactor_dir.<locals>.<listcomp>N) r�����extsep�walkr����r����r���r�����splitextr����r����) r{���Zdir_namer����r����Zpy_ext�dirpath�dirnames� filenamesr����fullnamer���r���r���r���� ��s���� �zRefactoringTool.refactor_dirc�������������� ���C���s����zt�|d�}W�n4�tyB�}�z|��d||��W�Y�d}~dS�d}~0�0�zt�|j�d�}W�|����n |����0�tj�|d|dd��}|���|fW��d����S�1�s�0����Y��dS�) zG Do our best to decode a Python source file correctly. �rbzCan't open %s: %sN)NNr���r'���r�����encoding�newline) �open�OSErrorr����r����detect_encodingrL����closerJ����read)r{���r�����f�errr����r���r���r����_read_python_source4��s����z#RefactoringTool._read_python_sourcec�����������������C���s����|���|�\}}|du�rdS�|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 %sr}���)r����r����zNo changes in %s)r����r�����refactor_docstringra����processed_file�refactor_string�was_changed�str)r{���r����r����r�����inputr�����output�treer���r���r���r����D��s �����zRefactoringTool.refactor_filec�������������� ���C���s����t�|�}d|v�rtj|�j_zfz|�j�|�}W�nF�tyr�}�z.|��d||jj |��W�Y�d}~W�|�j|�j_dS�d}~0�0�W�|�j|�j_n|�j|�j_0�||_ |��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. r_���zCan't parse %s: %s: %sNzRefactoring %s) r[���r ���Z!python_grammar_no_print_statementr���rk���Zparse_stringr%���r����� __class__r����future_featuresr����� refactor_tree)r{����datar���rX���r����r����r���r���r���r����[��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����ra���r����r����r����r����)r{���r����r����r����r����r���r���r����refactor_stdinv��s���� zRefactoringTool.refactor_stdinc����������� ��� ���C���s���t�|�j|�j�D�]}|�||��q|��|�j|�����|��|�j|�����|�j�|� ���}t |�����r�|�jjD��]B}||v�rj||�rj||�j tjjdd��|jr�||�j tjjd��t||��D�]�}|||�v�r�||��|��zt|��W�n�t��y����Y�q�Y�n0�|j�r||jv��rq�|�|�}|r�|�||�}|dur�|�|��|���D�] }|j�s\g�|_|j�|���qJ|�j�|� ���}|D�]*} | |v�r�g�|| <�|| ��|| ����q�q�qjqTt�|�j|�j�D�]}|�||���q�|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)r�����reverser����N)r���rt���ru���Z start_tree�traverse_byry���rz���rx����runZleaves�anyr/���rf���r����r���ZBase�depthZkeep_line_orderZ get_linenor,����remover���� ValueErrorZfixers_applied�match� transform�replacer���r1���Zfinish_treer����) r{���r����r���r3���Z match_set�node�results�newZnew_matchesZfxrr���r���r���r�������sJ���� zRefactoringTool.refactor_treec�����������������C���sV���|sdS�|D�]D}||j��D�]4}|�|�}|r|�||�}|dur|�|��|}qqdS�)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����)r{���rf���Z traversalr����r3���r����r����r���r���r���r�������s���� zRefactoringTool.traverse_byc�����������������C���s����|�j��|��|du�r.|��|�d�}|du�r.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)rv���r���r����r����r����ra���� write_file)r{���r����r����r����r����r����r����r���r���r���r�������s����zRefactoringTool.processed_filec�����������������C���s����zt�j|d|dd�}W�n4�tyJ�}�z|��d||��W�Y�d}~dS�d}~0�0�|�R�z|�|��W�n2�ty��}�z|��d||��W�Y�d}~n d}~0�0�W�d����n1�s�0����Y��|��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. �wr���r����zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rJ���r����r����r����r����r����rr���)r{���r����r����r����r�����fpr����r���r���r���r������s����BzRefactoringTool.write_filez>>> z... c����������� ��� ���C���s��g�}d}d}d}d}|j�dd�D�]�}|d7�}|����|�j�r~|durZ|�|��||||���|}|g}|�|�j�} |d| ��}q |dur�|�||�j��s�|||�j����d�kr�|� |��q |dur�|�|��||||���d}d}|� |��q |du�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����r���)� splitlines�lstripr����PS1r1����refactor_doctest�find�PS2�rstripr���r����) r{���r����r�����result�blockZblock_lineno�indent�lineno�line�ir���r���r���r������sD�������� �z"RefactoringTool.refactor_docstringc����������� ��� ������s.��z���||���}W�nh�tyz�}�zP�j�tj�rL|D�]}��d|�d���q4��d|||j j |��|W��Y�d}~S�d}~0�0���||��r*t|�j dd�}|d|d���||d�d���} }| dg|d��ks�J�| ��|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: %sNTr����r���r}���r���c��������������������s���g�|�]}���j��|��qS�r���)r����)r7���r�����r����r{���r���r���r;���^��r����z4RefactoringTool.refactor_doctest.<locals>.<listcomp>)�parse_blockr%���re����isEnabledForro����DEBUGr����r����r����r����r���r����r����r�����endswithr�����pop) r{���r����r����r����r����r����r����r����r����Zclippedr���r����r���r����D��s(�����"z RefactoringTool.refactor_doctestc�����������������C���s����|�j�rd}nd}|�js$|��d|��n"|��d|��|�jD�]}|��|��q6|�jrl|��d��|�jD�]}|��|��q\|�jr�t|�j�dkr�|��d��n|��dt|�j���|�jD�]"\}}}|�j|g|�R�i�|���q�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:)rr���rv���r����rq���rn���r����)r{���r�����file�messager����r����r����r���r���r���� summarizea��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_toksrM���r����)r{���r����r����r����r����r���r���r���r����x��s����zRefactoringTool.parse_blockc�����������������c���sd���t��|��||�j�}|D�]F\}}\}}\} } }||d�7�}| |d�7�} ||||f| | f|fV��qdS�)z;Wraps a tokenize stream to systematically modify start/end.r���N)r���rI���� gen_lines�__next__)r{���r����r����r����r0���r!���rZ���Zline0Zcol0Zline1Zcol1Z line_textr���r���r���r�������s ����zRefactoringTool.wrap_toksc�����������������c���sx���||�j��}||�j�}|}|D�]N}|�|�r>|t|�d��V��n(||���d�krVdV��ntd||f���|}qdV��qldS�)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=%rr���)r����r����r���r����r�����AssertionError)r{���r����r�����prefix1Zprefix2�prefixr����r���r���r���r�������s���� zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)r���r���r���rh���r����r����r|���rs���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@���� 7( O��� + r^���c�������������������@���s���e�Zd�ZdS�)�MultiprocessingUnsupportedNr���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|i�|���d�|�_d�|�_d�S�r=���)�superr����r|����queue�output_lock�r{���r�����kwargs�r����r���r���r|������s����z$MultiprocessRefactoringTool.__init__Fr���c�������������� ������s>��|dkrt�t���|||�S�zdd�l��W�n�ty>���t�Y�n0��jd�urRtd��������_��� ���_ ���fdd�t|�D��}zn|D�]}|����q�t�t���|||��W��j� ���t|�D�]}�j�d���q�|D�]}|���r�|� ���q�d��_nL�j� ���t|�D�]}�j�d����q�|D�]}|����r|� ����qd��_0�d�S�)Nr���r���z already doing multiple processesc��������������������s���g�|�]}��j��jd���qS�))�target)�Process�_child)r7���r������multiprocessingr{���r���r���r;������s����z8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)r���r����r����r ���ImportErrorr����r���RuntimeError� JoinableQueue�Lockr���range�startr�����put�is_alive)r{���r����r����r����Z num_processes� processesr(���r����r��r ��r���r�������sF���� � � � � z$MultiprocessRefactoringTool.refactorc��������������������s\���|�j����}|d�urX|\}}z$tt|��j|i�|���W�|�j�����n|�j�����0�|�j����}q d�S�r=���)r��rm���r���r����r����� task_done)r{���Ztaskr����r��r��r���r���r�����s���� ��z"MultiprocessRefactoringTool._childc��������������������s6���|�j�d�ur|�j��||f��ntt|��j|i�|��S�d�S�r=���)r��r��r���r����r����r��r��r���r���r�������s���� ��z)MultiprocessRefactoringTool.refactor_file)FFr���)r���r���r���r|���r����r��r����� __classcell__r���r���r��r���r�������s������r����)T)#r]���� __author__rJ���r����r���r����ro���r����r*���� itertoolsr���Zpgen2r���r���r���Z fixer_utilr���r���r���r ���r ���rw���r���r%���r���r"���r4���r<���r?���r[���r\����objectr^���r����r����r���r���r���r����<module>���s8��� (����