Skip to content
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

Add a get_children method #337

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions capellambse/model/common/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,31 @@ def _wrap_short_html(self, content: str) -> markupsafe.Markup:
def _repr_html_(self) -> str:
return self.__html__()

def get_children(self) -> MixedElementList:
"""Return all model elements that are children of this one.

The list returned from this method contains all model element
that are direct descendants of this element, with regard to the
XML hierarchy (usually referred to as "owned objects" in the
metamodel).
"""
elements: list[etree._Element] = []
for attr in dir(self):
if attr.startswith("_"):
continue
acc = getattr(type(self), attr, None)
if (
# pylint: disable-next=unidiomatic-typecheck
type(acc) is accessors.DirectProxyAccessor
and not acc.rootelem
or isinstance(acc, accessors.RoleTagAccessor)
):
if acc.aslist is None:
elements.append(getattr(self, attr)._element)
else:
elements.extend(i._element for i in getattr(self, attr))
return MixedElementList(self._model, elements)

if t.TYPE_CHECKING:

def __getattr__(self, attr: str) -> t.Any:
Expand Down