Salome HOME
0023645: EDF 18388 - problem with arguments of a script
authorvsr <vsr@opencascade.com>
Fri, 18 Jan 2019 12:04:03 +0000 (15:04 +0300)
committervsr <vsr@opencascade.com>
Mon, 25 Feb 2019 12:18:56 +0000 (15:18 +0300)
src/SalomeApp/SalomeApp_Application.cxx
tools/PyInterp/src/PyInterp_Interp.cxx

index aaf5ad2bf2d60cc734d042e09a6ab296511ff174..50a021ef1d02891b24e6e3ea68ec75642e69ee4c 100644 (file)
@@ -276,23 +276,19 @@ void SalomeApp_Application::start()
             QRegExp rxp ("\"(.+)\":[\\s]*\\[(.*)\\]");
             if ( rxp.indexIn( pyfiles[j] ) >= 0 && rxp.capturedTexts().count() == 3 ) {
               QString script = rxp.capturedTexts()[1];
-              QString args = "";
+              QStringList args;
               QStringList argList = __getArgsList(rxp.capturedTexts()[2]);
               for (int k = 0; k < argList.count(); k++ ) {
                 QString arg = argList[k].trimmed();
                 arg.remove( QRegExp("^[\"]") );
                 arg.remove( QRegExp("[\"]$") );
-                args += arg+",";
-              }
-              args.remove( QRegExp("[,]$") );
-              if (!args.isEmpty()) {
-                args = "args:"+args;
+                args << QString("\"%1\"").arg(arg);
               }
+              if (args.count() == 1)
+                args << "";
 
               script.remove( QRegExp("^python.*[\\s]+") );
-              QString cmd = script+" "+args;
-
-              QString command = QString( "exec(open(\"%1\", \"rb\").read())" ).arg(cmd.trimmed());
+              QString command = QString( "exec(open(\"%1\", \"rb\").read(), args=(%2))" ).arg(script).arg(args.join(","));
               pyConsole->exec(command);
             }
           } // end for loop on pyfiles QStringList
index 8e9dfb4995f4ce912dc35f98a83487316a90f012..bcedd6ac5e7aed6149c1097a8ef7dc1c05cb2948 100644 (file)
@@ -36,6 +36,7 @@
 #include <algorithm>
 
 #include <QRegExp>
+#include <QStringList>
 
 #define TOP_HISTORY_PY   "--- top of history ---"
 #define BEGIN_HISTORY_PY "--- begin of history ---"
@@ -449,7 +450,8 @@ __getArgsList(std::string argsString)
          python dictionary context if possible. Command might correspond to
          the execution of a script with optional arguments.
          In this case, command is:
-         execfile(r"/absolute/path/to/script.py [args:arg1,...,argn]")
+           exec(open(r"/absolute/path/to/script.py", "rb").read(), args=(arg1,...,argn))
+        and args parameter is optional one. This parameter is specified as a tuple of strings.
   \internal
   \param command Python command string
   \param context Python context (dictionary)
@@ -459,36 +461,26 @@ __getArgsList(std::string argsString)
 static int compile_command(const char *command, PyObject * global_ctxt, PyObject * local_ctxt)
 {
   // First guess if command is execution of a script with args, or a simple Python command
-  std::string singleCommand = command;
-  std::string commandArgs = "";
+  QString singleCommand = command;
+  QString commandArgs = "";
 
-  QRegExp rx("execfile\\s*\\(.*(args:.*)\"\\s*\\)");
+  QRegExp rx("exec\\s*\\(.*open\\s*\\(\\s*(.*)\\s*\\)\\s*\\.\\s*read\\s*\\(\\)(\\s*,\\s*args\\s*=\\s*\\(.*\\))\\s*\\)");
   if (rx.indexIn(command) != -1) {
-    commandArgs = rx.cap(1).remove(0,5).toStdString(); // arguments of command
-    singleCommand = rx.cap().remove(rx.cap(1)).remove(" ").toStdString(); // command for execution without arguments
+    commandArgs = rx.cap(2).remove(0, rx.cap(2).indexOf("(")); // arguments of command
+    commandArgs.insert(commandArgs.indexOf('(')+1, rx.cap(1).split(",")[0].trimmed() + ","); // prepend arguments list by the script file itself
+    singleCommand = singleCommand.remove(rx.pos(2), rx.cap(2).size()); // command for execution without arguments
   }
 
-  if (commandArgs.empty()) {
-    // process command: expression
-    // process command: execfile(r"/absolute/path/to/script.py") (no args)
-    return run_command(singleCommand.c_str(), global_ctxt, local_ctxt);
+  if (commandArgs.isEmpty()) {
+    return run_command(singleCommand.toStdString().c_str(), global_ctxt, local_ctxt);
   }
   else {
-    // process command: execfile(r"/absolute/path/to/script.py [args:arg1,...,argn]")
-    std::string script = singleCommand.substr(11); // remove leading execfile(r"
-    script = script.substr(0, script.length()-2); // remove trailing ")
-    std::vector<std::string> argList = __getArgsList(commandArgs);
-
-    std::string preCommandBegin = "import sys; save_argv = sys.argv; sys.argv=[";
-    std::string preCommandEnd = "];";
-    std::string completeCommand = preCommandBegin+"\""+script+"\",";
-    for (std::vector<std::string>::iterator itr = argList.begin(); itr != argList.end(); ++itr) {
-      if (itr != argList.begin())
-        completeCommand += ",";
-      completeCommand = completeCommand + "\"" + *itr + "\"";
-    }
-    completeCommand = completeCommand+preCommandEnd+singleCommand+";sys.argv=save_argv";
-    return run_command(completeCommand.c_str(), global_ctxt, local_ctxt);
+    ///////////////std::vector<std::string> argList = __getArgsList(commandArgs);
+    QString preCommandBegin = "import sys; save_argv = sys.argv; sys.argv=list(";
+    QString preCommandEnd = ");";
+    QString postCommand = ";sys.argv=save_argv";
+    QString completeCommand = preCommandBegin+commandArgs+preCommandEnd+singleCommand.trimmed()+postCommand;
+    return run_command(completeCommand.toStdString().c_str(), global_ctxt, local_ctxt);
   }
 }