Acquiring and Removing Licences
It can be a nuisance acquiring and removing licences manually every time you want to run some external scripts. Unique to the mastapy
package is the masta_licences
decorator and context manager. This is a simple and intuitive way of acquiring and removing licences in your external scripts, allowing you to restrict licence usage to the parts of your script that require them. It has been designed to be as flexible as possible to fit a variety of different use cases. The following examples demonstrate its functionality.
Decorator Usage
The easiest way to use masta_licences
is as a decorator. In this example, we decorate a method called my_method
and acquire 4 different licences. We also set the ip
argument to update the licence server settings to point towards the correct licence server. By using the decorator, licences are only acquired when execution enters my_method
. The licences are then removed once execution exits my_method
.
from mastapy import masta_licences
@masta_licences('MC101', 'MC900', 'MC109', 'MA103', ip='my-licence-server')
def my_method():
...
my_method()
Context Manager Usage
You can also use masta_licences
as a context manager. This allows you to acquire and remove licences anywhere in your code, not just when calling a function. The following example acquires a single licence. When execution enters the with
block the licence will be acquired. When execution leaves the with
block then the licence will be removed.
from mastapy import masta_licences
with masta_licences('MC101'):
...
In this example we do not set any keyword arguments. This means the licence server settings will not be updated.
Updating Licence Server Settings
You may want to configure your licence server settings in your script. As demonstrated by the Decorator Usage example this is possible by setting keyword arguments in masta_licences
. However, you may want to configure the licence server once and use masta_licences
many times.
In this example we create a LicenceServerDetails
object and set the relevant properties. We can then pass the LicenceServerDetails
object to the masta_licences
context manager using the server_details
keyword argument.
from mastapy import masta_licences
from mastapy.licensing import LicenceServerDetails
details = LicenceServerDetails()
details.ip = 'my-licence-server'
details.port = 5053
with masta_licences('M101', server_details=details):
...
It is also possible to update the server settings by using LicenceServer.update_server_settings
instead.
from mastapy import masta_licences
from mastapy.licensing import LicenceServer, LicenceServerDetails
details = LicenceServerDetails()
details.ip = 'my-licence-server'
details.port = 5053
LicenceServer.update_server_settings(details)
with masta_licences('M101'):
...