Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rename method from TODO list #116

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -6,6 +6,7 @@ Changelog

- fix some old call to log() weren't lazy, that could cause a crash in some situations by an infinite recursive call and also reduce performances
- fix in _iter_in_rendering_order method to avoid bug in edge cases (issue #107)
- add rename method for NameNode and def/class

0.6.1 (2016-03-28)
----------------
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
- raise an exception on .find/.find_all if the identifier given doesn't exists
- .help() seems really slow on big piece of code (for example RedBaron("baron/grammator.py").read())("dict")[0].help() is suuuuuuuuuuuuuuuuper slow)
- .at() return the first item starting at line X
- .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 @@ -879,6 +879,7 @@ def _get_helpers(self):
'path',
'find_by_path',
'replace',
'rename',
'edit',
'increase_indentation',
'decrease_indentation',
Expand Down Expand Up @@ -1020,6 +1021,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'):
setattr(self, 'name', new_value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly do self.name = new_value here, same for line 1028.

Copy link
Contributor Author

@b5y b5y Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In first time I thought about this solution, but this attribute is outside of init method.
So, I think it is not correct way. Please fix me if I am wrong.

elif self.type == 'name':
setattr(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
15 changes: 15 additions & 0 deletions tests/test_initial_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,21 @@ def test_replace():
assert red.dumps() == "caramba"


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


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