ó u®¸Qc @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((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt KeywordArgs cCsttjdƒS(Nu((RRtLPAR(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytLParenscCsttjdƒS(Nu)(RRtRPAR(((s(/sys/lib/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((s(/sys/lib/python2.7/lib2to3/fixer_util.pytAssigns    cCsttj|d|ƒS(sReturn a NAME leafR(RRtNAME(tnameR((s(/sys/lib/python2.7/lib2to3/fixer_util.pytName&scCs|ttjtƒ|gƒgS(sA node tuple for obj.attr(RRttrailertDot(tobjtattr((s(/sys/lib/python2.7/lib2to3/fixer_util.pytAttr*scCsttjdƒS(s A comma leafu,(RRtCOMMA(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytComma.scCsttjdƒS(sA period (.) leafu.(RRtDOT(((s(/sys/lib/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((s(/sys/lib/python2.7/lib2to3/fixer_util.pytArgList6s$cCs:ttj|t|ƒgƒ}|dk r6||_n|S(sA function callN(RRtpowerR)tNoneR(t func_nameR%RR(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytCall=s  cCsttjdƒS(sA newline literalu (RRtNEWLINE(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytNewlineDscCsttjdƒS(s A blank lineu(RRR.(((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt BlankLineHscCsttj|d|ƒS(NR(RRtNUMBER(tnR((s(/sys/lib/python2.7/lib2to3/fixer_util.pytNumberLscCs1ttjttjdƒ|ttjdƒgƒS(sA numeric or string subscriptu[u](RRRRRtLBRACEtRBRACE(t index_node((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt SubscriptOscCsttj|d|ƒS(s A string leafR(RRtSTRING(tstringR((s(/sys/lib/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((s(/sys/lib/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((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt FromImportqs c Cs¾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(((s(/sys/lib/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(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytis_list•s cCsttjtƒ|tƒgƒS(N(RRRR R(R(((s(/sys/lib/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((s(/sys/lib/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((s(/sys/lib/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((s(/sys/lib/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((s(/sys/lib/python2.7/lib2to3/fixer_util.pytfind_indentationús'   cCsW|jtjkr|S|jƒ}|jd}|_ttj|gƒ}||_|S(N(RqRRyR"RcR+R(R(RcRy((s(/sys/lib/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(((s(/sys/lib/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((s(/sys/lib/python2.7/lib2to3/fixer_util.pytdoes_tree_importscCs|jtjtjfkS(s0Returns true if the node is an import statement.(RqRt import_nameRK(R(((s(/sys/lib/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(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytis_import_stmt)sNiiuimportRu (R€R…R_RORqRRˆRR8R+RR†RRRQR/R#( RƒRR(R‰troott insert_postoffsettidxtnode2timport_RO((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt touch_import&s4            !$c CsKxD|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_findRR}tif_stmtt while_stmtttry_stmtR_RtCOLONR t _def_symst_is_import_bindingRˆRtR‡(RR(RƒtchildtretR2titkid((s(/sys/lib/python2.7/lib2to3/fixer_util.pyRTsH    !#%     cCs||g}xl|rw|jƒ}|jdkrO|jtkrO|j|jƒq |jtjkr |j|kr |Sq WdS(Ni( tpopRqt _block_symstextendRORRR R+(RR(tnodes((s(/sys/lib/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((s(/sys/lib/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‡RRsRrR˜RRRŸR“R™(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytsZ                       - *