Skip to content

Commit

Permalink
feat: Upgrade to capellambse v0.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Aug 27, 2024
1 parent 35d4b56 commit 22da27f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 43 deletions.
4 changes: 2 additions & 2 deletions capella2polarion/converters/data_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import dataclasses

from capellambse.model import common, diagram
from capellambse import model as m

from capella2polarion import data_models as dm
from capella2polarion.converters import converter_config
Expand All @@ -17,7 +17,7 @@ class ConverterData:

layer: str
type_config: converter_config.CapellaTypeConfig
capella_element: diagram.Diagram | common.GenericElement
capella_element: m.Diagram | m.ModelElement
work_item: dm.CapellaWorkItem | None = None
description_references: list[str] = dataclasses.field(default_factory=list)
errors: set[str] = dataclasses.field(default_factory=set)
Expand Down
17 changes: 8 additions & 9 deletions capella2polarion/converters/element_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import markupsafe
import polarion_rest_api_client as polarion_api
from capellambse import helpers as chelpers
from capellambse.model import common
from capellambse.model.crosslayer import interaction
from capellambse.model.layers import oa
from capellambse import model as m
from capellambse.metamodel import interaction, oa
from lxml import etree

from capella2polarion import data_models
Expand Down Expand Up @@ -167,7 +166,7 @@ def _add_attachment(

def _draw_diagram_svg(
self,
diagram: capellambse.model.diagram.AbstractDiagram,
diagram: m.AbstractDiagram,
file_name: str,
title: str,
max_width: int,
Expand Down Expand Up @@ -236,7 +235,7 @@ def __make_href_filter(self, obj: object) -> str | None:
def __insert_diagram(
self,
work_item: data_models.CapellaWorkItem,
diagram: capellambse.model.diagram.AbstractDiagram,
diagram: m.AbstractDiagram,
file_name: str,
render_params: dict[str, t.Any] | None = None,
max_width: int = 800,
Expand Down Expand Up @@ -272,7 +271,7 @@ def __insert_diagram(
def _draw_additional_attributes_diagram(
self,
work_item: data_models.CapellaWorkItem,
diagram: capellambse.model.diagram.AbstractDiagram,
diagram: m.AbstractDiagram,
attribute: str,
title: str,
render_params: dict[str, t.Any] | None = None,
Expand All @@ -294,7 +293,7 @@ def _draw_additional_attributes_diagram(
}

def _sanitize_linked_text(
self, obj: common.GenericElement
self, obj: m.ModelElement
) -> tuple[
list[str], markupsafe.Markup, list[polarion_api.WorkItemAttachment]
]:
Expand All @@ -311,7 +310,7 @@ def _sanitize_linked_text(
return self._sanitize_text(obj, linked_text)

def _sanitize_text(
self, obj: common.GenericElement, text: markupsafe.Markup | str
self, obj: m.ModelElement, text: markupsafe.Markup | str
) -> tuple[
list[str], markupsafe.Markup, list[polarion_api.WorkItemAttachment]
]:
Expand Down Expand Up @@ -402,7 +401,7 @@ def _replace_markup(

def _get_requirement_types_text(
self,
obj: common.GenericElement,
obj: m.ModelElement,
) -> dict[str, dict[str, str]]:
type_texts = collections.defaultdict(list)
for req in getattr(obj, "requirements", []):
Expand Down
26 changes: 14 additions & 12 deletions capella2polarion/converters/link_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import capellambse
import polarion_rest_api_client as polarion_api
from capellambse.model import common
from capellambse.model import diagram as diag
from capellambse import model as m

import capella2polarion.converters.polarion_html_helper
from capella2polarion import data_models
Expand All @@ -22,7 +21,7 @@

TYPE_RESOLVERS = {"Part": lambda obj: obj.type.uuid}
_Serializer: t.TypeAlias = cabc.Callable[
[common.GenericElement, str, str, dict[str, t.Any]],
[m.ModelObject, str, str, dict[str, t.Any]],
list[polarion_api.WorkItemLink],
]

Expand Down Expand Up @@ -73,7 +72,7 @@ def create_links_for_work_item(
else:
refs = _resolve_attribute(obj, link_config.capella_attr)
new: cabc.Iterable[str]
if isinstance(refs, common.ElementList):
if isinstance(refs, m.ElementList):
new = refs.by_uuid # type: ignore[assignment]
else:
assert hasattr(refs, "uuid"), "No 'uuid' on value"
Expand Down Expand Up @@ -136,22 +135,24 @@ def _get_work_item_ids(

def _handle_description_reference_links(
self,
obj: common.GenericElement,
obj: m.ModelObject,
work_item_id: str,
role_id: str,
links: dict[str, polarion_api.WorkItemLink],
) -> list[polarion_api.WorkItemLink]:
assert isinstance(obj, m.ModelElement)
refs = self.converter_session[obj.uuid].description_references
ref_set = set(self._get_work_item_ids(work_item_id, refs, role_id))
return self._create(work_item_id, role_id, ref_set, links)

def _handle_diagram_reference_links(
self,
obj: diag.Diagram,
obj: m.ModelObject,
work_item_id: str,
role_id: str,
links: dict[str, polarion_api.WorkItemLink],
) -> list[polarion_api.WorkItemLink]:
assert isinstance(obj, m.Diagram)
try:
refs = set(self._collect_uuids(obj.nodes))
refs = set(self._get_work_item_ids(work_item_id, refs, role_id))
Expand All @@ -168,7 +169,7 @@ def _handle_diagram_reference_links(

def _collect_uuids(
self,
nodes: cabc.Iterable[common.GenericElement],
nodes: cabc.Iterable[m.ModelElement],
) -> cabc.Iterator[str]:
type_resolvers = TYPE_RESOLVERS
for node in nodes:
Expand Down Expand Up @@ -283,12 +284,13 @@ def _create_link_fields(
obj._short_repr_(),
)

if isinstance(attr, common.ElementList):
uuids = attr.by_uuid # type: ignore[assignment]
if isinstance(attr, m.ElementList):
uuids: list[str] = list(attr.by_uuid)
else:
assert hasattr(attr, "uuid")
uuids = [attr.uuid]

assert work_item.id is not None
work_item_ids = list(
self._get_work_item_ids(work_item.id, uuids, attr_name)
)
Expand Down Expand Up @@ -372,12 +374,12 @@ def _sorted_unordered_html_list(


def _resolve_attribute(
obj: common.GenericElement, attr_id: str
) -> common.ElementList[common.GenericElement] | common.GenericElement:
obj: m.ModelObject, attr_id: str
) -> m.ElementList[m.ModelElement] | m.ModelElement:
attr_name, _, map_id = attr_id.partition(".")
objs = getattr(obj, attr_name)
if map_id:
if isinstance(objs, common.GenericElement):
if isinstance(objs, m.ModelElement):
return _resolve_attribute(objs, map_id)
objs = objs.map(map_id)
return objs
18 changes: 4 additions & 14 deletions capella2polarion/converters/polarion_html_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import pathlib
import re

import capellambse
import jinja2
from capellambse import helpers as chelpers
from capellambse import model as m
from lxml import etree, html

heading_id_prefix = "polarion_wiki macro name=module-workitem;params=id="
Expand Down Expand Up @@ -80,11 +80,7 @@ def _get_jinja_env(self, template_folder: str | pathlib.Path):

def check_model_element(
self, obj: object
) -> (
capellambse.model.GenericElement
| capellambse.model.diagram.AbstractDiagram
| None
):
) -> m.ModelElement | m.AbstractDiagram | None:
"""Check if a model element was passed.
Return None if no element and raise a TypeError if a wrong typed
Expand All @@ -94,15 +90,9 @@ def check_model_element(
if jinja2.is_undefined(obj) or obj is None:
return None

if isinstance(obj, capellambse.model.ElementList):
if isinstance(obj, m.ElementList):
raise TypeError("Cannot make an href to a list of elements")
if not isinstance(
obj,
(
capellambse.model.GenericElement,
capellambse.model.diagram.AbstractDiagram,
),
):
if not isinstance(obj, (m.ModelElement, m.AbstractDiagram)):
raise TypeError(f"Expected a model object, got {obj!r}")
return obj

Expand Down
2 changes: 1 addition & 1 deletion jupyter-notebooks/document_templates/test-classes.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
{{ heading(1, "Class Document", session)}}
{{ add_class_dependencies(cls, classes) }}
{{ heading(2, "Data Classes", session)}}
{% for cl in classes | unique %}
{% for cl in classes | unique(attribute="uuid") %}
{{ insert_work_item(cl, session) }}
{% endfor %}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dependencies = [
"capellambse>=0.5,<0.6",
"capellambse_context_diagrams",
"capellambse>=0.6.5,<0.7",
"capellambse_context_diagrams>=0.3.4",
"click",
"PyYAML",
"polarion-rest-api-client==1.1.1",
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import markupsafe
import polarion_rest_api_client as polarion_api
import pytest
from capellambse import model as m

from capella2polarion import cli, data_models
from capella2polarion.connectors import polarion_repo, polarion_worker
Expand Down Expand Up @@ -79,7 +80,7 @@ def __init__(
name: str = "",
attribute: t.Any | None = None,
):
super().__init__(spec=capellambse.model.GenericElement)
super().__init__(spec=m.ModelElement)
self.uuid = uuid
self.name = name
self.attribute = attribute
Expand Down
4 changes: 2 additions & 2 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import markupsafe
import polarion_rest_api_client as polarion_api
import pytest
from capellambse.model import common
from capellambse import model as m

from capella2polarion import data_models
from capella2polarion.connectors import polarion_repo
Expand Down Expand Up @@ -616,7 +616,7 @@ def test_create_links_from_ElementList(base_object: BaseObjectContainer):
obj = FakeModelObject(
"uuid6",
name="Fake 6",
attribute=common.ElementList(
attribute=m.ElementList(
base_object.c2pcli.capella_model,
[fake, fake1],
FakeModelObject,
Expand Down

0 comments on commit 22da27f

Please sign in to comment.