Skip to content

Commit

Permalink
refactor: Implement generic make_owner_boxes function
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Nov 26, 2024
1 parent e4dfd0d commit f091a65
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
14 changes: 5 additions & 9 deletions capellambse_context_diagrams/collectors/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,11 @@ def _make_box(
for port in getattr(obj, attr, []):
self._make_port_and_owner(port)
if self.diagram._display_parent_relation:
current = obj
while (
current
and current.uuid not in self.diagram_target_owners
and getattr(current, "owner", None) is not None
and not isinstance(current.owner, generic.PackageTypes)
):
current = self._make_owner_box(current)
self.common_owners.add(current.uuid)
self.common_owners.add(
generic.make_owner_boxes(
obj, self.diagram_target_owners, self._make_owner_box
)
)
return box

def _make_owner_box(
Expand Down
18 changes: 6 additions & 12 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def process_context(self):
and not isinstance(current.owner, generic.PackageTypes)
):
current = self._make_owner_box(
self.diagram,
current,
)
self.common_owners.discard(current.uuid)
Expand Down Expand Up @@ -256,15 +255,11 @@ def _process_ports(self) -> None:
box.layoutOptions["portLabels.placement"] = "OUTSIDE"

if self.diagram._display_parent_relation:
current = owner
while (
current
and current.uuid not in self.diagram_target_owners
and getattr(current, "owner", None) is not None
and not isinstance(current.owner, generic.PackageTypes)
):
current = self._make_owner_box(self.diagram, current)
self.common_owners.add(current.uuid)
self.common_owners.add(
generic.make_owner_boxes(
owner, self.diagram_target_owners, self._make_owner_box
)
)

def _make_port(
self, port_obj: t.Any
Expand Down Expand Up @@ -295,13 +290,12 @@ def _make_box(

def _make_owner_box(
self,
diagram: context.ContextDiagram,
obj: t.Any,
) -> t.Any:
if not (parent_box := self.global_boxes.get(obj.owner.uuid)):
parent_box = self._make_box(
obj.owner,
no_symbol=diagram._display_symbols_as_boxes,
no_symbol=self.diagram._display_symbols_as_boxes,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
assert (obj_box := self.global_boxes.get(obj.uuid))
Expand Down
15 changes: 15 additions & 0 deletions capellambse_context_diagrams/collectors/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,18 @@ def get_all_owners(obj: m.ModelElement) -> cabc.Iterator[str]:
while current is not None:
yield current.uuid
current = getattr(current, "owner", None)


def make_owner_boxes(
obj: m.ModelElement, excluded: list[str], make_func: t.Callable
) -> str:
"""Create owner boxes for all owners of ``obj``."""
current = obj
while (
current
and current.uuid not in excluded
and getattr(current, "owner", None) is not None
and not isinstance(current.owner, PackageTypes)
):
current = make_func(current)
return current.uuid

0 comments on commit f091a65

Please sign in to comment.