j f; @svdZddlmZddlmZddlmZmZddlm Z ddl m Z dd Z d d Zd d ZddZdddZddZddZddZeeddZddddZddZdd Zdd!d"Zd#d$Zdd%d&Zdd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z d1d2Z!d3d4d5d6d7d8d9d:d;d<h Z"d=d>Z#d?a$d@a%dAa&dBa'dCdDZ(dEdFZ)dGdHZ*dIdJZ+dKdLZ,dMdNZ-dOdPZ.dQdRZ/e j0e j1hZ2ddSdTZ3e j1e j0e j4hZ5dUdVZ6ddWdXZ7dS)Yz1Utility functions, node construction macros, etc.)islice)token)LeafNode)python_symbols)patcompcCs%ttj|ttjd|gS)N=)rsymsZargumentrrEQUAL)keywordvaluer7/opt/alt/python34/lib64/python3.4/lib2to3/fixer_util.py KeywordArgs rcCsttjdS)N()rrLPARrrrrLParensrcCsttjdS)N))rrRPARrrrrRParensrcCslt|ts|g}nt|ts?d|_|g}nttj|ttjdddg|S)zBuild an assignment statement r prefix) isinstancelistrrr atomrrr )targetsourcerrrAssigns    rNcCsttj|d|S)zReturn a NAME leafr)rrNAME)namerrrrName&sr!cCs|ttjt|ggS)zA node tuple for obj.attr)rr trailerDot)objattrrrrAttr*sr&cCsttjdS)z A comma leaf,)rrCOMMArrrrComma.sr)cCsttjdS)zA period (.) leaf.)rrDOTrrrrr#2sr#cCsMttj|j|jg}|rI|jdttj|n|S)z-A parenthesised argument list, used by Call()r)rr r"clone insert_childarglist)argsZlparenZrparennoderrrArgList6s$r1cCs:ttj|t|g}|dk r6||_n|S)zA function callN)rr powerr1r)Z func_namer/rr0rrrCall=s  r3cCsttjdS)zA newline literal )rrNEWLINErrrrNewlineDsr6cCsttjdS)z A blank line)rrr5rrrr BlankLineHsr8cCsttj|d|S)Nr)rrNUMBER)nrrrrNumberLsr;cCs1ttjttjd|ttjdgS)zA numeric or string subscript[])rr r"rrLBRACERBRACE)Z index_noderrr SubscriptOsr@cCsttj|d|S)z A string leafr)rrSTRING)stringrrrrStringUsrCc Csd|_d|_d|_ttjd}d|_ttjd}d|_||||g}|rd|_ttjd}d|_|jttj||gnttj|ttj |g}ttj ttj d|ttj dgS)zuA list comprehension of the form [xp for fp in it if test]. If test is None, the "if test" part is omitted. r7rforinifr<r=) rrrrappendrr Zcomp_ifZ listmakerZcomp_forrr>r?) ZxpfpitZtestZfor_leafZin_leafZ inner_argsZif_leafinnerrrrListCompYs$       "$ rKcCsx|D]}|jqWttjdttj|ddttjdddttj|g}ttj|}|S)zO Return an import statement in the form: from package import name_leafsfromrrimport)removerrrrr import_as_names import_from)Z package_nameZ name_leafsZleafchildrenimprrr FromImportqs rSc Cs|dj}|jtjkr1|j}nttj|jg}|d}|rrdd|D}nttjtt|dt|dttj|dj||djgg|}|j |_ |S) zfReturns an import statement and calls a method of the module: import module module.name()r$aftercSsg|]}|jqSr)r,).0r:rrr s z!ImportAndCall..rrZlparZrpar) r,typer r.rr2r&r!r"r)r0resultsnamesr$Z newarglistrTnewrrr ImportAndCalls  Z r[cCst|tr.|jttgkr.dSt|tot|jdkot|jdtot|jdtot|jdto|jdjdko|jdjdkS)z(Does the node represent a tuple literal?Trrrr)rrrQrrlenrr )r0rrris_tuples*r_cCszt|toyt|jdkoyt|jdtoyt|jdtoy|jdjdkoy|jdjdkS)z'Does the node represent a list literal?rrr<r=r`)rrr^rQrr )r0rrris_lists racCsttjt|tgS)N)rr rrr)r0rrr parenthesizesrbsortedrsetanyalltuplesumminmax enumerateccs4t||}x|r/|Vt||}qWdS)alFollow 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)getattr)r$r%nextrrr attr_chains rnzefor_stmt< 'for' any 'in' node=any ':' any* > | comp_for< 'for' any 'in' node=any any* > z power< ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' | 'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) ) trailer< '(' node=any ')' > any* > z` power< ( 'sorted' | 'enumerate' ) trailer< '(' arglist ')' > any* > FcCsts<tjtatjtatjtadantttg}xRt|t|dD]8\}}i}|j||rd|d|krddSqdWdS)a 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. Tparentr0F) pats_builtrZcompile_patternp0p1p2ziprnmatch)r0ZpatternspatternrorXrrrin_special_contexts %"rwcCs|j}|dk r+|jtjkr+dS|j}|jtjtjfkrSdS|jtjkr||j d|kr|dS|jtj ks|jtj kr|dk r|jtj ks|j d|krdSdS)zG Check that something isn't an attribute or function name etc. NFrT) Z prev_siblingrWrr+ror funcdefclassdef expr_stmtrQZ parametersZ typedargslistr()r0prevrorrris_probably_builtins  %r|cCspxi|dk rk|jtjkr_t|jdkr_|jd}|jtjkr_|jSn|j}qWdS)zFind the indentation of *node*.Nr]rr7) rWr suiter^rQrINDENTr ro)r0indentrrrfind_indentations'   rcCsW|jtjkr|S|j}|jd}|_ttj|g}||_|S)N)rWr r}r,ror)r0ror}rrr make_suites  rcCs;x4|jtjkr6|j}|stdqqW|S)zFind the top level namespace.z,root found before file_input node was found.)rWr Z file_inputro ValueError)r0rrr find_root(s  rcCs"t|t||}t|S)z 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. ) find_bindingrbool)packager r0Zbindingrrrdoes_tree_import1srcCs|jtjtjfkS)z0Returns true if the node is an import statement.)rWr import_namerP)r0rrr is_import9src Csdd}t|}t|||r.dSd}}xtt|jD]c\}}||sfqHnx7t|j|dD]\}}||sPqqW||}PqHW|dkr xbt|jD]N\}}|jtjkr|jr|jdjtjkr|d}PqqWn|dkret tj t tj dt tj |ddg} n$t |t tj |ddg} | tg} |j|t tj| dS) z\ Works like `does_tree_import` but adds an import statement if it was not imported. cSs,|jtjko+|jo+t|jdS)Nr)rWr simple_stmtrQr)r0rrris_import_stmt@sz$touch_import..is_import_stmtNrrrMrr)rrrkrQrWr rrrArrrrrSr6r-) rr r0rrootZ insert_posoffsetidxZnode2import_rQrrr touch_import=s4    &       !$rcCsQxJ|jD]?}d}|jtjkrst||jdrB|St|t|jd|}|r&|}q&n|jtjtjfkrt|t|jd |}|r&|}q&ng|jtj krt|t|jd|}|r|}q&x$t |jddD]b\}}|jt j kr|j dkrt|t|j|d|}|r{|}q{qqWn|jtkr|jdj |kr|}nvt|||r|}n[|jtjkrt|||}n4|jtjkr&t||jdr&|}q&n|r |s6|St|rI|Sq q WdS) z 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.Nrr]r\:rr`r`)rQrWr Zfor_stmt_findrrZif_stmtZ while_stmtZtry_stmtrkrCOLONr _def_syms_is_import_bindingrrzr)r r0rchildZretr:iZkidrrrrksH   &!#%     rcCs||g}xl|rw|j}|jdkrO|jtkrO|j|jq |jtjkr |j|kr |Sq WdS)N)poprW _block_symsextendrQrrr )r r0Znodesrrrrs   !rcCs'|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|jtj krt ||r|S|jtjkr|jd}|jtjkr |j|kr |Sq#|jtjkr|j|kr|S|r#|jtjkr#|SndS)z Will reuturn node if node will import name, or node will import * from package. None is returned otherwise. See test cases for examples. rr]Nr\asr`)rWr rrQZdotted_as_namesZdotted_as_namer rrrPstrstriprrOZimport_as_nameSTAR)r0r rrRrlastr:rrrrs@ !  !!% ! !!r)8__doc__ itertoolsrZpgen2rZpytreerrZpygramrr r7rrrrrr!r&r)r#r1r3r6r8r;r@rCrKrSr[r_rarbZconsuming_callsrnrqrrrsrprwr|rrrrrrryrxrrr"rrrrrrrs\                     -*