ó ^ bc @sdZddlmZddlmZddlmZmZddlm Z ddl m Z d„Z d „Zd „Zd „Zd5d „Zd „Zd„Zd„Zeƒeƒd„Zd5d5d„Zd„Zd„Zd5d„Zd„Zd5d„Zd5d„Zd„Zd„Zd„Z d„Z!e"ddddd d!d"d#d$d%g ƒZ#d&„Z$d'a%d(a&d)a'e(a)d*„Z*d+„Z+d,„Z,d-„Z-d.„Z.d/„Z/d0„Z0d1„Z1e"e j2e j3gƒZ4d5d2„Z5e"e j3e j2e j6gƒZ7d3„Z8d5d4„Z9d5S(6s1Utility functions, node construction macros, etc.i’’’’(tislicei(ttoken(tLeaftNode(tpython_symbols(tpatcompcCs%ttj|ttjdƒ|gƒS(Nu=(RtsymstargumentRRtEQUAL(tkeywordtvalue((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt KeywordArgs cCsttjdƒS(Nu((RRtLPAR(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytLParenscCsttjdƒS(Nu)(RRtRPAR(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytRParenscCslt|tƒs|g}nt|tƒs?d|_|g}nttj|ttjdddƒg|ƒS(sBuild an assignment statementu u=tprefix( t isinstancetlistRRRtatomRRR(ttargettsource((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytAssigns    cCsttj|d|ƒS(sReturn a NAME leafR(RRtNAME(tnameR((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytName&scCs|ttjtƒ|gƒgS(sA node tuple for obj.attr(RRttrailertDot(tobjtattr((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytAttr*scCsttjdƒS(s A comma leafu,(RRtCOMMA(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytComma.scCsttjdƒS(sA period (.) leafu.(RRtDOT(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyR2scCsMttj|jƒ|jƒgƒ}|rI|jdttj|ƒƒn|S(s-A parenthesised argument list, used by Call()i(RRRtclonet insert_childtarglist(targstlparentrparentnode((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytArgList6s$cCs:ttj|t|ƒgƒ}|dk r6||_n|S(sA function callN(RRtpowerR)tNoneR(t func_nameR%RR(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytCall=s  cCsttjdƒS(sA newline literalu (RRtNEWLINE(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytNewlineDscCsttjdƒS(s A blank lineu(RRR.(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt BlankLineHscCsttj|d|ƒS(NR(RRtNUMBER(tnR((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytNumberLscCs1ttjttjdƒ|ttjdƒgƒS(sA numeric or string subscriptu[u](RRRRRtLBRACEtRBRACE(t index_node((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt SubscriptOscCsttj|d|ƒS(s A string leafR(RRtSTRING(tstringR((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytStringUsc Csd|_d|_d|_ttjdƒ}d|_ttjdƒ}d|_||||g}|rÆd|_ttjdƒ}d|_|jttj||gƒƒnttj|ttj |ƒgƒ}ttj ttj dƒ|ttj dƒgƒS(suA list comprehension of the form [xp for fp in it if test]. If test is None, the "if test" part is omitted. uu uforuinuifu[u]( RRRRtappendRRtcomp_ift listmakertcomp_forRR4R5( txptfptitttesttfor_leaftin_leaft inner_argstif_leaftinner((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytListCompYs$       "$ cCsx|D]}|jƒqWttjdƒttj|ddƒttjdddƒttj|ƒg}ttj|ƒ}|S(sO Return an import statement in the form: from package import name_leafsufromRu uimport(tremoveRRRRRtimport_as_namest import_from(t package_namet name_leafstleaftchildrentimp((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt FromImportqs cCs¾t|tƒr.|jtƒtƒgkr.tSt|tƒo½t|jƒdko½t|jdtƒo½t|jdtƒo½t|jdtƒo½|jdjdko½|jdjdkS(s(Does the node represent a tuple literal?iiiiu(u)( RRROR RtTruetlenRR (R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytis_tuple‰s*cCszt|tƒoyt|jƒdkoyt|jdtƒoyt|jdtƒoy|jdjdkoy|jdjdkS(s'Does the node represent a list literal?iii’’’’u[u](RRRSRORR (R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytis_list•s cCsttjtƒ|tƒgƒS(N(RRRR R(R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt parenthesize£stsortedRtsettanytallttupletsumtmintmaxt enumerateccs4t||ƒ}x|r/|Vt||ƒ}qWdS(slFollow an attribute chain. If you have a chain of objects where a.foo -> b, b.foo-> c, etc, use this to iterate over all objects in the chain. Iteration is terminated by getattr(x, attr) is None. Args: obj: the starting object attr: the name of the chaining attribute Yields: Each successive object in the chain. N(tgetattr(RRtnext((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt attr_chainŖs sefor_stmt< 'for' any 'in' node=any ':' any* > | comp_for< 'for' any 'in' node=any any* > s³ power< ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' | 'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) ) trailer< '(' node=any ')' > any* > s` power< ( 'sorted' | 'enumerate' ) trailer< '(' arglist ')' > any* > cCs¤ts<tjtƒatjtƒatjtƒatantttg}xRt|t|dƒƒD]8\}}i}|j ||ƒrd|d|krdtSqdWt S(s Returns true if node is in an environment where all that is required of it is being iterable (ie, it doesn't matter if it returns a list or an iterator). See test_map_nochange in test_fixers.py for some examples and tests. tparentR(( t pats_builtRtcompile_patterntp0tp1tp2RRtzipRbtmatchtFalse(R(tpatternstpatternRctresults((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytin_special_contextŠs %"cCsŁ|j}|dk r+|jtjkr+tS|j}|jtjtj fkrStS|jtj kr||j d|kr|tS|jtj ksŃ|jtj krÕ|dk r¾|jtjksŃ|j d|krÕtStS(sG Check that something isn't an attribute or function name etc. iN(t prev_siblingR+ttypeRR!RkRcRtfuncdeftclassdeft expr_stmtROt parameterst typedargslistRRR(R(tprevRc((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytis_probably_builtinćs  %cCspxi|dk rk|jtjkr_t|jƒdkr_|jd}|jtjkr_|jSn|j }qWdS(sFind the indentation of *node*.iiuN( R+RqRtsuiteRSRORtINDENTR Rc(R(tindent((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytfind_indentationśs'   cCsW|jtjkr|S|jƒ}|jd}|_ttj|gƒ}||_|S(N(RqRRyR"RcR+R(R(RcRy((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt make_suites  cCs;x4|jtjkr6|j}|stdƒ‚qqW|S(sFind the top level namespace.s,root found before file_input node was found.(RqRt file_inputRct ValueError(R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt find_roots  cCs"t|t|ƒ|ƒ}t|ƒS(sŽ Returns true if name is imported from package at the top level of the tree which node belongs to. To cover the case of an import like 'import foo', use None for the package and 'foo' for the name. (t find_bindingR€tbool(tpackageRR(tbinding((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytdoes_tree_importscCs|jtjtjfkS(s0Returns true if the node is an import statement.(RqRt import_nameRK(R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt is_import"sc CsÆd„}t|ƒ}t|||ƒr+dSd}}xnt|jƒD]]\}}||ƒscqEnx1t|j|ƒD]\}}||ƒswPqwqwW||}PqEW|dkrxbt|jƒD]N\}}|jtjkrĀ|jrĀ|jdjtjkrĀ|d}PqĀqĀWn|dkr\t tj t tj dƒt tj |ddƒgƒ} n$t|t tj |ddƒgƒ} | tƒg} |j|t tj| ƒƒdS(s\ Works like `does_tree_import` but adds an import statement if it was not imported. cSs,|jtjko+|jo+t|jdƒS(Ni(RqRt simple_stmtROR‡(R(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytis_import_stmt)sNiiuimportRu (R€R…R_RORqRRˆRR8R+RR†RRRQR/R#( RƒRR(R‰troott insert_postoffsettidxtnode2timport_RO((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyt touch_import&s4            !$cCsKxD|jD]9}d}|jtjkrst||jdƒrB|St|t|jdƒ|ƒ}|r |}q n­|jtjtj fkræt|t|jdƒ|ƒ}|r |}q na|jtj kr|t|t|jdƒ|ƒ}|r’|}q xt |jdƒD]b\}}|jt j kr|jdkrt|t|j|dƒ|ƒ}|ru|}quqqWn¤|jtkrŖ|jdj|krŖ|}nvt|||ƒrÅ|}n[|jtjkrģt|||ƒ}n4|jtjkr t||jdƒr |}q n|r |s0|St|ƒrC|Sq q WdS( sŗ Returns the node which binds variable name, otherwise None. If optional argument package is supplied, only imports will be returned. See test cases for examples.ii’’’’iit:iiN(ROR+RqRtfor_stmtt_findRR}tif_stmtt while_stmtttry_stmtR_RtCOLONR t _def_symst_is_import_bindingRˆRtR‡(RR(RƒtchildtretR2titkid((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyRTsH    !#%     cCs||g}xl|rw|jƒ}|jdkrO|jtkrO|j|jƒq |jtjkr |j|kr |Sq WdS(Ni( tpopRqt _block_symstextendRORRR R+(RR(tnodes((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyR“s   !cCs'|jtjkr| r|jd}|jtjkr£xŌ|jD]Z}|jtjkrw|jdj|krœ|SqB|jtjkrB|j|krB|SqBWq#|jtjkrź|jd}|jtjkr|j|kr|Sq#|jtjkr#|j|kr#|Sn|jtj kr#|rMt |jdƒj ƒ|krMdS|jd}|rst d|ƒrsdS|jtjkr˜t ||ƒr˜|S|jtjkrß|jd}|jtjkr |j|kr |Sq#|jtjkr|j|kr|S|r#|jtjkr#|SndS(s› Will reuturn node if node will import name, or node will import * from package. None is returned otherwise. See test cases for examples. iii’’’’iuasN(RqRR†ROtdotted_as_namestdotted_as_nameR RRRKtunicodetstripR+R“RJtimport_as_nametSTAR(R(RRƒRPRštlastR2((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pyR™‰s@ !  !!% ! !!N(:t__doc__t itertoolsRtpgen2RtpytreeRRtpygramRRtRR R RRR+RRR RR)R-R/R0R3R7R:RHRQRTRURVRXtconsuming_callsRbRfRgRhRkRdRoRxR|R}R€R…R‡RRsRrR˜RRRŸR“R™(((s7/opt/alt/python27/lib64/python2.7/lib2to3/fixer_util.pytsZ                       - *