This chapter contains the instruction notes to organise the python files of SALOME in a packaged python structure. This is the first step of the development process, whose goal is to validate the principles and show a possible way.
Contacts: Guillaume Boulant, Christian Caremoli, Renaud Barate
The main idea is to import SALOME python functions by doing:
instead of:
as it must be done up to now because of the flat organisation of python files in the installation folders of SALOME modules.
To reach this target, the files <myPythonModule>.py have to be organised in a packaged python structure where the main package is named salome, and then sub-packages could be created for each SALOME module:
The motivations of this objective are twice:
The starting point then is a python library named nepal that provides SALOME helper functions classified by modules (KERNEL,GEOM,...) and organized in a packaged python structure:
The target point is to have the salome.kernel part in the KERNEL module, the salome.geom part in the GEOM module, and so on. And with no impact on SALOME scripts that already exists (import salome, and all other stuff should be imported and work as before).
To reach this target, we have to face two problems:
The problem is solved by installing the salome.py file under the name __init__.py in a folder named ${salomepythondir}/salome.
By this operation, the ${salomepythondir}/salome directory is transformed in a python package and the instruction import salome do the same things as before this modification, without any modification of the sys.path.
When we use a SALOME virtual application, the problem is naturally solved by the fact that every sub-packages are virtually installed in the same directory, the directory ${salomepythondir}/salome containing the file __init__.py.
Nevertheless, some people doesn't use the virtual application. To get a robust configuration in any case, one can use the python namespace pattern. This consists in creating a virtual python package that aggregates all the sub-packages.
Technically speaking, this consists in implementing in the file ${salomepythondir}/salome/__init__.py (new version of salome.py) a function that automatically extend the __path__ variable with sub-packages that can be found in SALOME modules installation paths. The code looks something like that:
Due to the specific above choices, the apply_gen utility must be modified so that the sub-folder salome in ${salomepythondir} is not generated as a symbolic link any longer but as a real folder containing symbolic links towards the module specific python sub-packages (kernel, geom, smesh, ...) and to the single file __init__.py provided by the KERNEL module.
This adaptation can be done in the virtual_salome.py script.
Do nothing at this step, it works fine because the files are installed in a path included in the sys.path.
In a future version, it should be nice to reverse all the python files of the KERNEL library in this packaged structure. But this can't be done without impact on existing python user scripts.
Considering the elements described above, a procedure that works to get the packaged python structure is:
The naming convention for source folder is here the convention in place in the KERNEL module: the source code of the python packages of a SALOME module <MODULE_NAME> is located in the source directory <srcdir>/src/<MODULE_NAME>_PY.
Note also that all python files that were existing in the KERNEL module are leaft untouched but the file salome.py.
One special point for the documentation:
The instructions above provides you with a SALOME application whose modules embed there dedicated python packages. This installation can can be tested using some test use cases. For example, the visualisation of structural elements (provided by the package salome.geom can be tested by:
This can be enter in the GUI python console or in a python interpreter executed in a SALOME session.
For more details, read the API documentation in <installdir>/share/doc/salome/gui/KERNEL.