89
89
parent.insert_child(i, new_stmt)
90
90
new_leaf1 = new_stmt.children[0].children[0]
91
91
old_leaf1 = stmt_node.children[0].children[0]
92
new_leaf1.set_prefix(old_leaf1.get_prefix())
92
new_leaf1.prefix = old_leaf1.prefix
95
95
def remove_trailing_newline(node):
113
113
# Check if the expr_node is a simple assignment.
114
114
left_node = expr_node.children[0]
115
115
if isinstance(left_node, Leaf) and \
116
left_node.value == '__metaclass__':
116
left_node.value == u'__metaclass__':
117
117
# We found a assignment to __metaclass__.
118
118
fixup_simple_stmt(node, i, simple_node)
119
119
remove_trailing_newline(simple_node)
182
182
# Node(classdef, ['class', 'name', ':', suite])
184
184
arglist = Node(syms.arglist, [])
185
node.insert_child(2, Leaf(token.RPAR, ')'))
185
node.insert_child(2, Leaf(token.RPAR, u')'))
186
186
node.insert_child(2, arglist)
187
node.insert_child(2, Leaf(token.LPAR, '('))
187
node.insert_child(2, Leaf(token.LPAR, u'('))
189
189
raise ValueError("Unexpected class definition")
191
191
# now stick the metaclass in the arglist
192
192
meta_txt = last_metaclass.children[0].children[0]
193
193
meta_txt.value = 'metaclass'
194
orig_meta_prefix = meta_txt.get_prefix()
194
orig_meta_prefix = meta_txt.prefix
196
196
if arglist.children:
197
arglist.append_child(Leaf(token.COMMA, ','))
198
meta_txt.set_prefix(' ')
197
arglist.append_child(Leaf(token.COMMA, u','))
198
meta_txt.prefix = u' '
200
meta_txt.set_prefix('')
200
meta_txt.prefix = u''
202
202
# compact the expression "metaclass = Meta" -> "metaclass=Meta"
203
203
expr_stmt = last_metaclass.children[0]
204
204
assert expr_stmt.type == syms.expr_stmt
205
expr_stmt.children[1].set_prefix('')
206
expr_stmt.children[2].set_prefix('')
205
expr_stmt.children[1].prefix = u''
206
expr_stmt.children[2].prefix = u''
208
208
arglist.append_child(last_metaclass)
213
213
if not suite.children:
214
214
# one-liner that was just __metaclass_
216
pass_leaf = Leaf(text_type, 'pass')
217
pass_leaf.set_prefix(orig_meta_prefix)
216
pass_leaf = Leaf(text_type, u'pass')
217
pass_leaf.prefix = orig_meta_prefix
218
218
node.append_child(pass_leaf)
219
node.append_child(Leaf(token.NEWLINE, '\n'))
219
node.append_child(Leaf(token.NEWLINE, u'\n'))
221
221
elif len(suite.children) > 1 and \
222
222
(suite.children[-2].type == token.INDENT and
223
223
suite.children[-1].type == token.DEDENT):
224
224
# there was only one line in the class body and it was __metaclass__
225
pass_leaf = Leaf(text_type, 'pass')
225
pass_leaf = Leaf(text_type, u'pass')
226
226
suite.insert_child(-1, pass_leaf)
227
suite.insert_child(-1, Leaf(token.NEWLINE, '\n'))
227
suite.insert_child(-1, Leaf(token.NEWLINE, u'\n'))