Skip to content

FEAT: Add license_manager #1118

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
1 change: 1 addition & 0 deletions doc/changelog.d/1118.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add license_manager
7 changes: 7 additions & 0 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
@@ -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 @@
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 @@
self._messages = MessageManager(self._app)
return self._messages

@property
def license_manager(self):
"""Return license manager."""
return self._license_manager

Check warning on line 493 in src/ansys/mechanical/core/embedding/app.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L493

Added line #L493 was not covered by tests

def _share(self, other) -> None:
"""Shares the state of self with other.

97 changes: 97 additions & 0 deletions src/ansys/mechanical/core/embedding/license_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# 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:
LOG.warning("License manager is only available in Ansys 2025 R2 and later.")
return

self._license_preference = self._app.ExtAPI.Application.LicensePreference

Check warning on line 39 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L39

Added line #L39 was not covered by tests

def _get_all_licenses(self):
"""Return all active licenses."""
return self._license_preference.GetAllLicenses()

Check warning on line 43 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L43

Added line #L43 was not covered by tests

def get_license_status(self, license_name: str):
"""Return the status of a license."""
return self._license_preference.GetLicenseStatus(license_name)

Check warning on line 47 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L47

Added line #L47 was not covered by tests

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.")

Check warning on line 54 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L51-L54

Added lines #L51 - L54 were not covered by tests
else:
self._license_preference.SetLicenseStatus(license_name, _status.Disabled)
LOG.info(f"{license_name} is disabled.")

Check warning on line 57 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L56-L57

Added lines #L56 - L57 were not covered by tests

def show(self):
"""Print all active licenses."""
for lic in self._get_all_licenses():
print(f"{lic} - {self.get_license_status(lic)}")

Check warning on line 62 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L61-L62

Added lines #L61 - L62 were not covered by tests

def activate(self):
"""Activate a license."""
self._license_preference.ActivateLicense()

Check warning on line 66 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L66

Added line #L66 was not covered by tests

def deactivate(self):
"""Deactivate a license."""
self._license_preference.DeActivateLicense()

Check warning on line 70 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L70

Added line #L70 was not covered by tests

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()

Check warning on line 93 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L91-L93

Added lines #L91 - L93 were not covered by tests

def reset(self):
"""Reset the license preference."""
self._license_preference.Reset()

Check warning on line 97 in src/ansys/mechanical/core/embedding/license_manager.py

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/license_manager.py#L97

Added line #L97 was not covered by tests
Loading