ž zyMaØ8c @s|dZddlmZddlmZddlmZmZddlm Z ddl m Z dd „Z d d „Zd d „Zdd„ZdUdd„Zdd„Zdd„Zdd„Zeƒeƒdd„ZdUdUdd„Zdd„Zdd„ZdUd d!„Zd"d#„ZdUd$d%„ZdUd&d'„Zd(d)„Zd*d+„Zd,d-„Z d.d/„Z!e"d0d1d2d3d4d5d6d7d8d9g ƒZ#d:d;„Z$d<a%d=a&d>a'dVa)d?d@„Z*dAdB„Z+dCdD„Z,dEdF„Z-dGdH„Z.dIdJ„Z/dKdL„Z0dMdN„Z1e"e j2e j3gƒZ4dUdOdP„Z5e"e j3e j2e j6gƒZ7dQdR„Z8dUdSdT„Z9dUS(Wu1Utility functions, node construction macros, etc.i(uislicei(utoken(uLeafuNode(upython_symbols(upatcompcCs%ttj|ttjdƒ|gƒS(Nu=(uNodeusymsuargumentuLeafutokenuEQUAL(ukeyworduvalue((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu KeywordArgs u KeywordArgcCsttjdƒS(Nu((uLeafutokenuLPAR(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuLParensuLParencCsttjdƒS(Nu)(uLeafutokenuRPAR(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuRParensuRParencCslt|tƒs|g}nt|tƒs?d|_|g}nttj|ttjdddƒg|ƒS(uBuild an assignment statementu u=uprefix( u isinstanceulistuprefixuNodeusymsuatomuLeafutokenuEQUAL(utargetusource((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuAssigns    uAssigncCsttj|d|ƒS(uReturn a NAME leafuprefix(uLeafutokenuNAME(unameuprefix((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuName&suNamecCs|ttjtƒ|gƒgS(uA node tuple for obj.attr(uNodeusymsutraileruDot(uobjuattr((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuAttr*suAttrcCsttjdƒS(u A comma leafu,(uLeafutokenuCOMMA(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuComma.suCommacCsttjdƒS(uA period (.) leafu.(uLeafutokenuDOT(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuDot2suDotcCsMttj|jƒ|jƒgƒ}|rI|jdttj|ƒƒn|S(u-A parenthesised argument list, used by Call()i(uNodeusymsutrailerucloneu insert_childuarglist(uargsulparenurparenunode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuArgList6s$uArgListcCs:ttj|t|ƒgƒ}|dk r6||_n|S(uA function callN(uNodeusymsupoweruArgListuNoneuprefix(u func_nameuargsuprefixunode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuCall=s  uCallcCsttjdƒS(uA newline literalu (uLeafutokenuNEWLINE(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuNewlineDsuNewlinecCsttjdƒS(u A blank lineu(uLeafutokenuNEWLINE(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu BlankLineHsu BlankLinecCsttj|d|ƒS(Nuprefix(uLeafutokenuNUMBER(unuprefix((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuNumberLsuNumbercCs1ttjttjdƒ|ttjdƒgƒS(uA numeric or string subscriptu[u](uNodeusymsutraileruLeafutokenuLBRACEuRBRACE(u index_node((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu SubscriptOsu SubscriptcCsttj|d|ƒS(u A string leafuprefix(uLeafutokenuSTRING(ustringuprefix((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuStringUsuStringc Csd|_d|_d|_ttjdƒ}d|_ttjdƒ}d|_||||g}|r¯d|_ttjdƒ}d|_|jttj||gƒƒnttj|ttj |ƒgƒ}ttj ttj dƒ|ttj dƒgƒS(uuA 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]( uprefixuLeafutokenuNAMEuappenduNodeusymsucomp_ifu listmakerucomp_foruatomuLBRACEuRBRACE( uxpufpuitutestufor_leafuin_leafu inner_argsuif_leafuinner((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuListCompYs$       "$ uListCompcCsx|D]}|jƒqWttjdƒttj|ddƒttjdddƒttj|ƒg}ttj|ƒ}|S(uO Return an import statement in the form: from package import name_leafsufromuprefixu uimport(uremoveuLeafutokenuNAMEuNodeusymsuimport_as_namesu import_from(u package_nameu name_leafsuleafuchildrenuimp((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu FromImportqs u FromImportc Cs¾t|tƒr.|jtƒtƒgkr.dSt|tƒo½t|jƒdko½t|jdtƒo½t|jdtƒo½t|jdtƒo½|jdjdko½|jdjdkS(u(Does the node represent a tuple literal?iiiiu(u)T( u isinstanceuNodeuchildrenuLParenuRParenuTrueulenuLeafuvalue(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuis_tuple‰s*uis_tuplecCszt|tƒoyt|jƒdkoyt|jdtƒoyt|jdtƒoy|jdjdkoy|jdjdkS(u'Does the node represent a list literal?iiu[u]iÿÿÿÿiÿÿÿÿ(u isinstanceuNodeulenuchildrenuLeafuvalue(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuis_list•s uis_listcCsttjtƒ|tƒgƒS(N(uNodeusymsuatomuLParenuRParen(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu parenthesize£su parenthesizeusortedulistusetuanyuallutupleusumuminumaxu enumerateccs4t||ƒ}x|r/|Vt||ƒ}qWdS(ulFollow 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(ugetattr(uobjuattrunext((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu attr_chainªs u attr_chainuefor_stmt< 'for' any 'in' node=any ':' any* > | comp_for< 'for' any 'in' node=any any* > u³ power< ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' | 'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) ) trailer< '(' node=any ')' > any* > u` power< ( 'sorted' | 'enumerate' ) trailer< '(' arglist ')' > any* > cCs¤ts<tjtƒatjtƒatjtƒadantttg}xRt|t|dƒƒD]8\}}i}|j ||ƒrd|d|krddSqdWdS(u 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. uparentunodeTF( u pats_builtupatcompucompile_patternup0up1up2uTrueuzipu attr_chainumatchuFalse(unodeupatternsupatternuparenturesults((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuin_special_contextÐs %"uin_special_contextcCsÙ|j}|dk r+|jtjkr+dS|j}|jtjtj fkrSdS|jtj kr||j d|kr|dS|jtj ksÑ|jtj krÕ|dk r¾|jtjksÑ|j d|krÕdSdS(uG Check that something isn't an attribute or function name etc. iNFT(u prev_siblinguNoneutypeutokenuDOTuFalseuparentusymsufuncdefuclassdefu expr_stmtuchildrenu parametersu typedargslistuCOMMAuTrue(unodeuprevuparent((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuis_probably_builtinãs  %uis_probably_builtincCspxi|dk rk|jtjkr_t|jƒdkr_|jd}|jtjkr_|jSn|j }qWdS(uFind the indentation of *node*.iiuN( uNoneutypeusymsusuiteulenuchildrenutokenuINDENTuvalueuparent(unodeuindent((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyufind_indentationús'   ufind_indentationcCsW|jtjkr|S|jƒ}|jd}|_ttj|gƒ}||_|S(N(utypeusymsusuiteucloneuparentuNoneuNode(unodeuparentusuite((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu make_suites  u make_suitecCs;x4|jtjkr6|j}|stdƒ‚qqW|S(uFind the top level namespace.u,root found before file_input node was found.(utypeusymsu file_inputuparentu ValueError(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu find_roots  u find_rootcCs"t|t|ƒ|ƒ}t|ƒS(uÞ 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. (u find_bindingu find_rootubool(upackageunameunodeubinding((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyudoes_tree_importsudoes_tree_importcCs|jtjtjfkS(u0Returns true if the node is an import statement.(utypeusymsu import_nameu import_from(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu is_import"su is_importc Cs¸dd„}t|ƒ}t|||ƒr.dSd}}xtt|jƒD]c\}}||ƒsfqHnx7t|j|d…ƒD]\}}||ƒs€Pq€q€W||}PqHW|dkr xbt|jƒD]N\}}|jtjkrË|jrË|jdjtjkrË|d}PqËqËWn|dkret tj t tj dƒt tj |ddƒgƒ} n$t|t tj |ddƒgƒ} | tƒg} |j|t tj| ƒƒdS( u\ Works like `does_tree_import` but adds an import statement if it was not imported. cSs,|jtjko+|jo+t|jdƒS(Ni(utypeusymsu simple_stmtuchildrenu is_import(unode((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyuis_import_stmt)su$touch_import..is_import_stmtNiiuimportuprefixu (u find_rootudoes_tree_importu enumerateuchildrenutypeusymsu simple_stmtutokenuSTRINGuNoneuNodeu import_nameuLeafuNAMEu FromImportuNewlineu insert_child( upackageunameunodeuis_import_stmturootu insert_posuoffsetuidxunode2uimport_uchildren((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu touch_import&s4    &       !$u touch_importc CsQxJ|jD]?}d}|jtjkrst||jdƒrB|St|t|jdƒ|ƒ}|r&|}q&n³|jtjtj fkr¿t|t|jd ƒ|ƒ}|r&|}q&ng|jtj kr‚t|t|jdƒ|ƒ}|rÿ|}q&x$t |jdd…ƒD]b\}}|jt j kr|jdkrt|t|j|dƒ|ƒ}|r{|}q{qqWn¤|jtkr°|jdj|kr°|}nvt|||ƒrË|}n[|jtjkròt|||ƒ}n4|jtjkr&t||jdƒr&|}q&n|r |s6|St|ƒrI|Sq q WdS( uº 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.iiiNu:iiiÿÿÿÿiÿÿÿÿ(uchildrenuNoneutypeusymsufor_stmtu_findu find_bindingu make_suiteuif_stmtu while_stmtutry_stmtu enumerateutokenuCOLONuvalueu _def_symsu_is_import_bindingu simple_stmtu expr_stmtu is_import(unameunodeupackageuchilduretunuiukid((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu find_bindingTsH   &!#%     u find_bindingcCs||g}xl|rw|jƒ}|jdkrO|jtkrO|j|jƒq |jtjkr |j|kr |Sq WdS(Ni( upoputypeu _block_symsuextenduchildrenutokenuNAMEuvalueuNone(unameunodeunodes((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu_finds   !u_findcCs'|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(u› Will reuturn node if node will import name, or node will import * from package. None is returned otherwise. See test cases for examples. iiiuasiÿÿÿÿN(utypeusymsu import_nameuchildrenudotted_as_namesudotted_as_nameuvalueutokenuNAMEu import_fromustrustripuNoneu_finduimport_as_namesuimport_as_nameuSTAR(unodeunameupackageuimpuchildulastun((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyu_is_import_binding‰s@ !  !!% ! !!u_is_import_bindingNF(:u__doc__u itertoolsuisliceupgen2utokenupytreeuLeafuNodeupygramupython_symbolsusymsuupatcompu KeywordArguLParenuRParenuAssignuNoneuNameuAttruCommauDotuArgListuCalluNewlineu BlankLineuNumberu SubscriptuStringuListCompu FromImportuis_tupleuis_listu parenthesizeusetuconsuming_callsu attr_chainup0up1up2uFalseu pats_builtuin_special_contextuis_probably_builtinufind_indentationu make_suiteu find_rootudoes_tree_importu is_importu touch_importuclassdefufuncdefu _def_symsu find_bindingutraileru _block_symsu_findu_is_import_binding(((u7/opt/alt/python33/lib64/python3.3/lib2to3/fixer_util.pyusZ                    -*