ofc @sdZddlmZddlmZddlmZmZddlm Z ddl m Z dZ d Zd Zd Zd5d Zd ZdZdZeedZd5d5dZdZdZd5dZdZd5dZd5dZdZdZdZ dZ!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/d0Z0d1Z1e"e j2e j3gZ4d5d2Z5e"e j3e j2e j6gZ7d3Z8d5d4Z9d5S(6s1Utility functions, node construction macros, etc.i(tislicei(ttoken(tLeaftNode(tpython_symbols(tpatcompcCs%ttj|ttjd|gS(Nu=(RtsymstargumentRRtEQUAL(tkeywordtvalue((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt KeywordArgs cCsttjdS(Nu((RRtLPAR(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytLParenscCsttjdS(Nu)(RRtRPAR(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytRParenscCslt|ts|g}nt|ts?d|_|g}nttj|ttjdddg|S(sBuild an assignment statementu u=tprefix( t isinstancetlistRRRtatomRRR(ttargettsource((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytAssigns    cCsttj|d|S(sReturn a NAME leafR(RRtNAME(tnameR((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytName&scCs|ttjt|ggS(sA node tuple for obj.attr(RRttrailertDot(tobjtattr((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytAttr*scCsttjdS(s A comma leafu,(RRtCOMMA(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytComma.scCsttjdS(sA period (.) leafu.(RRtDOT(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyR2scCsMttj|j|jg}|rI|jdttj|n|S(s-A parenthesised argument list, used by Call()i(RRRtclonet insert_childtarglist(targstlparentrparentnode((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytArgList6s$cCs:ttj|t|g}|dk r6||_n|S(sA function callN(RRtpowerR)tNoneR(t func_nameR%RR(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytCall=s  cCsttjdS(sA newline literalu (RRtNEWLINE(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytNewlineDscCsttjdS(s A blank lineu(RRR.(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt BlankLineHscCsttj|d|S(NR(RRtNUMBER(tnR((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytNumberLscCs1ttjttjd|ttjdgS(sA numeric or string subscriptu[u](RRRRRtLBRACEtRBRACE(t index_node((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt SubscriptOscCsttj|d|S(s A string leafR(RRtSTRING(tstringR((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytStringUsc Csd|_d|_d|_ttjd}d|_ttjd}d|_||||g}|rd|_ttjd}d|_|jttj||gnttj|ttj |g}ttj ttj d|ttj dgS(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*/usr/lib64/python2.7/lib2to3/fixer_util.pytListCompYs$       "$ cCsx|D]}|jqWttjdttj|ddttjdddttj|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*/usr/lib64/python2.7/lib2to3/fixer_util.pyt FromImportqs c Cst|tr.|jttgkr.tSt|tot|jdkot|jdtot|jdtot|jdto|jdjdko|jdjdkS(s(Does the node represent a tuple literal?iiiiu(u)( RRROR RtTruetlenRR (R(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytis_tuples*cCszt|toyt|jdkoyt|jdtoyt|jdtoy|jdjdkoy|jdjdkS(s'Does the node represent a list literal?iiiu[u](RRRSRORR (R(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytis_lists cCsttjt|tgS(N(RRRR R(R(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt parenthesizestsortedRtsettanytallttupletsumtmintmaxt 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*/usr/lib64/python2.7/lib2to3/fixer_util.pyt attr_chains 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* > cCsts<tjtatjtatjtatantttg}xRt|t|dD]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*/usr/lib64/python2.7/lib2to3/fixer_util.pytin_special_contexts %"cCs|j}|dk r+|jtjkr+tS|j}|jtjtj fkrStS|jtj kr||j d|kr|tS|jtj ks|jtj kr|dk r|jtjks|j d|krtStS(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*/usr/lib64/python2.7/lib2to3/fixer_util.pytis_probably_builtins  %cCspxi|dk rk|jtjkr_t|jdkr_|jd}|jtjkr_|jSn|j }qWdS(sFind the indentation of *node*.iiuN( R+RqRtsuiteRSRORtINDENTR Rc(R(tindent((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytfind_indentations'   cCsW|jtjkr|S|j}|jd}|_ttj|g}||_|S(N(RqRRyR"RcR+R(R(RcRy((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt make_suites  cCs;x4|jtjkr6|j}|stdqqW|S(sFind the top level namespace.s,root found before file_input node was found.(RqRt file_inputRct ValueError(R(((s*/usr/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_bindingRtbool(tpackageRR(tbinding((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytdoes_tree_importscCs|jtjtjfkS(s0Returns true if the node is an import statement.(RqRt import_nameRK(R(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt is_import"sc Csd}t|}t|||r+dSd}}xnt|jD]]\}}||scqEnx1t|j|D]\}}||swPqwqwW||}PqEW|dkrxbt|jD]N\}}|jtjkr|jr|jdjtjkr|d}PqqWn|dkr\t tj t tj dt tj |ddg} n$t|t tj |ddg} | tg} |j|t tj| dS(s\ Works like `does_tree_import` but adds an import statement if it was not imported. cSs,|jtjko+|jo+t|jdS(Ni(RqRt simple_stmtROR(R(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytis_import_stmt)sNiiuimportRu (RRR_RORqRRRR8R+RRRRRQR/R#( RRR(Rtroott insert_postoffsettidxtnode2timport_RO((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyt touch_import&s4            !$c CsKxD|jD]9}d}|jtjkrst||jdrB|St|t|jd|}|r |}q n|jtjtj fkrt|t|jd|}|r |}q na|jtj kr|t|t|jd|}|r|}q xt |jdD]b\}}|jt j kr|jdkrt|t|j|d|}|ru|}quqqWn|jtkr|jdj|kr|}nvt|||r|}n[|jtjkrt|||}n4|jtjkr t||jdr |}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.iiiit:iiN(ROR+RqRtfor_stmtt_findRR}tif_stmtt while_stmtttry_stmtR_RtCOLONR t _def_symst_is_import_bindingRRtR(RR(RtchildtretR2titkid((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyRTsH    !#%     cCs||g}xl|rw|j}|jdkrO|jtkrO|j|jq |jtjkr |j|kr |Sq WdS(Ni( tpopRqt _block_symstextendRORRR R+(RR(tnodes((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyRs   !cCs'|jtjkr| r|jd}|jtjkrx|jD]Z}|jtjkrw|jdj|kr|SqB|jtjkrB|j|krB|SqBWq#|jtjkr|jd}|jtjkr|j|kr|Sq#|jtjkr#|j|kr#|Sn|jtj kr#|rMt |jdj |krMdS|jd}|rst d|rsdS|jtjkrt ||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. iiiiuasN(RqRRROtdotted_as_namestdotted_as_nameR RRRKtunicodetstripR+RRJtimport_as_nametSTAR(R(RRRPRtlastR2((s*/usr/lib64/python2.7/lib2to3/fixer_util.pyRs@ !  !!% ! !!N(:t__doc__t itertoolsRtpgen2RtpytreeRRtpygramRRtRR R RRR+RRR RR)R-R/R0R3R7R:RHRQRTRURVRXtconsuming_callsRbRfRgRhRkRdRoRxR|R}RRRRRsRrRRRRRR(((s*/usr/lib64/python2.7/lib2to3/fixer_util.pytsZ                       - *