Skip to content

Commit

Permalink
chore: Restructure data_models
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 committed Oct 24, 2024
1 parent 6f77e4c commit f0204bf
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 298 deletions.
16 changes: 8 additions & 8 deletions capella2polarion/connectors/polarion_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import bidict
import polarion_rest_api_client as polarion_api

from capella2polarion import data_models
from capella2polarion import data_model


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

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

def __init__(
self,
polarion_work_items: list[data_models.CapellaWorkItem] | None = None,
polarion_work_items: list[data_model.CapellaWorkItem] | None = None,
):
if polarion_work_items is None:
polarion_work_items = []
Expand Down Expand Up @@ -58,7 +58,7 @@ def __iter__(self) -> cabc.Iterator[str]:

def items(
self,
) -> cabc.Iterator[tuple[str, str, data_models.CapellaWorkItem]]:
) -> cabc.Iterator[tuple[str, str, data_model.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 @@ -73,19 +73,19 @@ def get_capella_uuid(self, work_item_id: str) -> str | None:

def get_work_item_by_capella_uuid(
self, capella_uuid: str
) -> data_models.CapellaWorkItem | None:
) -> data_model.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:
) -> data_model.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]):
def update_work_items(self, work_items: list[data_model.CapellaWorkItem]):
"""Update all mappings for the given Work Items."""
for work_item in work_items:
assert work_item.id is not None
Expand Down Expand Up @@ -123,7 +123,7 @@ def remove_work_items_by_capella_uuid(self, uuids: cabc.Iterable[str]):
"""


def check_work_items(work_items: cabc.Iterable[data_models.CapellaWorkItem]):
def check_work_items(work_items: cabc.Iterable[data_model.CapellaWorkItem]):
"""Raise a ``ValueError`` if any work item has no ID."""
if work_item_without_id := next(
(wi for wi in work_items if wi.id is None), None
Expand Down
23 changes: 11 additions & 12 deletions capella2polarion/connectors/polarion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

import collections.abc as cabc
import json
import logging
import typing as t
from urllib import parse
Expand All @@ -13,7 +12,7 @@
from capellambse import helpers as chelpers
from lxml import etree

from capella2polarion import data_models
from capella2polarion import data_model
from capella2polarion.connectors import polarion_repo
from capella2polarion.converters import data_session

Expand Down Expand Up @@ -122,7 +121,7 @@ def load_polarion_work_item_map(self):
work_items = self.project_client.work_items.get_all(
"HAS_VALUE:uuid_capella",
fields={"workitems": "id,uuid_capella,checksum,status,type"},
work_item_cls=data_models.CapellaWorkItem,
work_item_cls=data_model.CapellaWorkItem,
)
self.polarion_data_repo.update_work_items(work_items)

Expand All @@ -140,7 +139,7 @@ def delete_orphaned_work_items(
if work_item.status != "deleted"
}
uuids: set[str] = existing_work_items - set(converter_session)
work_items: list[data_models.CapellaWorkItem] = []
work_items: list[data_model.CapellaWorkItem] = []
for uuid in uuids:
if wi := self.polarion_data_repo.get_work_item_by_capella_uuid(
uuid
Expand All @@ -159,7 +158,7 @@ def create_missing_work_items(
self, converter_session: data_session.ConverterSession
) -> None:
"""Post work items in a Polarion project."""
missing_work_items: list[data_models.CapellaWorkItem] = []
missing_work_items: list[data_model.CapellaWorkItem] = []
for uuid, converter_data in converter_session.items():
if not (work_item := converter_data.work_item):
logger.warning(
Expand Down Expand Up @@ -207,7 +206,7 @@ def compare_and_update_work_item(
try:
if work_item_changed or self.force_update:
old = self.project_client.work_items.get(
old.id, work_item_cls=data_models.CapellaWorkItem
old.id, work_item_cls=data_model.CapellaWorkItem
)
assert old is not None
assert old.id is not None
Expand Down Expand Up @@ -319,7 +318,7 @@ def compare_and_update_work_item(
)
raise error

def _refactor_attached_images(self, new: data_models.CapellaWorkItem):
def _refactor_attached_images(self, new: data_model.CapellaWorkItem):
def set_attachment_id(node: etree._Element) -> None:
if node.tag != "img":
return
Expand Down Expand Up @@ -352,7 +351,7 @@ def set_attachment_id(node: etree._Element) -> None:

def update_attachments(
self,
new: data_models.CapellaWorkItem,
new: data_model.CapellaWorkItem,
old_checksums: dict[str, str],
new_checksums: dict[str, str],
old_attachments: list[polarion_api.WorkItemAttachment],
Expand Down Expand Up @@ -468,7 +467,7 @@ def compare_and_update_work_items(

def create_documents(
self,
document_datas: list[data_models.DocumentData],
document_datas: list[data_model.DocumentData],
document_project: str | None = None,
):
"""Create new documents.
Expand All @@ -485,7 +484,7 @@ def create_documents(

def update_documents(
self,
document_datas: list[data_models.DocumentData],
document_datas: list[data_model.DocumentData],
document_project: str | None = None,
):
"""Update existing documents.
Expand All @@ -506,7 +505,7 @@ def update_documents(
def _process_document_datas(
self,
client: polarion_api.ProjectClient,
document_datas: list[data_models.DocumentData],
document_datas: list[data_model.DocumentData],
):
documents: list[polarion_api.Document] = []
headings: list[polarion_api.WorkItem] = []
Expand Down Expand Up @@ -545,7 +544,7 @@ def get_document(

def load_polarion_documents(
self,
document_infos: t.Iterable[data_models.DocumentInfo],
document_infos: t.Iterable[data_model.DocumentInfo],
) -> polarion_repo.DocumentRepository:
"""Load the documents referenced and text work items from Polarion."""
return {
Expand Down
2 changes: 1 addition & 1 deletion capella2polarion/converters/data_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from capellambse import model as m

from capella2polarion import data_models as dm
from capella2polarion import data_model as dm
from capella2polarion.converters import converter_config


Expand Down
6 changes: 3 additions & 3 deletions capella2polarion/converters/document_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pydantic
import yaml

from capella2polarion import data_models
from capella2polarion import data_model
from capella2polarion.converters import polarion_html_helper

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -83,11 +83,11 @@ class DocumentConfigs(pydantic.BaseModel):
pydantic.Field(default_factory=list)
)

def iterate_documents(self) -> t.Iterator[data_models.DocumentInfo]:
def iterate_documents(self) -> t.Iterator[data_model.DocumentInfo]:
"""Yield all document paths of the config as tuples."""
for conf in self.full_authority + self.mixed_authority:
for inst in conf.instances:
yield data_models.DocumentInfo(
yield data_model.DocumentInfo(
project_id=conf.project_id,
module_folder=inst.polarion_space,
module_name=inst.polarion_name,
Expand Down
18 changes: 9 additions & 9 deletions capella2polarion/converters/document_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from capella2polarion.connectors import polarion_repo

from .. import data_models
from .. import data_model
from . import document_config, polarion_html_helper
from . import text_work_item_provider as twi

Expand Down Expand Up @@ -54,10 +54,10 @@ class RenderingSession:
class ProjectData:
"""A class holding data of a project which documents are rendered for."""

new_docs: list[data_models.DocumentData] = dataclasses.field(
new_docs: list[data_model.DocumentData] = dataclasses.field(
default_factory=list
)
updated_docs: list[data_models.DocumentData] = dataclasses.field(
updated_docs: list[data_model.DocumentData] = dataclasses.field(
default_factory=list
)

Expand Down Expand Up @@ -203,7 +203,7 @@ def render_document(
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
) -> data_model.DocumentData:
"""Render a new Polarion document."""

@t.overload
Expand All @@ -216,7 +216,7 @@ def render_document(
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
) -> data_model.DocumentData:
"""Update an existing Polarion document."""

def render_document(
Expand All @@ -232,7 +232,7 @@ def render_document(
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
) -> data_model.DocumentData:
"""Render a Polarion document."""
text_work_item_provider = (
text_work_item_provider or twi.TextWorkItemProvider()
Expand Down Expand Up @@ -279,7 +279,7 @@ def render_document(
)
document.rendering_layouts = session.rendering_layouts

return data_models.DocumentData(
return data_model.DocumentData(
document, session.headings, text_work_item_provider
)

Expand All @@ -292,7 +292,7 @@ def update_mixed_authority_document(
section_parameters: dict[str, dict[str, t.Any]],
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
) -> data_models.DocumentData:
) -> data_model.DocumentData:
"""Update a mixed authority document."""
text_work_item_provider = (
text_work_item_provider or twi.TextWorkItemProvider()
Expand Down Expand Up @@ -360,7 +360,7 @@ def update_mixed_authority_document(
)
document.rendering_layouts = session.rendering_layouts

return data_models.DocumentData(
return data_model.DocumentData(
document, session.headings, text_work_item_provider
)

Expand Down
Loading

0 comments on commit f0204bf

Please sign in to comment.