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
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
39 changes: 38 additions & 1 deletion capellambse/model/common/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,43 @@ 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.

.... seealso::

:meth:`iter_children`
"""
return MixedElementList(
self._model, [ele._element for ele in self.iter_children()]
)

def has_children(self) -> bool:
return next(self.iter_children(), None) is not None

def iter_children(self) -> t.Iterator[GenericElement]:
"""Yield model elements that are children of this one.

The elements yielded from this generator are the direct
descendants of this element, with regard to the XML hierarchy
(usually referred to as "owned objects" in the metamodel).
"""
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:
if getattr(self, attr) is not None:
yield getattr(self, attr)
else:
yield from getattr(self, attr)

if t.TYPE_CHECKING:

def __getattr__(self, attr: str) -> t.Any:
Expand Down Expand Up @@ -756,7 +793,7 @@ def __html__(self) -> markupsafe.Markup:
return markupsafe.Markup("<p><em>(Empty list)</em></p>")

fragments = ['<ol start="0" style="text-align: left;">']
for i in self:
for i in [i for i in self if i is not None]:
fragments.append(f"<li>{i._short_html_()}</li>")
fragments.append("</ol>")
return markupsafe.Markup("".join(fragments))
Expand Down
1 change: 0 additions & 1 deletion capellambse/model/crosslayer/information/datavalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ def __eq__(self, other: object) -> bool:

@c.xtype_handler(None)
class EnumerationReference(c.GenericElement):
name = c.AttributeProperty("name", returntype=str)
type = c.AttrProxyAccessor(c.GenericElement, "abstractType")
value = c.AttrProxyAccessor(c.GenericElement, "referencedValue")