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
#include <algorithm>
#include <QRegExp>
+#include <QStringList>
#define TOP_HISTORY_PY "--- top of history ---"
#define BEGIN_HISTORY_PY "--- begin of history ---"
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)
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);
}
}