Merge remote-tracking branch 'origin/master' into cleaning

This commit is contained in:
Mikaël Capelle
2020-05-20 18:32:22 +02:00
2 changed files with 46 additions and 8 deletions
+15 -3
View File
@@ -28,7 +28,6 @@ along with python proxy plugin. If not, see <http://www.gnu.org/licenses/>.
#include <QCoreApplication>
#include "resource.h"
using namespace MOBase;
@@ -214,11 +213,24 @@ QList<PluginSetting> ProxyPython::settings() const
QStringList ProxyPython::pluginList(const QString &pluginPath) const
{
QDirIterator iter(pluginPath, QStringList("*.py"));
QDir dir(pluginPath);
dir.setFilter(dir.filter() | QDir::NoDotAndDotDot);
QDirIterator iter(dir);
// Note: We put python script (.py) and directory names, not the __init__.py
// files in those since it is easier for the runner to import them.
QStringList result;
while (iter.hasNext()) {
result.append(iter.next());
QString name = iter.next();
QFileInfo info = iter.fileInfo();
if (info.isFile() && name.endsWith(".py")) {
result.append(name);
}
else if (info.isDir() && QDir(info.absoluteFilePath()).exists("__init__.py")) {
result.append(name);
// result.append(info.baseName());
}
}
return result;
+31 -5
View File
@@ -483,7 +483,7 @@ BOOST_PYTHON_MODULE(mobase)
.def("addCategory", &IModInterface::addCategory)
.def("removeCategory", &IModInterface::removeCategory)
.def("categories", &IModInterface::categories)
.def("setGamePlugin", &IModInterface::setGamePlugin)
.def("setGameName", &IModInterface::setGameName)
.def("setName", &IModInterface::setName)
.def("remove", &IModInterface::remove)
;
@@ -798,6 +798,11 @@ private:
void initPath();
/**
* @brief Ensure that the given folder is in sys.path.
*/
void ensureFolderInPath(QString folder);
/**
* @brief Append the underlying object of the given python object to the
* interface list if it is an instance (pointer) of the given type.
@@ -983,6 +988,17 @@ void PythonRunner::initPath()
Py_SetPath(paths.join(';').toStdWString().c_str());
}
void PythonRunner::ensureFolderInPath(QString folder) {
bpy::object sys = bpy::import("sys");
bpy::list sysPath = bpy::extract<bpy::list>(sys.attr("path"));
// Converting to QStringList for Qt::CaseInsensitive and because .index()
// raise an exception:
QStringList currentPath = bpy::extract<QStringList>(sysPath);
if (!currentPath.contains(folder, Qt::CaseInsensitive)) {
sysPath.insert(0, folder);
}
}
@@ -1005,11 +1021,21 @@ QList<QObject*> PythonRunner::instantiate(const QString &pluginName)
moduleNamespace["sys"] = sys;
moduleNamespace["mobase"] = bpy::import("mobase");
std::string temp = ToString(pluginName);
if (handled_exec_file(temp.c_str(), moduleNamespace)) {
throw pyexcept::PythonError();
if (pluginName.endsWith(".py")) {
std::string temp = ToString(pluginName);
if (handled_exec_file(temp.c_str(), moduleNamespace)) {
throw pyexcept::PythonError();
}
m_PythonObjects[pluginName] = moduleNamespace["createPlugin"]();
}
else {
// Retrieve the module name:
QStringList parts = pluginName.split("/");
std::string moduleName = ToString(parts.takeLast());
ensureFolderInPath(parts.join("/"));
bpy::object createPlugin = bpy::import(moduleName.c_str()).attr("createPlugin");
m_PythonObjects[pluginName] = createPlugin();
}
m_PythonObjects[pluginName] = moduleNamespace["createPlugin"]();
bpy::object pluginObj = m_PythonObjects[pluginName];
QList<QObject *> interfaceList;