%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/lib/python2.7/lib2to3/
Upload File :
Create Path :
Current File : //usr/local/lib/python2.7/lib2to3/refactor.pyo


Pac@sdZddlmZdZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZmZmZddlmZdd	lmZmZdd
lmZddlmZed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 <guido@python.org>N(tchaini(tdriverttokenizettoken(t	find_root(tpytreetpygram(t	btm_utils(tbtm_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((s,/usr/local/lib/python2.7/lib2to3/refactor.pytget_all_fix_names"s"
t
_EveryNodecBseZRS((t__name__t
__module__(((s,/usr/local/lib/python2.7/lib2to3/refactor.pyR.scCst|tjtjfrC|jdkr3tnt|jgSt|tjrt|j	rkt
|j	Stnt|tjrt}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
isinstanceRtNodePatterntLeafPatternttypetNoneRtsettNegatedPatterntcontentt_get_head_typestWildcardPatterntupdatet	Exception(tpattrtptx((s,/usr/local/lib/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
jjj
t
jjD]}||j|qWt|S(s^ Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  N(tcollectionstdefaultdicttlisttpatternR%RRt_accept_typeR!RRtpython_grammart
symbol2numbert
itervaluesttokenstextendtdict(t
fixer_listt
head_nodesteverytfixertheadst	node_type((s,/usr/local/lib/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/local/lib/python2.7/lib2to3/refactor.pytget_fixers_from_packagegscCs|S(N((tobj((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt	_identitynsiicCs|jddS(Nu
u
(treplace(tinput((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt_from_system_newlinesuscCs*tjdkr"|jdtjS|SdS(Ns
u
(tostlinesepRF(RG((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt_to_system_newlineswscst}tjtj|jfd}ttjtjtj	f}t
}ykxdtr|\}}||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/local/lib/python2.7/lib2to3/refactor.pytadvancesufromu
__future__uimportu(u,(R@Rtgenerate_tokenstStringIOtreadlinet	frozensetRtNEWLINEtNLtCOMMENTR"tTruetSTRINGtNAMEtOPtaddt
StopIteration(tsourcethave_docstringROtignoretfeaturesttptvalue((RNs,/usr/local/lib/python2.7/lib2to3/refactor.pyt_detect_future_featuressD			
	
t
FixerErrorcBseZdZRS(sA fixer could not be loaded.(RRt__doc__(((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRdstRefactoringToolcBs!eZied6ed6ZdZdZdddZdZdZ	dZ
dZd	Zeed
Z
eedZdZeed
ZdZedZdZdZdeddZddZdZdZdZdZdZdZdZdZRS(tprint_functiontwrite_unchanged_filestFixRcCs||_|pg|_|jj|_|dk	rI|jj|n|jdretj|_	ntj
|_	|jjd|_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(tselftfixer_namesRpRmR;((s,/usr/local/lib/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'tk
rtd||fnX|	|j
|j}
|
jr?|jtk	r?||jkr?|jd|qn|jd	||
jd
krn|j|
q|
jdkr|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trsplitRtFILE_PREFIXtlentsplittCLASS_PREFIXtjointtitletgetattrtAttributeErrorRdRpRwRmRWtlog_messaget	log_debugtorderRtoperatort
attrgettertsort(Rtpre_order_fixerstpost_order_fixerstfix_mod_pathtmodRBtpartsR+t
class_namet	fix_classR;tkey_func((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRzs8/
cOsdS(sCalled when an error occurs.N((Rtmsgtargstkwds((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt	log_errorscGs'|r||}n|jj|dS(sHook to log a message.N(Rktinfo(RRR((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRs
cGs'|r||}n|jj|dS(N(Rktdebug(RRR((s,/usr/local/lib/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/local/lib/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_onlytdir_or_file((s,/usr/local/lib/python2.7/lib2to3/refactor.pytrefactor&s
cCstjd}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((s,/usr/local/lib/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((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt_read_python_sourceCscCs|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_stringtwas_changedtunicode(RRRRRGRtoutputttree((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRSs
cCst|}d|kr*tj|j_nzMy|jj|}Wn0tk
ru}|jd||jj	|dSXWd|j|j_X||_
|jd||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((s,/usr/local/lib/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 stdins<stdin>sNo doctest changes in stdinsNo changes in stdin(
tsyststdinRRRRhRRRR(RRRGRR((s,/usr/local/lib/python2.7/lib2to3/refactor.pytrefactor_stdins
c
Csx-t|j|jD]}|j||qW|j|j|j|j|j|j|jj|j	}xt
|jrcx|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||	<n||	j||	qWqUqqWqqWq}Wx-t|j|jD]}|j||qzW|j S(sRefactors 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.
        RtreverseN(!RR{R|t
start_treettraverse_byRRRtruntleavestanytvaluesRlRRtBasetdepthRWtkeep_line_ordert
get_linenoR/tremoveRt
ValueErrortfixers_appliedtmatcht	transformR!RFRR6tfinish_treeR(
RRRR;t	match_settnodetresultstnewtnew_matchestfxr((s,/usr/local/lib/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!RF(RRlt	traversalRR;RR((s,/usr/local/lib/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(R}RR!RRRRht
write_file(RRRRRRR((s,/usr/local/lib/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(
RRIterrorRRRKRRRWRx(RRRRRRR((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRss>>> 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((s,/usr/local/lib/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<n||j|jdg}|rL|g|D]}||j|^q(7}qLn|S(sRefactors 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).

        s
Source: %su
s+Can't parse docstring in %s line %s: %s: %siii(tparse_blockR(RktisEnabledForRutDEBUGRRRRRRRRRWtendswithRtpopR(
RRRRRRRRRtclipped((s,/usr/local/lib/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((s,/usr/local/lib/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((s,/usr/local/lib/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((s,/usr/local/lib/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((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRs



	N(RRR@RnRRR!RRzRRRRRRRRRRRRRRRRRRRR	RR(((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRfs:

4	(								O		+			
	tMultiprocessingUnsupportedcBseZRS((RR(((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRstMultiprocessRefactoringToolcBs5eZdZeeddZdZdZRS(cOs/tt|j||d|_d|_dS(N(tsuperR RR!tqueuetoutput_lock(RRtkwargs((s,/usr/local/lib/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 RtmultiprocessingtImportErrorRR"R!tRuntimeErrort
JoinableQueuetLockR#txrangetProcesst_childtstartRtputtis_alive(	RRRRt
num_processesR&Rt	processesR+((s,/usr/local/lib/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$((s,/usr/local/lib/python2.7/lib2to3/refactor.pyR-scOsE|jdk	r(|jj||fntt|j||SdS(N(R"R!R/R!R R(RRR$((s,/usr/local/lib/python2.7/lib2to3/refactor.pyRs(RRRR@RR-R(((s,/usr/local/lib/python2.7/lib2to3/refactor.pyR s
		(.Ret
__future__Rt
__author__RIR
RRuRR-RQt	itertoolsRtpgen2RRRt
fixer_utilRRRRRtbuR	R~RWRR(RR%R>RCREtversion_infotcodecsRRRHRKRcRdtobjectRfRR (((s,/usr/local/lib/python2.7/lib2to3/refactor.pyt<module>	sH							(

Zerion Mini Shell 1.0