From 06b7ea9b2823e17207295ab3a7d12e99b2797b52 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Tue, 6 Nov 2018 19:38:17 +0100
Subject: [PATCH] Add ability to run python scripts

---
 include/gepetto/gui/pythonwidget.hh |  1 +
 include/gepetto/gui/settings.hh     |  2 ++
 src/gui/pythonwidget.cc             | 18 +++++++++++++++---
 src/gui/settings.cc                 | 16 ++++++++++++++++
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/include/gepetto/gui/pythonwidget.hh b/include/gepetto/gui/pythonwidget.hh
index f2309f8..0fae49f 100644
--- a/include/gepetto/gui/pythonwidget.hh
+++ b/include/gepetto/gui/pythonwidget.hh
@@ -119,6 +119,7 @@ namespace gepetto {
       void loadModulePlugin(QString moduleName);
       void unloadModulePlugin(QString moduleName);
       void loadScriptPlugin(QString moduleName, QString fileName);
+      void runScript(QString fileName);
 
     private:
       void loadPlugin(QString moduleName, PythonQtObjectPtr module);
diff --git a/include/gepetto/gui/settings.hh b/include/gepetto/gui/settings.hh
index a0e3b92..fc30f78 100644
--- a/include/gepetto/gui/settings.hh
+++ b/include/gepetto/gui/settings.hh
@@ -79,6 +79,7 @@ namespace gepetto {
       PluginManager pluginManager_;
       QStringList pluginsToInit_;
       QStringList pyplugins_;
+      QStringList pyscripts_;
 
       void setMainWindow (MainWindow* main);
 
@@ -127,6 +128,7 @@ namespace gepetto {
       void addEnvFromString (const std::string& envStr);
       void addPlugin (const QString& plg, bool init);
       void addPyPlugin (const QString& plg, bool init);
+      void addPyScript (const QString& fileName);
       void addOmniORB (const QString& arg, const QString& value);
 
       inline void log (const QString& t);
diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc
index bfd33c3..1c5b5d8 100644
--- a/src/gui/pythonwidget.cc
+++ b/src/gui/pythonwidget.cc
@@ -151,10 +151,10 @@ namespace gepetto {
         PythonQt* pqt = PythonQt::self();
         PythonQtObjectPtr module = pqt->createModuleFromFile (moduleName, fileName);
         if (pqt->handleError()) {
+          pqt->clearError();
           return;
         }
         if (module.isNull()) {
-          pqt->handleError();
           qDebug() << "Enable to load module" << moduleName << "from script"
             << fileName;
           return;
@@ -162,16 +162,28 @@ namespace gepetto {
         loadPlugin (moduleName, module);
       }
 
+      void PythonWidget::runScript(QString fileName)
+      {
+        PythonQt* pqt = PythonQt::self();
+        PythonQtObjectPtr mainContext = pqt->getMainModule();
+        mainContext.evalFile(fileName);
+
+        if (pqt->handleError()) {
+          pqt->clearError();
+          qDebug() << "Failed to run script" << fileName;
+        }
+      }
+
       void PythonWidget::loadModulePlugin(QString moduleName)
       {
         PythonQt* pqt = PythonQt::self();
         PythonQtObjectPtr module = pqt->importModule (moduleName);
         if (pqt->handleError()) {
+          pqt->clearError();
           return;
         }
         if (module.isNull()) {
-          pqt->handleError();
-          qDebug() << "Enable to load module" << moduleName;
+          qDebug() << "Unable to load module" << moduleName;
           return;
         }
         loadPlugin (moduleName, module);
diff --git a/src/gui/settings.cc b/src/gui/settings.cc
index 06f693a..b7dd63e 100644
--- a/src/gui/settings.cc
+++ b/src/gui/settings.cc
@@ -103,6 +103,7 @@ namespace gepetto {
       au->addCommandLineOption("-p or --load-plugin", "load the plugin");
 #if GEPETTO_GUI_HAS_PYTHONQT
       au->addCommandLineOption("-q or --load-pyplugin", "load the PythonQt module as a plugin");
+      au->addCommandLineOption("-x or --run-pyscript", "run a script into the PythonQt console");
 #endif
       au->addCommandLineOption("-P or --no-plugin", "do not load any plugin");
       au->addCommandLineOption("-w or --auto-write-settings", "write the settings in the configuration file");
@@ -134,6 +135,8 @@ namespace gepetto {
         addPlugin (QString::fromStdString(opt), !noPlugin);
       while (arguments.read ("-q", opt) || arguments.read ("--load-pyplugin", opt))
         addPyPlugin (QString::fromStdString(opt), !noPlugin);
+      while (arguments.read ("-x", opt) || arguments.read ("--run-pyscript", opt))
+        addPyScript (QString::fromStdString(opt));
 
       if (arguments.read("-c", configurationFile) || arguments.read("--config-file", configurationFile)) {}
       if (arguments.read("--predefined-robots",       predifinedRobotConf)) {}
@@ -200,11 +203,19 @@ namespace gepetto {
         } else
           pw->loadModulePlugin (name);
       }
+      // TODO Wouldn't it be better to do this later ?
+      foreach (QString fileName, pyscripts_) {
+        pw->runScript(fileName);
+      }
 #else
       foreach (QString name, pyplugins_) {
         logError ("gepetto-viewer-corba was compiled without GEPETTO_GUI_HAS_"
             "PYTHONQT flag. Cannot not load Python plugin " + name);
       }
+      foreach (QString fileName, pyscripts_) {
+        logError ("gepetto-viewer-corba was compiled without GEPETTO_GUI_HAS_"
+            "PYTHONQT flag. Cannot not load Python script " + fileName);
+      }
 #endif
     }
 
@@ -513,6 +524,11 @@ namespace gepetto {
       if (init) pyplugins_.append (plg);
     }
 
+    void Settings::addPyScript (const QString& fileName)
+    {
+      pyscripts_.append (fileName);
+    }
+
     void Settings::addOmniORB (const QString& arg, const QString& value)
     {
       omniORBargv_ << arg << value;
-- 
GitLab