From: vsr Date: Fri, 18 Jan 2019 12:04:03 +0000 (+0300) Subject: 0023645: EDF 18388 - problem with arguments of a script X-Git-Tag: V9_2_2~5 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2d19eb00e359a12a35891aeb016468713dbfe19a;p=modules%2Fgui.git 0023645: EDF 18388 - problem with arguments of a script --- diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index aaf5ad2bf..50a021ef1 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -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 diff --git a/tools/PyInterp/src/PyInterp_Interp.cxx b/tools/PyInterp/src/PyInterp_Interp.cxx index 8e9dfb499..bcedd6ac5 100644 --- a/tools/PyInterp/src/PyInterp_Interp.cxx +++ b/tools/PyInterp/src/PyInterp_Interp.cxx @@ -36,6 +36,7 @@ #include #include +#include #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 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::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 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); } }