Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add min version decorator #5631

Merged
merged 19 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/application/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
from ansys.aedt.core.generic.constants import AEDT_UNITS
from ansys.aedt.core.generic.constants import unit_system
from ansys.aedt.core.generic.data_handlers import variation_string_to_dict
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import check_and_download_file
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import inner_project_settings
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/application/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from ansys.aedt.core.generic.constants import SI_UNITS
from ansys.aedt.core.generic.constants import _resolve_unit_system
from ansys.aedt.core.generic.constants import unit_system
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import check_numeric_equivalence
from ansys.aedt.core.generic.general_methods import is_array
from ansys.aedt.core.generic.general_methods import is_number
Expand Down
71 changes: 71 additions & 0 deletions src/ansys/aedt/core/generic/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Provides functions for performing common checks."""

from ansys.aedt.core.generic.errors import AEDTRuntimeError


def min_aedt_version(min_version: str):
"""Compare a minimum required version to the current AEDT version.

This decorator should only be used on methods where the associated object can reach the desktop instance.
Otherwise, there is no way to check version compatibility and an error is raised.

Parameters
----------
min_version: str
Minimum AEDT version required by the method.
The value should follow the format YEAR.RELEASE, for example '2024.2'.

Raises
------

AEDTRuntimeError
If the method version is higher than the AEDT version.
"""

def aedt_version_decorator(method):
def wrapper(self, *args, **kwargs):
attributes_to_check = ["odesktop", "_odesktop", "_desktop"]
# Browse attributes and retrieve the first non-null value
available_attributes = [attr for attr in attributes_to_check if hasattr(self, attr)]
if not available_attributes:
raise AEDTRuntimeError("The desktop is not available.")
odesktop = next(
(getattr(self, attr) for attr in available_attributes if getattr(self, attr) is not None), None
)
if odesktop is not None:
desktop_version = odesktop.GetVersion()
if desktop_version < min_version:
raise AEDTRuntimeError(
f"The method '{method.__name__}' requires a minimum Ansys release version of "
+ f"{min_version}, but the current version used is {desktop_version}."
)
else:
return method(self, *args, **kwargs)

return wrapper

return aedt_version_decorator
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ansys.aedt.core import __version__
from ansys.aedt.core.application.variables import decompose_variable_value
from ansys.aedt.core.generic.data_handlers import _arg2dict
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import generate_unique_folder_name
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import open_file
Expand Down
37 changes: 37 additions & 0 deletions src/ansys/aedt/core/generic/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Provide PyAEDT errors."""


class GrpcApiError(RuntimeError):
"""Exception raised for errors encountered while interacting with the gRPC API."""


class MethodNotSupportedError(RuntimeError):
"""Exception raised when attempting to call a method that is not supported."""


class AEDTRuntimeError(RuntimeError):
"""Exception raised for errors occurring during the runtime execution of AEDT scripts."""
14 changes: 2 additions & 12 deletions src/ansys/aedt/core/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
from ansys.aedt.core.aedt_logger import pyaedt_logger
from ansys.aedt.core.generic.aedt_versions import aedt_versions
from ansys.aedt.core.generic.constants import CSS4_COLORS
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.errors import MethodNotSupportedError
from ansys.aedt.core.generic.settings import inner_project_settings # noqa: F401
from ansys.aedt.core.generic.settings import settings
import psutil
Expand All @@ -67,18 +69,6 @@
]


class GrpcApiError(Exception):
""" """

pass


class MethodNotSupportedError(Exception):
""" """

pass


def _write_mes(mes_text):
mes_text = str(mes_text)
parts = [mes_text[i : i + 250] for i in range(0, len(mes_text), 250)]
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/generic/grpc_plugin_dll_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import time
import types

from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import _retry_ntimes
from ansys.aedt.core.generic.general_methods import inclusion_list
from ansys.aedt.core.generic.general_methods import settings
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/icepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from ansys.aedt.core.generic.data_handlers import _arg2dict
from ansys.aedt.core.generic.data_handlers import _dict2arg
from ansys.aedt.core.generic.data_handlers import random_string
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import open_file
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modeler/modeler_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import warnings

from ansys.aedt.core.application.variables import generate_validation_errors
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError

Check warning on line 34 in src/ansys/aedt/core/modeler/modeler_3d.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/modeler/modeler_3d.py#L34

Added line #L34 was not covered by tests
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import open_file
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modules/boundary/layout_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from ansys.aedt.core.generic.data_handlers import _dict2arg
from ansys.aedt.core.generic.data_handlers import random_string
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
from ansys.aedt.core.modeler.cad.elements_3d import BinaryTreeNode
from ansys.aedt.core.modules.boundary.common import BoundaryCommon
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modules/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from ansys.aedt.core.application.design_solutions import model_names
from ansys.aedt.core.generic.data_handlers import _dict2arg
from ansys.aedt.core.generic.general_methods import MethodNotSupportedError
from ansys.aedt.core.generic.errors import MethodNotSupportedError
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
from ansys.aedt.core.generic.general_methods import settings
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modules/mesh_icepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import warnings

from ansys.aedt.core.generic.data_handlers import _dict2arg
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError
from ansys.aedt.core.generic.general_methods import _dim_arg
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/visualization/post/field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from ansys.aedt.core.generic.constants import AllowedMarkers
from ansys.aedt.core.generic.constants import EnumUnits
from ansys.aedt.core.generic.data_handlers import _dict2arg
from ansys.aedt.core.generic.general_methods import GrpcApiError
from ansys.aedt.core.generic.errors import GrpcApiError

Check warning on line 37 in src/ansys/aedt/core/visualization/post/field_data.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/visualization/post/field_data.py#L37

Added line #L37 was not covered by tests
from ansys.aedt.core.generic.general_methods import check_and_download_file
from ansys.aedt.core.generic.general_methods import open_file
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@

import logging
import os
import time
from unittest.mock import MagicMock
from unittest.mock import PropertyMock
from unittest.mock import patch

from ansys.aedt.core.generic.checks import AEDTRuntimeError
from ansys.aedt.core.generic.checks import min_aedt_version
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
from ansys.aedt.core.generic.settings import ALLOWED_AEDT_ENV_VAR_SETTINGS
from ansys.aedt.core.generic.settings import ALLOWED_GENERAL_SETTINGS
Expand Down Expand Up @@ -266,3 +269,58 @@ def test_write_toml(tmp_path):
_create_toml_file(TOML_DATA, file_path)

assert file_path.exists()


CURRENT_YEAR = current_year = time.localtime().tm_year
CURRENT_YEAR_VERSION = f"{CURRENT_YEAR}.2"
NEXT_YEAR_VERSION = f"{CURRENT_YEAR + 1}.2"
PREVIOUS_YEAR_VERSION = f"{CURRENT_YEAR - 1}.2"


def test_min_aedt_version_success():
class Dummy:
"""Dummy class to test min version."""

odesktop = MagicMock()
odesktop.GetVersion.return_value = CURRENT_YEAR_VERSION

@min_aedt_version(PREVIOUS_YEAR_VERSION)
def old_method(self):
pass

dummy = Dummy()

SMoraisAnsys marked this conversation as resolved.
Show resolved Hide resolved
dummy.old_method()


def test_min_aedt_version_raise_error_on_future_version():
class Dummy:
"""Dummy class to test min version."""

odesktop = MagicMock()
odesktop.GetVersion.return_value = CURRENT_YEAR_VERSION

@min_aedt_version(NEXT_YEAR_VERSION)
def future_method(self):
pass

dummy = Dummy()
pattern = (
f"The method 'future_method' requires a minimum Ansys release version of {NEXT_YEAR_VERSION}, "
"but the current version used is .+"
)

with pytest.raises(AEDTRuntimeError, match=pattern):
dummy.future_method()


def test_min_aedt_version_raise_error_on_non_decorable_object():
class Dummy:
@min_aedt_version(PREVIOUS_YEAR_VERSION)
def dummy_method(self):
pass

dummy = Dummy()

with pytest.raises(AEDTRuntimeError, match="The desktop is not available."):
dummy.dummy_method()
Loading