-
Notifications
You must be signed in to change notification settings - Fork 0
Python Code Snippets
Xyna 10.1.0.0 introduces python code snippets as an alternative to java implementations for coded services.
https://github.com/ninia/jep?tab=readme-ov-file#dependencies
Before you can use python code snippets, you need to
- install python
- (optional) create and activate a python virtual environment
- install jep (https://github.com/Xyna-Factory/xyna/wiki/Python-Code-Snippets):
pip3 install jep
There are two properties stored in /etc/opt/xyna/environment/black_edition_001.properties that are required for python code snippets to work:
- python.venv.path - path to the python virtual environment in which jep is installed
- jep.module.path - path to the jep native library (/libjep.so) If you do not want to use a virtual environment, you can leave python.venv.path blank. While installing xyna prerequisites, you will be prompted to set these properties. If a python virtual environment is active, it will be used to determine the relevant paths. Otherwise you can edit the values in black_edition_001.properties. They take effect after restarting the xyna factory.
- If you set the properties manually, you also need to comment in the load library runtime permission of the jep native library in server/server.policy. It should look like
permission java.lang.RuntimePermission "loadLibrary./etc/opt/xyna/environment/venv/lib/python3.9/site-packages/jep/libjep.so";
.
Like java code snippets, you can open a datatype in the modeller and select "Coded Service Python". Use the edit window to write the service implementation in python. You can access all modeled xyna datatypes and exceptions by typing "mdm." followed by their fully qualified name substituting "." with "_". For example, you can instantiate a new base.Text variable like this: myText = mdm.base_Text()
Only the parameterless constructor is available. To set a member variable, you can use the point notation: myText.text = "Hello World"
. There are neither getter nor setter methods. Instead of mdm.base_Text
you might want to create a type alias to shorten the type name: from mdm import base_Text as Text
. Afterwards, you can create a new base.Text object with myText = Text()
.
Every service of available datatypes can be called from a python code snippet. The syntax is similar to java: <instance>.<serviceName>(<input>)
for instance services or <class name>.<serviceName>(<input>)
for static services. Calling a different service will always result in a return to the java environment resulting in a performance penalty. To call a different method on the same datatype directly in python without returning to the java environment, use a service project.
Like java code snippets, it is possible to export a service project for python and implement services there, instead of inline. After filling the service signature, press the template call button to fill the code snippet with the call to the service project. Afterwards, click the 'Download Python Template' Button on the left side of the screen. A zip file will be downloaded containing an implementation file for your datatype, as well as an mdm.py with all available xyna objects. All service implementations in mdm.py are empty. It is only used to facilitate intellisense in an IDE like vscode. Once you implemented the services, upload your files using the '+' icon next to 'Python Libraries'. Do not upload the mdm.py.
You can upload additional python scripts like the service project discussed above. Definitions inside these scripts can be imported in the code snippet.
These steps might help to identify problems with python code snippet configuration:
- make sure the properties python.venv.path and jep.module.path in /etc/opt/xyna/environment/black_edition_001.properties are correct
- configure the logger 'com.gip.xyna.xprc.xfractwfe.python.JepInterpreterFactory' to 'debug' by adding this line to log4j2.xml in the server folder:
<Logger name="com.gip.xyna.xprc.xfractwfe.python.JepInterpreterFactory" level="debug" />
. During factory startup, you should see the JepInterpreterFactory reporting the jep library path. - check the xyna process. The process should contain a reference to the jep native library:
ps -aux | grep jep
- check the server.policy file. It should contain a runtime permission to load the jep native library.