Skip to content

Commit c0fecb1

Browse files
authored
Enhance robustness of the basic flatten command (#20)
Older connector versions and still some edge cases in the current set refer to elements with the @ decorator (Groups in Revit for example)
1 parent 4b9d6b0 commit c0fecb1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

flatten.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@
66

77

88
def flatten_base(base: Base) -> Iterable[Base]:
9-
"""Take a base and flatten it to an iterable of bases."""
10-
if hasattr(base, "elements") and base.elements is not None:
11-
for element in base["elements"]:
9+
"""Flatten a base object into an iterable of bases.
10+
11+
This function recursively traverses the `elements` or `@elements` attribute of the
12+
base object, yielding each nested base object.
13+
14+
Args:
15+
base (Base): The base object to flatten.
16+
17+
Yields:
18+
Base: Each nested base object in the hierarchy.
19+
"""
20+
# Attempt to get the elements attribute, fallback to @elements if necessary
21+
elements = getattr(base, "elements", getattr(base, "@elements", None))
22+
23+
if elements is not None:
24+
for element in elements:
1225
yield from flatten_base(element)
26+
1327
yield base

0 commit comments

Comments
 (0)