Skip to content

Commit

Permalink
fix #191, consistent behaviour on RenderTree str and by_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Oct 11, 2023
1 parent 1979e20 commit 9d90af8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
37 changes: 26 additions & 11 deletions anytree/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ def __item(node, continues, style):
return Row(pre, fill, node)

def __str__(self):
lines = ["%s%r" % (pre, node) for pre, _, node in self]
return "\n".join(lines)
def get():
for row in self:
lines = repr(row.node).splitlines() or [""]
yield "%s%s" % (row.pre, lines[0])
for line in lines[1:]:
yield "%s%s" % (row.fill, line)

return "\n".join(get())

def __repr__(self):
classname = self.__class__.__name__
Expand Down Expand Up @@ -332,19 +338,28 @@ def by_attr(self, attrname="name"):
"""

def get():
for pre, fill, node in self:
attr = attrname(node) if callable(attrname) else getattr(node, attrname, "")
if isinstance(attr, (list, tuple)):
lines = attr
else:
lines = str(attr).split("\n")
yield "%s%s" % (pre, lines[0])
for line in lines[1:]:
yield "%s%s" % (fill, line)
if callable(attrname):
for row in self:
attr = attrname(row.node)
yield from _format_row_any(row, attr)
else:
for row in self:
attr = getattr(row.node, attrname, "")
yield from _format_row_any(row, attr)

return "\n".join(get())


def _format_row_any(row, attr):
if isinstance(attr, (list, tuple)):
lines = attr or [""]
else:
lines = str(attr).splitlines() or [""]
yield "%s%s" % (row.pre, lines[0])
for line in lines[1:]:
yield "%s%s" % (row.fill, line)


def _is_last(iterable):
iter_ = iter(iterable)
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_jsonexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_json_exporter():
AnyNode(id="sub1Ca", parent=s1c)

exporter = JsonExporter(indent=2, sort_keys=True)
exported = exporter.export(root).split("\n")
exported = exporter.export(root).splitlines()
exported = [e.rstrip() for e in exported] # just a fix for a strange py2x behavior.
lines = [
"{",
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_json_exporter():
eq_(exported, lines)

exporter = JsonExporter(indent=2, sort_keys=True, maxlevel=2)
exported = exporter.export(root).split("\n")
exported = exporter.export(root).splitlines()
exported = [e.rstrip() for e in exported] # just a fix for a strange py2x behavior.
limitedlines = [
"{",
Expand Down
18 changes: 18 additions & 0 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,21 @@ def test_by_attr():
"│ └── s:u:b:0:A",
"└── s:u:b:1",
]


def test_repr():
"""Repr."""

class ReprNode(anytree.Node):
def __repr__(self):
return "{name}\n{name}".format(name=self.name)

root = ReprNode("root", lines=["root"])
s0 = ReprNode("sub0", parent=root, lines=["su", "b0"])
ReprNode("sub0B", parent=s0, lines=["sub", "0B"])
ReprNode("sub0A", parent=s0)
ReprNode("sub1", parent=root, lines=["sub1"])

bystr = str(anytree.RenderTree(root)).splitlines()
byident = anytree.RenderTree(root).by_attr(lambda node: node).splitlines()
assert bystr == byident

0 comments on commit 9d90af8

Please sign in to comment.