f _f;c@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=)rsymsZargumentrrEQUAL)Zkeywordvaluer +/usr/lib64/python3.10/lib2to3/fixer_util.py KeywordArgsr cC ttjdS)N()rrZLPARr r r r LParenrcCr )N))rrZRPARr r r r RParenrrcCsHt|ts|g}t|ts&d|_|g}ttj|ttjdddg|S)zBuild an assignment statement rprefix) isinstancelistrrratomrrr)ZtargetZsourcer r r Assigns  rNcCttj||dS)zReturn a NAME leafr)rrNAME)namerr r r Name$rcCs|ttjt|ggS)zA node tuple for obj.attr)rrtrailerDot)objattrr r r Attr(rr#cCr )z A comma leafz,)rrCOMMAr r r r Comma,rr%cCr )zA period (.) leafZ.)rrDOTr r r r r 0rr cCs4ttj||g}|r0|dttj||S)z-A parenthesised argument list, used by Call()r)rrrclone insert_childarglist)argsZlparenZrparennoder r r ArgList4sr,cCs&ttj|t|g}|dur"||_|S)zA function callN)rrpowerr,r)Z func_namer*rr+r r r Call;sr.cCr )zA newline literalZ rrZNEWLINEr r r r NewlineBrr0cCr )z A blank liner/r r r r BlankLineFrr2cCr)Nr)rrZNUMBER)nrr r r NumberJrr4cCs"ttjttjd|ttjdgS)zA numeric or string subscript[])rrrrrLBRACERBRACE)Z index_noder r r SubscriptMs r9cCr)z A string leafr)rrSTRING)Zstringrr r r StringSrr;c 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. r1rZforZinZifr5r6) rrrrZappendrrZcomp_ifZ listmakerZcomp_forrr7r8) ZxpZfpZitZtestZfor_leafZin_leafZ inner_argsZif_leafZinnerr r r ListCompWs(      r<cCsV|D] }|qttjdttj|ddttjdddttj|g}ttj|}|S)zO Return an import statement in the form: from package import name_leafsZfromrrimport)Zremoverrrrrimport_as_names import_from)Z package_nameZ name_leafsZleafchildrenimpr r r FromImportos    rBc 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')Z.0r3r r r Z sz!ImportAndCall..rZlparZrpar) r'typerr)rr-r#rrr)r+resultsZnamesr!Z newarglistrCZnewr r r ImportAndCalls*     rGcCst|tr |jttgkr dSt|tot|jdkot|jdtot|jdtot|jdto|jdjdko|jdjdkS)z(Does the node represent a tuple literal?TrDrrr)rrr@rrlenrr r+r r r is_tuples  rLcCsXt|toVt|jdkoVt|jdtoVt|jdtoV|jdjdkoV|jdjdkS)z'Does the node represent a list literal?rrDr5r6)rrrJr@rr rKr r r is_lists  rNcCsttjt|tgSN)rrrrrrKr r r parenthesizerrP> ZsumZtuplerZmax enumerateZminZsetZsortedZallZanyccs$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)Zgetattr)r!r"Znextr r r attr_chains rRzefor_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. Tparentr+F) pats_builtrZcompile_patternp0p1p2ZziprRZmatch)r+ZpatternsZpatternrSrFr r r in_special_contexts    rXcCs|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. NFrDT) Z prev_siblingrErr&rSrfuncdefclassdef expr_stmtr@Z parametersZ typedargslistr$)r+ZprevrSr r r is_probably_builtins&    r\cCsJ|durF|jtjkr>t|jdkr>|jd}|jtjkr>|jS|j}qdS)zFind the indentation of *node*.NrIrr1) rErsuiterJr@rZINDENTr rS)r+Zindentr r r find_indentations  r^cCs>|jtjkr|S|}|jd}|_ttj|g}||_|SrO)rErr]r'rSr)r+rSr]r r r make_suites r_cCs$|jtjkr |j}|stdq|S)zFind the top level namespace.z,root found before file_input node was found.)rErZ file_inputrSZ ValueErrorrKr r r find_root&s   r`cCst|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_bindingr`Zbool)packagerr+Zbindingr r r does_tree_import/srccCs|jtjtjfvS)z0Returns true if the node is an import statement.)rEr import_namer?rKr r r is_import7rrec 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)NrD)rEr simple_stmtr@rerKr r r is_import_stmt>s z$touch_import..is_import_stmtNrDrr=rr)r`rcrQr@rErrfrr:rrdrrrBr0r() rbrr+rgZrootZ insert_posZoffsetZidxZnode2Zimport_r@r r r touch_import;s8   rhcCs|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.NrrMrIrHz:irD)r@rErZfor_stmt_findrar_Zif_stmtZ while_stmtZtry_stmtrQrZCOLONr _def_syms_is_import_bindingrfr[re)rr+rbchildZretr3ZiZkidr r r raisB    racCsR|g}|rN|}|jdkr4|jtvr4||jq|jtjkr|j|kr|SdS)Ni)ZpoprE _block_symsZextendr@rrr )rr+Znodesr r r risricCs|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. rrIrMNrHZas)rErrdr@Zdotted_as_namesZdotted_as_namer rrr?ZstrZstriprir>Zimport_as_nameZSTAR)r+rrbrArlZlastr3r r r rks@        rkrO)NNrOrOrOrOrO)6Z__doc__Zpgen2rZpytreerrZpygramrrr1rr rrrrr#r%r r,r.r0r2r4r9r;r<rBrGrLrNrPZconsuming_callsrRrUrVrWrTrXr\r^r_r`rcrerhrZrYrjrarrmrirkr r r r ZsX           -  *