Skip to content

Commit

Permalink
refactor: Remove prefix class attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Aug 26, 2024
1 parent d27e28d commit c39b08a
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions capella2polarion/converters/converter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,25 @@ def __init__(self):
self.diagram_config: CapellaTypeConfig | None = None
self.__global_config = CapellaTypeConfig()

self._type_prefix: str
self._role_prefix: str

def read_config_file(
self,
synchronize_config: t.TextIO,
type_prefix: str = "",
role_prefix: str = "",
):
"""Read a given yaml file as config."""
self._type_prefix = type_prefix
self._role_prefix = role_prefix
config_dict = yaml.safe_load(synchronize_config)
# We handle the cross layer config separately as global_configs
global_config_dict = config_dict.pop("*", {})
all_type_config = global_config_dict.pop("*", {})
global_links = all_type_config.get("links", [])
self.__global_config.links = self._force_link_config(global_links)
self.__global_config.links = self._force_link_config(
global_links, role_prefix
)

if "Diagram" in global_config_dict:
diagram_config = global_config_dict.pop("Diagram") or {}
self.set_diagram_config(diagram_config)
self.set_diagram_config(diagram_config, type_prefix)

for c_type, type_config in global_config_dict.items():
type_config = type_config or {}
Expand All @@ -103,7 +100,9 @@ def read_config_file(
type_configs = type_configs or {}
self.add_layer(layer)
for c_type, c_type_config in type_configs.items():
self.set_layer_config(c_type, c_type_config, layer)
self.set_layer_config(
c_type, c_type_config, layer, type_prefix, role_prefix
)

def add_layer(self, layer: str):
"""Add a new layer without configuring any types."""
Expand All @@ -117,6 +116,8 @@ def set_layer_config(
c_type: str,
c_type_config: dict[str, t.Any] | list[dict[str, t.Any]] | None,
layer: str,
type_prefix: str = "",
role_prefix: str = "",
):
"""Set one or multiple configs for a type to an existing layer."""
type_configs = _read_capella_type_configs(c_type_config)
Expand All @@ -140,10 +141,12 @@ def set_layer_config(
or closest_config.p_type
or _default_type_conversion(c_type)
),
self._type_prefix,
type_prefix,
)
self.polarion_types.add(p_type)
links = self._force_link_config(type_config.get("links", []))
links = self._force_link_config(
type_config.get("links", []), role_prefix
)
self._layer_configs[layer][c_type].append(
CapellaTypeConfig(
p_type,
Expand Down Expand Up @@ -171,7 +174,9 @@ def set_global_config(self, c_type: str, type_config: dict[str, t.Any]):
type_config.get("nature", _C2P_DEFAULT),
)

def set_diagram_config(self, diagram_config: dict[str, t.Any]):
def set_diagram_config(
self, diagram_config: dict[str, t.Any], type_prefix: str = ""
):
"""Set the diagram config."""
c_type = "diagram"
p_type = diagram_config.get("polarion_type") or "diagram"
Expand All @@ -180,18 +185,20 @@ def set_diagram_config(self, diagram_config: dict[str, t.Any]):
c_type, self._force_link_config(diagram_config.get("links", []))
)
self.diagram_config = CapellaTypeConfig(
add_prefix(p_type, self._type_prefix),
add_prefix(p_type, type_prefix),
diagram_config.get("serializer") or "diagram",
links + self._get_global_links(c_type),
)

def _force_link_config(self, links: t.Any) -> list[LinkConfig]:
def _force_link_config(
self, links: t.Any, role_prefix: str = ""
) -> list[LinkConfig]:
result: list[LinkConfig] = []
for link in links:
if isinstance(link, str):
config = LinkConfig(
capella_attr=link,
polarion_role=add_prefix(link, self._role_prefix),
polarion_role=add_prefix(link, role_prefix),
link_field=link,
reverse_field=f"{link}_reverse",
)
Expand All @@ -200,7 +207,7 @@ def _force_link_config(self, links: t.Any) -> list[LinkConfig]:
capella_attr=(lid := link["capella_attr"]),
polarion_role=add_prefix(
(pid := link.get("polarion_role", lid)),
self._role_prefix,
role_prefix,
),
include=link.get("include", {}),
link_field=(lf := link.get("link_field", pid)),
Expand Down

0 comments on commit c39b08a

Please sign in to comment.