U ifk@s dZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ddlmZddlmZmZddlmZd d d ZGd d d eZddZddZddZddZddZGdddeZGdddeZGdddeZ GdddeZ!dS)!z#Guido van Rossum N)chain)drivertokenizetoken) find_root)pytreepygram) btm_matcherTcCsTt|ggdg}g}t|jD].\}}}|dr |rD|dd}||q |S)N*fix_) __import__pkgutilZ iter_modules__path__ startswithappend)Z fixer_pkgZ remove_prefixZpkgZ fix_namesfindernameZispkgr5/opt/alt/python38/lib64/python3.8/lib2to3/refactor.pyget_all_fix_namess   rc@s eZdZdS) _EveryNodeN__name__ __module__ __qualname__rrrrr+srcCst|tjtjfr(|jdkr t|jhSt|tjrH|jrDt|jStt|tj rt }|jD]}|D]}| t|qhq`|St d|dS)Nz$Oh no! I don't understand pattern %s) isinstancerZ NodePatternZ LeafPatterntyperZNegatedPatternZcontent_get_head_typesZWildcardPatternsetupdate Exception)Zpatrpxrrrr/s     rc Cstt}g}|D]x}|jrdzt|j}Wntk rH||YqX|D]}|||qNq|jdk r||j|q||qtt j j t j j D]}|||qt|SN) collections defaultdictlistpatternrrrZ _accept_typerr python_grammarZ symbol2numbervaluestokensextenddict)Z fixer_listZ head_nodesZeveryfixerZheadsZ node_typerrr_get_headnode_dictKs$    r1csfddtdDS)Ncsg|]}d|qS.r).0fix_nameZpkg_namerr hsz+get_fixers_from_package..F)rr6rr6rget_fixers_from_packageds r8cCs|Sr&r)objrrr _identityksr:csXd}tt|jfdd}ttjtjtj h}t }z|\}}||krTq>q>|tj krl|rfq6d}q>|tj kr6|dkr6|\}}|tj ks|dkrq6|\}}|tj ks|dkrq6|\}}|tj kr|dkr|\}}|tj kr4|||\}}|tj ks.|d kr"q4|\}}qq>q6q>Wntk rNYnXt|S) NFcst}|d|dfS)Nrr)next)tokgenrradvancersz(_detect_future_features..advanceTfromZ __future__import(,)rgenerate_tokensioStringIOreadline frozensetrNEWLINENLCOMMENTr STRINGNAMEOPadd StopIteration)sourceZhave_docstringr?ignorefeaturestpvaluerr=r_detect_future_featuresosB           rVc@s eZdZdS) FixerErrorNrrrrrrWsrWc@seZdZdddZdZdZd4ddZdd Zd d Zd d Z ddZ ddZ d5ddZ d6ddZ ddZd7ddZddZd8ddZddZd d!Zd9d"d#Zd:d$d%Zd&Zd'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3ZdS);RefactoringToolF)print_functionwrite_unchanged_filesZFixr NcCs.||_|p g|_|j|_|dk r0|j||jdrDtj|_ntj |_|j d|_ g|_ t d|_g|_d|_tj|jtj|jd|_|\|_|_g|_t|_g|_g|_t|j|jD]F}|j r|j!|q||jkr|j"|q||jkr|j"|qt#|j|_$t#|j|_%dS)NrYrZrXF)convertlogger)&fixersexplicit_default_optionscopyoptionsr!r !python_grammar_no_print_statementgrammarr+getrZerrorsloggingZ getLoggerr\ fixer_logwroterZDriverrr[ get_fixers pre_order post_orderfilesbmZ BottomMatcherBMZ bmi_pre_orderZbmi_post_orderrZ BM_compatibleZ add_fixerrr1bmi_pre_order_headsbmi_post_order_heads)selfZ fixer_namesrar^r0rrr__init__s>          zRefactoringTool.__init__c CsXg}g}|jD]}t|iidg}|ddd}||jrR|t|jd}|d}|jddd|D}zt ||}Wn&t k rt d ||fdYnX||j |j } | jr|jd k r||jkr|d |q|d || jd kr|| q| jdkr|| qt d| jqtd} |j| d|j| d||fS)Nr r3r_cSsg|] }|qSr)title)r4r$rrrr7sz.RefactoringTool.get_fixers..zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ run_orderkey)r]rrsplitr FILE_PREFIXlensplit CLASS_PREFIXjoingetattrAttributeErrorrWrargr^ log_message log_debugorderroperator attrgettersort) rqZpre_order_fixersZpost_order_fixersZ fix_mod_pathmodr5parts class_nameZ fix_classr0Zkey_funcrrrris:            zRefactoringTool.get_fixerscOsdSr&r)rqmsgargskwdsrrr log_errorszRefactoringTool.log_errorcGs|r ||}|j|dSr&)r\inforqrrrrrrszRefactoringTool.log_messagecGs|r ||}|j|dSr&)r\debugrrrrr szRefactoringTool.log_debugcCsdSr&r)rqold_textnew_textfilenameequalrrr print_outputszRefactoringTool.print_outputcCs8|D].}tj|r$||||q||||qdSr&)ospathisdir refactor_dir refactor_file)rqitemswrite doctests_onlyZ dir_or_filerrrrefactors zRefactoringTool.refactorc Cstjd}t|D]\}}}|d||||D]>}|ds>tj|d|kr>tj||} | | ||q>dd|D|dd<qdS)NpyzDescending into %sr3rcSsg|]}|ds|qSr2)r)r4Zdnrrrr7.s z0RefactoringTool.refactor_dir..) rextsepwalkrrrrsplitextr~r) rqZdir_namerrZpy_extdirpathZdirnames filenamesrfullnamerrrrs   zRefactoringTool.refactor_dirc Cszt|d}Wn6tk rD}z|d||WYdSd}~XYnXzt|jd}W5|Xtj|d|dd}||fW5QRSQRXdS)NrbzCan't open %s: %s)NNrr#ruencodingnewline) openOSErrorrcloserdetect_encodingrGrEread)rqrferrrrrr_read_python_source0s z#RefactoringTool._read_python_sourcecCs||\}}|dkrdS|d7}|rn|d||||}|jsL||kr`||||||q|d|nH|||}|js|r|jr|jt|dd|||dn |d|dS)N zRefactoring doctests in %szNo doctest changes in %srs)rrzNo changes in %s)rrrefactor_docstringrZprocessed_filerefactor_string was_changedstr)rqrrrinputroutputtreerrrr@s"   zRefactoringTool.refactor_filec Cst|}d|krtj|j_zVz|j|}Wn@tk rl}z"|d||jj |WYWdSd}~XYnXW5|j|j_X||_ | d|| |||S)NrYzCan't parse %s: %s: %szRefactoring %s) rVr rbrrcZ parse_stringr"r __class__rfuture_featuresr refactor_tree)rqdatarrSrrrrrrWs"      zRefactoringTool.refactor_stringcCstj}|rN|d||d}|js2||krB||d|q|dn:||d}|jsj|r~|jr~|t |d|n |ddS)NzRefactoring doctests in stdinzzNo doctest changes in stdinzNo changes in stdin) sysstdinrrrrZrrrr)rqrrrrrrrrefactor_stdinrs     zRefactoringTool.refactor_stdinc Cst|j|jD]}|||q||j|||j||j| }t | r|jj D]D}||krj||rj||j tjjdd|jr||j tjjdt||D]}|||kr|||z t|Wntk rYqYnX|jr||jkrq||}|r|||}|dk r|||D] }|js^g|_|j|qL|j| }|D]*} | |krg|| <|| || qqqjqTt|j|jD]}|||q|jS)NT)rxreverserw)rrjrkZ start_tree traverse_byrorprnrunZleavesanyr,r]rrZBaseZdepthZkeep_line_orderZ get_linenor)remover ValueErrorZfixers_appliedmatch transformreplacerr.Z finish_treer) rqrrr0Z match_setnoderesultsnewZ new_matchesZfxrrrrrsJ        zRefactoringTool.refactor_treecCsV|sdS|D]D}||jD]4}||}|r|||}|dk r|||}qq dSr&)rrrr)rqr]Z traversalrr0rrrrrrs    zRefactoringTool.traverse_bycCs|j||dkr.||d}|dkr.dS||k}||||||r`|d||js`dS|rv|||||n |d|dS)NrzNo changes to %szNot writing changes to %s)rlrrrrrZ write_file)rqrrrrrrrrrrs  zRefactoringTool.processed_filec Csztj|d|dd}Wn6tk rL}z|d||WYdSd}~XYnX|Fz||Wn0tk r}z|d||W5d}~XYnXW5QRX|d|d|_dS)NwrurzCan't create %s: %szCan't write %s: %szWrote changes to %sT)rErrrrrrh)rqrrrrfprrrrrs* zRefactoringTool.write_filez>>> z... c Csg}d}d}d}d}|jddD]}|d7}||jr~|dk rZ|||||||}|g}||j} |d| }q |dk r|||js|||jdkr| |q |dk r||||||d}d}| |q |dk r ||||||d |S)NrTkeependsrrru) splitlineslstriprPS1r.refactor_doctestfindPS2rstriprr~) rqrrresultblockZ block_linenoindentlinenolineirrrrsJ        z"RefactoringTool.refactor_docstringc sz||}Wnjtk r|}zLjtjrN|D]}d|dq6d|||j j ||WYSd}~XYnX ||rt |j dd}|d|d||dd} }|dds|dd7<j|dg}|r|fd d |D7}|S) Nz Source: %srz+Can't parse docstring in %s line %s: %s: %sTrrrsrcsg|]}j|qSr)r)r4rrrqrrr7Zsz4RefactoringTool.refactor_doctest..) parse_blockr"r\Z isEnabledForrfDEBUGrrrrrrrrendswithrpop) rqrrrrrrrrZclippedrrrr@s,"z RefactoringTool.refactor_doctestcCs|jr d}nd}|js$|d|n"|d||jD]}||q6|jrl|d|jD]}||q\|jrt|jdkr|dn|dt|j|jD]\}}}|j|f||qdS) Nwerez need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:rzThere was 1 error:zThere were %d errors:)rhrlrrgrer{)rqrfilemessagerrrrrr summarize]s$       zRefactoringTool.summarizecCs"|j||||}t|_|Sr&)rZ parse_tokens wrap_toksrHr)rqrrrrrrrrtszRefactoringTool.parse_blockc csdt|||j}|D]F\}}\}}\} } } ||d7}| |d7} ||||f| | f| fVqdS)Nr)rrD gen_lines__next__) rqrrrr-rrUZline0Zcol0Zline1Zcol1Z line_textrrrr~s   zRefactoringTool.wrap_toksccsx||j}||j}|}|D]N}||r>|t|dVn(||dkrVdVntd||f|}qdVqldS)Nrzline=%r, prefix=%rru)rrrr{rAssertionError)rqrrprefix1Zprefix2prefixrrrrrs   zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrr_r}rzrrrirrrrrrrrrrrrrrrrrrrrrrrrrrrXs> 4(   O  + rXc@s eZdZdS)MultiprocessingUnsupportedNrrrrrrsrcsBeZdZfddZd fdd ZfddZfd d ZZS) MultiprocessRefactoringToolcs"tt|j||d|_d|_dSr&)superrrrqueue output_lockrqrkwargsrrrrrsz$MultiprocessRefactoringTool.__init__Frc s|dkrtt|||Sz ddlWntk r@tYnXjdk rTtd_ _ fddt |D}z*|D] }|qtt|||W5j t |D]}j dq|D]}|r| qd_XdS)Nrrz already doing multiple processescsg|]}jjdqS))target)ZProcess_child)r4rmultiprocessingrqrrr7sz8MultiprocessRefactoringTool.refactor..)rrrr ImportErrorrr RuntimeErrorZ JoinableQueueZLockrranger~putZis_alivestart)rqrrrZ num_processesZ processesrr$rrrrs<           z$MultiprocessRefactoringTool.refactorcsN|j}|dk rJ|\}}ztt|j||W5|jX|j}q dSr&)rrdZ task_donerrr)rqZtaskrrrrrrs   z"MultiprocessRefactoringTool._childcs2|jdk r|j||fntt|j||SdSr&)rrrrrrrrrrs   z)MultiprocessRefactoringTool.refactor_file)FFr)rrrrrrrr __classcell__rrrrrs   r)T)" __author__rErrrrfrr' itertoolsrZpgen2rrrZ fixer_utilrrurr r rmrr"rrr1r8r:rVrWobjectrXrrrrrr s6    (