From c39b08a5556cfcfc57bc8b3c40a61e599342ad5f Mon Sep 17 00:00:00 2001 From: ewuerger Date: Mon, 26 Aug 2024 04:03:57 +0200 Subject: [PATCH] refactor: Remove prefix class attributes --- .../converters/converter_config.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/capella2polarion/converters/converter_config.py b/capella2polarion/converters/converter_config.py index 44a6806c..2725d73e 100644 --- a/capella2polarion/converters/converter_config.py +++ b/capella2polarion/converters/converter_config.py @@ -72,9 +72,6 @@ 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, @@ -82,18 +79,18 @@ def read_config_file( 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 {} @@ -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.""" @@ -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) @@ -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, @@ -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" @@ -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", ) @@ -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)),