Skip to content

Commit

Permalink
fix: Fix mutability bug
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Nov 27, 2024
1 parent f091a65 commit bed6a30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
47 changes: 24 additions & 23 deletions capellambse_context_diagrams/collectors/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def _make_box(
obj: m.ModelElement,
**kwargs: t.Any,
) -> _elkjs.ELKInputChild:
if box := self.boxes.get(obj.uuid):
return box
box = makers.make_box(
obj,
no_symbol=self.diagram._display_symbols_as_boxes,
Expand All @@ -209,15 +211,13 @@ def _make_owner_box(
self,
obj: t.Any,
) -> t.Any:
if not (parent_box := self.boxes.get(obj.owner.uuid)):
parent_box = self._make_box(
obj.owner,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
parent_box = self._make_box(
obj.owner,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
assert (obj_box := self.boxes.get(obj.uuid))
for box in (children := parent_box.children):
if box.id == obj.uuid:
box = obj_box
break
else:
children.append(obj_box)
Expand All @@ -234,6 +234,8 @@ def _make_edge_and_ports(
self,
edge_obj: m.ModelElement,
) -> _elkjs.ELKInputEdge | None:
if self.edges.get(edge_obj.uuid):
return None
src_obj = edge_obj.source
tgt_obj = edge_obj.target
src_owner = src_obj.owner
Expand Down Expand Up @@ -263,20 +265,10 @@ def _make_edge_and_ports(

if not self.ports.get(src_obj.uuid):
port = self._make_port_and_owner(src_obj)
self.min_heights.setdefault(
src_owner.uuid, {"left": 0.0, "right": 0.0}
)["right"] += makers.PORT_SIZE + max(
2 * makers.PORT_PADDING,
sum(label.height for label in port.labels),
)
self._update_min_heights(src_owner.uuid, "right", port)
if not self.ports.get(tgt_obj.uuid):
port = self._make_port_and_owner(tgt_obj)
self.min_heights.setdefault(
tgt_owner.uuid, {"left": 0.0, "right": 0.0}
)["left"] += makers.PORT_SIZE + max(
2 * makers.PORT_PADDING,
sum(label.height for label in port.labels),
)
self._update_min_heights(tgt_owner.uuid, "left", port)

edge = _elkjs.ELKInputEdge(
id=edge_obj.uuid,
Expand All @@ -289,6 +281,16 @@ def _make_edge_and_ports(
self.edges[edge_obj.uuid] = edge
return edge

def _update_min_heights(
self, owner_uuid: str, side: str, port: _elkjs.ELKInputPort
) -> None:
self.min_heights.setdefault(owner_uuid, {"left": 0.0, "right": 0.0})[
side
] += makers.PORT_SIZE + max(
2 * makers.PORT_PADDING,
sum(label.height for label in port.labels),
)

def _need_switch(
self,
src_owners: list[str],
Expand Down Expand Up @@ -342,11 +344,10 @@ def _make_port_and_owner(
self, port_obj: m.ModelElement
) -> _elkjs.ELKInputPort:
owner_obj = port_obj.owner
if not (box := self.boxes.get(owner_obj.uuid)):
box = self._make_box(
owner_obj,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
box = self._make_box(
owner_obj,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
if port := self.ports.get(port_obj.uuid):
return port
port = makers.make_port(port_obj.uuid)
Expand Down
14 changes: 7 additions & 7 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ def _make_box(
obj: t.Any,
**kwargs: t.Any,
) -> _elkjs.ELKInputChild:
if box := self.global_boxes.get(obj.uuid):
return box
box = makers.make_box(
obj,
**kwargs,
Expand All @@ -292,16 +294,14 @@ def _make_owner_box(
self,
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=self.diagram._display_symbols_as_boxes,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
parent_box = self._make_box(
obj.owner,
no_symbol=self.diagram._display_symbols_as_boxes,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
assert (obj_box := self.global_boxes.get(obj.uuid))
for box in (children := parent_box.children):
if box.id == obj.uuid:
box = obj_box
break
else:
children.append(obj_box)
Expand Down

0 comments on commit bed6a30

Please sign in to comment.