��Qc@sdZddlmZddlmZddlmZmZddlm Z
ddlmZd�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(sA 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(RRRtclonetinsert_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=scCsttjd�S(sA newline literalu
(RRtNEWLINE(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytNewlineDscCsttjd�S(sA 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
ttjd�|ttjd�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_namestimport_from(tpackage_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�scCsttjt�|t�g�S(N(RRRR
R(R(((s(/sys/lib/python2.7/lib2to3/fixer_util.pytparenthesize�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<node=any any*> ')' >
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||jd|kr|tS|jtjks�|jtj
kr�|dk r�|jtjks�|jd|kr�tStS(sG
Check that something isn't an attribute or function name etc.
iN(tprev_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. (tfind_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.(RqRtimport_nameRK(R(((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt is_import"scCs�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
tjttj
d�ttj
|dd�g�} n$t|ttj
|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(RqRtsimple_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.pyttouch_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\}}|jtj
kr|jdkrt|t|j|d�|�}|ru|}quqqWn�|jtkr�|jdj|kr�|}nvt|||�r�|}n[|jtjkr�|||�}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_findR�R}tif_stmtt
while_stmtttry_stmtR_RtCOLONR
t _def_symst_is_import_bindingR�RtR�(RR(R�tchildtretR2titkid((s(/sys/lib/python2.7/lib2to3/fixer_util.pyR�TsH !#% cCs||g}xl|rw|j�}|jdkrO|jtkrO|j|j�q|jtjkr|j|kr|SqWdS(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 itertoolsRtpgen2RtpytreeRRtpygramRRtRRR
RRR+RRR RR)R-R/R0R3R7R:RHRQRTRURVRXtconsuming_callsRbRfRgRhRkRdRoRxR|R}R�R�R�R�RsRrR�R�RR�R�R�(((s(/sys/lib/python2.7/lib2to3/fixer_util.pyt<module>sZ -*
|