Skip to content
Snippets Groups Projects
Commit 2866320e authored by Pierre Penninckx's avatar Pierre Penninckx
Browse files

Merge remote-tracking branch 'origin/master' into feature/find_iter

parents 22cdfd64 ee74b0da
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -88,4 +88,5 @@ tramp
/auto/
 
# PyCharm files
.idea/*.*
\ No newline at end of file
.idea/*.*
.idea/
\ No newline at end of file
Changelog
=========
 
0.6.2 (unreleased)
0.6.3 (unreleased)
-----------------
- 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
0.6.2 (2016-10-03)
----------------
 
- 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
Loading
Loading
Loading
Loading
@@ -64,7 +64,7 @@ Links
 
**RedBaron is fully documented, be sure to check the turorial and documentation**.
 
* [Tutorial](https://redbaron.pycqa.org/en/latest/tuto.html)
* [Documentation](https://redbaron.pycqa.org)
* [Tutorial](https://redbaron.readthedocs.io/en/latest/tuto.html)
* [Documentation](https://redbaron.readthedocs.io/en/latest/)
* [Baron](https://github.com/PyCQA/baron)
* IRC chat: [irc.freenode.net#baron](https://webchat.freenode.net/?channels=%23baron)
Loading
Loading
@@ -2,9 +2,7 @@
 
### Important
 
- 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.
 
Loading
Loading
Loading
Loading
@@ -476,6 +476,17 @@ You can find which node is located at a given line and column:
red.find_by_position((1, 5))
red.find_by_position((1, 6)) # '(' is not a redbaron node
 
.at()
-------------------
Returns first node at specific line
.. ipython:: python
red = RedBaron("def a():\n return 42")
red.at(1) # Gives DefNode
red.at(2) # Gives ReturnNode
.. _Node.from_fst:
 
Node.from_fst()
Loading
Loading
Loading
Loading
@@ -118,7 +118,11 @@ class Path(object):
parent = parent.node_list
 
if isinstance(parent, NodeList):
pos = parent.index(node.node_list if isinstance(node, ProxyList) else node)
if isinstance(node, ProxyList):
item = node.node_list
else:
item = node
pos = parent.index(item)
return pos
 
if isinstance(node, NodeList):
Loading
Loading
@@ -207,10 +211,27 @@ class GenericNodesUtils(object):
 
def find_by_position(self, position):
path = Path.from_baron_path(self, baron.path.position_to_path(self.fst(), position))
if path:
return path.node
else:
return None
return path.node if path else None
def at(self, line_no):
if not 0 <= line_no <= self.absolute_bounding_box.bottom_right.line:
raise IndexError("Line number {0} is outside of the file".format(line_no))
node = self.find_by_position((line_no, 1))
if node.absolute_bounding_box.top_left.line == line_no:
if hasattr(node.parent, 'absolute_bounding_box') and \
node.parent.absolute_bounding_box.top_left.line == line_no and \
node.parent.parent is not None:
return node.parent
return node
elif node is not None and hasattr(node, 'next_rendered'):
return list(self._iter_in_rendering_order(node.next_rendered))[0]
elif node.parent is None:
node = node.data[0][0]
while True:
if node.absolute_bounding_box.top_left.line == line_no:
return node
node = node.next_rendered
return node
 
def _string_to_node_list(self, string, parent, on_attribute):
return NodeList.from_fst(baron.parse(string), parent=parent, on_attribute=on_attribute)
Loading
Loading
@@ -304,7 +325,7 @@ class NodeList(UserList, GenericNodesUtils):
 
def __setitem__(self, key, value):
self.data[key] = self._convert_input_to_node_object(value, parent=self.parent, on_attribute=self.on_attribute)
def find_iter(self, identifier, *args, **kwargs):
for node in self.data:
for matched_node in node.find_iter(identifier, *args, **kwargs):
Loading
Loading
@@ -634,7 +655,10 @@ class Node(GenericNodesUtils):
if self.on_attribute is "root":
in_list = self.parent
elif self.on_attribute is not None:
in_list = getattr(self.parent, self.on_attribute)
if isinstance(self.parent, NodeList):
in_list = getattr(self.parent.parent, self.on_attribute)
else:
in_list = getattr(self.parent, self.on_attribute)
else:
return None
 
Loading
Loading
@@ -827,37 +851,46 @@ class Node(GenericNodesUtils):
 
def _get_helpers(self):
not_helpers = set([
'at',
'copy',
'decrease_indentation',
'dumps',
'edit',
'find',
'findAll',
'find_all',
'findAll',
'find_by_path',
'find_by_position',
'find_iter',
'from_fst',
'fst',
'help',
'next_generator',
'previous_generator',
'fst',
'generate_identifiers',
'get_absolute_bounding_box_of_attribute',
'get_indentation_node',
'get_indentation_node',
'indentation_node_is_direct',
'parent_find',
'path',
'find_by_path',
'replace',
'edit',
'increase_indentation',
'decrease_indentation',
'has_render_key',
'get_absolute_bounding_box_of_attribute',
'find_by_position',
'parse_code_block',
'parse_decorators',
'from_fst',
'help',
'help',
'increase_indentation',
'indentation_node_is_direct',
'indentation_node_is_direct',
'index_on_parent',
'index_on_parent_raw',
'insert_before',
'insert_after',
'insert_before',
'next_generator',
'next_generator',
'parent_find',
'parent_find',
'parse_code_block',
'parse_decorators',
'path',
'path',
'previous_generator',
'previous_generator',
'replace',
'to_python',
'generate_identifiers',
])
return [x for x in dir(self) if
not x.startswith("_") and x not in not_helpers and inspect.ismethod(getattr(self, x))]
Loading
Loading
@@ -1604,9 +1637,9 @@ class LineProxyList(ProxyList):
{"type": "endl", "formatting": [], "value": "\n", "indent": " "})
 
def _synchronise(self):
log("Before synchronise, self.data = '%s' + '%s'", self.first_blank_lines, self.data)
log("Before synchronise, self.data = '%s' + '%s'", self.first_blank_lines, self.node_list)
super(LineProxyList, self)._synchronise()
log("After synchronise, self.data = '%s' + '%s'", self.first_blank_lines, self.data)
log("After synchronise, self.data = '%s' + '%s'", self.first_blank_lines, self.node_list)
 
def _build_inner_list(self, node_list):
result = []
Loading
Loading
Loading
Loading
@@ -12,7 +12,7 @@ except ImportError:
 
 
setup(name='redbaron',
version='0.6.1',
version='0.6.2',
description='Abstraction on top of baron, a FST for python to make writing refactoring code a realistic task',
author='Laurent Peuch',
long_description=read_md("README.md") + "\n\n" + open("CHANGELOG", "r").read(),
Loading
Loading
# -*- coding:Utf-8 -*-
"""Test for at method"""
import redbaron
from redbaron import RedBaron
redbaron.DEBUG = True
red = RedBaron("""\
class Foo(object):
def __init__(self):
self.a = None
def bar(self):
for x in range(5):
yield self.a + x
setup(name='redbaron',
version='0.6.1')
""")
def test_at():
assert red.at(1) is red.class_
assert red.at(2) is red.find_all('DefNode')[0]
assert red.at(3) is red.find('AssignmentNode')
assert red.at(4) is red.find_all('DefNode')[1]
assert red.at(5) is red.find('ForNode')
assert red.at(6) is red.find('YieldNode')
assert red.at(7) is red.find_all('EndlNode')[6]
assert red.at(8) is red.find_all('AtomTrailersNode')[3]
assert red.at(9) is red.find_all('CallArgumentNode')[2]
Loading
Loading
@@ -99,11 +99,49 @@ def test_get_helpers():
assert red[0]._get_helpers() == ['modules', 'names']
 
 
def test_help_is_not_crashing():
def test_help_is_not_crashing1():
some_code = "ax + (z * 4)"
red = RedBaron(some_code)
red.help()
red[0].help()
red.help(5)
red[0].help(5)
red.help(True)
red[0].help(True)
def test_help_is_not_crashing2():
some_code = "a(b)"
red = RedBaron(some_code)
red.help()
red[0].help()
red.help(5)
red[0].help(5)
red.help(True)
red[0].help(True)
def test_help_is_not_crashing3():
some_code = "a(b, c)"
red = RedBaron(some_code)
red.help()
red[0].help()
red.help(5)
red[0].help(5)
red.help(True)
red[0].help(True)
def test_help_is_not_crashing4():
some_code = "a(b)"
red = RedBaron(some_code)
red[0].call.append("c")
red.help()
red[0].help()
red.help(5)
red[0].help(5)
red.help(True)
red[0].help(True)
 
 
def test_assign_on_string_value():
Loading
Loading
Loading
Loading
@@ -8,8 +8,8 @@ import pytest
import redbaron
from redbaron import RedBaron
 
redbaron.DEBUG = True
# (alekum): switch off debug mode, to reproduce a bug with __repr__ implicit recursion
redbaron.DEBUG = False
 
def assert_with_indent(left, right):
# Replace is not strictly necessary but shows indents
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment