From e2ddca9929f0e2ee9fd83724af88a3ef62c69dbf Mon Sep 17 00:00:00 2001 From: dkunhamb <dipin.kunhambunair@ansys.com> Date: Fri, 7 Mar 2025 15:20:57 -0600 Subject: [PATCH 1/3] add license_manager --- src/ansys/mechanical/core/embedding/app.py | 7 ++ .../core/embedding/license_manager.py | 96 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/ansys/mechanical/core/embedding/license_manager.py diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index e6339b70a..aa53e94cd 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -35,6 +35,7 @@ from ansys.mechanical.core.embedding.addins import AddinConfiguration from ansys.mechanical.core.embedding.appdata import UniqueUserProfile from ansys.mechanical.core.embedding.imports import global_entry_points, global_variables +from ansys.mechanical.core.embedding.license_manager import LicenseManager from ansys.mechanical.core.embedding.poster import Poster from ansys.mechanical.core.embedding.ui import launch_ui from ansys.mechanical.core.embedding.warnings import connect_warnings, disconnect_warnings @@ -225,6 +226,7 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs): self._updated_scopes: typing.List[typing.Dict[str, typing.Any]] = [] self._subscribe() self._messages = None + self._license_manager = LicenseManager(self) globals = kwargs.get("globals") if globals: @@ -485,6 +487,11 @@ def messages(self): self._messages = MessageManager(self._app) return self._messages + @property + def license_manager(self): + """Return license manager.""" + return self._license_manager + def _share(self, other) -> None: """Shares the state of self with other. diff --git a/src/ansys/mechanical/core/embedding/license_manager.py b/src/ansys/mechanical/core/embedding/license_manager.py new file mode 100644 index 000000000..b23137c8f --- /dev/null +++ b/src/ansys/mechanical/core/embedding/license_manager.py @@ -0,0 +1,96 @@ +# Copyright (C) 2022 - 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. + +"""License Manager.""" +from ansys.mechanical.core import LOG + +# TODO: enable logging + + +class LicenseManager: + """Message manager for adding, fetching, and printing messages.""" + + def __init__(self, app): + """Initialize the message manager.""" + self._app = app + if self._app.version < 252: + raise ValueError("License manager is only available in Ansys 2022 R1 or later.") + + self._license_preference = self._app.ExtAPI.Application.LicensePreference + + def _get_all_licenses(self): + """Return all active licenses.""" + return self._license_preference.GetAllLicenses() + + def get_license_status(self, license_name: str): + """Return the status of a license.""" + return self._license_preference.GetLicenseStatus(license_name) + + def set_license_status(self, license_name: str, status: bool): + """Set the status of a license.""" + _status = Ansys.Mechanical.DataModel.Enums.LicenseStatus + if status: + self._license_preference.SetLicenseStatus(license_name, _status.Enabled) + LOG.info(f"{license_name} is enabled.") + else: + self._license_preference.SetLicenseStatus(license_name, _status.Disabled) + LOG.info(f"{license_name} is disabled.") + + def show(self): + """Print all active licenses.""" + for lic in self._get_all_licenses(): + print(f"{lic} - {self.get_license_status(lic)}") + + def activate(self): + """Activate a license.""" + self._license_preference.ActivateLicense() + + def deactivate(self): + """Deactivate a license.""" + self._license_preference.DeActivateLicense() + + def switch_preference(self, license_name, location): + """Switch a license preference. + + Move license to in priority order. + + Parameters + ---------- + license_name : str + License name. + location : int + Location to move the license to. + + Examples + -------- + Move Ansys Mechanical Premium to the first location. + + >>> license_manager = LicenseManager(app) + >>> license_manager.switch_preference('Ansys Mechanical Premium', 0) + """ + LOG.info(f"Switching license preference for {license_name} to location {location}") + self._license_preference.MoveLicenseToLocation(license_name, location) + self._license_preference.Save() + + def reset(self): + """Reset the license preference.""" + self._license_preference.Reset() From cdabb70e57d65c5cf6c0a4c56c6fe8b71844ddad Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 7 Mar 2025 21:39:20 +0000 Subject: [PATCH 2/3] chore: adding changelog file 1118.added.md [dependabot-skip] --- doc/changelog.d/1118.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/1118.added.md diff --git a/doc/changelog.d/1118.added.md b/doc/changelog.d/1118.added.md new file mode 100644 index 000000000..6a489ba65 --- /dev/null +++ b/doc/changelog.d/1118.added.md @@ -0,0 +1 @@ +Add license_manager \ No newline at end of file From 28626fc533f63cd325f56a9d0f9fe98f98a33190 Mon Sep 17 00:00:00 2001 From: dkunhamb <dipin.kunhambunair@ansys.com> Date: Mon, 10 Mar 2025 09:26:09 -0500 Subject: [PATCH 3/3] warning instead of error --- src/ansys/mechanical/core/embedding/license_manager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/mechanical/core/embedding/license_manager.py b/src/ansys/mechanical/core/embedding/license_manager.py index b23137c8f..3dd068224 100644 --- a/src/ansys/mechanical/core/embedding/license_manager.py +++ b/src/ansys/mechanical/core/embedding/license_manager.py @@ -33,7 +33,8 @@ def __init__(self, app): """Initialize the message manager.""" self._app = app if self._app.version < 252: - raise ValueError("License manager is only available in Ansys 2022 R1 or later.") + LOG.warning("License manager is only available in Ansys 2025 R2 and later.") + return self._license_preference = self._app.ExtAPI.Application.LicensePreference