ó u®¸Qc@s›dZddlmZddlmZddlmZmZmZm Z d„Z d„Z d„Z d„Z d „Zd „Zd ejfd „ƒYZd S(sŒFixer for __metaclass__ = X -> (metaclass=X) methods. The various forms of classef (inherits nothing, inherits once, inherints many) don't parse the same in the CST so we look at ALL classes for a __metaclass__ and if we find one normalize the inherits to all be an arglist. For one-liner classes ('class X: pass') there is no indent/dedent so we normalize those into having a suite. Moving the __metaclass__ into the classdef can also cause the class body to be empty so there is some special casing for that as well. This fixer also tries very hard to keep original indenting and spacing in all those corner cases. i(t fixer_base(ttoken(tNametsymstNodetLeafcCs¬x¥|jD]š}|jtjkr,t|ƒS|jtjkr |jr |jd}|jtjkr¤|jr¤|jd}t|tƒr¡|j dkr¡t Sq¤q q Wt S(së we have to check the cls_node without changing it. There are two possiblities: 1) clsdef => suite => simple_stmt => expr_stmt => Leaf('__meta') 2) clsdef => simple_stmt => expr_stmt => Leaf('__meta') it __metaclass__( tchildrenttypeRtsuitet has_metaclasst simple_stmtt expr_stmtt isinstanceRtvaluetTruetFalse(tparenttnodet expr_nodet left_side((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyR s   cCsÛx'|jD]}|jtjkr dSq Wx?t|jƒD]"\}}|jtjkr:Pq:q:Wtdƒ‚ttjgƒ}xC|j|drÃ|j|d}|j |j ƒƒ|j ƒqW|j |ƒ|}dS(sf one-line classes don't get a suite in the parse tree so we add one to normalize the tree NsNo class suite and no ':'!i( RRRR t enumerateRtCOLONt ValueErrorRt append_childtclonetremove(tcls_nodeRtiR t move_node((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pytfixup_parse_tree-s  c Csñx7t|jƒD]"\}}|jtjkrPqqWdS|jƒttjgƒ}ttj |gƒ}x;|j|r¨|j|}|j |j ƒƒ|jƒqnW|j ||ƒ|jdjd}|jdjd} | j |_ dS(sÑ if there is a semi-colon all the parts count as part of the same simple_stmt. We just want the __metaclass__ part so we move everything efter the semi-colon into its own simple_stmt node Ni(RRRRtSEMIRRRR R RRt insert_childtprefix( RRt stmt_nodetsemi_indRtnew_exprtnew_stmtRt new_leaf1t old_leaf1((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pytfixup_simple_stmtGs  cCs:|jr6|jdjtjkr6|jdjƒndS(Niÿÿÿÿ(RRRtNEWLINER(R((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pytremove_trailing_newline_s"ccsÿx3|jD]}|jtjkr Pq q Wtdƒ‚xÂtt|jƒƒD]«\}}|jtjkrL|jrL|jd}|jtjkr÷|jr÷|jd}t |t ƒrô|j dkrôt |||ƒt |ƒ|||fVqôq÷qLqLWdS(NsNo class suite!iu __metaclass__(RRRR RtlistRR R R RRR(R*(RRRt simple_nodeRt left_node((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyt find_metasds "   cCs¹|jddd…}x,|rD|jƒ}|jtjkrPqqWxm|r´|jƒ}t|tƒr”|jtjkr”|jrd|_ndS|j |jddd…ƒqHWdS(s› If an INDENT is followed by a thing with a prefix then nuke the prefix Otherwise we get in trouble when removing __metaclass__ at suite start Niÿÿÿÿu( RtpopRRtINDENTR RtDEDENTR!textend(R tkidsR((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyt fixup_indent{s    !  t FixMetaclasscBseZeZdZd„ZRS(s classdef cCst|ƒsdSt|ƒd}x-t|ƒD]\}}}|}|jƒq-W|jdj}t|jƒdkrÙ|jdjtj krž|jd}q‘|jdj ƒ} t tj | gƒ}|j d|ƒn¸t|jƒdkrt tj gƒ}|j d|ƒn~t|jƒdkr…t tj gƒ}|j dttjdƒƒ|j d|ƒ|j dttjdƒƒn td ƒ‚|jdjd} d | _| j} |jrå|jttjd ƒƒd | _n d | _|jd} d | jd_d | jd_|j|ƒt|ƒ|js†|jƒt|dƒ} | | _|j| ƒ|jttjdƒƒn…t|jƒdkr |jdjtjkr |jdjtjkr t|dƒ} |j d| ƒ|j dttjdƒƒndS(Niiiiiiu)u(sUnexpected class definitiont metaclassu,u uiupassu iþÿÿÿiÿÿÿÿ(R RtNoneR.RRRtlenRtarglistRRt set_childR RRtRPARtLPARRRR!RtCOMMAR4R)R0R1(tselfRtresultstlast_metaclassR Rtstmtt text_typeR9Rtmeta_txttorig_meta_prefixR t pass_leaf((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyt transform˜s^               (t__name__t __module__Rt BM_compatibletPATTERNRF(((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyR5‘sN(t__doc__tRtpygramRt fixer_utilRRRRR RR(R*R.R4tBaseFixR5(((s1/sys/lib/python2.7/lib2to3/fixes/fix_metaclass.pyts"