ofc@sdZddlmZdZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZmZddlmZdd lmZmZdd lmZdd lmZed Zd efdYZdZdZdZdZ ej!ddfkr[ddl"Z"e"j#Z$dZ%dZ&ne#Z$e Z%e Z&dZ'defdYZ(de)fdYZ*defdYZ+de*fdYZ,dS( sRefactoring 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. i(twith_statements#Guido van Rossum N(tchaini(tdriverttokenizettoken(t find_root(tpytreetpygram(t btm_utils(t btm_matchercCst|ggdg}tjj|j}g}xbttj|D]K}|jdrI|jdrI|r|d}n|j |d qIqIW|S(sEReturn a sorted list of all available fix names in the given package.t*tfix_s.pyii( t __import__tostpathtdirnamet__file__tsortedtlistdirt startswithtendswithtappend(t fixer_pkgt remove_prefixtpkgt fixer_dirt fix_namestname((s(/usr/lib64/python2.7/lib2to3/refactor.pytget_all_fix_names!s t _EveryNodecBseZRS((t__name__t __module__(((s(/usr/lib64/python2.7/lib2to3/refactor.pyR.scCst|tjtjfrC|jdkr3tnt|jgSt|tjrt|j rkt |j Stnt|tj rt}x5|j D]*}x!|D]}|j t |qWqW|St d|dS(sf Accepts a pytree Pattern Node and returns a set of the pattern types which will match first. s$Oh no! I don't understand pattern %sN(t isinstanceRt NodePatternt LeafPatternttypetNoneRtsettNegatedPatterntcontentt_get_head_typestWildcardPatterntupdatet Exception(tpattrtptx((s(/usr/lib64/python2.7/lib2to3/refactor.pyR(2s      cCstjt}g}x|D]}|jryt|j}Wntk r^|j|qXxU|D]}||j|qfWq|jdk r||jj|q|j|qWx:t t j j j t j jD]}||j|qWt|S(s^ Accepts a list of fixers and returns a dictionary of head node type --> fixer list. N(t collectionst defaultdicttlisttpatternR(RRt _accept_typeR$RRtpython_grammart symbol2numbert itervaluesttokenstextendtdict(t fixer_listt head_nodesteverytfixertheadst node_type((s(/usr/lib64/python2.7/lib2to3/refactor.pyt_get_headnode_dictNs"    cCs(gt|tD]}|d|^qS(sN Return the fully qualified names for fixers in the package pkg_name. t.(RtFalse(tpkg_nametfix_name((s(/usr/lib64/python2.7/lib2to3/refactor.pytget_fixers_from_packagegscCs|S(N((tobj((s(/usr/lib64/python2.7/lib2to3/refactor.pyt _identitynsiicCs|jddS(Nu u (treplace(tinput((s(/usr/lib64/python2.7/lib2to3/refactor.pyt_from_system_newlinesuscCs*tjdkr"|jdtjS|SdS(Ns u (R tlinesepRI(RJ((s(/usr/lib64/python2.7/lib2to3/refactor.pyt_to_system_newlineswscst}tjtj|jfd}ttjtjtj f}t }ykxdt r|\}}||krq]q]|tj kr|rPnt }q]|tj kr|dkr|\}}|tj ks|dkrPn|\}}|tj ks|dkrPn|\}}|tjkrY|dkrY|\}}nxa|tj kr|j||\}}|tjks|dkrPn|\}}q\Wq]Pq]WWntk rnXt|S(Ncsj}|d|dfS(Nii(tnext(ttok(tgen(s(/usr/lib64/python2.7/lib2to3/refactor.pytadvances ufromu __future__uimportu(u,(RCRtgenerate_tokenstStringIOtreadlinet frozensetRtNEWLINEtNLtCOMMENTR%tTruetSTRINGtNAMEtOPtaddt StopIteration(tsourcethave_docstringRQtignoretfeaturesttptvalue((RPs(/usr/lib64/python2.7/lib2to3/refactor.pyt_detect_future_featuressD       t FixerErrorcBseZdZRS(sA fixer could not be loaded.(RRt__doc__(((s(/usr/lib64/python2.7/lib2to3/refactor.pyRfstRefactoringToolcBs!eZied6ed6ZdZdZdddZdZdZ dZ dZ d Z eed Z eed Zd Zeed ZdZedZdZdZdeddZddZdZdZdZdZdZdZdZdZRS(tprint_functiontwrite_unchanged_filestFixR cCs||_|pg|_|jj|_|dk rI|jj|n|jdretj|_ n tj |_ |jj d|_ g|_ tjd|_g|_t|_tj|j dtjd|j|_|j\|_|_g|_tj|_g|_g|_ x}t!|j|jD]f}|j"rT|jj#|q2||jkrv|jj$|q2||jkr2|j j$|q2q2Wt%|j|_&t%|j |_'dS(sInitializer. Args: fixer_names: a list of fixers to import options: an dict with configuration. explicit: a list of fixers to run even if they are explicit. RiRjRhtconverttloggerN((tfixerstexplicitt_default_optionstcopytoptionsR$R*Rt!python_grammar_no_print_statementtgrammarR5tgetRjterrorstloggingt getLoggerRmt fixer_logRCtwroteRtDriverRRlt get_fixerst pre_ordert post_ordertfilestbmt BottomMatchertBMt bmi_pre_ordertbmi_post_orderRt BM_compatiblet add_fixerRRAtbmi_pre_order_headstbmi_post_order_heads(tselft fixer_namesRrRoR>((s(/usr/lib64/python2.7/lib2to3/refactor.pyt__init__s<            c Csg}g}x|jD]}t|iidg}|jddd}|j|jrr|t|j}n|jd}|jdjg|D]}|j ^q}yt ||} Wn't k rt d||fnX| |j |j} | jr?|jtk r?||jkr?|jd|qn|jd || jd krn|j| q| jd kr|j| qt d | jqWtjd } |jd| |jd| ||fS(sInspects 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 RBiit_tsCan't find %s.%ssSkipping implicit fixer: %ssAdding transformation: %stpretpostsIllegal fixer order: %rt run_ordertkey(RnR trsplitRt FILE_PREFIXtlentsplitt CLASS_PREFIXtjointtitletgetattrtAttributeErrorRfRrRyRoRYt log_messaget log_debugtorderRtoperatort attrgettertsort( Rtpre_order_fixerstpost_order_fixerst fix_mod_pathtmodREtpartsR.t class_namet fix_classR>tkey_func((s(/usr/lib64/python2.7/lib2to3/refactor.pyR|s8/ cOsdS(sCalled when an error occurs.N((Rtmsgtargstkwds((s(/usr/lib64/python2.7/lib2to3/refactor.pyt log_errorscGs'|r||}n|jj|dS(sHook to log a message.N(Rmtinfo(RRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs cGs'|r||}n|jj|dS(N(Rmtdebug(RRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs cCsdS(sTCalled with the old version, new version, and filename of a refactored file.N((Rtold_texttnew_texttfilenametequal((s(/usr/lib64/python2.7/lib2to3/refactor.pyt print_output!scCsPxI|D]A}tjj|r5|j|||q|j|||qWdS(s)Refactor a list of files and directories.N(R Rtisdirt refactor_dirt refactor_file(Rtitemstwritet doctests_onlyt dir_or_file((s(/usr/lib64/python2.7/lib2to3/refactor.pytrefactor&s c Cstjd}xtj|D]\}}}|jd||j|jxe|D]]}|jd rWtjj|d|krWtjj||} |j | ||qWqWWg|D]} | jds| ^q|(qWdS(sDescends 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. tpysDescending into %sRBiN( R textseptwalkRRRRtsplitextRR( Rtdir_nameRRtpy_exttdirpathtdirnamest filenamesRtfullnametdn((s(/usr/lib64/python2.7/lib2to3/refactor.pyR/s    cCsyt|d}Wn'tk r<}|jd||dSXztj|jd}Wd|jXt|dd|}t |j |fSWdQXdS(sG Do our best to decode a Python source file correctly. trbsCan't open %s: %siNR-tencoding(NN( topentIOErrorRR$Rtdetect_encodingRTtcloset_open_with_encodingRKtread(RRtfterrR((s(/usr/lib64/python2.7/lib2to3/refactor.pyt_read_python_sourceCs c Cs|j|\}}|dkr%dS|d7}|r|jd||j||}|jsl||kr|j|||||q|jd|nc|j||}|js|r|jr|jt|d |d|d|n|jd|dS( sRefactors a file.Nu sRefactoring doctests in %ssNo doctest changes in %siRRsNo changes in %s( RR$Rtrefactor_docstringRjtprocessed_filetrefactor_stringt was_changedtunicode(RRRRRJRtoutputttree((s(/usr/lib64/python2.7/lib2to3/refactor.pyRSs  cCst|}d|kr*tj|j_nzMy|jj|}Wn0tk ru}|jd||jj |dSXWd|j|j_X||_ |j d||j |||S(sFRefactor 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. RisCan't parse %s: %s: %sNsRefactoring %s( ReRRsRRtt parse_stringR+Rt __class__Rtfuture_featuresRt refactor_tree(RtdataRRbRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRjs     cCstjj}|ro|jd|j|d}|jsI||kr_|j|d|q|jdnS|j|d}|js|r|jr|jt |d|n |jddS(NsRefactoring doctests in stdinssNo doctest changes in stdinsNo changes in stdin( tsyststdinRRRRjRRRR(RRRJRR((s(/usr/lib64/python2.7/lib2to3/refactor.pytrefactor_stdins c Csx-t|j|jD]}|j||qW|j|j|j|j|j|j|jj|j }xt |j rcx|jj D]}||kr||r||j dtjjdt|jr||j dtjjnx[t||D]F}|||kr9||j|nyt|Wntk r]qnX|jr|||jkr|qn|j|}|r|j||}|dk rU|j|x9|jD]+}|jsg|_n|jj|qW|jj|j }x?|D]4} | |kr6g|| t match_settnodetresultstnewt new_matchestfxr((s(/usr/lib64/python2.7/lib2to3/refactor.pyRsJ          .cCs|s dSxr|D]j}xa||jD]R}|j|}|r%|j||}|dk rw|j||}qwq%q%WqWdS(sTraverse 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#RRR$RI(RRnt traversalRR>RR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs    cCs|jj||dkrB|j|d}|dkrBdSn||k}|j|||||r|jd||jsdSn|r|j||||n|jd|dS(sR Called when a file has been refactored and there may be changes. iNsNo changes to %ssNot writing changes to %s(RRR$RRRRjt write_file(RRRRRRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs    cCsyt|dd|}Wn*tjk rE}|jd||dSXzGy|jt|Wn)tjk r}|jd||nXWd|jX|jd|t|_ dS(sWrites 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. twRsCan't create %s: %sNsCan't write %s: %ssWrote changes to %s( RR terrorRRRMRRRYRz(RRRRRRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs s>>> s... c Csg}d}d}d}d}x+|jtD]}|d7}|jj|jr|dk r|j|j||||n|}|g}|j|j} || }q.|dk r|j||j s|||j j dkr|j |q.|dk r/|j|j||||nd}d}|j |q.W|dk rz|j|j||||ndj |S(sRefactors 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.) iiu uN( R$t splitlinesRYtlstripRtPS1R9trefactor_doctesttfindtPS2trstripRR( RRJRtresulttblockt block_linenotindenttlinenotlineti((s(/usr/lib64/python2.7/lib2to3/refactor.pyR(s:        c CsPy|j|||}Wnutk r}|jjtjrmx*|D]}|jd|jdqGWn|jd|||j j ||SX|j ||rLt |j t}||d ||d} }|djds|dcd7>>" (possibly indented), while the remaining lines start with "..." (identically indented). s Source: %su s+Can't parse docstring in %s line %s: %s: %siii(t parse_blockR+Rmt isEnabledForRwtDEBUGRRRRRRRRRYRRtpopR( RRRRRRRRRtclipped((s(/usr/lib64/python2.7/lib2to3/refactor.pyRSs$   .cCs|jrd}nd}|js4|jd|n1|jd|x|jD]}|j|qNW|jr|jdx!|jD]}|j|qWn|jrt|jdkr|jdn|jdt|jx0|jD]"\}}}|j|||qWndS( Ntweres need to besNo files %s modified.sFiles that %s modified:s$Warnings/messages while refactoring:isThere was 1 error:sThere were %d errors:(RzRRRyRvR(RRtfiletmessageRRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyt summarizeps$      cCs1|jj|j|||}t|_|S(sParses 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. (Rt parse_tokenst wrap_toksRUR(RRRRR((s(/usr/lib64/python2.7/lib2to3/refactor.pyR s! c cstj|j||j}xe|D]]\}}\}}\} } } ||d7}| |d7} ||||f| | f| fVq%WdS(s;Wraps a tokenize stream to systematically modify start/end.iN(RRRt gen_linesRN( RRRRR8R#Rdtline0tcol0tline1tcol1t line_text((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs (ccs||j}||j}|}xi|D]a}|j|rN|t|Vn4||jdkrldVntd||f|}q'WxtrdVqWdS(sGenerates lines as expected by tokenize from a list of lines. This strips the first len(indent + self.PS1) characters off each line. u sline=%r, prefix=%rRN(RRRRRtAssertionErrorRY(RRRtprefix1tprefix2tprefixR((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs     N(RRRCRpRRR$RR|RRRRRRRRRRRRRRRRRRRR RR(((s(/usr/lib64/python2.7/lib2to3/refactor.pyRhs:  4 (         O    +   tMultiprocessingUnsupportedcBseZRS((RR(((s(/usr/lib64/python2.7/lib2to3/refactor.pyRstMultiprocessRefactoringToolcBs5eZdZeeddZdZdZRS(cOs/tt|j||d|_d|_dS(N(tsuperR RR$tqueuet output_lock(RRtkwargs((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs ic Csf|dkr(tt|j|||Syddl}Wntk rQtnX|jdk rptdn|j |_|j |_ gt |D]}|j d|j^q}z;x|D]}|jqWtt|j|||Wd|jjx$t |D]}|jjdqWx'|D]}|jr5|jq5q5Wd|_XdS(Niis already doing multiple processesttarget(R!R Rtmultiprocessingt ImportErrorRR"R$t RuntimeErrort JoinableQueuetLockR#txrangetProcesst_childtstartRtputtis_alive( RRRRt num_processesR&R t processesR.((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs2    +     cCso|jj}xY|dk rj|\}}ztt|j||Wd|jjX|jj}qWdS(N(R"RuR$R!R Rt task_done(RttaskRR$((s(/usr/lib64/python2.7/lib2to3/refactor.pyR-s cOsE|jdk r(|jj||fntt|j||SdS(N(R"R$R/R!R R(RRR$((s(/usr/lib64/python2.7/lib2to3/refactor.pyRs(RRRRCRR-R(((s(/usr/lib64/python2.7/lib2to3/refactor.pyR s    (-Rgt __future__Rt __author__R RRwRR0RSt itertoolsRtpgen2RRRt fixer_utilRRRRRtbuR RRYRR+RR(RARFRHt version_infotcodecsRRRKRMReRftobjectRhRR (((s(/usr/lib64/python2.7/lib2to3/refactor.pyt sF                (