Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
- fix help() after append
- fix _synchronise() for base_nodes to avoid recursion in __repr__ function if code run not in the shell
- add at method
- add rename method

0.6.2 (2016-10-03)
----------------
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
### Important

- .help() seems really slow on big piece of code (for example RedBaron("baron/grammator.py").read())("dict")[0].help() is suuuuuuuuuuuuuuuuper slow)
- .rename() (name -> value, def/class -> name)
- .replace() expect a whole valid python program. This could be fixed by look at "on_attribute" and resetting itself like that.

- generate default constructors for nodes using nodes_rendering_order
Expand Down
9 changes: 9 additions & 0 deletions redbaron/base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ def _get_helpers(self):
'previous_generator',
'previous_generator',
'replace',
'rename',
'to_python',
])
return [x for x in dir(self) if
Expand Down Expand Up @@ -1017,6 +1018,14 @@ def replace(self, new_node):
self.__class__ = new_node.__class__ # YOLO
self.__init__(new_node.fst(), parent=self.parent, on_attribute=self.on_attribute)

def rename(self, new_value):
if self.type in ('def', 'class'):
self.name = new_value
elif self.type == 'name':
self.value = new_value
else:
raise TypeError('Rename method does not support {0} type'.format(self.type))

def edit(self, editor=None):
if editor is None:
editor = os.environ.get("EDITOR", "nano")
Expand Down
29 changes: 29 additions & 0 deletions tests/test_initial_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,35 @@ def test_replace():
assert red.dumps() == "caramba"


def test_rename():
red_def = RedBaron('def foo(a):\n a = 5\n b = a')
red_def[0].rename('bar')
a = red_def.find_all('NameNode')[1]
a.rename('b')
red_def.find('DefArgumentNode').find('NameNode').rename('b')
red_def.find('NameNode', value='b').rename('q')
red_def.find_all('NameNode')[2].rename('q')
red_def.find_all('NameNode')[3].rename('b')

red_class = RedBaron('class Baz(q):\n h = 10\n m = 42')
red_class[0].rename('baz')
h = red_class.find_all('NameNode')[1]
h.rename('v')
red_class.find_all('NameNode')[0].rename('z')
red_class.find('NameNode', value='m').rename('p')

assert red_def.find('DefNode').name == 'bar'
assert red_def.find_all('NameNode')[0].value == 'q'
assert red_def.find_all('NameNode')[1].value == 'b'
assert red_def.find_all('NameNode')[2].value == 'q'
assert red_def.find_all('NameNode')[3].value == 'b'

assert red_class.find('ClassNode').name == 'baz'
assert red_class.find_all('NameNode')[0].value == 'z'
assert red_class.find_all('NameNode')[1].value == 'v'
assert red_class.find_all('NameNode')[2].value == 'p'


def test_insert_before():
red = RedBaron("a = 1\nprint(pouet)\n")
red.print_.insert_before("chocolat")
Expand Down