Run python script from CLI Add the '--python-script file' CLI option to run 'file' as python script. Modified scribus/main_nix.cpp diff --git a/scribus/main_nix.cpp b/scribus/main_nix.cpp index 8132b43..1562cc4 100644 --- a/scribus/main_nix.cpp +++ b/scribus/main_nix.cpp @@ -39,6 +39,7 @@ for which a new license (GPL+exception) is in place. #include "scimagecachemanager.h" #include "scconfig.h" +#include "scraction.h" // need to be able to trigger action for scripter to run from CLI int mainApp(int argc, char **argv); void initCrashHandler(); @@ -83,7 +84,13 @@ int mainApp(int argc, char **argv) int appRetVal=app.init(); if (appRetVal==EXIT_FAILURE) return(EXIT_FAILURE); - return app.exec(); + if (!app.pythonScript.isNull()) + { + if (ScCore->primaryMainWindow()->scrActions.contains("scripterRunPythonScript")) + ScCore->primaryMainWindow()->scrActions.value("scripterRunPythonScript")->trigger(); + } + else + return app.exec(); } return EXIT_SUCCESS; } Modified scribus/main_win32.cpp diff --git a/scribus/main_win32.cpp b/scribus/main_win32.cpp index 9dccfa1..d8bc04b 100644 --- a/scribus/main_win32.cpp +++ b/scribus/main_win32.cpp @@ -50,6 +50,7 @@ using namespace std; #include "scimagecachemanager.h" #include "scconfig.h" +#include "scraction.h" // need to be able to trigger action for scripter to run from CLI #include #include @@ -118,7 +119,13 @@ int mainApp(ScribusQApp& app) { appRetVal = app.init(); if (appRetVal != EXIT_FAILURE) - appRetVal = app.exec(); + if (!app.pythonScript.isNull()) + { + if (ScCore->primaryMainWindow()->scrActions.contains("scripterRunPythonScript")) + ScCore->primaryMainWindow()->scrActions.value("scripterRunPythonScript")->trigger(); + } + else + appRetVal = app.exec(); } #ifndef _DEBUG } Modified scribus/plugins/scriptplugin/scriptercore.cpp diff --git a/scribus/plugins/scriptplugin/scriptercore.cpp b/scribus/plugins/scriptplugin/scriptercore.cpp index 0bf95ad..c61c84a 100644 --- a/scribus/plugins/scriptplugin/scriptercore.cpp +++ b/scribus/plugins/scriptplugin/scriptercore.cpp @@ -36,6 +36,7 @@ for which a new license (GPL+exception) is in place. #include "prefscontext.h" #include "prefstable.h" #include "prefsmanager.h" +#include "scribusapp.h" // need it to acces ScQApp->pythonScript ScripterCore::ScripterCore(QWidget* parent) { @@ -61,6 +62,10 @@ ScripterCore::ScripterCore(QWidget* parent) QObject::connect( scrScripterActions["scripterShowConsole"], SIGNAL(toggled(bool)) , this, SLOT(slotInteractiveScript(bool)) ); QObject::connect( scrScripterActions["scripterAboutScript"], SIGNAL(triggered()) , this, SLOT(aboutScript()) ); + // Create an action that will run python file from CLI + ScCore->primaryMainWindow()->scrActions.insert("scripterRunPythonScript", new ScrAction(this)); + QObject::connect( ScCore->primaryMainWindow()->scrActions.value("scripterRunPythonScript"), SIGNAL(triggered()) , this, SLOT(slotRunPythonScript()) ); + SavedRecentScripts.clear(); ReadPlugPrefs(); @@ -177,7 +182,7 @@ void ScripterCore::FinishScriptRun() void ScripterCore::runScriptDialog() { QString fileName; - QString curDirPath = QDir::currentPath(); + // QString curDirPath = QDir::currentPath(); RunScriptDialog dia( ScCore->primaryMainWindow(), m_enableExtPython ); if (dia.exec()) { @@ -193,7 +198,7 @@ void ScripterCore::runScriptDialog() } rebuildRecentScriptsMenu(); } - QDir::setCurrent(curDirPath); + // QDir::setCurrent(curDirPath); FinishScriptRun(); } @@ -247,7 +252,7 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter) global_state = PyThreadState_Get(); state = Py_NewInterpreter(); // Chdir to the dir the script is in - QDir::setCurrent(fi.absolutePath()); + // QDir::setCurrent(fi.absolutePath()); // Init the scripter module in the sub-interpreter initscribus(ScCore->primaryMainWindow()); } @@ -353,6 +358,13 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter) enableMainWindowMenu(); } +// needed for running scriptfrom CLI - this is activated by action from main_nix.cpp +void ScripterCore::slotRunPythonScript() +{ + slotRunScriptFile(ScQApp->pythonScript); + FinishScriptRun(); +} + void ScripterCore::slotRunScript(const QString Script) { // Prevent two scripts to be run concurrently or face crash! Modified scribus/plugins/scriptplugin/scriptercore.h diff --git a/scribus/plugins/scriptplugin/scriptercore.h b/scribus/plugins/scriptplugin/scriptercore.h index ff1e001..1e929f4 100644 --- a/scribus/plugins/scriptplugin/scriptercore.h +++ b/scribus/plugins/scriptplugin/scriptercore.h @@ -39,6 +39,7 @@ public slots: void StdScript(QString filebasename); void RecentScript(QString fn); void slotRunScriptFile(QString fileName, bool inMainInterpreter = false); + void slotRunPythonScript(); // needed for running python script from CLI void slotRunScript(const QString Script); void slotInteractiveScript(bool); void slotExecute(); Modified scribus/scribus.cpp diff --git a/scribus/scribus.cpp b/scribus/scribus.cpp index dbcb911..b51555c 100644 --- a/scribus/scribus.cpp +++ b/scribus/scribus.cpp @@ -347,7 +347,8 @@ int ScribusMainWindow::initScMW(bool primaryMainWindow) setWindowIcon(loadIcon("AppIcon.png")); setObjectName("MainWindow"); scrActionGroups.clear(); - scrActions.clear(); + // DO NOT clear actions : scripter plugin has created one already! +// scrActions.clear(); scrRecentFileActions.clear(); scrRecentPasteActions.clear(); scrWindowsActions.clear(); Modified scribus/scribusapp.cpp diff --git a/scribus/scribusapp.cpp b/scribus/scribusapp.cpp index aa2c818..df44714 100644 --- a/scribus/scribusapp.cpp +++ b/scribus/scribusapp.cpp @@ -66,6 +66,7 @@ for which a new license (GPL+exception) is in place. #define ARG_PREFS "--prefs" #define ARG_UPGRADECHECK "--upgradecheck" #define ARG_TESTS "--tests" +#define ARG_PYTHONSCRIPT "--python-script" #define ARG_VERSION_SHORT "-v" #define ARG_HELP_SHORT "-h" @@ -81,6 +82,7 @@ for which a new license (GPL+exception) is in place. #define ARG_PREFS_SHORT "-pr" #define ARG_UPGRADECHECK_SHORT "-u" #define ARG_TESTS_SHORT "-T" +#define ARG_PYTHONSCRIPT_SHORT "-py" // Qt wants -display not --display or -d #define ARG_DISPLAY_QT "-display" @@ -252,6 +254,21 @@ void ScribusQApp::parseCommandLine() } else if (strncmp(arg.toLocal8Bit().data(),"-psn_",4) == 0) { // Andreas Vox: Qt/Mac has -psn_blah flags that must be accepted. + } else if (arg == ARG_PYTHONSCRIPT || arg == ARG_PYTHONSCRIPT_SHORT) { + pythonScript = QFile::decodeName(args[i + 1].toLocal8Bit()); + if (!QFileInfo(pythonScript).exists()) { + showHeader(); + if (pythonScript.left(1) == "-" || pythonScript.left(2) == "--") { + std::cout << tr("Invalid argument: ").toLocal8Bit().data() << pythonScript.toLocal8Bit().data() << std::endl; + } else { + std::cout << tr("File %1 does not exist, aborting.").arg(pythonScript).toLocal8Bit().data() << std::endl; + } + showUsage(); + useGUI=false; + return; + } else { + ++i; + } } else { fileName = QFile::decodeName(args[i].toLocal8Bit()); if (!QFileInfo(fileName).exists()) { @@ -464,7 +481,7 @@ void ScribusQApp::showUsage() printArgLine(ts, ARG_SWAPDIABUTTONS_SHORT, ARG_SWAPDIABUTTONS, tr("Use right to left dialog button ordering (eg. Cancel/No/Yes instead of Yes/No/Cancel)") ); printArgLine(ts, ARG_UPGRADECHECK_SHORT, ARG_UPGRADECHECK, tr("Download a file from the Scribus website and show the latest available version.") ); printArgLine(ts, ARG_VERSION_SHORT, ARG_VERSION, tr("Output version information and exit") ); - + printArgLine(ts, ARG_PYTHONSCRIPT_SHORT, QString(QString(ARG_PYTHONSCRIPT) + QString(" ") + tr("filename")).toLocal8Bit().constData(), tr("Run filename in Python scripter") ); #if defined(_WIN32) && !defined(_CONSOLE) printArgLine(ts, ARG_CONSOLE_SHORT, ARG_CONSOLE, tr("Display a console window") ); Modified scribus/scribusapp.h diff --git a/scribus/scribusapp.h b/scribus/scribusapp.h index fe27092..428481d 100644 --- a/scribus/scribusapp.h +++ b/scribus/scribusapp.h @@ -69,6 +69,7 @@ class SCRIBUS_API ScribusQApp : public QApplication bool neverSplashExists(); const QString& currGUILanguage() { return GUILang; } ScDLManager* dlManager() { return m_scDLMgr; } + QString pythonScript; // script to be run in python from CLI private: ScribusCore* m_ScCore;