Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add add_custom_diagrams serializer #134

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Main entry point into capella2polarion."""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -115,6 +116,12 @@ def print_cli_state(capella2polarion_cli: Capella2PolarionCli) -> None:
is_flag=True,
default=True,
)
@click.option(
"--generate-figure-captions",
envvar="CAPELLA2POLARION_GENERATE_FIGURE_CAPTIONS",
is_flag=True,
default=False,
)
@click.pass_context
def synchronize(
ctx: click.core.Context,
Expand All @@ -123,6 +130,7 @@ def synchronize(
type_prefix: str,
role_prefix: str,
grouped_links_custom_fields: bool,
generate_figure_captions: bool,
) -> None:
"""Synchronise model elements."""
capella_to_polarion_cli: Capella2PolarionCli = ctx.obj
Expand Down Expand Up @@ -159,6 +167,7 @@ def synchronize(
generate_links=True,
generate_attachments=True,
generate_grouped_links_custom_fields=grouped_links_custom_fields,
generate_figure_captions=generate_figure_captions,
)

polarion_worker.compare_and_update_work_items(converter.converter_session)
Expand Down
57 changes: 48 additions & 9 deletions capella2polarion/converters/converter_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Module providing capella2polarion config class."""

from __future__ import annotations

import dataclasses
Expand All @@ -18,6 +19,10 @@
DESCRIPTION_REFERENCE_SERIALIZER = "description_reference"
DIAGRAM_ELEMENTS_SERIALIZER = "diagram_elements"

ConverterConfigDict_type: t.TypeAlias = dict[
str, t.Union[dict[str, t.Any], list[dict[str, t.Any]]]
]


@dataclasses.dataclass
class LinkConfig:
Expand Down Expand Up @@ -45,14 +50,27 @@ class CapellaTypeConfig:
"""A single Capella Type configuration."""

p_type: str | None = None
converters: str | list[str] | dict[str, dict[str, t.Any]] | None = None
converters: str | list[str] | ConverterConfigDict_type | None = None
links: list[LinkConfig] = dataclasses.field(default_factory=list)
is_actor: bool | None = None
nature: str | None = None

def __post_init__(self):
"""Post processing for the initialization."""
self.converters = _force_dict(self.converters)
self.converters = _force_dict( # type: ignore[assignment]
self.converters
)


@dataclasses.dataclass
class CustomDiagramConfig:
"""A single Capella Custom Diagram configuration."""

capella_attr: str
polarion_id: str
title: str
render_params: dict[str, t.Any] | None = None
filters: list[str] | None = None


def _default_type_conversion(c_type: str) -> str:
Expand All @@ -71,7 +89,7 @@ def __init__(self):

def read_config_file(
self,
synchronize_config: t.TextIO,
synchronize_config: str | t.TextIO,
type_prefix: str = "",
role_prefix: str = "",
):
Expand Down Expand Up @@ -283,7 +301,7 @@ def config_matches(config: CapellaTypeConfig | None, **kwargs: t.Any) -> bool:


def _read_capella_type_configs(
conf: dict[str, t.Any] | list[dict[str, t.Any]] | None
conf: dict[str, t.Any] | list[dict[str, t.Any]] | None,
) -> list[dict]:
if conf is None:
return [{}]
Expand All @@ -300,7 +318,7 @@ def _read_capella_type_configs(


def _force_dict(
config: str | list[str] | dict[str, dict[str, t.Any]] | None
config: str | list[str] | ConverterConfigDict_type | None,
) -> dict[str, dict[str, t.Any]]:
match config:
case None:
Expand All @@ -323,26 +341,47 @@ def add_prefix(polarion_type: str, prefix: str) -> str:


def _filter_converter_config(
config: dict[str, dict[str, t.Any]]
config: ConverterConfigDict_type,
) -> dict[str, dict[str, t.Any]]:
custom_converters = (
"include_pre_and_post_condition",
"linked_text_as_description",
"add_context_diagram",
"add_tree_diagram",
"add_custom_diagrams",
"add_context_diagram", # TODO: Deprecated, so remove in next release
"add_tree_diagram", # TODO: Deprecated, so remove in next release
"add_jinja_fields",
"jinja_as_description",
)
filtered_config = {}
filtered_config: dict[str, dict[str, t.Any]] = {}
for name, params in config.items():
params = params or {}
if name not in custom_converters:
logger.error("Unknown converter in config %r", name)
continue

if name in ("add_context_diagram", "add_tree_diagram"):
assert isinstance(params, dict)
params = _filter_context_diagram_config(params)

if name in ("add_custom_diagrams",):
if isinstance(params, list):
params = {
"custom_diagrams_configs": [
CustomDiagramConfig(
**_filter_context_diagram_config(rp)
)
for rp in params
]
}
elif isinstance(params, dict):
assert "custom_diagrams_configs" in params
else:
logger.error( # type: ignore[unreachable]
"Unknown 'add_custom_diagrams' config %r", params
)
continue

assert isinstance(params, dict)
filtered_config[name] = params

return filtered_config
Expand Down
Loading
Loading