<s> """<STR_LIT>""" <EOL> from lib2to3 import fixer_base <EOL> from lib2to3 . pygram import token <EOL> from lib2to3 . fixer_util import Name , syms , Node , Leaf , touch_import , Call , String , Comma , parenthesize <EOL> def has_metaclass ( parent ) : <EOL> """<STR_LIT>""" <EOL> for node in parent . children : <EOL> if node . type == syms . suite : <EOL> return has_metaclass ( node ) <EOL> elif node . type == syms . simple_stmt and node . children : <EOL> expr_node = node . children [ <NUM_LIT:0> ] <EOL> if expr_node . type == syms . expr_stmt and expr_node . children : <EOL> left_side = expr_node . children [ <NUM_LIT:0> ] <EOL> if isinstance ( left_side , Leaf ) and left_side . value == '<STR_LIT>' : <EOL> return True <EOL> return False <EOL> def fixup_parse_tree ( cls_node ) : <EOL> """<STR_LIT>""" <EOL> for node in cls_node . children : <EOL> if node . type == syms . suite : <EOL> return <EOL> for i , node in enumerate ( cls_node . children ) : <EOL> if node . type == token . COLON : <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> suite = Node ( syms . suite , [ ] ) <EOL> while cls_node . children [ i + <NUM_LIT:1> : ] : <EOL> move_node = cls_node . children [ i + <NUM_LIT:1> ] <EOL> suite . append_child ( move_node . clone ( ) ) <EOL> move_node . remove ( ) <EOL> cls_node . append_child ( suite ) <EOL> node = suite <EOL> def fixup_simple_stmt ( parent , i , stmt_node ) : <EOL> """<STR_LIT>""" <EOL> for semi_ind , node in enumerate ( stmt_node . children ) : <EOL> if node . type == token . SEMI : <EOL> break <EOL> else : <EOL> return <EOL> node . remove ( ) <EOL> new_expr = Node ( syms . expr_stmt , [ ] ) <EOL> new_stmt = Node ( syms . simple_stmt , [ new_expr ] ) <EOL> while stmt_node . children [ semi_ind : ] : <EOL> move_node = stmt_node . children [ semi_ind ] <EOL> new_expr . append_child ( move_node . clone ( ) ) <EOL> move_node . remove ( ) <EOL> parent . insert_child ( i , new_stmt ) <EOL> new_leaf1 = new_stmt . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] <EOL> old_leaf1 = stmt_node . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] <EOL> new_leaf1 . prefix = old_leaf1 . prefix <EOL> def remove_trailing_newline ( node ) : <EOL> if node . children and node . children [ - <NUM_LIT:1> ] . type == token . NEWLINE : <EOL> node . children [ - <NUM_LIT:1> ] . remove ( ) <EOL> def find_metas ( cls_node ) : <EOL> for node in cls_node . children : <EOL> if node . type == syms . suite : <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> for i , simple_node in list ( enumerate ( node . children ) ) : <EOL> if simple_node . type == syms . simple_stmt and simple_node . children : <EOL> expr_node = simple_node . children [ <NUM_LIT:0> ] <EOL> if expr_node . type == syms . expr_stmt and expr_node . children : <EOL> left_node = expr_node . children [ <NUM_LIT:0> ] <EOL> if isinstance ( left_node , Leaf ) and left_node . value == u'<STR_LIT>' : <EOL> fixup_simple_stmt ( node , i , simple_node ) <EOL> remove_trailing_newline ( simple_node ) <EOL> yield ( node , i , simple_node ) <EOL> def fixup_indent ( suite ) : <EOL> """<STR_LIT>""" <EOL> kids = suite . children [ : : - <NUM_LIT:1> ] <EOL> while kids : <EOL> node = kids . pop ( ) <EOL> if node . type == token . INDENT : <EOL> break <EOL> while kids : <EOL> node = kids . pop ( ) <EOL> if isinstance ( node , Leaf ) and node . type != token . DEDENT : <EOL> if node . prefix : <EOL> node . prefix = u'<STR_LIT>' <EOL> return <EOL> else : <EOL> kids . extend ( node . children [ : : - <NUM_LIT:1> ] ) <EOL> class FixMetaclass ( fixer_base . BaseFix ) : <EOL> BM_compatible = True <EOL> PATTERN = """<STR_LIT>""" <EOL> def transform ( self , node , results ) : <EOL> if not has_metaclass ( node ) : <EOL> return <EOL> fixup_parse_tree ( node ) <EOL> last_metaclass = None <EOL> for suite , i , stmt in find_metas ( node ) : <EOL> last_metaclass = stmt <EOL> stmt . remove ( ) <EOL> text_type = node . children [ <NUM_LIT:0> ] . type <EOL> if len ( node . children ) == <NUM_LIT:7> : <EOL> if node . children [ <NUM_LIT:3> ] . type == syms . arglist : <EOL> arglist = node . children [ <NUM_LIT:3> ] <EOL> else : <EOL> parent = node . children [ <NUM_LIT:3> ] . clone ( ) <EOL> arglist = Node ( syms . arglist , [ parent ] ) <EOL> node . set_child ( <NUM_LIT:3> , arglist ) <EOL> elif len ( node . children ) == <NUM_LIT:6> : <EOL> arglist = Node ( syms . arglist , [ ] ) <EOL> node . insert_child ( <NUM_LIT:3> , arglist ) <EOL> elif len ( node . children ) == <NUM_LIT:4> : <EOL> arglist = Node ( syms . arglist , [ ] ) <EOL> node . insert_child ( <NUM_LIT:2> , Leaf ( token . RPAR , u'<STR_LIT:)>' ) ) <EOL> node . insert_child ( <NUM_LIT:2> , arglist ) <EOL> node . insert_child ( <NUM_LIT:2> , Leaf ( token . LPAR , u'<STR_LIT:(>' ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> meta_txt = last_metaclass . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] <EOL> meta_txt . value = '<STR_LIT>' <EOL> orig_meta_prefix = meta_txt . prefix <EOL> touch_import ( u'<STR_LIT>' , u'<STR_LIT>' , node ) <EOL> metaclass = last_metaclass . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:2> ] . clone ( ) <EOL> metaclass . prefix = u'<STR_LIT>' <EOL> arguments = [ metaclass ] <EOL> if arglist . children : <EOL> if len ( arglist . children ) == <NUM_LIT:1> : <EOL> base = arglist . children [ <NUM_LIT:0> ] . clone ( ) <EOL> base . prefix = u'<STR_LIT:U+0020>' <EOL> else : <EOL> bases = parenthesize ( arglist . clone ( ) ) <EOL> bases . prefix = u'<STR_LIT:U+0020>' <EOL> base = Call ( Name ( '<STR_LIT:type>' ) , [ <EOL> String ( "<STR_LIT>" ) , <EOL> Comma ( ) , <EOL> bases , <EOL> Comma ( ) , <EOL> Node ( <EOL> syms . atom , <EOL> [ Leaf ( token . LBRACE , u'<STR_LIT:{>' ) , Leaf ( token . RBRACE , u'<STR_LIT:}>' ) ] , <EOL> prefix = u'<STR_LIT:U+0020>' <EOL> ) <EOL> ] , prefix = u'<STR_LIT:U+0020>' ) <EOL> arguments . extend ( [ Comma ( ) , base ] ) <EOL> arglist . replace ( Call ( <EOL> Name ( u'<STR_LIT>' , prefix = arglist . prefix ) , <EOL> arguments <EOL> ) ) <EOL> fixup_indent ( suite ) <EOL> if not suite . children : <EOL> suite . remove ( ) <EOL> pass_leaf = Leaf ( text_type , u'<STR_LIT>' ) <EOL> pass_leaf . prefix = orig_meta_prefix <EOL> node . append_child ( pass_leaf ) <EOL> node . append_child ( Leaf ( token . NEWLINE , u'<STR_LIT:\n>' ) ) <EOL> elif len ( suite . children ) > <NUM_LIT:1> and ( suite . children [ - <NUM_LIT:2> ] . type == token . INDENT and <EOL> suite . children [ - <NUM_LIT:1> ] . type == token . DEDENT ) : <EOL> pass_leaf = Leaf ( text_type , u'<STR_LIT>' ) <EOL> suite . insert_child ( - <NUM_LIT:1> , pass_leaf ) <EOL> suite . insert_child ( - <NUM_LIT:1> , Leaf ( token . NEWLINE , u'<STR_LIT:\n>' ) ) </s>
