Skip to content

Commit

Permalink
refactor: Better code
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed May 15, 2024
1 parent 8634a24 commit ad07607
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
34 changes: 13 additions & 21 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ def collector(
diagram: context.ContextDiagram, params: dict[str, t.Any] | None = None
) -> _elkjs.ELKInputData:
"""Collect context data from ports of centric box."""
diagram.display_derived_interfaces = (params or {}).pop(
"display_derived_interfaces", diagram.display_derived_interfaces
)
data = generic.collector(diagram, no_symbol=True)
ports = port_collector(diagram.target, diagram.type)
centerbox = data["children"][0]
connections = port_exchange_collector(ports)
centerbox["ports"] = [
makers.make_port(uuid) for uuid, edges in connections.items() if edges
]
centerbox["ports"] = [makers.make_port(uuid) for uuid in connections]
ex_datas: list[generic.ExchangeData] = []
edges: common.ElementList[fa.AbstractExchange] = list(
chain.from_iterable(connections.values())
Expand Down Expand Up @@ -83,21 +78,21 @@ def _make_box_and_update_globals(
made_boxes[obj.uuid] = box
return box

def _make_owner_box(current: t.Any) -> t.Any:
if not (parent_box := global_boxes.get(current.owner.uuid)):
def _make_owner_box(obj: t.Any) -> t.Any:
if not (parent_box := global_boxes.get(obj.owner.uuid)):
parent_box = _make_box_and_update_globals(
current.owner,
obj.owner,
no_symbol=diagram.display_symbols_as_boxes,
layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS,
)
for box in (children := parent_box.setdefault("children", [])):
if box["id"] == current.uuid:
box = global_boxes.get(current.uuid, current)
if box["id"] == obj.uuid:
box = global_boxes.get(obj.uuid, obj)
break
else:
children.append(global_boxes.get(current.uuid, current))
boxes_to_delete.add(current.uuid)
return current.owner
children.append(global_boxes.get(obj.uuid, obj))
boxes_to_delete.add(obj.uuid)
return obj.owner

if diagram.display_parent_relation:
try:
Expand Down Expand Up @@ -155,16 +150,12 @@ def _make_owner_box(current: t.Any) -> t.Any:

if diagram.display_parent_relation and diagram.target.owner:
current = diagram.target.owner
common_owner_uuid = current.uuid
for owner in diagram_target_owners[::-1]:
if owner in common_owners:
common_owner_uuid = owner
break
while current and current.uuid != common_owner_uuid:
while current and common_owners:
try:
if isinstance(current.owner, generic.PackageTypes):
break
current = _make_owner_box(current)
common_owners.discard(current.uuid)
except AttributeError:
break

Expand Down Expand Up @@ -228,7 +219,8 @@ def port_exchange_collector(
edges: dict[str, common.ElementList[fa.AbstractExchange]] = {}
for i in ports:
try:
edges[i.uuid] = filter(getattr(i, "exchanges"))
if edge := filter(getattr(i, "exchanges")):
edges[i.uuid] = edge
except AttributeError:
pass
return edges
Expand Down
20 changes: 8 additions & 12 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,14 @@ def render(self, fmt: str | None, /, **params) -> t.Any:

def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
transparent_background = params.pop("transparent_background", False)
self.display_parent_relation = params.pop(
"display_parent_relation", self.display_parent_relation
)
self.display_derived_interfaces = params.pop(
"display_derived_interfaces", self.display_derived_interfaces
)
self.display_symbols_as_boxes = params.pop(
"display_symbols_as_boxes", self.display_symbols_as_boxes
)
self.slim_center_box = params.pop(
"slim_center_box", self.slim_center_box
)
for param_name in [
"display_parent_relation",
"display_derived_interfaces",
"display_symbols_as_boxes",
"slim_center_box",
]:
default = getattr(self, param_name)
setattr(self, param_name, default)
data = params.get("elkdata") or get_elkdata(self, params)
layout = try_to_layout(data)
add_context(layout, params.get("is_legend", False))
Expand Down

0 comments on commit ad07607

Please sign in to comment.