Skip to content

Commit

Permalink
UGLY-ENH: Fix python wrap conditionals
Browse files Browse the repository at this point in the history
This introduces `Slicer_USE_PYTHONQT` as a variable to consider for
python wrapping in `Base` and `Libs` components. This should probably be
merged with the developments on Slicer#6895 and with the
modernization of the python detection in earlier Systole patches
  • Loading branch information
RafaelPalomar committed Mar 25, 2023
1 parent b731247 commit 7b56842
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Base/Logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endif()
# VTK
#
if(Slicer_USE_PYTHONQT AND NOT VTK_WRAP_PYTHON)
message(FATAL_ERROR "Since Slicer_USE_PYTHONQT is ON, VTK_WRAP_PYTHON is expected to be ON. "
message(FATAL_ERROR "Since Slicer_USE_PYTHONQT is ON, Slicer_USE_PYTHONQT AND VTK_WRAP_PYTHON is expected to be ON. "
"Re-configure VTK with python wrapping.")
endif()

Expand Down Expand Up @@ -196,7 +196,7 @@ install(TARGETS ${lib_name}
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT AND VTK_WRAP_PYTHON)
include(vtkMacroKitPythonWrap)

set(_python_wrapped_libraries
Expand Down
4 changes: 2 additions & 2 deletions Base/QTCLI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ set(KIT_target_libraries
ModuleDescriptionParser ${ITK_LIBRARIES}
MRMLCLI
)
if(VTK_WRAP_PYTHON AND ${VTK_VERSION} VERSION_GREATER_EQUAL "8.90")
if(Slicer_USE_PYTHONQT AND VTK_WRAP_PYTHON AND ${VTK_VERSION} VERSION_GREATER_EQUAL "8.90")
# HACK Explicitly list transitive VTK dependencies because _get_dependencies_recurse
# used in vtkAddon/CMake/vtkMacroKitPythonWrap.cmake only recurses over dependencies
# that are VTK python wrapped.
Expand Down Expand Up @@ -149,7 +149,7 @@ endif()
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT AND VTK_WRAP_PYTHON)
include(vtkMacroKitPythonWrap)

set(_python_wrapped_libraries
Expand Down
9 changes: 7 additions & 2 deletions Base/QTCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ if(Slicer_USE_PYTHONQT)

endif()

set(WRAP_PYTHONQT)
if(Slicer_USE_PYTHONQT)
set(WRAP_PYTHONQT WRAP_PYTHONQT)
endif()

SlicerMacroBuildBaseQtLibrary(
NAME ${PROJECT_NAME}
EXPORT_DIRECTIVE ${KIT_export_directive}
Expand All @@ -274,10 +279,10 @@ SlicerMacroBuildBaseQtLibrary(
MOC_SRCS ${KIT_MOC_SRCS}
TARGET_LIBRARIES ${KIT_target_libraries}
RESOURCES ${KIT_resources}
WRAP_PYTHONQT
${WRAP_PYTHONQT}
)

if(Slicer_USE_PYTHONQT_WITH_OPENSSL)
if(Slicer_USE_PYTHONQT AND Slicer_USE_PYTHONQT_WITH_OPENSSL)
configure_file(
Resources/Certs/Slicer.crt
${Slicer_BINARY_DIR}/${Slicer_SHARE_DIR}/Slicer.crt
Expand Down
87 changes: 42 additions & 45 deletions Base/QTCore/qSlicerLoadableModuleFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,61 @@
//-----------------------------------------------------------------------------
qSlicerLoadableModuleFactoryItem::qSlicerLoadableModuleFactoryItem() = default;


#ifdef Slicer_USE_PYTHONQT
#include "qSlicerCorePythonManager.h"

namespace
{

//-----------------------------------------------------------------------------
bool importModulePythonExtensions(
qSlicerCorePythonManager * pythonManager,
const QString& intDir,const QString& modulePath,
bool isEmbedded)
{
Q_UNUSED(intDir);
#ifdef Slicer_USE_PYTHONQT
if(!pythonManager)
{
return false;
}
//-----------------------------------------------------------------------------
bool importModulePythonExtensions(
qSlicerCorePythonManager * pythonManager,
const QString& intDir,const QString& modulePath,
bool isEmbedded)
{
Q_UNUSED(intDir);
if(!pythonManager)
{
return false;
}

QString pythonModuleDir = QFileInfo(modulePath).absoluteFilePath();
if (!QFileInfo(pythonModuleDir).isDir())
{
pythonModuleDir = QFileInfo(pythonModuleDir).absolutePath();
}
QString pythonModuleDir = QFileInfo(modulePath).absoluteFilePath();
if (!QFileInfo(pythonModuleDir).isDir())
{
pythonModuleDir = QFileInfo(pythonModuleDir).absolutePath();
}

// Update current application directory, so that *PythonD modules can be loaded
ctkScopedCurrentDir scopedCurrentDir(pythonModuleDir);
// Update current application directory, so that *PythonD modules can be loaded
ctkScopedCurrentDir scopedCurrentDir(pythonModuleDir);

if (!isEmbedded)
{
QStringList paths; paths << scopedCurrentDir.currentPath();
pythonManager->appendPythonPaths(paths);
}
if (!isEmbedded)
{
QStringList paths; paths << scopedCurrentDir.currentPath();
pythonManager->appendPythonPaths(paths);
}

pythonManager->executeString(QString(
"from slicer.util import importVTKClassesFromDirectory;"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleLogicPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleMRMLPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleMRMLDisplayableManagerPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleVTKWidgetsPython.*');"
).arg(qSlicerCorePythonManager::toPythonStringLiteral(scopedCurrentDir.currentPath())));
pythonManager->executeString(QString(
"from slicer.util import importQtClassesFromDirectory;"
"importQtClassesFromDirectory(%1, 'slicer', filematch='qSlicer*PythonQt.*');"
).arg(qSlicerCorePythonManager::toPythonStringLiteral(scopedCurrentDir.currentPath())));
return !pythonManager->pythonErrorOccured();
#else
Q_UNUSED(isEmbedded);
Q_UNUSED(modulePath);
Q_UNUSED(pythonManager);
return false;
#endif
}
}
pythonManager->executeString(QString(
"from slicer.util import importVTKClassesFromDirectory;"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleLogicPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleMRMLPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleMRMLDisplayableManagerPython.*');"
"importVTKClassesFromDirectory(%1, 'slicer', filematch='vtkSlicer*ModuleVTKWidgetsPython.*');"
).arg(qSlicerCorePythonManager::toPythonStringLiteral(scopedCurrentDir.currentPath())));
pythonManager->executeString(QString(
"from slicer.util import importQtClassesFromDirectory;"
"importQtClassesFromDirectory(%1, 'slicer', filematch='qSlicer*PythonQt.*');"
).arg(qSlicerCorePythonManager::toPythonStringLiteral(scopedCurrentDir.currentPath())));
return !pythonManager->pythonErrorOccured();
}

}
#endif
//-----------------------------------------------------------------------------
qSlicerAbstractCoreModule* qSlicerLoadableModuleFactoryItem::instanciator()
{
qSlicerAbstractCoreModule * module =
ctkFactoryPluginItem<qSlicerAbstractCoreModule>::instanciator();
ctkFactoryPluginItem<qSlicerAbstractCoreModule>::instanciator();
module->setPath(this->path());

qSlicerCoreApplication * app = qSlicerCoreApplication::application();
Expand Down
2 changes: 1 addition & 1 deletion Libs/MRML/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ install(TARGETS ${lib_name}
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT)
vtkMacroKitPythonWrap(
KIT_NAME ${lib_name}
KIT_SRCS ${MRMLCore_SRCS}
Expand Down
2 changes: 1 addition & 1 deletion Libs/MRML/Logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ install(TARGETS ${lib_name}
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT AND VTK_WRAP_PYTHON)
include(vtkMacroKitPythonWrap)

set(_python_wrapped_libraries)
Expand Down
2 changes: 1 addition & 1 deletion Libs/vtkITK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ install(TARGETS ${lib_name}
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT)
vtkMacroKitPythonWrap(
KIT_NAME ${lib_name}
KIT_SRCS ${vtkITK_SRCS}
Expand Down
2 changes: 1 addition & 1 deletion Libs/vtkSegmentationCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ install(TARGETS ${PROJECT_NAME}
# --------------------------------------------------------------------------
# Python wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON AND BUILD_SHARED_LIBS)
if(Slicer_USE_PYTHONQT AND BUILD_SHARED_LIBS)

set(Slicer_Wrapped_LIBRARIES
)
Expand Down
4 changes: 2 additions & 2 deletions Libs/vtkTeem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ include(${Teem_USE_FILE})
# See https://sourceforge.net/p/teem/code/4168/
set(Teem_LIBRARIES teem)

if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT)
find_package(vtkAddon REQUIRED)
endif()

Expand Down Expand Up @@ -150,7 +150,7 @@ install(TARGETS ${lib_name}
# --------------------------------------------------------------------------
# Python Wrapping
# --------------------------------------------------------------------------
if(VTK_WRAP_PYTHON)
if(Slicer_USE_PYTHONQT)
include(vtkMacroKitPythonWrap)

vtkMacroKitPythonWrap(
Expand Down

0 comments on commit 7b56842

Please sign in to comment.