+ \brief Compile Python command and evaluate it in the
+ 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]")
+ \internal
+ \param command Python command string
+ \param context Python context (dictionary)
+ \return -1 on fatal error, 1 if command is incomplete and 0
+ if command is executed successfully
+ */
+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 = "";
+
+ std::size_t pos = std::string(command).find("args:");
+ if (pos != std::string::npos) {
+ commandArgs = singleCommand.substr(pos+5);
+ commandArgs = commandArgs.substr(0, commandArgs.length()-3);
+ singleCommand = singleCommand.substr(0, pos-1)+"\")";
+ }
+
+ 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);
+ }
+ 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::string preCommandBegin = "import sys; save_argv = sys.argv; sys.argv=[";
+ std::string preCommandEnd = "];";
+ replaceAll(commandArgs, ",", "\",\"");
+ commandArgs = "\""+commandArgs+"\"";
+ std::string completeCommand = preCommandBegin+"\""+script+"\","+commandArgs+preCommandEnd+singleCommand+";sys.argv=save_argv";
+ return run_command(completeCommand.c_str(), global_ctxt, local_ctxt);
+ }
+}
+
+/*!
+ \brief Run Python command - the command has to fit on a single line (no \n!).
+ Use ';' if you need multiple statements evaluated at once.