Skip to content

Commit

Permalink
Enhance robustness of the basic flatten command (#20)
Browse files Browse the repository at this point in the history
Older connector versions and still some edge cases in the current set refer to elements with the @ decorator  (Groups in Revit for example)
  • Loading branch information
jsdbroughton committed May 16, 2024
1 parent 4b9d6b0 commit c0fecb1
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@


def flatten_base(base: Base) -> Iterable[Base]:
"""Take a base and flatten it to an iterable of bases."""
if hasattr(base, "elements") and base.elements is not None:
for element in base["elements"]:
"""Flatten a base object into an iterable of bases.
This function recursively traverses the `elements` or `@elements` attribute of the
base object, yielding each nested base object.
Args:
base (Base): The base object to flatten.
Yields:
Base: Each nested base object in the hierarchy.
"""
# Attempt to get the elements attribute, fallback to @elements if necessary
elements = getattr(base, "elements", getattr(base, "@elements", None))

if elements is not None:
for element in elements:
yield from flatten_base(element)

yield base

0 comments on commit c0fecb1

Please sign in to comment.