From 123df9c5fcc662679543e32cb84e061454263008 Mon Sep 17 00:00:00 2001 From: Sophie Frasnedo Date: Wed, 11 Oct 2023 11:32:50 +0200 Subject: [PATCH] Deal with backward compatibility (second try) Signed-off-by: Sophie Frasnedo --- pypowsybl/network/impl/layout_parameters.py | 9 ++-- pypowsybl/network/impl/network.py | 48 +++++++++++---------- pypowsybl/network/impl/parent_parameters.py | 14 ++++++ pypowsybl/network/impl/sld_parameters.py | 3 +- 4 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 pypowsybl/network/impl/parent_parameters.py diff --git a/pypowsybl/network/impl/layout_parameters.py b/pypowsybl/network/impl/layout_parameters.py index 75a2fae179..4839ef6577 100644 --- a/pypowsybl/network/impl/layout_parameters.py +++ b/pypowsybl/network/impl/layout_parameters.py @@ -5,13 +5,16 @@ # SPDX-License-Identifier: MPL-2.0 # from .sld_parameters import SldParameters +from .parent_parameters import ParentParameters -class LayoutParameters: +class LayoutParameters(ParentParameters): """ - This class is only used for backward compatibility and represents sld parameters for a single line diagram svg generation.""" + This class is only used for backward compatibility and represents sld parameters for a single line diagram svg + generation.""" - def __init__(self, use_name: bool = False, center_name: bool = False, diagonal_label: bool = False, topological_coloring: bool = True, nodes_infos: bool = False, component_library: str = 'Convergence'): + def __init__(self, use_name: bool = False, center_name: bool = False, diagonal_label: bool = False, + topological_coloring: bool = True, nodes_infos: bool = False, component_library: str = 'Convergence'): self._use_name = use_name self._center_name = center_name self._diagonal_label = diagonal_label diff --git a/pypowsybl/network/impl/network.py b/pypowsybl/network/impl/network.py index 578b66fb18..37e2af2d3d 100644 --- a/pypowsybl/network/impl/network.py +++ b/pypowsybl/network/impl/network.py @@ -41,9 +41,11 @@ from .node_breaker_topology import NodeBreakerTopology from .sld_parameters import SldParameters from .layout_parameters import LayoutParameters +from .parent_parameters import ParentParameters from .svg import Svg from .util import create_data_frame_from_series_array, ParamsDict + class Network: # pylint: disable=too-many-public-methods def __init__(self, handle: _pp.JavaHandle): @@ -178,7 +180,7 @@ def reduce(self, v_min: float = 0, v_max: float = sys.float_info.max, ids: List[ _pp.reduce_network(self._handle, v_min, v_max, ids, vls, depths, with_dangling_lines) def write_single_line_diagram_svg(self, container_id: str, svg_file: PathOrStr, metadata_file: PathOrStr = None, - parameters: SldParameters = None) -> None: + parameters: ParentParameters = None) -> None: """ Create a single line diagram in SVG format from a voltage level or a substation and write to a file. @@ -190,21 +192,20 @@ def write_single_line_diagram_svg(self, container_id: str, svg_file: PathOrStr, """ svg_file = path_to_str(svg_file) - p = parameters._to_c_parameters() if parameters is not None else _pp.SldParameters() # pylint: disable=protected-access - _pp.write_single_line_diagram_svg(self._handle, container_id, svg_file, - '' if metadata_file is None else path_to_str(metadata_file), p) - - def write_single_line_diagram_svg(self, container_id: str, svg_file: PathOrStr, metadata_file: PathOrStr = None, - parameters: LayoutParameters = None) -> None: - """ - Ensure backward compatibility for get_single_line_diagram(self, container_id: str, parameters: SldParameters = None) -> Svg - """ - warnings.warn("LayoutParameters is deprecated, use SldParameters instead", DeprecationWarning) + p = _pp.SldParameters() - sld_parameters = parameters._to_sld_parameters() if parameters is not None else None - write_single_line_diagram_svg(self, container_id, svg_file, metadata_file, sld_parameters) + if isinstance(parameters, SldParameters) and parameters is not None: + p = parameters._to_c_parameters() # pylint: disable=protected-access + elif isinstance(parameters, LayoutParameters) and parameters is not None: + warnings.warn("LayoutParameters is deprecated, use SldParameters instead", DeprecationWarning) + sld_parameters = parameters._to_sld_parameters() # pylint: disable=protected-access + p = sld_parameters._to_c_parameters() # pylint: disable=protected-access + elif not isinstance(parameters, SldParameters) and not isinstance(parameters, LayoutParameters): + warnings.warn("Wrong class for single-line-diagram parameters, switching to default parameters") + _pp.write_single_line_diagram_svg(self._handle, container_id, svg_file, + '' if metadata_file is None else path_to_str(metadata_file), p) def get_single_line_diagram(self, container_id: str, parameters: SldParameters = None) -> Svg: """ @@ -218,18 +219,19 @@ def get_single_line_diagram(self, container_id: str, parameters: SldParameters = the single line diagram """ - p = parameters._to_c_parameters() if parameters is not None else _pp.SldParameters() # pylint: disable=protected-access - svg_and_metadata: List[str] = _pp.get_single_line_diagram_svg_and_metadata(self._handle, container_id, p) - return Svg(svg_and_metadata[0], svg_and_metadata[1]) + p = _pp.SldParameters() - def get_single_line_diagram(self, container_id: str, parameters: LayoutParameters = None) -> Svg: - """ - Ensure backward compatibility for get_single_line_diagram(self, container_id: str, parameters: SldParameters = None) -> Svg - """ + if isinstance(parameters, SldParameters) and parameters is not None: + p = parameters._to_c_parameters() # pylint: disable=protected-access + elif isinstance(parameters, LayoutParameters) and parameters is not None: + warnings.warn("LayoutParameters is deprecated, use SldParameters instead", DeprecationWarning) + sld_parameters = parameters._to_sld_parameters() # pylint: disable=protected-access + p = sld_parameters._to_c_parameters() # pylint: disable=protected-access + elif not isinstance(parameters, SldParameters) and not isinstance(parameters, LayoutParameters): + warnings.warn("Wrong class for single-line-diagram parameters, switching to default parameters") - warnings.warn("LayoutParameters is deprecated, use SldParameters instead", DeprecationWarning) - sld_parameters = parameters._to_sld_parameters() if parameters is not None else None - get_single_line_diagram(self, container_id, sld_parameters) + svg_and_metadata: List[str] = _pp.get_single_line_diagram_svg_and_metadata(self._handle, container_id, p) + return Svg(svg_and_metadata[0], svg_and_metadata[1]) def write_network_area_diagram_svg(self, svg_file: PathOrStr, voltage_level_ids: Union[str, List[str]] = None, depth: int = 0, high_nominal_voltage_bound: float = -1, diff --git a/pypowsybl/network/impl/parent_parameters.py b/pypowsybl/network/impl/parent_parameters.py new file mode 100644 index 0000000000..8cdfd85508 --- /dev/null +++ b/pypowsybl/network/impl/parent_parameters.py @@ -0,0 +1,14 @@ +# Copyright (c) 2023, RTE (http://www.rte-france.com) +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# SPDX-License-Identifier: MPL-2.0 +# + +class ParentParameters: + """ + This class is only used for backward compatibility and represents the parent class of LayoutParameters ( + deprecated) and SldParameters.""" + + def __init__(self): + pass diff --git a/pypowsybl/network/impl/sld_parameters.py b/pypowsybl/network/impl/sld_parameters.py index adaff9a791..7a27e6c825 100644 --- a/pypowsybl/network/impl/sld_parameters.py +++ b/pypowsybl/network/impl/sld_parameters.py @@ -5,9 +5,10 @@ # SPDX-License-Identifier: MPL-2.0 # import pypowsybl._pypowsybl as _pp +from .parent_parameters import ParentParameters -class SldParameters: +class SldParameters(ParentParameters): """ This class represents sld parameters for a single line diagram svg generation."""