Skip to content

Commit

Permalink
refactor: Minor refactoring (#110)
Browse files Browse the repository at this point in the history
* ci: fix mypy

* refactor: introduce new constants and use an existing one in an additional place

* fix: Don't raise exceptions but render error text if unexpected non-model objects are passed to a filter or function
  • Loading branch information
micha91 authored Sep 16, 2024
1 parent 42234ef commit 9c1c406
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions capella2polarion/converters/document_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
from . import document_config, polarion_html_helper
from . import text_work_item_provider as twi

AREA_END_CLS = "c2pAreaEnd"
"""This class is expected for a div in a wiki macro to start a rendering area
in mixed authority documents."""
AREA_START_CLS = "c2pAreaStart"
"""This class is expected for a div in a wiki macro to end a rendering area in
mixed authority documents."""

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -92,8 +99,11 @@ def __insert_work_item(
self, obj: object, session: RenderingSession, level: int | None = None
) -> str:
if (obj := self.check_model_element(obj)) is None:
logger.error(
"A non-model object was passed to insert a work item."
)
return polarion_html_helper.RED_TEXT.format(
text="A none model object was passed to insert a work item."
text="A non-model object was passed to insert a work item."
)

if wi := self.polarion_repository.get_work_item_by_capella_uuid(
Expand Down Expand Up @@ -136,7 +146,10 @@ def __insert_work_item(

def __link_work_item(self, obj: object) -> str:
if (obj := self.check_model_element(obj)) is None:
raise TypeError("object passed was no model element")
logger.error("A non-model object was passed to link a work item.")
return polarion_html_helper.RED_TEXT.format(
text="A non-model object was passed to link a work item."
)

if wi := self.polarion_repository.get_work_item_by_capella_uuid(
obj.uuid
Expand All @@ -162,7 +175,10 @@ def __heading(self, level: int, text: str, session: RenderingSession):

def __work_item_field(self, obj: object, field: str) -> t.Any:
if (obj := self.check_model_element(obj)) is None:
raise TypeError("object passed was no model element")
logger.error(
"A non-model object was passed to get a work item field."
)
return "A non-model object was passed to get a work item field."

if wi := self.polarion_repository.get_work_item_by_capella_uuid(
obj.uuid
Expand Down Expand Up @@ -356,6 +372,7 @@ def _update_rendering_layouts(
"""Keep existing work item layouts in their original order."""
document.rendering_layouts = document.rendering_layouts or []
for rendering_layout in rendering_layouts:
assert rendering_layout.type is not None
index = polarion_html_helper.get_layout_index(
"section", document.rendering_layouts, rendering_layout.type
)
Expand Down Expand Up @@ -579,7 +596,7 @@ def _extract_section_areas(self, html_elements: list[etree._Element]):
and content[0].tag == "div"
):
element_id = content[0].get("id")
if content[0].get("class") == "c2pAreaStart":
if content[0].get("class") == AREA_START_CLS:
assert (
element_id is not None
), "There was no id set to identify the area"
Expand All @@ -589,7 +606,7 @@ def _extract_section_areas(self, html_elements: list[etree._Element]):
)
current_area_id = element_id
current_area_start = element_index
elif content[0].get("class") == "c2pAreaEnd":
elif content[0].get("class") == AREA_END_CLS:
assert (
element_id is not None
), "There was no id set to identify the area"
Expand Down
2 changes: 1 addition & 1 deletion capella2polarion/converters/text_work_item_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def insert_text_work_items(
if not isinstance(element, html.HtmlElement):
continue

if element.tag == "workitem":
if element.tag == html_helper.WORK_ITEM_TAG:
new_content.extend(html_fragments[last_match + 1 : index])
last_match = index
if work_item := self.new_text_work_items.get(
Expand Down

0 comments on commit 9c1c406

Please sign in to comment.