From f091a65e7ad9927eecec33dba7918d89a2562fb5 Mon Sep 17 00:00:00 2001 From: huyenngn Date: Tue, 26 Nov 2024 14:02:03 +0100 Subject: [PATCH] refactor: Implement generic make_owner_boxes function --- .../collectors/custom.py | 14 +++++--------- .../collectors/default.py | 18 ++++++------------ .../collectors/generic.py | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/capellambse_context_diagrams/collectors/custom.py b/capellambse_context_diagrams/collectors/custom.py index f7ddf9f..9f0314a 100644 --- a/capellambse_context_diagrams/collectors/custom.py +++ b/capellambse_context_diagrams/collectors/custom.py @@ -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( diff --git a/capellambse_context_diagrams/collectors/default.py b/capellambse_context_diagrams/collectors/default.py index c311e0e..316cd2f 100644 --- a/capellambse_context_diagrams/collectors/default.py +++ b/capellambse_context_diagrams/collectors/default.py @@ -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) @@ -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 @@ -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)) diff --git a/capellambse_context_diagrams/collectors/generic.py b/capellambse_context_diagrams/collectors/generic.py index cbbe21b..faf8512 100644 --- a/capellambse_context_diagrams/collectors/generic.py +++ b/capellambse_context_diagrams/collectors/generic.py @@ -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