Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for command line arguments to be passed on to python script (-py option) #19

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
use QStringList to call python script with arguments
  • Loading branch information
berteh committed Aug 5, 2015
commit 35e17efbc051678d5d78f4958a08ac6bc7079bc0
26 changes: 11 additions & 15 deletions scribus/plugins/scriptplugin/scriptercore.cpp
Original file line number Diff line number Diff line change
@@ -234,6 +234,7 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
}

void ScripterCore::slotRunScriptFileArgs(QString fileName, QStringList arguments, bool inMainInterpreter)
/** run "filename" python script with the additional arguments provided in "arguments" */
{
// Prevent two scripts to be run concurrently or face crash!
if (ScCore->primaryMainWindow()->scriptIsRunning())
@@ -260,29 +261,24 @@ void ScripterCore::slotRunScriptFileArgs(QString fileName, QStringList arguments
initscribus(ScCore->primaryMainWindow());
}

// handle additional arguments
bool argsExtra = !(arguments.isEmpty());
int argsc = 0;
if (argsExtra)
{
argsc = arguments.size();
//std::cout << tr("running script with %1 arguments").arg(argsc).toLocal8Bit().data() << std::endl;
}
// Make sure sys.argv[0] is the path to the script
char* comm[2+argsc];
comm[0] = na.data();
arguments.prepend(na.data());

// and tell the script if it's running in the main intepreter or
// a subinterpreter using the second argument, ie sys.argv[1]
if (inMainInterpreter)
comm[1] = const_cast<char*>("ext");
arguments.insert(1,QString("ext"));
else
comm[1] = const_cast<char*>("sub");
for (int j = 0; j < argsc; ++j)
arguments.insert(1,QString("sub"));

//convert arguments (QListString) to char** for Python bridge
/* typically arguments == ['path/to/script.py','ext','--argument1','valueforarg1','--flag']*/
char *comm[arguments.size()];
for (int i = 0; i < arguments.size(); i++)
{
comm[2+j] = arguments.at(j).toLocal8Bit().data() ; // TODO fix here: some copy is needed, otherwise all comm[>2] point to same (last) value for j.
comm[i] = arguments.at(i).mid(0).toLocal8Bit().data(); // TODO fix here: unexpected border effects. all comm[*] point to same (last) value for i... despite mid() that should create a copy.
}
PySys_SetArgv(2+argsc, comm);
PySys_SetArgv(arguments.size(), comm);

// call python script
PyObject* m = PyImport_AddModule((char*)"__main__");