From ee52fd4f8daf719437b1b3d8a8a6d429a8c35e51 Mon Sep 17 00:00:00 2001 From: huyenngn Date: Wed, 15 May 2024 16:41:26 +0200 Subject: [PATCH] refactor: Code improvements --- .../collectors/default.py | 39 +++++++------------ capellambse_context_diagrams/context.py | 20 ++++------ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/capellambse_context_diagrams/collectors/default.py b/capellambse_context_diagrams/collectors/default.py index b2e49d6a..b918209c 100644 --- a/capellambse_context_diagrams/collectors/default.py +++ b/capellambse_context_diagrams/collectors/default.py @@ -35,20 +35,13 @@ 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()) - ) + edges = list(chain.from_iterable(connections.values())) for ex in edges: if is_hierarchical := exchanges.is_hierarchical(ex, centerbox): if not diagram.display_parent_relation: @@ -83,21 +76,22 @@ 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, ) + assert (obj_box := global_boxes.get(obj.uuid)) 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 = obj_box break else: - children.append(global_boxes.get(current.uuid, current)) - boxes_to_delete.add(current.uuid) - return current.owner + children.append(obj_box) + boxes_to_delete.add(obj.uuid) + return obj.owner if diagram.display_parent_relation: try: @@ -155,16 +149,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 @@ -228,7 +218,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 diff --git a/capellambse_context_diagrams/context.py b/capellambse_context_diagrams/context.py index a5072bdc..3f0ffc33 100644 --- a/capellambse_context_diagrams/context.py +++ b/capellambse_context_diagrams/context.py @@ -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, params.pop(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))