Skip to content

FEAT: Update print_tree method #804

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

Merged
merged 3 commits into from
Jul 5, 2024
Merged
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 doc/changelog.d/804.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FEAT: Update print_tree method
30 changes: 22 additions & 8 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,28 @@ def _print_tree(self, node, max_lines, lines_count, indentation):
node_name = node.Name
if hasattr(node, "Suppressed") and node.Suppressed is True:
node_name += " (Suppressed)"
print(f"{indentation}├── {node.Name}")
print(f"{indentation}├── {node_name}")
lines_count += 1

if lines_count >= max_lines and max_lines != -1:
print(f"... truncating after {max_lines} lines")
return lines_count

if hasattr(node, "Children") and node.Children is not None and node.Children.Count > 0:
for child in node.Children:
_lines_count = self._print_tree(child, max_lines, lines_count, indentation + "| ")
if _lines_count >= max_lines and max_lines != -1:
lines_count = self._print_tree(child, max_lines, lines_count, indentation + "| ")
if lines_count >= max_lines and max_lines != -1:
break

return lines_count

def print_tree(self, node, max_lines=80, lines_count=0, indentation=""):
def print_tree(self, node=None, max_lines=80, lines_count=0, indentation=""):
"""
Print the hierarchical tree representation of the Mechanical project structure.

Parameters
----------
node: ExtAPI object
node: DataModel object, optional
The starting object of the tree.
max_lines: int, optional
The maximum number of lines to print. Default is 80. If set to -1, no limit is applied.
Expand All @@ -425,7 +429,7 @@ def print_tree(self, node, max_lines=80, lines_count=0, indentation=""):
>>> import ansys.mechanical.core as mech
>>> app = mech.App()
>>> app.update_globals(globals())
>>> app.print_tree(DataModel.Project)
>>> app.print_tree()
... ├── Project
... | ├── Model
... | | ├── Geometry Imports
Expand All @@ -436,8 +440,18 @@ def print_tree(self, node, max_lines=80, lines_count=0, indentation=""):
... | | ├── Remote Points
... | | ├── Mesh

>>> app.print_tree(DataModel.Project, 1)
>>> app.print_tree(Model, 3)
... ├── Model
... | ├── Geometry Imports
... | ├── Geometry
... ... truncating after 3 lines

>>> app.print_tree(max_lines=2)
... ├── Project
... ... truncating after 1 lines
... | ├── Model
... ... truncating after 2 lines
"""
if node is None:
node = self.DataModel.Project

self._print_tree(node, max_lines, lines_count, indentation)
7 changes: 5 additions & 2 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ def test_app_print_tree(embedded_app, capsys, assets):
geometry_import.Import(geometry_file)
allbodies = Model.GetChildren(DataModelObjectCategory.Body, True)
allbodies[0].Suppressed = True
embedded_app.print_tree(DataModel.Project, 1)
embedded_app.print_tree()
captured = capsys.readouterr()
printed_output = captured.out.strip()
assert "Project" in printed_output
embedded_app.print_tree(DataModel.Project, 2)
assert "Suppressed" in printed_output

embedded_app.print_tree(max_lines=2)
captured = capsys.readouterr()
printed_output = captured.out.strip()
assert "Model" in printed_output
assert "truncating after" in printed_output

with pytest.raises(AttributeError):
embedded_app.print_tree(DataModel)
Expand Down
Loading