Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve scripted classes readability renaming setPythonSource parameter and associated internal variable #7352

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions Base/Logic/vtkSlicerScriptedLoadableModuleLogic.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class vtkSlicerScriptedLoadableModuleLogic::vtkInternal
// static int APIMethodCount;
// static const char * APIMethodNames[2];

std::string PythonSource;
std::string PythonSourceFilePath;
PyObject * PythonSelf;
// PyObject * PythonAPIMethods[2];
};
Expand Down Expand Up @@ -177,16 +177,16 @@ void vtkSlicerScriptedLoadableModuleLogic::PrintSelf(ostream& os, vtkIndent inde
//}

//---------------------------------------------------------------------------
bool vtkSlicerScriptedLoadableModuleLogic::SetPythonSource(const std::string& pythonSource)
bool vtkSlicerScriptedLoadableModuleLogic::SetPythonSource(const std::string& filePath)
{
if(pythonSource.find(".py") == std::string::npos &&
pythonSource.find(".pyc") == std::string::npos)
if(filePath.find(".py") == std::string::npos &&
filePath.find(".pyc") == std::string::npos)
{
return false;
}

// Extract filename - It should match the associated python class
std::string className = vtksys::SystemTools::GetFilenameWithoutExtension(pythonSource);
std::string className = vtksys::SystemTools::GetFilenameWithoutExtension(filePath);
className+= "Logic";
//std::cout << "SetPythonSource - className:" << className << std::endl;

Expand All @@ -199,24 +199,24 @@ bool vtkSlicerScriptedLoadableModuleLogic::SetPythonSource(const std::string& py
if (!classToInstantiate)
{
PyObject * pyRes = nullptr;
if (pythonSource.find(".pyc") != std::string::npos)
if (filePath.find(".pyc") != std::string::npos)
{
std::string pyRunStr = std::string("with open('") + pythonSource +
std::string("', 'rb') as f:import imp;imp.load_module('__main__', f, '") + pythonSource +
std::string pyRunStr = std::string("with open('") + filePath +
std::string("', 'rb') as f:import imp;imp.load_module('__main__', f, '") + filePath +
std::string("', ('.pyc', 'rb', 2))");
pyRes = PyRun_String(
pyRunStr.c_str(),
Py_file_input, global_dict, global_dict);
}
else if (pythonSource.find(".py") != std::string::npos)
else if (filePath.find(".py") != std::string::npos)
{
std::string pyRunStr = std::string("execfile('") + pythonSource + std::string("')");
std::string pyRunStr = std::string("execfile('") + filePath + std::string("')");
pyRes = PyRun_String(pyRunStr.c_str(),
Py_file_input, global_dict, global_dict);
}
if (!pyRes)
{
vtkErrorMacro(<< "setPythonSource - Failed to execute file" << pythonSource << "!");
vtkErrorMacro(<< "setPythonSource - Failed to execute file" << filePath << "!");
return false;
}
Py_DECREF(pyRes);
Expand All @@ -225,7 +225,7 @@ bool vtkSlicerScriptedLoadableModuleLogic::SetPythonSource(const std::string& py
if (!classToInstantiate)
{
vtkErrorMacro(<< "SetPythonSource - Failed to load displayable manager class definition from "
<< pythonSource);
<< filePath);
return false;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ bool vtkSlicerScriptedLoadableModuleLogic::SetPythonSource(const std::string& py

//std::cout << "self (" << className << ", instance:" << self << ")" << std::endl;

this->Internal->PythonSource = pythonSource;
this->Internal->PythonSourceFilePath = filePath;
this->Internal->PythonSelf = self;

return true;
Expand Down
2 changes: 1 addition & 1 deletion Base/Logic/vtkSlicerScriptedLoadableModuleLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class VTK_SLICER_BASE_LOGIC_EXPORT vtkSlicerScriptedLoadableModuleLogic :
vtkTypeMacro(vtkSlicerScriptedLoadableModuleLogic, vtkSlicerModuleLogic);
void PrintSelf(ostream& os, vtkIndent indent) override;

bool SetPythonSource(const std::string& pythonSource);
bool SetPythonSource(const std::string& filePath);

protected:

Expand Down
10 changes: 5 additions & 5 deletions Base/QTCore/qSlicerScriptedFileReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ QString qSlicerScriptedFileReader::pythonSource()const
}

//-----------------------------------------------------------------------------
bool qSlicerScriptedFileReader::setPythonSource(const QString& newPythonSource, const QString& _className, bool missingClassIsExpected)
bool qSlicerScriptedFileReader::setPythonSource(const QString& filePath, const QString& _className, bool missingClassIsExpected)
{
Q_D(qSlicerScriptedFileReader);

Expand All @@ -103,13 +103,13 @@ bool qSlicerScriptedFileReader::setPythonSource(const QString& newPythonSource,
return false;
}

if(!newPythonSource.endsWith(".py") && !newPythonSource.endsWith(".pyc"))
if(!filePath.endsWith(".py") && !filePath.endsWith(".pyc"))
{
return false;
}

// Extract moduleName from the provided filename
QString moduleName = QFileInfo(newPythonSource).baseName();
QString moduleName = QFileInfo(filePath).baseName();

QString className = _className;
if (className.isEmpty())
Expand Down Expand Up @@ -144,7 +144,7 @@ bool qSlicerScriptedFileReader::setPythonSource(const QString& newPythonSource,
PyErr_SetString(PyExc_RuntimeError,
QString("qSlicerScriptedFileReader::setPythonSource - "
"Failed to load scripted file Reader: "
"class %1 was not found in file %2").arg(className).arg(newPythonSource).toUtf8());
"class %1 was not found in file %2").arg(className).arg(filePath).toUtf8());
return false;
}

Expand All @@ -154,7 +154,7 @@ bool qSlicerScriptedFileReader::setPythonSource(const QString& newPythonSource,
return false;
}

d->PythonSource = newPythonSource;
d->PythonSource = filePath;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Base/QTCore/qSlicerScriptedFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Q_SLICER_BASE_QTCORE_EXPORT qSlicerScriptedFileReader

/// \warning Setting the source is a no-op. See detailed comment in the source code.
/// If missingClassIsExpected is true (default) then missing class is expected and not treated as an error.
bool setPythonSource(const QString& newPythonSource, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);
bool setPythonSource(const QString& filePath, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);

/// Convenience method allowing to retrieve the associated scripted instance
Q_INVOKABLE PyObject* self() const;
Expand Down
10 changes: 5 additions & 5 deletions Base/QTCore/qSlicerScriptedFileWriter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ QString qSlicerScriptedFileWriter::pythonSource()const
}

//-----------------------------------------------------------------------------
bool qSlicerScriptedFileWriter::setPythonSource(const QString& newPythonSource, const QString& _className, bool missingClassIsExpected)
bool qSlicerScriptedFileWriter::setPythonSource(const QString& filePath, const QString& _className, bool missingClassIsExpected)
{
Q_D(qSlicerScriptedFileWriter);

Expand All @@ -103,13 +103,13 @@ bool qSlicerScriptedFileWriter::setPythonSource(const QString& newPythonSource,
return false;
}

if(!newPythonSource.endsWith(".py") && !newPythonSource.endsWith(".pyc"))
if(!filePath.endsWith(".py") && !filePath.endsWith(".pyc"))
{
return false;
}

// Extract moduleName from the provided filename
QString moduleName = QFileInfo(newPythonSource).baseName();
QString moduleName = QFileInfo(filePath).baseName();

QString className = _className;
if (className.isEmpty())
Expand Down Expand Up @@ -144,7 +144,7 @@ bool qSlicerScriptedFileWriter::setPythonSource(const QString& newPythonSource,
PyErr_SetString(PyExc_RuntimeError,
QString("qSlicerScriptedFileWriter::setPythonSource - "
"Failed to load scripted file writer: "
"class %1 was not found in file %2").arg(className).arg(newPythonSource).toUtf8());
"class %1 was not found in file %2").arg(className).arg(filePath).toUtf8());
return false;
}

Expand All @@ -154,7 +154,7 @@ bool qSlicerScriptedFileWriter::setPythonSource(const QString& newPythonSource,
return false;
}

d->PythonSource = newPythonSource;
d->PythonSource = filePath;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Base/QTCore/qSlicerScriptedFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Q_SLICER_BASE_QTCORE_EXPORT qSlicerScriptedFileWriter

/// \warning Setting the source is a no-op. See detailed comment in the source code.
/// If missingClassIsExpected is true (default) then missing class is expected and not treated as an error.
bool setPythonSource(const QString& newPythonSource, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);
bool setPythonSource(const QString& filePath, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);

/// Convenience method allowing to retrieve the associated scripted instance
Q_INVOKABLE PyObject* self() const;
Expand Down
12 changes: 6 additions & 6 deletions Base/QTCore/qSlicerScriptedUtils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@

//-----------------------------------------------------------------------------
bool qSlicerScriptedUtils::loadSourceAsModule(const QString& moduleName,
const QString& fileName,
const QString& filePath,
PyObject * global_dict,
PyObject * local_dict)
{
PyObject* pyRes = nullptr;
if (fileName.endsWith(".py"))
if (filePath.endsWith(".py"))
{
pyRes = PyRun_String(
QString("import imp;imp.load_source(%2, %1);del imp;")
.arg(qSlicerCorePythonManager::toPythonStringLiteral(fileName))
.arg(qSlicerCorePythonManager::toPythonStringLiteral(filePath))
.arg(qSlicerCorePythonManager::toPythonStringLiteral(moduleName)).toUtf8(),
Py_file_input, global_dict, local_dict);
}
else if (fileName.endsWith(".pyc"))
else if (filePath.endsWith(".pyc"))
{
pyRes = PyRun_String(
QString("with open(%1, 'rb') as f:import imp;imp.load_module(%2, f, %1, ('.pyc', 'rb', 2));del imp")
.arg(qSlicerCorePythonManager::toPythonStringLiteral(fileName))
.arg(qSlicerCorePythonManager::toPythonStringLiteral(filePath))
.arg(qSlicerCorePythonManager::toPythonStringLiteral(moduleName)).toUtf8(),
Py_file_input, global_dict, local_dict);
}
if (!pyRes)
{
PythonQt::self()->handleError();
qCritical() << "loadSourceAsModule - Failed to load file" << fileName
qCritical() << "loadSourceAsModule - Failed to load file" << filePath
<< " as module" << moduleName << "!";
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Base/QTCore/qSlicerScriptedUtils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Q_SLICER_BASE_QTCORE_EXPORT qSlicerScriptedUtils
public:
typedef qSlicerScriptedUtils Self;

static bool loadSourceAsModule(const QString& moduleName, const QString& fileName, PyObject * global_dict, PyObject *local_dict);
static bool loadSourceAsModule(const QString& moduleName, const QString& filePath, PyObject * global_dict, PyObject *local_dict);

/// \brief Set the value of the attribute named \a attributeName, for module
/// named \a moduleName, to the value \a attributeValue.
Expand Down
14 changes: 7 additions & 7 deletions Base/QTGUI/qSlicerScriptedFileDialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class qSlicerScriptedFileDialogPrivate

mutable qSlicerPythonCppAPI PythonCppAPI;

QString PythonSource;
QString PythonSourceFilePath;
QString PythonClassName;
};

Expand Down Expand Up @@ -93,11 +93,11 @@ qSlicerScriptedFileDialog::~qSlicerScriptedFileDialog() = default;
QString qSlicerScriptedFileDialog::pythonSource()const
{
Q_D(const qSlicerScriptedFileDialog);
return d->PythonSource;
return d->PythonSourceFilePath;
}

//-----------------------------------------------------------------------------
bool qSlicerScriptedFileDialog::setPythonSource(const QString& newPythonSource, const QString& _className, bool missingClassIsExpected)
bool qSlicerScriptedFileDialog::setPythonSource(const QString& filePath, const QString& _className, bool missingClassIsExpected)
{
Q_D(qSlicerScriptedFileDialog);

Expand All @@ -106,13 +106,13 @@ bool qSlicerScriptedFileDialog::setPythonSource(const QString& newPythonSource,
return false;
}

if(!newPythonSource.endsWith(".py") && !newPythonSource.endsWith(".pyc"))
if(!filePath.endsWith(".py") && !filePath.endsWith(".pyc"))
{
return false;
}

// Extract moduleName from the provided filename
QString moduleName = QFileInfo(newPythonSource).baseName();
QString moduleName = QFileInfo(filePath).baseName();

QString className = _className;
if (className.isEmpty())
Expand Down Expand Up @@ -191,7 +191,7 @@ bool qSlicerScriptedFileDialog::setPythonSource(const QString& newPythonSource,
return false;
}

d->PythonSource = newPythonSource;
d->PythonSourceFilePath = filePath;

return true;
}
Expand All @@ -215,7 +215,7 @@ bool qSlicerScriptedFileDialog::exec(const qSlicerIO::IOProperties& ioProperties
}
if (!PyBool_Check(result))
{
qWarning() << d->PythonSource
qWarning() << d->PythonSourceFilePath
<< " - In" << d->PythonClassName << "class, function 'execDialog' "
<< "is expected to return a boolean";
return false;
Expand Down
2 changes: 1 addition & 1 deletion Base/QTGUI/qSlicerScriptedFileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Q_SLICER_BASE_QTGUI_EXPORT qSlicerScriptedFileDialog

/// \warning Setting the source is a no-op. See detailed comment in the source code.
/// If missingClassIsExpected is true (default) then missing class is expected and not treated as an error.
bool setPythonSource(const QString& newPythonSource, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);
bool setPythonSource(const QString& filePath, const QString& className = QLatin1String(""), bool missingClassIsExpected = true);

/// Convenience method allowing to retrieve the associated scripted instance
Q_INVOKABLE PyObject* self() const;
Expand Down