Skip to content

Commit

Permalink
Merge branch 'refactor-config' into new-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger authored Jan 11, 2024
2 parents 518efc9 + e8540b8 commit 5b5bbcf
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 307 deletions.
32 changes: 20 additions & 12 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import click
from capellambse import cli_helpers

from capella2polarion import worker as pw
from capella2polarion.cli import Capella2PolarionCli
from capella2polarion.connectors import polarion_worker as pw
from capella2polarion.converters import model_converter

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,8 +77,6 @@ def cli(
)
capella2polarion_cli.setup_logger()
ctx.obj = capella2polarion_cli
capella2polarion_cli.echo = click.echo
capella2polarion_cli.echo("Start")


@cli.command()
Expand Down Expand Up @@ -106,23 +105,32 @@ def synchronize(ctx: click.core.Context) -> None:
capella_to_polarion_cli.capella_diagram_cache_index_content is not None
)

polarion_worker = pw.CapellaPolarionWorker(
capella_to_polarion_cli.polarion_params,
converter = model_converter.ModelConverter(
capella_to_polarion_cli.capella_model,
capella_to_polarion_cli.capella_diagram_cache_folder_path,
capella_to_polarion_cli.polarion_params.project_id,
)

converter.read_model(
capella_to_polarion_cli.config,
capella_to_polarion_cli.capella_diagram_cache_index_content,
capella_to_polarion_cli.capella_diagram_cache_folder_path,
)
polarion_worker.generate_converter_session()

polarion_worker = pw.CapellaPolarionWorker(
capella_to_polarion_cli.polarion_params, capella_to_polarion_cli.config
)

polarion_worker.load_polarion_work_item_map()
polarion_worker.create_work_items()
polarion_worker.delete_work_items()
polarion_worker.post_work_items()

converter.generate_work_items(polarion_worker.polarion_data_repo)

polarion_worker.delete_work_items(converter.converter_session)
polarion_worker.post_work_items(converter.converter_session)

# Create missing links for new work items
polarion_worker.create_work_items()
polarion_worker.patch_work_items()
converter.generate_work_items(polarion_worker.polarion_data_repo, True)

polarion_worker.patch_work_items(converter.converter_session)


if __name__ == "__main__":
Expand Down
11 changes: 5 additions & 6 deletions capella2polarion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import capellambse
import click

from capella2polarion import worker as pw
from capella2polarion.connectors import polarion_worker as pw
from capella2polarion.converters import converter_config

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -54,7 +54,6 @@ def __init__(
self.synchronize_config_io: typing.TextIO = synchronize_config_io
self.synchronize_config_content: dict[str, typing.Any] = {}
self.synchronize_config_roles: dict[str, list[str]] | None = None
self.echo = click.echo
self.config = converter_config.ConverterConfig()

def _none_save_value_string(self, value: str | None) -> str | None:
Expand All @@ -69,7 +68,7 @@ def _type(value):
def _value(value):
return value

self.echo("---------------------------------------")
click.echo("---------------------------------------")
lighted_member_vars = [
attribute
for attribute in dir(self)
Expand All @@ -96,14 +95,14 @@ def _value(value):
else:
string_value = _type(member_value)
string_value = self._none_save_value_string(string_value)
self.echo(f"{lighted_member_var}: '{string_value}'")
click.echo(f"{lighted_member_var}: '{string_value}'")

echo = ("NO", "YES")[
self.capella_diagram_cache_index_file_path.is_file()
]
self.echo(f"""Capella Diagram Cache Index-File exists: {echo}""")
click.echo(f"""Capella Diagram Cache Index-File exists: {echo}""")
echo = ("YES", "NO")[self.synchronize_config_io.closed]
self.echo(f"""Synchronize Config-IO is open: {echo}""")
click.echo(f"""Synchronize Config-IO is open: {echo}""")

def setup_logger(self) -> None:
"""Set the logger in the right mood."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@

import collections.abc as cabc
import logging
import pathlib
import typing as t
from urllib import parse

import capellambse
import polarion_rest_api_client as polarion_api

from capella2polarion import data_models
from capella2polarion.connectors import polarion_repo
from capella2polarion.converters import (
converter_config,
data_session,
element_converter,
link_converter,
)
from capella2polarion.converters import converter_config, data_session

logger = logging.getLogger(__name__)

Expand All @@ -42,32 +34,25 @@ class CapellaPolarionWorker:
def __init__(
self,
params: PolarionWorkerParams,
model: capellambse.MelodyModel,
config: converter_config.ConverterConfig,
diagram_idx: list[dict[str, t.Any]],
diagram_cache_path: pathlib.Path,
) -> None:
self.polarion_params = params
self.polarion_data_repo = polarion_repo.PolarionDataRepository()
self.converter_session: data_session.ConverterSession = {}
self.model = model
self.config = config
self.diagram_idx = diagram_idx
self.diagram_cache_path = diagram_cache_path

if (self.polarion_params.project_id is None) or (
len(self.polarion_params.project_id) == 0
):
raise ValueError(
f"""ProjectId invalid. Value
'{self._save_value_string(self.polarion_params.project_id)}'"""
'{self.polarion_params.project_id}'"""
)

result_url = parse.urlparse(self.polarion_params.url)
if not all([result_url.scheme, result_url.netloc]):
raise ValueError(
f"""Polarion URL parameter is not a valid url.
Value {self._save_value_string(self.polarion_params.url)}"""
Value {self.polarion_params.url}"""
)
if self.polarion_params.private_access_token is None:
raise ValueError(
Expand All @@ -84,64 +69,14 @@ def __init__(
)
self.check_client()

def _save_value_string(self, value: str | None) -> str | None:
return "None" if value is None else value

def check_client(self) -> None:
"""Instantiate the polarion client as member."""
if not self.client.project_exists():
raise KeyError(
f"Miss Polarion project with id "
f"{self._save_value_string(self.polarion_params.project_id)}"
f"{self.polarion_params.project_id}"
)

def generate_converter_session(
self,
) -> None:
"""Return an elements and UUID to Polarion type map."""
missing_types: set[tuple[str, str, dict[str, t.Any]]] = set()
for layer, c_type in self.config.layers_and_types():
below = getattr(self.model, layer)
if c_type == "Diagram":
continue

objects = self.model.search(c_type, below=below)
for obj in objects:
attributes = {
"actor": getattr(obj, "is_actor", None),
"nature": getattr(obj, "nature", None),
}
if config := self.config.get_type_config(
layer, c_type, **attributes
):
self.converter_session[
obj.uuid
] = data_session.ConverterData(layer, config, obj)
else:
missing_types.add((layer, c_type, attributes))

if self.config.diagram_config:
diagrams_from_cache = {
d["uuid"] for d in self.diagram_idx if d["success"]
}
for d in self.model.diagrams:
if d.uuid in diagrams_from_cache:
self.converter_session[
d.uuid
] = data_session.ConverterData(
"", self.config.diagram_config, d
)

if missing_types:
for missing_type in missing_types:
layer, c_type, attributes = missing_type
logger.warning(
"Capella type %r is configured in layer %r, but not for %s.",
layer,
c_type,
", ".join(f"{k!r}={v!r}" for k, v in attributes.items()),
)

def load_polarion_work_item_map(self):
"""Return a map from Capella UUIDs to Polarion work items."""
_type = " ".join(self.config.polarion_types)
Expand All @@ -153,29 +88,9 @@ def load_polarion_work_item_map(self):

self.polarion_data_repo.update_work_items(work_items)

def create_work_items(
self,
) -> dict[str, data_models.CapellaWorkItem]:
"""Create a list of work items for Polarion."""
serializer = element_converter.CapellaWorkItemSerializer(
self.diagram_cache_path,
self.model,
self.polarion_data_repo,
self.converter_session,
)
work_items = serializer.serialize_all()
for work_item in work_items:
assert work_item is not None
assert work_item.title is not None
assert work_item.type is not None
if old := self.polarion_data_repo.get_work_item_by_capella_uuid(
work_item.uuid_capella
):
work_item.id = old.id

return {wi.uuid_capella: wi for wi in work_items}

def delete_work_items(self) -> None:
def delete_work_items(
self, converter_session: data_session.ConverterSession
) -> None:
"""Delete work items in a Polarion project.
If the delete flag is set to ``False`` in the context work items are
Expand All @@ -192,7 +107,7 @@ def serialize_for_delete(uuid: str) -> str:
for uuid, _, work_item in self.polarion_data_repo.items()
if work_item.status != "deleted"
}
uuids: set[str] = existing_work_items - set(self.converter_session)
uuids: set[str] = existing_work_items - set(converter_session)
work_item_ids = [serialize_for_delete(uuid) for uuid in uuids]
if work_item_ids:
try:
Expand All @@ -204,11 +119,11 @@ def serialize_for_delete(uuid: str) -> str:
logger.error("Deleting work items failed. %s", error.args[0])

def post_work_items(
self,
self, converter_session: data_session.ConverterSession
) -> None:
"""Post work items in a Polarion project."""
missing_work_items: list[data_models.CapellaWorkItem] = []
for uuid, converter_data in self.converter_session.items():
for uuid, converter_data in converter_session.items():
work_item = converter_data.work_item
if work_item is None:
logger.warning(
Expand All @@ -231,24 +146,17 @@ def post_work_items(
logger.error("Creating work items failed. %s", error.args[0])

def patch_work_item(
self,
new: data_models.CapellaWorkItem,
old: data_models.CapellaWorkItem,
self, uuid: str, converter_session: data_session.ConverterSession
):
"""Patch a given WorkItem.
Parameters
----------
api
The context to execute the patch for.
new
The updated CapellaWorkItem
old
The CapellaWorkItem currently present on polarion
"""
"""Patch a given WorkItem."""
new = converter_session[uuid].work_item
_, old = self.polarion_data_repo[uuid]
if new == old:
return

assert old is not None
assert new is not None

log_args = (old.id, new.type, new.title)
logger.info("Update work item %r for model %s %r...", *log_args)
if "uuid_capella" in new.additional_attributes:
Expand Down Expand Up @@ -322,48 +230,8 @@ def _get_link_id(link: polarion_api.WorkItemLink) -> str:
)

def patch_work_items(
self,
self, converter_session: data_session.ConverterSession
) -> None:
"""Update work items in a Polarion project."""

back_links: dict[str, list[polarion_api.WorkItemLink]] = {}
link_serializer = link_converter.LinkSerializer(
self.polarion_data_repo,
self.converter_session,
self.polarion_params.project_id,
self.model,
)

for uuid, converter_data in self.converter_session.items():
if converter_data.work_item is None:
logger.warning(
"Expected to find a WorkItem for %s, but there is none",
uuid,
)
continue

links = link_serializer.create_links_for_work_item(uuid)
converter_data.work_item.linked_work_items = links

link_converter.create_grouped_link_fields(
converter_data.work_item, back_links
)

for uuid, converter_data in self.converter_session.items():
if converter_data.work_item is None:
logger.warning(
"Expected to find a WorkItem for %s, but there is none",
uuid,
)
continue

_, old_work_item = self.polarion_data_repo[uuid]
if old_work_item.id in back_links:
link_converter.create_grouped_back_link_fields(
converter_data.work_item, back_links[old_work_item.id]
)

self.patch_work_item(
converter_data.work_item,
old_work_item,
)
for uuid in converter_session:
self.patch_work_item(uuid, converter_session)
Loading

0 comments on commit 5b5bbcf

Please sign in to comment.