^ bc@sdZddlmZdZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZmZddlmZdd lmZmZdd lmZdd lmZed Zd efdYZdZdZdZ dZ!ej"ddfkrgddl#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_matchercCszt|ggdg}g}xUtj|jD]A\}}}|jdr1|rb|d}n|j|q1q1W|S(sEReturn a sorted list of all available fix names in the given package.t*tfix_i(t __import__tpkgutilt iter_modulest__path__t startswithtappend(t fixer_pkgt remove_prefixtpkgt fix_namestfindertnametispkg((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pytget_all_fix_names"s" t _EveryNodecBseZRS((t__name__t __module__(((s5/opt/alt/python27/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((s5/opt/alt/python27/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((s5/opt/alt/python27/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((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pytget_fixers_from_packagegscCs|S(N((tobj((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt _identitynsiicCs|jddS(Nu u (treplace(tinput((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt_from_system_newlinesuscCs*tjdkr"|jdtjS|SdS(Ns u (tostlinesepRF(RG((s5/opt/alt/python27/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(s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pytadvances ufromu __future__uimportu(u,(R@Rtgenerate_tokenstStringIOtreadlinet frozensetRtNEWLINEtNLtCOMMENTR"tTruetSTRINGtNAMEtOPtaddt StopIteration(tsourcethave_docstringROtignoretfeaturesttptvalue((RNs5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt_detect_future_featuressD       t FixerErrorcBseZdZRS(sA fixer could not be loaded.(RRt__doc__(((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRdstRefactoringToolcBs!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: a dict with configuration. explicit: a list of fixers to run even if they are explicit. RgRhRftconverttloggerN((tfixerstexplicitt_default_optionstcopytoptionsR!R'Rt!python_grammar_no_print_statementtgrammarR2tgetRhterrorstloggingt getLoggerRkt fixer_logR@twroteRtDriverRRjt get_fixerst pre_ordert post_ordertfilestbmt BottomMatchertBMt bmi_pre_ordertbmi_post_orderRt BM_compatiblet add_fixerRR>tbmi_pre_order_headstbmi_post_order_heads(tselft fixer_namesRpRmR;((s5/opt/alt/python27/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 R?iit_tsCan't find %s.%ssSkipping optional fixer: %ssAdding transformation: %stpretpostsIllegal fixer order: %rt run_ordertkey(RlR trsplitRt FILE_PREFIXtlentsplitt CLASS_PREFIXtjointtitletgetattrtAttributeErrorRdRpRwRmRWt log_messaget log_debugtorderRtoperatort attrgettertsort( Rtpre_order_fixerstpost_order_fixerst fix_mod_pathtmodRBtpartsR+t class_namet fix_classR;tkey_func((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRzs8/ cOsdS(sCalled when an error occurs.N((Rtmsgtargstkwds((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt log_errorscGs'|r||}n|jj|dS(sHook to log a message.N(Rktinfo(RRR((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRs cGs'|r||}n|jj|dS(N(Rktdebug(RRR((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRs cCsdS(sTCalled with the old version, new version, and filename of a refactored file.N((Rtold_texttnew_texttfilenametequal((s5/opt/alt/python27/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(RItpathtisdirt refactor_dirt refactor_file(Rtitemstwritet doctests_onlyt dir_or_file((s5/opt/alt/python27/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 %sR?iN( RItextseptwalkRRRRtsplitextRR( Rtdir_nameRRtpy_exttdirpathtdirnamest filenamesRtfullnametdn((s5/opt/alt/python27/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_encodingRRtcloset_open_with_encodingRHtread(RRtfterrR((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt_read_python_sourceCs cCs|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_docstringRhtprocessed_filetrefactor_stringt was_changedtunicode(RRRRRGRtoutputttree((s5/opt/alt/python27/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. RgsCan't parse %s: %s: %sNsRefactoring %s( RcRRqRRrt parse_stringR(Rt __class__Rtfuture_featuresRt refactor_tree(RtdataRR`RR((s5/opt/alt/python27/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( tsyststdinRRRRhRRRR(RRRGRR((s5/opt/alt/python27/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|| >> 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 splitlinesRWtlstripRtPS1R6trefactor_doctesttfindtPS2trstripRR( RRGRtresulttblockt block_linenotindenttlinenotlineti((s5/opt/alt/python27/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(Rkt isEnabledForRutDEBUGRRRRRRRRRWtendswithRtpopR( RRRRRRRRRtclipped((s5/opt/alt/python27/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:(RxR}RRwRtR(RRtfiletmessageRRR((s5/opt/alt/python27/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_toksRSR(RRRRR((s5/opt/alt/python27/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(RRPt gen_linesRL( RRRRR5R Rbtline0tcol0tline1tcol1t line_text((s5/opt/alt/python27/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(RRRRRtAssertionErrorRW(RRRtprefix1tprefix2tprefixR((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRs     N(RRR@RnRRR!RRzRRRRRRRRRRRRRRRRRRRR RR(((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRfs:  4 (         O    +   tMultiprocessingUnsupportedcBseZRS((RR(((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRstMultiprocessRefactoringToolcBs5eZdZeeddZdZdZRS(cOs/tt|j||d|_d|_dS(N(tsuperR RR!tqueuet output_lock(RRtkwargs((s5/opt/alt/python27/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&Rt processesR+((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRs2    +     cCso|jj}xY|dk rj|\}}ztt|j||Wd|jjX|jj}qWdS(N(R"RsR!R!R Rt task_done(RttaskRR$((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyR-s cOsE|jdk r(|jj||fntt|j||SdS(N(R"R!R/R!R R(RRR$((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyRs(RRRR@RR-R(((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyR s    (.Ret __future__Rt __author__RIR RRuRR-RQt itertoolsRtpgen2RRRt fixer_utilRRRRRtbuR R~RWRR(RR%R>RCREt version_infotcodecsRRRHRKRcRdtobjectRfRR (((s5/opt/alt/python27/lib64/python2.7/lib2to3/refactor.pyt sH                 (