f _f;@sdZddlmZddlmZmZddlmZddl m Z ddZ dd Z d d Z d d ZdNddZddZddZddZe e fddZdOddZddZddZdPdd Zd!d"ZdQd#d$ZdRd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zhd1Z d2d3Z!d4a"d5a#d6a$d7a%d8d9Z&d:d;Z'dd?Z)d@dAZ*dBdCZ+dDdEZ,dFdGZ-ej.ej/hZ0dSdHdIZ1ej/ej.ej2hZ3dJdKZ4dTdLdMZ5dS)Uz1Utility functions, node construction macros, etc.)token)LeafNode)python_symbols)patcompcCsttj|ttjd|gS)N=)rsymsargumentrrEQUAL)keywordvaluer +/usr/lib64/python3.10/lib2to3/fixer_util.py KeywordArgsrcCs ttjdS)N()rrLPARr r r rLParensrcCs ttjdS)N))rrRPARr r r rRParensrcCsHt|ts|g}t|ts&d|_|g}ttj|ttjdddg|S)zBuild an assignment statement rprefix) isinstancelistrrratomrrr )targetsourcer r rAssigns  rNcCsttj||dS)zReturn a NAME leafr)rrNAME)namerr r rName$sr!cCs|ttjt|ggS)zA node tuple for obj.attr)rrtrailerDot)objattrr r rAttr(sr&cCs ttjdS)z A comma leaf,)rrCOMMAr r r rComma,sr)cCs ttjdS)zA period (.) leaf.)rrDOTr r r rr#0sr#cCs4ttj||g}|r0|dttj||S)z-A parenthesised argument list, used by Call()r)rrr"clone insert_childarglist)argsZlparenZrparennoder r rArgList4sr1cCs&ttj|t|g}|dur"||_|S)zA function callN)rrpowerr1r) func_namer/rr0r r rCall;sr4cCs ttjdS)zA newline literal rrNEWLINEr r r rNewlineBsr8cCs ttjdS)z A blank liner6r r r r BlankLineFsr:cCsttj||dS)Nr)rrNUMBER)nrr r rNumberJsr=cCs"ttjttjd|ttjdgS)zA numeric or string subscript[])rrr"rrLBRACERBRACE)Z index_noder r r SubscriptMs rBcCsttj||dS)z A string leafr)rrSTRING)stringrr r rStringSsrEc Csd|_d|_d|_ttjd}d|_ttjd}d|_||||g}|rtd|_ttjd}d|_|ttj||gttj|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. r9rforinifr>r?) rrrrappendrrZcomp_ifZ listmakerZcomp_forrr@rA) ZxpfpittestZfor_leafZin_leafZ inner_argsZif_leafinnerr r rListCompWs(      rNcCsV|D] }|qttjdttj|ddttjdddttj|g}ttj|}|S)zO Return an import statement in the form: from package import name_leafsfromrrimport)removerrrrrimport_as_names import_from)Z package_nameZ name_leafsZleafchildrenimpr r r FromImportos    rVc Cs|d}|jtjkr"|}nttj|g}|d}|rNdd|D}ttjtt|dt|dttj|d||dgg|}|j |_ |S) zfReturns an import statement and calls a method of the module: import module module.name()r$aftercSsg|] }|qSr )r,).0r<r r r z!ImportAndCall..rZlparZrpar) r,typerr.rr2r&r!r"r)r0resultsnamesr$Z newarglistrWnewr r r ImportAndCalls*     r`cCst|tr |jttgkr dSt|tot|jdkot|jdtot|jdtot|jdto|jdjdko|jdjdkS)z(Does the node represent a tuple literal?Tr[rrr)rrrTrrlenrr r0r r ris_tuples  recCsXt|toVt|jdkoVt|jdtoVt|jdtoV|jdjdkoV|jdjdkS)z'Does the node represent a list literal?rr[r>r?)rrrcrTrr rdr r ris_lists  rgcCsttjt|tgSN)rrrrrrdr r r parenthesizesri> sumtuplermax enumerateminsetsortedallanyccs$t||}|r |Vt||}q dS)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%nextr r r attr_chains ruzefor_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* > FcCsnts&ttattattadatttg}t|t|dD](\}}i}|||r@|d|ur@dSdS)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_builtrcompile_patternp0p1p2ziprumatch)r0patternspatternrvr]r r rin_special_contexts    rcCs|j}|dur|jtjkrdS|j}|jtjtjfvr:dS|jtjkrX|j d|urXdS|jtj ks|jtj kr|dur|jtj ks|j d|urdSdS)zG Check that something isn't an attribute or function name etc. NFr[T) Z prev_siblingr\rr+rvrfuncdefclassdef expr_stmtrTZ parametersZ typedargslistr()r0prevrvr r ris_probably_builtins&    rcCsJ|durF|jtjkr>t|jdkr>|jd}|jtjkr>|jS|j}qdS)zFind the indentation of *node*.Nrbrr9) r\rsuitercrTrINDENTr rv)r0indentr r rfind_indentations  rcCs>|jtjkr|S|}|jd}|_ttj|g}||_|Srh)r\rrr,rvr)r0rvrr r r make_suites rcCs$|jtjkr |j}|stdq|S)zFind the top level namespace.z,root found before file_input node was found.)r\rZ file_inputrv ValueErrorrdr r r find_root&s   rcCst|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 r0Zbindingr r rdoes_tree_import/srcCs|jtjtjfvS)z0Returns true if the node is an import statement.)r\r import_namerSrdr r r is_import7src Cs.dd}t|}t|||r dSd}}t|jD]F\}}||sDq2t|j|dD]\}}||sVqlqV||}qzq2|dkrt|jD]8\}}|jtjkr|jr|jdjtjkr|d}qq|durt tj t tj dt tj |ddg} nt |t tj |ddg} | tg} ||t tj| dS) z\ Works like `does_tree_import` but adds an import statement if it was not imported. cSs |jtjko|jot|jdS)Nr[)r\r simple_stmtrTrrdr r ris_import_stmt>s z$touch_import..is_import_stmtNr[rrPrr)rrrmrTr\rrrrCrrrrrVr8r-) rr r0rrootZ insert_posoffsetidxZnode2import_rTr r r touch_import;s8   rcCs|jD]}d}|jtjkrXt||jdr4|St|t|jd|}|r|}n0|jtjtjfvrt|t|jd|}|r|}n|jtj krt|t|jd|}|r|}nt |jddD]@\}}|jt j kr|j dkrt|t|j|d|}|r|}qnx|jtvr4|jdj |kr4|}nTt|||rH|}n@|jtjkrdt|||}n$|jtjkrt||jdr|}|r|s|St|r|SdS) 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.Nrrfrbra:r[)rTr\rZfor_stmt_findrrZif_stmtZ while_stmtZtry_stmtrmrCOLONr _def_syms_is_import_bindingrrr)r r0rchildretr<iZkidr r rrisB    rcCsR|g}|rN|}|jdkr4|jtvr4||jq|jtjkr|j|kr|SdS)N)popr\ _block_symsextendrTrrr )r r0Znodesr r rrsrcCs|jtjkr|s|jd}|jtjkrx|jD]F}|jtjkrT|jdj|krr|S|jtjkr,|j|kr,|Sq,n&|jtjkr|jd}|jtjkr|j|kr|S|jtjkr|j|kr|Sn|jtj kr|rt |jd |krdS|jd}|rt d|rdS|jtj kr2t ||r2|S|jtjkrh|jd}|jtjkr|j|kr|S|jtjkr|j|kr|S|r|jtjkr|SdS)z Will return node if node will import name, or node will import * from package. None is returned otherwise. See test cases for examples. rrbrfNraas)r\rrrTZdotted_as_namesZdotted_as_namer rrrSstrstriprrRZimport_as_nameSTAR)r0r rrUrZlastr<r r rrs@        r)N)NN)N)N)N)N)N)6__doc__Zpgen2rZpytreerrZpygramrrr9rrrrrr!r&r)r#r1r4r8r:r=rBrErNrVr`rergriZconsuming_callsruryrzr{rwrrrrrrrrrrrrr"rrrr r r rsX           -  *