|
| 1 | +# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +"""License Manager.""" |
| 24 | +from typing import List, Optional, Union |
| 25 | + |
| 26 | +from ansys.mechanical.core import LOG |
| 27 | + |
| 28 | + |
| 29 | +class LicenseManager: |
| 30 | + """Class to manage licenses in Ansys Mechanical. |
| 31 | +
|
| 32 | + This class provides methods to enable, disable, and check the status of licenses. |
| 33 | + It also allows for moving licenses to specific indices in the license preference list. |
| 34 | + It is initialized with an instance of the Ansys Mechanical application. |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, app): |
| 38 | + """Initialize the message manager.""" |
| 39 | + self._app = app |
| 40 | + self._license_preference = self._app.ExtAPI.Application.LicensePreference |
| 41 | + import Ansys |
| 42 | + |
| 43 | + self._license_status = Ansys.Mechanical.DataModel.Enums.LicenseStatus |
| 44 | + |
| 45 | + def get_all_licenses(self) -> list[str]: |
| 46 | + """Return list of all licenses.""" |
| 47 | + return self._license_preference.GetAllLicenses() |
| 48 | + |
| 49 | + def get_license_status( |
| 50 | + self, license_name: str |
| 51 | + ) -> "Ansys.Mechanical.DataModel.Enums.LicenseStatus": |
| 52 | + """Return status of the specific license. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + license_name : str |
| 57 | + Name of the license to check. |
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + "Ansys.Mechanical.DataModel.Enums.LicenseStatus" |
| 62 | + The status of the license. |
| 63 | + """ |
| 64 | + LOG.info( |
| 65 | + f"{license_name} status: {self._license_preference.GetLicenseStatus(license_name)}" |
| 66 | + ) |
| 67 | + return self._license_preference.GetLicenseStatus(license_name) |
| 68 | + |
| 69 | + def set_license_status(self, license_name: str, status: bool) -> None: |
| 70 | + """Set the status of a license and save the preference. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + license_name : str |
| 75 | + Name of the license to set the status for. |
| 76 | + status : bool |
| 77 | + True to enable the license, False to disable it. |
| 78 | + """ |
| 79 | + if status: |
| 80 | + self._license_preference.SetLicenseStatus(license_name, self._license_status.Enabled) |
| 81 | + LOG.info(f"{license_name} is enabled.") |
| 82 | + else: |
| 83 | + self._license_preference.SetLicenseStatus(license_name, self._license_status.Disabled) |
| 84 | + LOG.info(f"{license_name} is disabled.") |
| 85 | + self._license_preference.Save() |
| 86 | + |
| 87 | + def show(self) -> None: |
| 88 | + """Print all active licenses.""" |
| 89 | + for lic in self.get_all_licenses(): |
| 90 | + print(f"{lic} - {self._license_preference.GetLicenseStatus(lic)}") |
| 91 | + |
| 92 | + def disable_session_license(self) -> None: |
| 93 | + """Disable all licenses for current session.""" |
| 94 | + self._license_preference.DeActivateLicense() |
| 95 | + |
| 96 | + def enable_session_license(self, license: Optional[Union[str, List[str]]] = None) -> None: |
| 97 | + """Enable license(s) for the current session. |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + license : Optional[Union[str, List[str]]], optional |
| 102 | + If None, activates the first enabled license in the priority order. |
| 103 | + If a string, activates that specific license. |
| 104 | + If a list of strings, activates all specified licenses in the order provided. |
| 105 | + """ |
| 106 | + from System import String |
| 107 | + from System.Collections.Generic import List as DotNetList |
| 108 | + |
| 109 | + if license is None: |
| 110 | + self._license_preference.ActivateLicense() |
| 111 | + elif isinstance(license, str): |
| 112 | + self._license_preference.ActivateLicense(String(license)) |
| 113 | + elif isinstance(license, list): |
| 114 | + licenses = DotNetList[String]() |
| 115 | + for lic in license: |
| 116 | + licenses.Add(String(lic)) |
| 117 | + self._license_preference.ActivateLicense(licenses) |
| 118 | + else: |
| 119 | + raise TypeError("License must be None, a string, or a list of strings.") |
| 120 | + LOG.info(f"License(s) {license} enabled for the current session.") |
| 121 | + |
| 122 | + def move_to_index(self, license_name: str, location: int) -> None: |
| 123 | + """Move a license preference. |
| 124 | +
|
| 125 | + Move license to zero-based index location in the license preference list. |
| 126 | + This is useful for setting the preferred license location. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + license_name : str |
| 131 | + License name. |
| 132 | + location : int |
| 133 | + Location to move the license to. |
| 134 | +
|
| 135 | + Examples |
| 136 | + -------- |
| 137 | + Move Ansys Mechanical Premium to the first location. |
| 138 | +
|
| 139 | + >>> license_manager = LicenseManager(app) |
| 140 | + >>> license_manager.move_to_index('Ansys Mechanical Premium', 0) |
| 141 | + """ |
| 142 | + LOG.info(f"Moving license preference for {license_name} to location {location}") |
| 143 | + self._license_preference.MoveLicenseToLocation(license_name, location) |
| 144 | + self._license_preference.Save() |
| 145 | + |
| 146 | + def reset_preference(self) -> None: |
| 147 | + """Reset the license preference. |
| 148 | +
|
| 149 | + This method will reset the license location order and the status of all licenses |
| 150 | + to the default state. |
| 151 | + """ |
| 152 | + LOG.info("Resetting license preferences to default state.") |
| 153 | + self._license_preference.Reset() |
0 commit comments