a ÕDOgskã@sdZdZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z mZddlmZddlmZmZdd lmZd!d d „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 éN)Úchainé)ÚdriverÚtokenizeÚtoken)Ú find_root)ÚpytreeÚpygram)Ú btm_matcherTcCsTt|ggdgƒ}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__ÚpkgutilZ iter_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_namess   rc@s eZdZdS)Ú _EveryNodeN©Ú__name__Ú __module__Ú __qualname__rrrrr+srcCs’t|tjtjfƒr(|jdur t‚|jhSt|tjƒrH|jrDt|jƒSt‚t|tj ƒr‚t ƒ}|jD]}|D]}|  t|ƒ¡qhq`|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) Ú isinstancerZ NodePatternZ LeafPatternÚtyperZNegatedPatternZcontentÚ_get_head_typesZWildcardPatternÚsetÚupdateÚ Exception)ZpatÚrÚpÚxrrrr/s     rc Cs¼t t¡}g}|D]v}|jrbzt|jƒ}WntyF| |¡Yqˆ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ÚpatternrrrZ _accept_typerr Úpython_grammarZ symbol2numberÚvaluesÚtokensÚextendÚdict)Z fixer_listZ head_nodesZeveryÚfixerZheadsZ node_typerrrÚ_get_headnode_dictKs$     ÿr0cs‡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©Zpkg_namerrÚ hsÿz+get_fixers_from_package..F)rr5rr5rÚget_fixers_from_packageds ÿr7cCs|S©Nr)ÚobjrrrÚ _identityksr:csVd}t t |¡j¡‰‡fdd„}ttjtjtj hƒ}t ƒ}zü|ƒ\}}||vrTq>q>|tj krl|rfq6d}q>|tj kr6|dkr6|ƒ\}}|tj ksž|dkr¢q6|ƒ\}}|tj ks¾|dkrÂq6|ƒ\}}|tj krè|dkrè|ƒ\}}|tj kr4| |¡|ƒ\}}|tj ks.|d kr"q4|ƒ\}}qèq>q6q>WntyLYn0t|ƒS) NFcstˆƒ}|d|dfS)Nrr)Únext)Útok©ÚgenrrÚadvancersz(_detect_future_features..advanceTÚfromZ __future__Úimportú(ú,)rÚgenerate_tokensÚioÚStringIOÚreadlineÚ frozensetrÚNEWLINEÚNLÚCOMMENTr ÚSTRINGÚNAMEÚOPÚaddÚ StopIteration)ÚsourceZhave_docstringr?ÚignoreÚfeaturesÚtpÚvaluerr=rÚ_detect_future_featuresosB           rVc@seZdZdZdS)Ú FixerErrorzA fixer could not be loaded.N)rrrÚ__doc__rrrrrW—srWc@sæeZdZddddœZdZdZd4dd„Zdd „Zd d „Zd d „Z dd„Z dd„Z d5dd„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Ú exec_functionÚwrite_unchanged_filesZFixr NcCsJ||_|p g|_|j ¡|_|dur0|j |¡tj ¡|_|jdrR|jj d=n|jdrf|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ä||jvr|j "|¡qä||jvrä|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\rYF)ÚconvertÚlogger)&ÚfixersÚexplicitÚ_default_optionsÚcopyÚoptionsr!r r*ÚgrammarÚkeywordsÚgetr\ÚerrorsÚloggingZ getLoggerr`Ú fixer_logÚwroterZDriverrr_Ú get_fixersÚ pre_orderÚ post_orderÚfilesÚbmZ BottomMatcherÚBMZ bmi_pre_orderZbmi_post_orderrZ BM_compatibleZ add_fixerrr0Úbmi_pre_order_headsÚbmi_post_order_heads)ÚselfZ fixer_namesrerbr/rrrÚ__init__¤sB         þ    zRefactoringTool.__init__c CsVg}g}|jD]}t|iidgƒ}| dd¡d}| |j¡rR|t|jƒd…}| d¡}|jd dd „|Dƒ¡}zt ||ƒ}Wn$t y¨t d ||fƒd‚Yn0||j |j ƒ} | jrà|jd urà||jvrà| d |¡q| d |¡| jdkr| | ¡q| jdkr| | ¡qt d| jƒ‚qt 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. r r2réÿÿÿÿNÚ_ÚcSsg|] }| ¡‘qSr)Útitle)r3r$rrrr6ëóz.RefactoringTool.get_fixers..zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ run_order©Úkey)rarÚrsplitrÚ FILE_PREFIXÚlenÚsplitÚ CLASS_PREFIXÚjoinÚgetattrÚAttributeErrorrWrerkrbÚ log_messageÚ log_debugÚorderrÚoperatorÚ attrgetterÚsort) ruZpre_order_fixersZpost_order_fixersZ fix_mod_pathÚmodr4ÚpartsÚ class_nameZ fix_classr/Zkey_funcrrrrmÛs:    ÿ         zRefactoringTool.get_fixerscOs‚dS)zCalled when an error occurs.Nr)ruÚmsgÚargsÚkwdsrrrÚ log_errorszRefactoringTool.log_errorcGs|r ||}|j |¡dS)zHook to log a message.N)r`Úinfo©rurrrrrr†szRefactoringTool.log_messagecGs|r ||}|j |¡dSr8)r`Údebugr”rrrr‡ szRefactoringTool.log_debugcCsdS)zTCalled with the old version, new version, and filename of a refactored file.Nr)ruÚold_textÚnew_textÚfilenameÚequalrrrÚ print_outputszRefactoringTool.print_outputcCs8|D].}tj |¡r$| |||¡q| |||¡qdS)z)Refactor a list of files and directories.N)ÚosÚpathÚisdirÚ refactor_dirÚ refactor_file)ruÚitemsÚwriteÚ doctests_onlyZ dir_or_filerrrÚrefactors zRefactoringTool.refactorc Csštjd}t |¡D]€\}}}| d|¡| ¡| ¡|D]>}| d¡s>tj |¡d|kr>tj ||¡} |  | ||¡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 %sr2rcSsg|]}| d¡s|‘qSr1)r)r3Zdnrrrr62r{z0RefactoringTool.refactor_dir..N) r›ÚextsepÚwalkr‡r‹rrœÚsplitextrƒrŸ) ruZdir_namer¡r¢Zpy_extÚdirpathZdirnamesÚ filenamesrÚfullnamerrrrž s   ÿzRefactoringTool.refactor_dirc Cs®zt|dƒ}Wn4tyB}z| d||¡WYd}~dSd}~00zt |j¡d}W| ¡n | ¡0tj|d|dd}| ¡|fWdƒS1s 0YdS) zG Do our best to decode a Python source file correctly. ÚrbzCan't open %s: %sN)NNrr#ry©ÚencodingÚnewline) ÚopenÚOSErrorr’rÚdetect_encodingrGÚcloserEÚread)rur˜ÚfÚerrr­rrrÚ_read_python_source4sz#RefactoringTool._read_python_sourcecCsº| |¡\}}|durdS|d7}|rn| d|¡| ||¡}|jsL||kr`| |||||¡q¶| d|¡nH| ||¡}|jsŠ|rª|jrª|jt|ƒdd…|||dn | d|¡dS)zRefactors a file.NÚ zRefactoring doctests in %szNo doctest changes in %srw)r¡r­zNo changes in %s)r¶r‡Úrefactor_docstringr\Úprocessed_fileÚrefactor_stringÚ was_changedÚstr)rur˜r¡r¢Úinputr­ÚoutputÚtreerrrrŸDs    ÿzRefactoringTool.refactor_filec Cs°t|ƒ}d|vrtj|j_zfz|j |¡}WnFtyr}z.| d||jj |¡WYd}~W|j|j_dSd}~00W|j|j_n |j|j_0||_ |  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. rZzCan't parse %s: %s: %sNzRefactoring %s) rVr Z!python_grammar_no_print_statementrrfZ parse_stringr"r’Ú __class__rÚfuture_featuresr‡Ú refactor_tree)ruÚdatarrSr¿rµrrrrº[s"   ÿ  þ  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 stdinzzNo doctest changes in stdinzNo changes in stdin) ÚsysÚstdinr³r‡r¸r\r¹rºr»r¼)rur¢r½r¾r¿rrrÚrefactor_stdinvs     zRefactoringTool.refactor_stdinc CsÚt|j|jƒD]}| ||¡q| |j| ¡¡| |j| ¡¡|j |  ¡¡}t |  ¡ƒr²|jj D]B}||vrj||rj||j tjjdd|jr°||j tjjdt||ƒD]ð}|||vrÚ|| |¡z t|ƒWntyþYq¼Yn0|jr||jvrq¼| |¡}|r¼| ||¡}|dur¼| |¡| ¡D] }|js\g|_|j |¡qJ|j |  ¡¡}|D]*} | |vr–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)rrnroZ start_treeÚ traverse_byrsrtrrÚrunZleavesÚanyr+rar‹rZBaseZdepthZkeep_line_orderZ get_linenor(ÚremoverÚ ValueErrorZfixers_appliedÚmatchÚ transformÚreplacerr-Z finish_treer») rur¿rr/Z match_setÚnodeÚresultsÚnewZ new_matchesZfxrrrrr†sJ        zRefactoringTool.refactor_treecCsV|sdS|D]D}||jD]4}| |¡}|r| ||¡}|dur| |¡|}qq dS)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Ï)ruraZ traversalrÐr/rÑrÒrrrrÈÕs    zRefactoringTool.traverse_bycCs†|j |¡|dur.| |¡d}|dur.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)rprr¶ršr‡r\Ú write_file)rur—r˜r–r¡r­r™rrrr¹ìs  zRefactoringTool.processed_filec CsÈztj|d|dd}Wn4tyJ}z| d||¡WYd}~dSd}~00|Rz| |¡Wn2ty’}z| d||¡WYd}~n d}~00Wdƒn1s¨0Y| 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. Úwryr¬zCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rEr¯r°r’r¡r‡rl)rur—r˜r–r­ÚfprµrrrrÓsB zRefactoringTool.write_filez>>> z... c Csg}d}d}d}d}|jddD]È}|d7}| ¡ |j¡r~|durZ| | ||||¡¡|}|g}| |j¡} |d| …}q |dur¸| ||j¡s¬|||j ¡dkr¸|  |¡q |durÖ| | ||||¡¡d}d}|  |¡q |dur | | ||||¡¡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·ry) Ú splitlinesÚlstriprÚPS1r-Úrefactor_doctestÚfindÚPS2Úrstriprrƒ) rur½r˜ÚresultÚblockZ block_linenoÚindentÚlinenoÚlineÚirrrr¸sD  ÿ ÿþ  ÿ   ÿz"RefactoringTool.refactor_docstringc s.zˆ ||ˆ¡}Wnhtyz}zPˆj tj¡rL|D]}ˆ d| d¡¡q4ˆ d|||j j |¡|WYd}~Sd}~00ˆ  ||¡r*t |ƒj dd}|d|d…||dd…} }| dg|dksÖJ| ƒ‚|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: %sNTrÖrrwrcsg|]}ˆˆj|‘qSr)rÝ)r3rã©rárurrr6^r{z4RefactoringTool.refactor_doctest..)Ú parse_blockr"r`Z isEnabledForrjÚDEBUGr‡rÞr’rÀrrÂr¼rØÚendswithrÚÚpop) ruràrârár˜r¿rµrãrÒZclippedrrårrÛDs( ÿ"z RefactoringTool.refactor_doctestcCsÌ|jr d}nd}|js$| d|¡n"| d|¡|jD]}| |¡q6|jrl| d¡|jD]}| |¡q\|jrÈt|jƒdkrŒ| d¡n| dt|jƒ¡|jD]"\}}}|j|g|¢Ri|¤Žq¤dS) 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:)rlrpr†rkrir€)rurêÚfileÚmessagerrr‘rrrÚ summarizeas$       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_toksrHrÁ)ruràrârár¿rrrræxszRefactoringTool.parse_blockc csdt | ||¡j¡}|D]F\}}\}}\} } } ||d7}| |d7} ||||f| | f| fVqdS)z;Wraps a tokenize stream to systematically modify start/end.rN)rrDÚ gen_linesÚ__next__) ruràrârár,rrUZline0Zcol0Zline1Zcol1Z line_textrrrrî‚s   zRefactoringTool.wrap_toksccsx||j}||j}|}|D]N}| |¡r>|t|ƒd…Vn(|| ¡dkrVdVntd||fƒ‚|}qdVqldS)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=%rry)rÚrÝrr€rÞÚAssertionError)ruràráÚprefix1Zprefix2Úprefixrãrrrrïs   zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrrcr‚rrvrmr’r†r‡ršr£ržr¶rŸrºrÆrÂrÈr¹rÓrÚrÝr¸rÛrírærîrïrrrrrY›s@þ 7(   Oÿ  + rYc@s eZdZdS)ÚMultiprocessingUnsupportedNrrrrrrô¤srôcsBeZdZ‡fdd„Zd ‡fdd„ Z‡fdd„Z‡fd d „Z‡ZS) ÚMultiprocessRefactoringToolcs&tt|ƒj|i|¤Žd|_d|_dSr8)ÚsuperrõrvÚqueueÚ output_lock©rurÚkwargs©rÀrrrvªsz$MultiprocessRefactoringTool.__init__Frc s>|dkrttˆƒ |||¡Sz ddl‰Wnty>t‚Yn0ˆjdurRtdƒ‚ˆ ¡ˆ_ˆ  ¡ˆ_ ‡‡fdd„t |ƒDƒ}zn|D] }|  ¡q„ttˆƒ |||¡Wˆj  ¡t |ƒD]}ˆj d¡qº|D]}| ¡rÐ|  ¡qÐdˆ_nLˆj  ¡t |ƒD]}ˆj d¡q|D]}| ¡r|  ¡qdˆ_0dS)Nrrz already doing multiple processescsg|]}ˆjˆjd‘qS))Útarget)ZProcessÚ_child)r3rä©Úmultiprocessingrurrr6¼sÿz8MultiprocessRefactoringTool.refactor..)rörõr£rÿÚ ImportErrorrôr÷Ú RuntimeErrorZ JoinableQueueZLockrøÚrangeÚstartrƒÚputZis_alive)rur r¡r¢Z num_processesZ processesr$rärûrþrr£¯sF ÿ       ÿ ÿ   ú    z$MultiprocessRefactoringTool.refactorcs\|j ¡}|durX|\}}z$tt|ƒj|i|¤ŽW|j ¡n |j ¡0|j ¡}q dSr8)r÷rhrörõrŸZ task_done)ruZtaskrrúrûrrrýÌs  ÿÿz"MultiprocessRefactoringTool._childcs6|jdur|j ||f¡ntt|ƒj|i|¤ŽSdSr8)r÷rrörõrŸrùrûrrrŸ×s  ÿÿz)MultiprocessRefactoringTool.refactor_file)FFr)rrrrvr£rýrŸÚ __classcell__rrrûrrõ¨s  ÿ rõ)T)#rXÚ __author__rEr›rrÄrjr‰r&Ú itertoolsrZpgen2rrrZ fixer_utilrryrr r rqrr"rrr0r7r:rVrWÚobjectrYrôrõrrrrÚs8    (