Skip to content

Commit

Permalink
refactor: Renaming again
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Jan 5, 2024
1 parent 80185e7 commit d716080
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 82 deletions.
4 changes: 2 additions & 2 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import click
from capellambse import cli_helpers

from capella2polarion import data
from capella2polarion import data_models
from capella2polarion import worker as pw
from capella2polarion.cli import Capella2PolarionCli
from capella2polarion.converters import element_converter
Expand Down Expand Up @@ -122,7 +122,7 @@ def synchronize(ctx: click.core.Context) -> None:
polarion_worker.fill_xtypes()
polarion_worker.load_polarion_work_item_map()
description_references: typing.Any = {}
new_work_items: dict[str, data.CapellaWorkItem]
new_work_items: dict[str, data_models.CapellaWorkItem]
new_work_items = polarion_worker.create_work_items(
capella_to_polarion_cli.capella_diagram_cache_folder_path,
capella_to_polarion_cli.capella_model,
Expand Down
116 changes: 116 additions & 0 deletions capella2polarion/connector/polarion_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Module providing a universal PolarionDataRepository class."""
from __future__ import annotations

import collections.abc as cabc

import bidict

from capella2polarion import data_models


class PolarionDataRepository:
"""A mapping to access all contents by Capella and Polarion IDs.
This class only holds data already present in Polarion. It only
receives updates if data were written to Polarion. There shall be no
intermediate data stored here during serialization.
"""

_id_mapping: bidict.bidict[str, str]
_work_items: dict[str, data_models.CapellaWorkItem]

def __init__(
self,
polarion_work_items: list[data_models.CapellaWorkItem] | None = None,
):
if polarion_work_items is None:
polarion_work_items = []
self._id_mapping = bidict.bidict(
{
work_item.uuid_capella: work_item.id
for work_item in polarion_work_items
},
)
self._id_mapping.on_dup = bidict.OnDup(
key=bidict.DROP_OLD, val=bidict.DROP_OLD, kv=bidict.DROP_OLD
)
self._work_items = {
work_item.uuid_capella: work_item
for work_item in polarion_work_items
}

def __contains__(self, item: str) -> bool:
"""Return True, if the given capella UUID is in the repository."""
return item in self._id_mapping

def __sizeof__(self) -> int:
"""Return the amount of registered Capella UUIDs."""
return len(self._id_mapping)

def __getitem__(
self, item: str
) -> tuple[str, data_models.CapellaWorkItem]:
"""Return the polarion ID and work_item for a given Capella UUID."""
return self._id_mapping[item], self._work_items[item]

def __iter__(self) -> cabc.Iterator[str]:
"""Iterate all Capella UUIDs."""
return self._id_mapping.__iter__()

def items(
self,
) -> cabc.Iterator[tuple[str, str, data_models.CapellaWorkItem]]:
"""Yield all Capella UUIDs, Work Item IDs and Work Items."""
for uuid, polarion_id in self._id_mapping.items():
yield uuid, polarion_id, self._work_items[uuid]

def get_work_item_id(self, capella_uuid: str) -> str | None:
"""Return a Work Item ID for a given Capella UUID."""
return self._id_mapping.get(capella_uuid)

def get_capella_uuid(self, work_item_id: str) -> str | None:
"""Return a Capella UUID for a given Work Item ID."""
return self._id_mapping.inverse.get(work_item_id)

def get_work_item_by_capella_uuid(
self, capella_uuid: str
) -> data_models.CapellaWorkItem | None:
"""Return a Work Item for a provided Capella UUID."""
return self._work_items.get(capella_uuid)

def get_work_item_by_polarion_id(
self, work_item_id: str
) -> data_models.CapellaWorkItem | None:
"""Return a Work Item for a provided Work Item ID."""
return self.get_work_item_by_capella_uuid(
self.get_capella_uuid(work_item_id) # type: ignore
)

def update_work_items(
self,
work_items: list[data_models.CapellaWorkItem],
):
"""Update all mappings for the given Work Items."""
for work_item in work_items:
if uuid_capella := self._id_mapping.inverse.get(work_item.id):
del self._id_mapping[uuid_capella]
del self._work_items[uuid_capella]

self._id_mapping.update(
{
work_item.uuid_capella: work_item.id
for work_item in work_items
if work_item.id is not None
}
)
self._work_items.update(
{work_item.uuid_capella: work_item for work_item in work_items}
)

def remove_work_items_by_capella_uuid(self, uuids: cabc.Iterable[str]):
"""Remove entries for the given Capella UUIDs."""
for uuid in uuids:
del self._work_items[uuid]
del self._id_mapping[uuid]
18 changes: 10 additions & 8 deletions capella2polarion/connectors/polarion_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import bidict

from capella2polarion import data
from capella2polarion import data_models


class PolarionDataRepository:
Expand All @@ -19,11 +19,11 @@ class PolarionDataRepository:
"""

_id_mapping: bidict.bidict[str, str]
_work_items: dict[str, data.CapellaWorkItem]
_work_items: dict[str, data_models.CapellaWorkItem]

def __init__(
self,
polarion_work_items: list[data.CapellaWorkItem] | None = None,
polarion_work_items: list[data_models.CapellaWorkItem] | None = None,
):
if polarion_work_items is None:
polarion_work_items = []
Expand All @@ -49,7 +49,9 @@ def __sizeof__(self) -> int:
"""Return the amount of registered Capella UUIDs."""
return len(self._id_mapping)

def __getitem__(self, item: str) -> tuple[str, data.CapellaWorkItem]:
def __getitem__(
self, item: str
) -> tuple[str, data_models.CapellaWorkItem]:
"""Return the polarion ID and work_item for a given Capella UUID."""
return self._id_mapping[item], self._work_items[item]

Expand All @@ -59,7 +61,7 @@ def __iter__(self) -> cabc.Iterator[str]:

def items(
self,
) -> cabc.Iterator[tuple[str, str, data.CapellaWorkItem]]:
) -> cabc.Iterator[tuple[str, str, data_models.CapellaWorkItem]]:
"""Yield all Capella UUIDs, Work Item IDs and Work Items."""
for uuid, polarion_id in self._id_mapping.items():
yield uuid, polarion_id, self._work_items[uuid]
Expand All @@ -74,21 +76,21 @@ def get_capella_uuid(self, work_item_id: str) -> str | None:

def get_work_item_by_capella_uuid(
self, capella_uuid: str
) -> data.CapellaWorkItem | None:
) -> data_models.CapellaWorkItem | None:
"""Return a Work Item for a provided Capella UUID."""
return self._work_items.get(capella_uuid)

def get_work_item_by_polarion_id(
self, work_item_id: str
) -> data.CapellaWorkItem | None:
) -> data_models.CapellaWorkItem | None:
"""Return a Work Item for a provided Work Item ID."""
return self.get_work_item_by_capella_uuid(
self.get_capella_uuid(work_item_id) # type: ignore
)

def update_work_items(
self,
work_items: list[data.CapellaWorkItem],
work_items: list[data_models.CapellaWorkItem],
):
"""Update all mappings for the given Work Items."""
for work_item in work_items:
Expand Down
28 changes: 16 additions & 12 deletions capella2polarion/converters/element_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from capella2polarion.connectors import polarion_repo

from .. import data
from .. import data_models

RE_DESCR_LINK_PATTERN = re.compile(
r"<a href=\"hlink://([^\"]+)\">([^<]+)<\/a>"
Expand Down Expand Up @@ -126,7 +126,9 @@ def _get_requirement_types_text(
return _format_texts(type_texts)


def _condition(html: bool, value: str) -> data.CapellaWorkItem.Condition:
def _condition(
html: bool, value: str
) -> data_models.CapellaWorkItem.Condition:
_type = "text/html" if html else "text/plain"
return {"type": _type, "value": value}

Expand All @@ -142,7 +144,7 @@ class CapellaWorkItemSerializer:

serializers: dict[
str,
cabc.Callable[[common.GenericElement], data.CapellaWorkItem],
cabc.Callable[[common.GenericElement], data_models.CapellaWorkItem],
]
serializer_mapping: dict[str, str]

Expand Down Expand Up @@ -170,7 +172,7 @@ def __init__(

def serialize(
self, obj: diagr.Diagram | common.GenericElement
) -> data.CapellaWorkItem | None:
) -> data_models.CapellaWorkItem | None:
"""Return a CapellaWorkItem for the given diagram or element."""
try:
if isinstance(obj, diagr.Diagram):
Expand All @@ -188,7 +190,7 @@ def serialize(
logger.error("Serializing model element failed. %s", error.args[0])
return None

def diagram(self, diag: diagr.Diagram) -> data.CapellaWorkItem:
def diagram(self, diag: diagr.Diagram) -> data_models.CapellaWorkItem:
"""Serialize a diagram for Polarion."""
diagram_path = self.diagram_cache_path / f"{diag.uuid}.svg"
src = _decode_diagram(diagram_path)
Expand All @@ -198,7 +200,7 @@ def diagram(self, diag: diagr.Diagram) -> data.CapellaWorkItem:
description = (
f'<html><p><img style="{style}" src="{src}" /></p></html>'
)
return data.CapellaWorkItem(
return data_models.CapellaWorkItem(
type="diagram",
title=diag.name,
description_type="text/html",
Expand All @@ -209,13 +211,13 @@ def diagram(self, diag: diagr.Diagram) -> data.CapellaWorkItem:

def _generic_work_item(
self, obj: common.GenericElement
) -> data.CapellaWorkItem:
) -> data_models.CapellaWorkItem:
xtype = self.polarion_type_map.get(obj.uuid, type(obj).__name__)
raw_description = getattr(obj, "description", markupsafe.Markup(""))
uuids, value = self._sanitize_description(obj, raw_description)
self.descr_references[obj.uuid] = uuids
requirement_types = _get_requirement_types_text(obj)
return data.CapellaWorkItem(
return data_models.CapellaWorkItem(
type=resolve_element_type(xtype),
title=obj.name,
description_type="text/html",
Expand Down Expand Up @@ -287,7 +289,7 @@ def replace_markup(

def include_pre_and_post_condition(
self, obj: PrePostConditionElement
) -> data.CapellaWorkItem:
) -> data_models.CapellaWorkItem:
"""Return generic attributes and pre- and post-condition."""

def get_condition(cap: PrePostConditionElement, name: str) -> str:
Expand Down Expand Up @@ -321,7 +323,9 @@ def get_linked_text(
self.descr_references[obj.uuid] = uuids
return value

def constraint(self, obj: capellacore.Constraint) -> data.CapellaWorkItem:
def constraint(
self, obj: capellacore.Constraint
) -> data_models.CapellaWorkItem:
"""Return attributes for a ``Constraint``."""
work_item = self._generic_work_item(obj)
if work_item.uuid_capella == "b24a9e54-4386-4d38-aea3-7c5172d73bb6":
Expand All @@ -332,7 +336,7 @@ def constraint(self, obj: capellacore.Constraint) -> data.CapellaWorkItem:

def _include_actor_in_type(
self, obj: cs.Component
) -> data.CapellaWorkItem:
) -> data_models.CapellaWorkItem:
"""Return attributes for a ``Component``."""
work_item = self._generic_work_item(obj)
if obj.is_actor:
Expand All @@ -345,7 +349,7 @@ def _include_actor_in_type(

def _include_nature_in_type(
self, obj: pa.PhysicalComponent
) -> data.CapellaWorkItem:
) -> data_models.CapellaWorkItem:
"""Return attributes for a ``PhysicalComponent``."""
work_item = self._include_actor_in_type(obj)
xtype = work_item.type
Expand Down
10 changes: 5 additions & 5 deletions capella2polarion/converters/link_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from capellambse.model import diagram as diag
from capellambse.model.crosslayer import fa

from capella2polarion import data
from capella2polarion import data_models
from capella2polarion.connectors import polarion_repo
from capella2polarion.converters import element_converter

Expand All @@ -30,7 +30,7 @@ class LinkSerializer:
def __init__(
self,
capella_polarion_mapping: polarion_repo.PolarionDataRepository,
new_work_items: dict[str, data.CapellaWorkItem],
new_work_items: dict[str, data_models.CapellaWorkItem],
description_references: dict[str, list[str]],
project_id: str,
model: capellambse.MelodyModel,
Expand Down Expand Up @@ -193,7 +193,7 @@ def _handle_exchanges(


def create_grouped_link_fields(
work_item: data.CapellaWorkItem,
work_item: data_models.CapellaWorkItem,
back_links: dict[str, list[polarion_api.WorkItemLink]] | None = None,
):
"""Create the grouped link fields from the primary work item.
Expand All @@ -220,7 +220,7 @@ def create_grouped_link_fields(


def create_grouped_back_link_fields(
work_item: data.CapellaWorkItem,
work_item: data_models.CapellaWorkItem,
links: list[polarion_api.WorkItemLink],
):
"""Create fields for the given WorkItem using a list of backlinks.
Expand Down Expand Up @@ -266,7 +266,7 @@ def _make_url_list(


def _create_link_fields(
work_item: data.CapellaWorkItem,
work_item: data_models.CapellaWorkItem,
role: str,
links: list[polarion_api.WorkItemLink],
reverse: bool = False,
Expand Down
File renamed without changes.
Loading

0 comments on commit d716080

Please sign in to comment.