View Issue Details

IDProjectCategoryView StatusLast Update
0012572ScribusScripterpublic2016-05-01 00:46
Reporterwilliam Assigned Tocbradney  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
PlatformLinuxOSFedora  
Fixed in Version1.5.0svn 
Summary0012572: [patch] Add a --python-script command line option to run a script and exit
DescriptionThese patches were written by Juraj Fedel and posted for Scribus 1.4.4.
I have posted another set of patches relative to Scribus 1.5.0svn of 28 July 2014.
Additional InformationFrom http://lists.scribus.net/pipermail/scribus/2014-July/050757.html

You can create template scribus document 'mydoc.sla' with two text
frames and this small python script 'data.py' in the same directory:

import scribus
scribus.openDoc('mydoc.sla')
scribus.setText('Name', 'Text1') # get 'Name' and 'Address' from database
scribus.setText('Address', 'Text2')
pdf = scribus.PDFfile()
pdf.file = 'output1.pdf'
pdf.save()

Then run scribus as folow:
scribus --python-script data.py

and you have your output1.pdf file created!

WARNING!
- Use only python script without bugs (he, he :)
- You will anyway find some limitation because scribus really is not in
  its core coded for use without GUI

PS:
I have been posting this or similar solution since 2006 (and Scribus
version 1.2 I was using at that time).
TagsNo tags attached.
PatchYes

Relationships

related to 0000238 assignedcbradney New CL option: scribus --to-pdf <scribus-file> <output.pdf> 
related to 0000967 assignedcbradney CLI option -p 
related to 0007741 closed Provide a way to run Scribus script from PyDev in Eclipse 
related to 0012786 closedcbradney [patch] Do not show the startup dialog when GUI is not requested 
has duplicate 0009331 closed Allow command line switch so that a user may specify a start-up script 
has duplicate 0008967 closedjainbasil [patch] Implement --plugin-script-run in the command line to start a script on startup 
related to 0012594 closedjghali [patch] Add a --python-script command line option to Scribus 1.4.5svn to run a script and exit 
related to 0012774 closedjghali [patch] Write messages to cerr instead of QMessageBox when Scribus is run without a GUI 

Activities

william

2014-07-29 19:44

updater  

william

2014-07-31 21:36

updater   ~0033119

The patches are in the link at the top "Attached Files scribus-cmd-wb-28jul14.pat [^] (6,294 bytes) 14-Jul-29 21:44".
It was the first time that I used Mantis, and when I made the entry, the "Attached Files" seemed a logical place for the patch.

Juraj said that he is OK with my making what changes are necessary to have the patch accepted.

a.l.e posted earlier posted several objections to the patches:

1) Several comments have the initials "JF". I can change them to "Juraj Fedel".

2) I think that removing the "ugly hack" of testing app.pythonScript.isNull() instead of app.useGUI affects Scribus at a much more systemic level and would change this 150 line patch that affects 6 files into a several thousand line patch that affects a large number of files and should be its own issue in Mantis related to a truly headless version of Scribus.

Also, maybe it is not that ugly at all -- the same mechanism would allow a --initial-python-script that could run before continuing with the GUI, so testing app.pythonScript.isNull() might be more appropriate than testing app.useGUI.

3) The comments say Linux-only because it was never tested under Windows rather than because it does anything operating system specific. Unfortunately, I do not have a Windows build environment either.
Since the patch does not add any new files, I think that the Windows build scripts will probably not need changes.
Another posting said to check if python is enabled. Does that mean testing COMPILE_PYTHON (in config.h) and additionally on Windows, checking HAVE_PYTHON (in win32/vc*/win-config.h)?
I can add the #if defined() tests. It looks like no variables are declared conditionally on the presence of python, so maybe only the command line parsing needs to check for python and exit with a "python is not enabled" message if the build does not include python scripting.

william

2014-08-05 00:46

updater  

Run-python-script-from-CLI-4aug14.patch (8,558 bytes)   
    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 <windows.h>
 #include <wincon.h>
@@ -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;

william

2014-08-05 01:00

updater   ~0033180

I uploaded a new set of patches from Juraj in the attachment "Run-python-script-from-CLI-4aug14.patch".
These patches apply cleanly against the current Scribus 1.5.0svn.
The patches should satisfy the earlier objections.
1) Juraj removed all occurrences of his name and initials.
2) The "hack" comment is removed. It is not really a hack.
3) The Linux-only comment is removed. The patches contain no operating-system dependent system calls. The patches now update main_win32.cpp similarly to main_nix.cpp.

In addition, the python script target of --python-script can now have a path. In the original version, it had to be in the current directory.

ale

2014-08-05 15:27

manager   ~0033182

thanks!

let's see if the team is willing to apply this patch.

i think it can be of some use, even if the GUI still fires up.

personally, i still would love to see a "scribus" that really works from the command line, but i'm more and more convinced that such a tool should be written from scratch.
let's see if it will happen one day!

Kunda

2014-08-06 13:35

updater   ~0033188

Changing status to 'feedback' to trigger a discussion.

william

2014-08-20 23:42

updater  

william

2014-08-21 00:21

updater   ~0033306

I uploaded an improved set of patches for Scribus 1.5 from Juraj as patch_bundle_v150-20aug14.tar.gz

The first patch in the set replaces the original patch to add a '--python-script file' command line option.
The seventh patch adds python functions to save and read PDF options.
The eleventh patch enables the '--no-gui' command line option.
The other patches fix problems in Scribus that become apparent when you try to script exporting documents to PDF.

I tested the patches with Scribus 1.5.0svn from August 20, 2014.

0001: Add the '--python-script file' CLI option to run 'file' as python script.
0002: Now help(scribus.newDocument) in python console runs without error.
0003: Translate strings to variable names with unitGetUntranslatedStrFromIndex instead of unitGetStrFromIndex so python scripts are not dependent on the locale.
0004: Fix the help message for scribus.PDFfile.
0005: Fix reading the prefs file. Scribus was opening the wrong file.
0006: Set ScribusMainWindow::ScriptRunning earlier in the initialization process. Scribus was using it before it was initialized.
0007: Add two python commands readPDFOptions(file) and savePDFOptions(file).
0008: Fix errors when reading PDFOptions.
0009: Fix errors in error messages related to reading PDFOptions files.
0010: Escape HTML codes in scripter error messages so error messages enclosed by <> show correctly.
0011: Enable the --no-gui CLI option so 'scribus --no-gui --python-script myscript.py' runs a script and exits without starting the GUI.
0012: Update the python scripter to allow exporting documents in PDF 1.5. Scribus limited the 'version' variable to 14 instead of 15.

To test the pdf version patch, create a mydoc.sla as described in the initial note and run the script below and then check that output1.pdf is PDF 1.5.

import scribus
scribus.openDoc('mydoc.sla')
scribus.setText('Name', 'Text1') # get 'Name' and 'Address' from database
scribus.setText('Address', 'Text2')
scribus.saveDocAs('mydocnew.sla')
pdf = scribus.PDFfile()
pdf.file = 'output1.pdf'
pdf.version = 15
pdf.save()

To test the PDF options patches, run the script below without and with an open document. Without an open document, it should raise a NoDocOpenError error and not crash Scribus. With an open document, it should create and then load an XML with PDF preferences.

import scribus
scribus.savePDFOptions('test.xml')
scribus.readPDFOptions('test.xml')

william

2014-08-29 02:31

updater  

0013-Do-not-show-messagebox-when-python-script-raise-an-e.patch (3,904 bytes)   
From 11cfd1c842a7e9aded3a5723f2a0d4d4948d9322 Mon Sep 17 00:00:00 2001
From: Juraj Fedel <wtxnh-scribus@yahoo.com.au>
Date: Sun, 24 Aug 2014 20:20:45 +0200
Subject: [PATCH 13/13] Do not show messagebox when python script raise an error

If app.useGUI is false and error occur while running python script
error message is printed on stderr and no user intervention is needed
to close messagebox with error message.

As a plus mainApp() function is almost as it should be. If we manage to
change app.init() function to do reasonable job even if useGUI is false
than we can dispose of three line around it and change

bool runApplication = app.useGUI;
app.useGUI = true;
int appRetVal=app.init();
app.useGUI = runApplication;

into

int appRetVal=app.init();
---
 scribus/main_nix.cpp                          |    3 ++-
 scribus/main_win32.cpp                        |    3 ++-
 scribus/plugins/scriptplugin/scriptercore.cpp |   10 ++++------
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/scribus/main_nix.cpp b/scribus/main_nix.cpp
index f5ce6e2..dc698b2 100644
--- a/scribus/main_nix.cpp
+++ b/scribus/main_nix.cpp
@@ -82,6 +82,7 @@ int mainApp(int argc, char **argv)
 	bool runApplication = app.useGUI;
 	app.useGUI = true;
 	int appRetVal=app.init();
+	app.useGUI = runApplication;
 	if (appRetVal==EXIT_FAILURE)
 		return(EXIT_FAILURE);
 	if (!app.pythonScript.isNull())
@@ -89,7 +90,7 @@ int mainApp(int argc, char **argv)
 		if (ScCore->primaryMainWindow()->scrActions.contains("scripterRunPythonScript"))
 			ScCore->primaryMainWindow()->scrActions.value("scripterRunPythonScript")->trigger();
 	}
-	if (runApplication)
+	if (app.useGUI)
 		return app.exec();
 	return EXIT_SUCCESS;	
 }
diff --git a/scribus/main_win32.cpp b/scribus/main_win32.cpp
index bd6339e..25f6a39 100644
--- a/scribus/main_win32.cpp
+++ b/scribus/main_win32.cpp
@@ -118,6 +118,7 @@ int mainApp(ScribusQApp& app)
 		bool runApplication = app.useGUI;
 		app.useGUI = true;
 		appRetVal = app.init();
+		app.useGUI = runApplication;
 		if (appRetVal != EXIT_FAILURE)
 		{
 			if (!app.pythonScript.isNull())
@@ -125,7 +126,7 @@ int mainApp(ScribusQApp& app)
 				if (ScCore->primaryMainWindow()->scrActions.contains("scripterRunPythonScript"))
 					ScCore->primaryMainWindow()->scrActions.value("scripterRunPythonScript")->trigger();
 			}
-			if (runApplication)
+			if (app.useGUI)
 				appRetVal = app.exec();
 		}
 #ifndef _DEBUG
diff --git a/scribus/plugins/scriptplugin/scriptercore.cpp b/scribus/plugins/scriptplugin/scriptercore.cpp
index 3218341..c2b10fe 100644
--- a/scribus/plugins/scriptplugin/scriptercore.cpp
+++ b/scribus/plugins/scriptplugin/scriptercore.cpp
@@ -297,11 +297,9 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
 		// into a StringIO buffer for later extraction.
 		cm        += QString("except:\n");
 		cm        += QString("    import traceback\n");
-		cm        += QString("    import scribus\n");                  // we stash our working vars here
-		cm        += QString("    scribus._f=cStringIO.StringIO()\n");
-		cm        += QString("    traceback.print_exc(file=scribus._f)\n");
-		cm        += QString("    _errorMsg = scribus._f.getvalue()\n");
-		cm        += QString("    del(scribus._f)\n");
+		cm        += QString("    _errorMsg = traceback.format_exc()\n");
+		if (!ScQApp->useGUI)
+			cm += QString("    traceback.print_exc()\n");
 		// We re-raise the exception so the return value of PyRun_StringFlags reflects
 		// the fact that an exception has ocurred.
 		cm        += QString("    raise\n");
@@ -326,7 +324,7 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
 				qDebug("Exception was:");
 				PyErr_Print();
 			}
-			else
+			else if (ScQApp->useGUI)
 			{
 				QString errorMsg = PyString_AsString(errorMsgPyStr);
 				// Display a dialog to the user with the exception
-- 
1.7.2.3

william

2014-08-29 02:45

updater  

0014-Test-to-convert-qmessagebox.patch (2,201 bytes)   
If app.useGUI is false, messages should use qWarning (or qDebug, qCritical or qFatal)
instead of QMessageBox.  This patch changes a few for testing.
Scribus 1.5 has about 250 calls to QMessageBox.
Can they be converted without duplicating the strings?

diff --git a/scribus/scribus.cpp b/scribus/scribus.cpp
--- a/scribus/scribus.cpp	2014-08-29 01:36:20.376668643 +0200
+++ b/scribus/scribus.cpp	2014-08-29 03:33:45.555606043 +0200
@@ -737,6 +737,12 @@
 bool ScribusMainWindow::warningVersion(QWidget *parent)
 {
 	bool retval = false;
+	if (!ScribusQApp::useGUI)
+	{
+		qWarning() << QObject::tr("Scribus Development Version");
+		qWarning() << QObject::tr("You are running a development version of Scribus 1.5.x. The document you are working with was created in Scribus 1.2.x.  Saving the current file under 1.5.x renders it unable to be edited in Scribus 1.2.x versions. To preserve the ability to edit in 1.2.x, save this file under a different name and further edit the newly named file and the original will be untouched. Are you sure you wish to proceed with this operation?");
+		return true;
+	}
 	int t = QMessageBox::warning(parent, QObject::tr("Scribus Development Version"), "<qt>" +
 								 QObject::tr("You are running a development version of Scribus 1.5.x. The document you are working with was created in Scribus 1.2.x.  Saving the current file under 1.5.x renders it unable to be edited in Scribus 1.2.x versions. To preserve the ability to edit in 1.2.x, save this file under a different name and further edit the newly named file and the original will be untouched. Are you sure you wish to proceed with this operation?") + "</qt>",
 								 QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
@@ -3872,6 +3878,11 @@
 	QFileInfo fi(fileName);
 	if (!fi.exists())
 	{
+		if (!ScribusQApp::useGUI)
+		{
+			qWarning() << QString(tr("File does not exist on the specified path :\n%1")).arg(QDir::toNativeSeparators(fileName));
+			return false;
+		}
 		QMessageBox::warning(this, CommonStrings::trWarning, tr("File does not exist on the specified path :\n%1").arg(QDir::toNativeSeparators(fileName)), 
 		                           CommonStrings::tr_OK);
 		return false;

william

2014-08-29 02:53

updater   ~0033359

I uploaded an additional patch from Juraj
0013: Do not show messagebox when python script raise an error

With this patch, when Scribus is run with --no-gui, Scribus writes python tracebacks to stderr.

william

2014-08-29 02:59

updater   ~0033360

I uploaded a test patch that I wrote to print the error message for scribus.openDoc() to stderr through qWarning() instead of to a dialog through QMessageBox when Scribus is run with --no-gui (ScribusQApp::useGUI is false).
0014: Test converting a QMessageBox error to qWarning when not using the GUI

Scribus has about 250 calls to QMessageBox that would need to be converted. In my test, I had to copy the message string. Is there a way to wrap QMessageBox similar to how scdebug.h wraps QDebug?

william

2014-08-29 03:43

updater  

serverplug.py (1,323 bytes)   
#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" 

Web server plugin

Tested with scribus 1.5.0

Author: William Bader

11Aug14 wb initial version

"""

# import utility packages

import sys

# check that the script is running from inside scribus

try:
    from scribus import *
    
except ImportError:
    print "This script only runs from within Scribus."
    sys.exit(1)

# import web server modules
    
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8080

# Handle incoming requests from the browser 

class myHandler(BaseHTTPRequestHandler):
	
	# Handler for the GET requests

	def do_GET(self):
		self.send_response(200)
		self.send_header('Content-type','text/html')
		self.end_headers()
		# Send the html message
		self.wfile.write("Hello World !")
		return

def main():

	try:
		# Create a web server and define the handler to manage the incoming request

		server = HTTPServer(('', PORT_NUMBER), myHandler)
		print 'Started httpserver on port ' , PORT_NUMBER
	
		# Wait forever for incoming http requests
		server.serve_forever()

	except KeyboardInterrupt:
		print '^C received, shutting down the web server'
		server.socket.close()

# start the script

if __name__ == '__main__':
	messageBox("Server", "Starting Web Server at http://localhost:8080/", ICON_INFORMATION)
	main()

serverplug.py (1,323 bytes)   

william

2014-08-29 03:48

updater   ~0033362

I uploaded an example script serverplug.py that makes Scribus into a web server that listens on http://localhost:8080/
It is only a test and returns a "Hello World!" message, but it shows an example of what is possible.

Kunda

2014-08-29 04:09

updater   ~0033363

Removed the 'Warning: Linux Only' from the 'Additional Information' field of this issue summary as per OPs comment in 0012572:0033180
This is not a linux only patch now because "The patches contain no operating-system dependent system calls. The patches now update main_win32.cpp similarly to main_nix.cpp."

jghali

2014-08-30 01:26

administrator   ~0033372

For now, i applied following patches:
12572-0002-sys.path-in-scripter-console-fixed.patch
12572-0004-Fix-module-name-for-objects-created-in-scripter.patch
12572-0005-Fix-broken-handling-of-prefs-option.patch
12572-0006-Startup-script-does-not-work.patch
12572-0007-Add-command-to-scripter-for-saving-and-reading-PDF-o.patch
12572-0008-Fix-PDFOptionsIO-readSettings.patch
12572-0009-Fix-PDFOptionsIO-readSettings-error-reporting.patch
12572-0010-Fix-omitting-some-words-from-error-message-in-messag.patch
12572-0012-Python-scripter-can-not-export-document-to-version-P.patch

The three last ones need to be discussed a bit more and I will wait for the return of vacation of other team members.

Kunda

2014-08-30 01:34

updater   ~0033373

Thanks jghali, william and Juraj !!
So the patches in question are:
12572-0001-Run-python-script-from-CLI.patch
12572-0003-Do-not-use-and-xB0-as-identifier-in-python.patch
12572-0011-Enable-the-use-of-no-gui-CLI-option.patch

william

2014-08-30 02:01

reporter   ~0033375

Thanks!

Can you also apply the corresponding patches in http://bugs.scribus.net/view.php?id=12594 to Scribus 1.4?

What are the issues with the patches
0001-Run-python-script-from-CLI.patch,
0011-Enable-the-use-of-no-gui-CLI-option.patch and
0013-Do-not-show-messagebox-when-python-script-raise-an-error?
What would it take to have them approved?

Is the issue with 0003-Do-not-use-and-xB0-as-identifier-in-python.patch that it might affect existing scripts? Would it help to have an example where this patch is required?

Is there a better way to do what I did in the 14th patch?
Could I make a ScribusMessageBox class that inherits from QMessageBox and adds a scribusWarning() function that calls either QMessageBox::warning or qWarning based on ScribusQApp::useGUI?

william

2014-09-02 01:48

reporter  

0015-All-in-one-Run-python-script-from-CLI_v150.patch (11,766 bytes)   
From 2b9d76105b2c313c8bdb464c097413b7bd480385 Mon Sep 17 00:00:00 2001
From: Juraj Fedel <wtxnh-scribus@yahoo.com.au>
Date: Sun, 31 Aug 2014 15:56:42 +0200
Subject: [PATCH] All in one: Run python script from CLI

Add the '--python-script file' CLI option to run 'file' as python
script.

Also enable the use of --no-gui CLI option
This option is useful to run a python script and then exit scribus with:
scribus --no-gui --python-script myscript.py
if it is run as:
scribus --python-script myscript.py
then after myscript.py finishes, the GUI is started.

I moved signal appStarted() from scribuscore to scribusapp,
it seems to be better place.
---
 scribus/main_nix.cpp                          |    8 ++---
 scribus/main_win32.cpp                        |    6 ++--
 scribus/plugins/scriptplugin/scriptercore.cpp |   29 +++++++++++-----
 scribus/plugins/scriptplugin/scriptercore.h   |    1 +
 scribus/scribusapp.cpp                        |   46 +++++++++++++++++++-----
 scribus/scribusapp.h                          |    4 ++
 scribus/scribuscore.cpp                       |    5 ---
 scribus/scribuscore.h                         |    5 ---
 8 files changed, 67 insertions(+), 37 deletions(-)

diff --git a/scribus/main_nix.cpp b/scribus/main_nix.cpp
index 8132b43..56156a1 100644
--- a/scribus/main_nix.cpp
+++ b/scribus/main_nix.cpp
@@ -78,13 +78,11 @@ int mainApp(int argc, char **argv)
 #endif // QT_VERSION == 0x040400
 #endif // Q_OS_UNIX
 	app.parseCommandLine();
+	int appRetVal=app.init();
+	if (appRetVal==EXIT_FAILURE)
+		return(EXIT_FAILURE);
 	if (app.useGUI)
-	{
-		int appRetVal=app.init();
-		if (appRetVal==EXIT_FAILURE)
-			return(EXIT_FAILURE);
 		return app.exec();
-	}
 	return EXIT_SUCCESS;	
 }
 
diff --git a/scribus/main_win32.cpp b/scribus/main_win32.cpp
index 9dccfa1..5ea48bc 100644
--- a/scribus/main_win32.cpp
+++ b/scribus/main_win32.cpp
@@ -114,10 +114,10 @@ int mainApp(ScribusQApp& app)
 	{
 #endif
 		app.parseCommandLine();
-		if (app.useGUI)
+		appRetVal = app.init();
+		if (appRetVal != EXIT_FAILURE)
 		{
-			appRetVal = app.init();
-			if (appRetVal != EXIT_FAILURE)
+			if (app.useGUI)
 				appRetVal = app.exec();
 		}
 #ifndef _DEBUG
diff --git a/scribus/plugins/scriptplugin/scriptercore.cpp b/scribus/plugins/scriptplugin/scriptercore.cpp
index 797f0ad..ac98839 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)
 {
@@ -66,6 +67,8 @@ ScripterCore::ScripterCore(QWidget* parent)
 
 	QObject::connect(pcon, SIGNAL(runCommand()), this, SLOT(slotExecute()));
 	QObject::connect(pcon, SIGNAL(paletteShown(bool)), this, SLOT(slotInteractiveScript(bool)));
+
+	QObject::connect(ScQApp, SIGNAL(appStarted()) , this, SLOT(slotRunPythonScript()) );
 }
 
 ScripterCore::~ScripterCore()
@@ -177,7 +180,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 +196,7 @@ void ScripterCore::runScriptDialog()
 		}
 		rebuildRecentScriptsMenu();
 	}
-	QDir::setCurrent(curDirPath);
+	// QDir::setCurrent(curDirPath);
 	FinishScriptRun();
 }
 
@@ -247,7 +250,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());
 	}
@@ -292,11 +295,9 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
 		// into a StringIO buffer for later extraction.
 		cm        += QString("except:\n");
 		cm        += QString("    import traceback\n");
-		cm        += QString("    import scribus\n");                  // we stash our working vars here
-		cm        += QString("    scribus._f=cStringIO.StringIO()\n");
-		cm        += QString("    traceback.print_exc(file=scribus._f)\n");
-		cm        += QString("    _errorMsg = scribus._f.getvalue()\n");
-		cm        += QString("    del(scribus._f)\n");
+		cm        += QString("    _errorMsg = traceback.format_exc()\n");
+		if (!ScCore->usingGUI())
+			cm += QString("    traceback.print_exc()\n");
 		// We re-raise the exception so the return value of PyRun_StringFlags reflects
 		// the fact that an exception has ocurred.
 		cm        += QString("    raise\n");
@@ -321,7 +322,7 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
 				qDebug("Exception was:");
 				PyErr_Print();
 			}
-			else
+			else if (ScCore->usingGUI())
 			{
 				QString errorMsg = PyString_AsString(errorMsgPyStr);
 				// Display a dialog to the user with the exception
@@ -353,6 +354,16 @@ void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
 	enableMainWindowMenu();
 }
 
+// needed for running script from CLI - this is activated by signal ScribusQApp::appStarted()
+void ScripterCore::slotRunPythonScript()
+{
+	if (!ScQApp->pythonScript.isNull())
+	{
+		slotRunScriptFile(ScQApp->pythonScript, true);
+		FinishScriptRun();
+	}
+}
+
 void ScripterCore::slotRunScript(const QString Script)
 {
 	// Prevent two scripts to be run concurrently or face crash!
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();
diff --git a/scribus/scribusapp.cpp b/scribus/scribusapp.cpp
index 98e397c..fc56dbd 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"
@@ -204,10 +206,9 @@ void ScribusQApp::parseCommandLine()
 		uc.fetch();
 	}
 	//Dont run the GUI init process called from main.cpp, and return
-	if (!header)
-		useGUI=true;
-	else
-		return;
+	if (header)
+		std::exit(EXIT_SUCCESS);
+	useGUI = true;
 	//We are going to run something other than command line help
 	for(int i = 1; i < argsc; i++) {
 		arg = args[i];
@@ -244,14 +245,27 @@ void ScribusQApp::parseCommandLine()
 					std::cout << tr("File %1 does not exist, aborting.").arg(prefsUserFile).toLocal8Bit().data() << std::endl;
 				}
 				showUsage();
-				useGUI=false;
-				return;
+				std::exit(EXIT_FAILURE);
 			} else {
 				++i;
 			}
 		} 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();
+				std::exit(EXIT_FAILURE);
+			} else {
+				++i;
+			}
 		} else {
 			fileName = QFile::decodeName(args[i].toLocal8Bit());
 			if (!QFileInfo(fileName).exists()) {
@@ -262,8 +276,7 @@ void ScribusQApp::parseCommandLine()
 					std::cout << tr("File %1 does not exist, aborting.").arg(fileName).toLocal8Bit().data() << std::endl;
 				}
 				showUsage();
-				useGUI=false;
-				return;
+				std::exit(EXIT_FAILURE);
 			}
 			else
 			{
@@ -283,8 +296,20 @@ int ScribusQApp::init()
 	processEvents();
 	ScCore->init(useGUI, swapDialogButtonOrder, filesToLoad);
 	int retVal=EXIT_SUCCESS;
-	if (useGUI)
+	/* TODO:
+	 * When Scribus is truly able to run without GUI
+	 * we should uncomment if (useGUI)
+	 * and delete if (true)
+	 */
+	// if (useGUI)
+	if (true)
 		retVal=ScCore->startGUI(showSplash, showFontInfo, showProfileInfo, lang, prefsUserFile);
+
+	// A hook for plugins and scripts to trigger on. Some plugins and scripts
+	// require the app to be fully set up (in particular, the main window to be
+	// built and shown) before running their setup.
+	emit appStarted();
+
 	return retVal;
 }
 
@@ -464,7 +489,8 @@ 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") );
+	printArgLine(ts, ARG_NOGUI_SHORT, ARG_NOGUI, tr("Do not start GUI") );
 	
 #if defined(_WIN32) && !defined(_CONSOLE)
 	printArgLine(ts, ARG_CONSOLE_SHORT, ARG_CONSOLE, tr("Display a console window") );
diff --git a/scribus/scribusapp.h b/scribus/scribusapp.h
index fe27092..e2f78a1 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;
@@ -105,6 +106,9 @@ class SCRIBUS_API ScribusQApp : public QApplication
 
 	protected slots:
 		void downloadComplete(const QString& t);
+
+	signals:
+		void appStarted();
 };
 
 #endif
diff --git a/scribus/scribuscore.cpp b/scribus/scribuscore.cpp
index 292406b..a275161 100644
--- a/scribus/scribuscore.cpp
+++ b/scribus/scribuscore.cpp
@@ -140,11 +140,6 @@ int ScribusCore::startGUI(bool showSplash, bool showFontInfo, bool showProfileIn
 	{
 		scribus->slotRaiseOnlineHelp();
 	}
-
-	// A hook for plugins and scripts to trigger on. Some plugins and scripts
-	// require the app to be fully set up (in particular, the main window to be
-	// built and shown) before running their setup.
-	emit appStarted();
 	
 	return EXIT_SUCCESS;
 }
diff --git a/scribus/scribuscore.h b/scribus/scribuscore.h
index ce0e6ef..91f5eb2 100644
--- a/scribus/scribuscore.h
+++ b/scribus/scribuscore.h
@@ -142,11 +142,6 @@ protected:
 	bool m_HaveGS;
 	bool m_HavePngAlpha;
 	bool m_HaveTiffSep;
-	
-	
-signals:
-	void appStarted();
-
 };
 
 /*
-- 
1.7.2.3

william

2014-09-02 01:48

reporter  

0003-errtest.py (1,108 bytes)   
# Test variable name translations
#
# This script is a test case for patch 0003
#     in http://bugs.scribus.net/view.php?id=12572 (1.5)
#     and http://bugs.scribus.net/view.php?id=12594 (1.4)
#
# You can run this script from the command line
# (if you have applied the command line patches)
# or from the Scripter menu item.
#
# This script runs OK with English strings:
# scribus-1.4.4 -g -py errtest.py -l en
# 0.0352777777778
# 0.352777777778
#
# The script fails with Russian strings:
# scribus-1.4.4 -g -py errtest.py -l ru
# 0.0352777777778
# Traceback (most recent call last):
#   File "<string>", line 8, in <module>
#   File "errtest.py", line 3, in <module>
#     print scribus.mm
# AttributeError: 'module' object has no attribute 'mm'
#
# The script fails with a different error with Ukrainian strings:
# scribus-1.4.4 -g -py errtest.py -l uk
# Traceback (most recent call last):
#   File "<string>", line 8, in <module>
#   File "errtest.py", line 2, in <module>
#     print scribus.cm
# AttributeError: 'module' object has no attribute 'cm'

import scribus
print scribus.cm
print scribus.mm
0003-errtest.py (1,108 bytes)   

william

2014-09-02 01:55

reporter   ~0033447

Juraj has a new patch 0015 that replaces and obsoletes patches 0001, 0011 and 0013. This patch is cleaner and works with the current SVN source.

0015-All-in-one-Run-python-script-from-CLI_v150.patch

william

2014-09-02 02:02

reporter   ~0033448

Juraj provided examples of the necessity of the patch 0003-Do-not-use-and-xB0-as-identifier-in-python.patch

1) If you open the python scripting console with Script -> Show Console and then run
  help(scribus)
you get the python error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib64/python2.7/site.py", line 459, in __call__
    return pydoc.help(*args, **kwds)
  File "/usr/lib64/python2.7/pydoc.py", line 1745, in __call__
    self.help(request)
  File "/usr/lib64/python2.7/pydoc.py", line 1792, in help
    else: doc(request, 'Help on %s:')
  File "/usr/lib64/python2.7/pydoc.py", line 1529, in doc
    pager(render_doc(thing, title, forceload))
  File "/usr/lib64/python2.7/pydoc.py", line 1524, in render_doc
    return title % desc + '\n\n' + text.document(object, name)
  File "/usr/lib64/python2.7/pydoc.py", line 326, in document
    if inspect.ismodule(object): return self.docmodule(*args)
  File "/usr/lib64/python2.7/pydoc.py", line 1113, in docmodule
    result = result + self.section('DATA', join(contents, '\n'))
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 4810: invalid start byte

2) If you run the commands
  import scribus
  print scribus.cm
  print scribus.mm
it works in English but gets errors in some other languages.
See the attached errtest.py script.

# Test variable name translations
#
# This script is a test case for patch 0003
# in http://bugs.scribus.net/view.php?id=12572 (1.5)
# and http://bugs.scribus.net/view.php?id=12594 (1.4)
#
# You can run this script from the command line
# (if you have applied the command line patches)
# or from the Scripter menu item.
#
# This script runs OK with English strings:
# scribus-1.4.4 -g -py errtest.py -l en
# 0.0352777777778
# 0.352777777778
#
# The script fails with Russian strings:
# scribus-1.4.4 -g -py errtest.py -l ru
# 0.0352777777778
# Traceback (most recent call last):
# File "<string>", line 8, in <module>
# File "errtest.py", line 3, in <module>
# print scribus.mm
# AttributeError: 'module' object has no attribute 'mm'
#
# The script fails with a different error with Ukrainian strings:
# scribus-1.4.4 -g -py errtest.py -l uk
# Traceback (most recent call last):
# File "<string>", line 8, in <module>
# File "errtest.py", line 2, in <module>
# print scribus.cm
# AttributeError: 'module' object has no attribute 'cm'

import scribus
print scribus.cm
print scribus.mm

jghali

2014-09-02 06:57

administrator   ~0033450

>> What would it take to have them approved?

Well someone other than me should review them. I see one clear issue in the first patch tho: not setting current directory is something which would likely break existing scripts.

william

2014-09-06 04:55

reporter  

william

2014-09-06 05:22

reporter   ~0033537

I uploaded a patch eliminate calls to QMessageBox when there is no GUI.
0016-scribus15-20140906-062901-messagebox.pat

This patch applies after 0003-Do-not-use-and-xB0-as-identifier-in-python and 0015-All-in-one-Run-python-script-from-CLI_v150.

This patch also replaces my test 0014-Test-to-convert-qmessagebox.

This patch creates a derived class ScribusMessageBox that uses QMessageBox when there is a GUI and qDebug when there is no GUI. Most of the changes only replace QMessageBox with ScribusMessageBox. I simplified ScribusMessageBox by not implementing obsolete member functions of QMessageBox. When I found calls to obsolete member functions, I updated them.

The purpose is to allow shell scripts to run Scribus without hanging if Scribus gets an error.

cbradney

2014-09-15 20:05

administrator   ~0033703

I'm going to help Jean review this. First.. can someone please identify all the patch files you want reviewed/tested/committed and zip them in one archive. Include a description of each patch (even if its just a copy of the above text).

Reason I'm asking this: We have many bugs with lots of patches and many are different versions of each other. This will clear this up for me and save my time.

Thanks
Craig

Kunda

2014-09-15 21:08

updater   ~0033706

I just tried to understand what has been done and what is left to be done and now I"m a little confused :/ Looking forward to seeing a clear breakdown of this issue. It's important to understand how many different files are effected implementing this patch.

william

2014-09-15 23:41

reporter   ~0033710

1)

Thanks, most of the patches to review for inclusion have already been combined as
0015-All-in-one-Run-python-script-from-CLI_v150.patch
That patch contains all but one of the remaining patches by Juraj that have not already been committed. Can you please review it and either commit it or say what needs to be changed?

2)

Also, patch 0003-Do-not-use-and-xB0-as-identifier-in-python.patch needs to be reviewed for being committed. It is separate because it does not depend on the other patches, and one of the reviewers had a question about whether it was necessary, and I posted a python script to demonstrate the issue.

3)

In addition, I have an additional patch 0016-scribus15-20140906-062901-messagebox.pat that replaces QMessageBox with a class that calls QMessageBox when there is a GUI and calls qDebug() otherwise. I am still working on it, so please don't commit 0016, but I would be interested in any comments. I am changing it to write to cerr instead of qDebug(), and to allow an additional default button when there is no GUI because some of the dialogs in the GUI default to not continuing the command.

Kunda

2014-09-16 23:53

updater   ~0033717

Last edited: 2014-09-16 23:54

First I want to say, this is very exciting development. Thank you to all that are involved.
Secondly, I interpreted Craig asking to simplify things which it looks like you have...to a degree.

I need to be careful here because I may be talking out of my behind...but maybe implementing 1 thing at at time..instead of introducing what maybe considered 'context switching'. I've heard this also called a "multi-headed, kitten-eating monster of a patch" (see: http://webchick.net/please-stop-eating-baby-kittens)

I'm sure Craig will comment on what he is specifically needing elaborations on. Perhaps an IRC session on this topic would be merited?

Edit: spelling

william

2014-09-17 03:31

reporter   ~0033721

Juraj originally submitted 13 small patches. After the majority were accepted, he had to rebase the remaining patches, and he made patch 0015 with the majority of the remaining patches. It is not a monster patch. It mostly adds parsing for the command line arguments --no-gui and --python-script.

My patch to replace QMessageBox is large, mainly because Scribus has about 250 calls to QMessageBox.

If it would help to have an IRC session, the best times for me for the next two months are between 3pm and 9pm US Eastern Time.

william

2014-09-21 17:05

reporter  

william

2014-09-21 17:10

reporter   ~0033788

Juraj has provided an updated set of patches that divides his patches into smaller pieces and includes additional bug fixes. It is attached as monster_beheaded_v150.tar.gz and the README is below.

---

To ease reviewing this issue, I have split it into several smaller
patches. Thank you Kunda for inspiring link:
http://webchick.net/please-stop-eating-baby-kittens
Hopefully few kittens will be saved by this :)

Since I am not able to join the eventual IRC session, here follows
detailed descriptions of all patches. You should forget about all previous
unapplied patches and consider only the patches in this archive.

0001-Run-python-script-from-CLI.patch

    Script can be run on Scribus startup with CLI option (-py, --python-script).

    How is it implemented?
    It adds code for parsing CLI options and
    in file scriptercore.cpp there is a new function:
    void ScripterCore::slotRunPythonScript()
    which is connected to the appStarted() signal emitted during Scribus
    initialization.

    The script is executed in the main interpreter. Originally it used
    a sub-interpreter, but I changed my mind so that it now closes the
    feature request 9331 (http://bugs.scribus.net/view.php?id=9331).
    Also if a script is executed in a sub-interpreter, the working directory is
    temporarily changed so it is possible only to execute a script successfully
    that is specified by an absolute path (e.g. ../script.py will not
    work). When a script is executed in the main interpreter, this restriction
    does not apply (please do not enforce directory changes for the main
    interpreter also, now that I have pointed this out :).

    After the script is executed, Scribus continues running with a GUI.

    To think about:
    In file scribus/plugins/scriptplugin/scriptercore.h
    there are now a few slots with similar names (mine included):
        void slotRunScriptFile(QString fileName, bool inMainInterpreter = false);
        void slotRunPythonScript(); // needed for running python script from CLI
        void slotRunScript(const QString Script);
        void runStartupScript();
    It is starting to be confusing (or already is :) which slot is used for
    what purpose. Maybe some renaming could help here (or some short
    comments). I realize that the names for new slots are not chosen well.
    Since I am not very versed in English I can only suggest a few
    alternatives and leave decision to somebody else.
    Instead of 'slotRunPythonScript' we could use one of:
    slotRunCLIPythonScript
    slotCLIPythonScript
    cliPythonScript
    slotRunCLIScript
    slotCLIScript
    cliScript
    runCLIScript


0002-Enable-to-run-Scribus-without-GUI.patch

    With this patch, Scribus does not continue with a GUI after initialization
    if the -g or --no-gui CLI option is used.
    This is most useful when Scribus is run with the --python-script option so
    that when the python script ends, Scribus ends too without the need for
    the user to close the main window manually.

    During Scribus initialization in function
    void ScribusQApp::parseCommandLine()
    if an error is detected, Scribus exits right away instead of
    needlessly setting useGUI=false and returning to mainApp().

    This behavior will enable running the GUI from function
    int mainApp(int argc, char **argv)
    only if needed.

    One controversial point in this patch is in function
    int ScribusQApp::init()
    where 'if (true)' is used instead of 'if (useGUI)'.
    There seems to be no easy solution to this.

    So, in fact Scribus does need the GUI to run,
    but it can be run without user interaction.


0003-Move-appStarted-signal-from-scribuscore.cpp-into-scr.patch

    Move appStarted() signal from scribuscore.cpp into scribusapp.cpp

    Currently appStarted() signal is emitted from ScribusCore::startGUI()
    which should be called only if the GUI is used. With this change signal
    will be emitted even if the GUI is not used.

    I was tempted to change the signature of this signal to
    void appStarted(bool useGUI);
    but resisted, leaving this change to developers if they find it useful.

    Since appStarted() signal is not used anywhere, this change should be
    painless.


0004-Do-not-show-messagebox-when-python-script-raise-an-e.patch

    Do not show a messagebox when a python script raises an error.

    When python raises an error and app.useGUI is false, the error message
    is printed on stderr instead of being displayed in a messagebox that
    requires user intervention to close.

    The above statement is valid only for _python_ errors (e.g. syntax errors
    or errors raised by the scripter) not for Scribus functions that pop up
    a messagebox on errors.


0005-Simplify-python-code.patch

    This patch does not change any functionality. It just does the same
    job with less/simpler code.

    While I was editing the previous patch, I saw an opportunity and seized it.

    This is definitely a 'context switching' element, perhaps few kittens
    has been eaten by this :)


0006-Do-not-change-working-directory-when-script-is-execu.patch

    Do not change the working directory when a script is executed.

    To be honest, this patch is not required when running CLI script in the main
    interpreter. It was introduced before, when I was using a sub-interpreter
    instead of the main interpreter. At that time it enabled the possibility of
    running a python script that is not in the current working directory e.g.
    you could run:
    scribus-1.5.0 -py subdirectory/script.py
    Without the patch, above command would fail.

    In the process of changing to use the main interpreter instead of
    a sub-interpreter, I failed to notice that this patch is not needed
    any more, therefore it was left here.

    Why do I think that changing directory is not good anyway?
    With introduction of --python-script CLI option, Scribus can be perceived
    as a special python interpreter that has one extra module available
    (import scribus). I would be very surprised to find an interpreter for any
    language that changes its working directory into the directory where the
    executed script is located.

    I do realize that some scripts in use may be broken by applying this
    patch, but I think those scripts should be adopted/changed instead.

    If you are not convinced by my arguments for applying this patch, please
    consider the fact that the working directory is changed in function
    void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
    while it is changed back to its previous value in function
    void ScripterCore::runScriptDialog()
    I would prefer to move corresponding code from runScriptDialog() into
    the slotRunScriptFile() function.


0007-Do-not-use-and-xB0-as-identifier-in-python.patch

    Do not use % and \xB0 as identifier in python.

    These characters are inaccessible in python as identifiers anyway
    so either don't create them or replace them similarly as 'in' -> 'inch'
    Here I choose to omit them for simplicity.

    Also it is not wise to use translated string as variable names in
    programming (python scripts can break depending on setting of the locale)
    therefore use unitGetUntranslatedStrFromIndex instead of unitGetStrFromIndex


             1) If you open the python scripting console with Script -> Show
             Console and then run
               help(scribus)
             you get the python error

             Traceback (most recent call last):
               File "<console>", line 1, in <module>
               File "/usr/lib64/python2.7/site.py", line 459, in __call__
                 return pydoc.help(*args, **kwds)
               File "/usr/lib64/python2.7/pydoc.py", line 1745, in __call__
                 self.help(request)
               File "/usr/lib64/python2.7/pydoc.py", line 1792, in help
                 else: doc(request, 'Help on %s:')
               File "/usr/lib64/python2.7/pydoc.py", line 1529, in doc
                 pager(render_doc(thing, title, forceload))
               File "/usr/lib64/python2.7/pydoc.py", line 1524, in render_doc
                 return title % desc + '\n\n' + text.document(object, name)
               File "/usr/lib64/python2.7/pydoc.py", line 326, in document
                 if inspect.ismodule(object): return self.docmodule(*args)
               File "/usr/lib64/python2.7/pydoc.py", line 1113, in docmodule
                 result = result + self.section('DATA', join(contents, '\n'))
             UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position
             4810: invalid start byte

             2) If you run the commands
               import scribus
               print scribus.cm
               print scribus.mm
             it works in English but gets errors in some other languages.
             See the attached errtest.py script.

             # Test variable name translations
             #
             # This script is a test case for patch 0003
             # in http://bugs.scribus.net/view.php?id=12572 [^] (1.5)
             # and http://bugs.scribus.net/view.php?id=12594 [^] (1.4)
             #
             # You can run this script from the command line
             # (if you have applied the command line patches)
             # or from the Scripter menu item.
             #
             # This script runs OK with English strings:
             # scribus-1.5.0 -g -py errtest.py -l en
             # 0.0352777777778
             # 0.352777777778
             #
             # The script fails with Russian strings:
             # scribus-1.5.0 -g -py errtest.py -l ru
             # 0.0352777777778
             # Traceback (most recent call last):
             # File "<string>", line 8, in <module>
             # File "errtest.py", line 3, in <module>
             # print scribus.mm
             # AttributeError: 'module' object has no attribute 'mm'
             #
             # The script fails with a different error with Ukrainian strings:
             # scribus-1.5.0 -g -py errtest.py -l uk
             # Traceback (most recent call last):
             # File "<string>", line 8, in <module>
             # File "errtest.py", line 2, in <module>
             # print scribus.cm
             # AttributeError: 'module' object has no attribute 'cm'



And finally for everyone who reads this far, here is one nugget not
presented before. With only two lines of edited code it prevents one
crash and closes two bugs :)

0008-Run-startup-script-only-after-Scribus-is-fully-initi.patch

    Run startup script only after Scribus is fully initialized.

    This patch will close issues 0011336 and 0008552

    Note that this patch could be applied without the rest of the patches
    included in this set. In that case, it should be modified.
    Change
        QObject::connect(ScQApp, SIGNAL(appStarted()) , this, SLOT(runStartupScript()) );
    to
        QObject::connect(ScCore, SIGNAL(appStarted()) , this, SLOT(runStartupScript()) );

Kunda

2014-09-21 19:43

updater   ~0033791

I know that adding commentary that I'm about to add is not recommended on the bug tracker. But as an exception, I want to say: "Holy Smokes..this is the most epic bug thread that I've experienced so far!"
Thanks for delivering the goods, fellas! :)

ale

2014-09-23 10:03

manager   ~0033821

if the team has not the resources to manage those patches, i think it could become a good test case for the github repository.

JLuc

2014-10-08 20:09

updater   ~0033944

Last edited: 2014-10-08 20:24

Hello William,

It looks like some of these patches have been merged in svn and some havent
It's difficult now to know where this report is about now.

Why are there so many patches in a single reports ?

Could you either merge them in a single patch
or post them in separate reports ?
And delete the uploaded patches that have been allready merged or that are now outdated ?
It would make it easier to test the relevant patches before merge.

Thanks a lot for your commitment

Kunda

2014-10-08 20:20

updater   ~0033945

Jluc,
I can follow the issue thread pretty easily. Perhaps William doesn't have privileges to delete previous attachments which makes this issue look a little overwhelming or cluttered.

Essentially, all the patches that jghali applied can stay applied. All that remains to be applied is monster_beheaded_v150.tar.gz
Is that correct ?

FYI, we also need testers to test these patches.

william

2014-10-08 21:05

reporter   ~0033948

Kunda, that is correct -- the patches that were applied a few weeks ago can stay applied and the only remaining patches are in monster_beheaded_v150.tar.gz.

The other patches have either already been applied or can be ignored.

I have privileges to remove attachments. If you want, I can remove the old patches. I had left them to provide a context for the notes, but maybe now it is too confusing to have so many old versions.

Do not apply my 0016-scribus15-20140906-062901-messagebox.pat patch that replaces QMessageBox. I am still working on it, but can you let me know if it would need any changes to be acceptable?

Kunda

2014-10-09 00:34

updater   ~0033955

It may be better to keep the patches for those who want to study the workflow and logic. For those that just want the facts just pay attention to monster_beheaded*.zip and jean's patch commits in 0012572:0033372

re: 0016-scribus15-20140906-062901-messagebox.pat replacing QMessageBox
I think perhaps opening a new issue for this ?

Kunda

2014-10-17 15:40

updater   ~0034035

Hey William,
Lets move 0016 to a new issue? and start de-cluttering this ticket.

JLuc

2014-10-17 18:11

updater   ~0034041

Last edited: 2014-10-17 18:16

I applied all patches (1 to 8) being part of "beheaded" patch on today svn and it compiled fine.

I tried 0011336 and 0008552 and these bugs are fixed with patch (no more crash)

I tried the example py script and command line given in the above report : it works also fine. Great.

Question : when scribus is launched, it opens the file open dialog. This modal dialog prevents the launch of the script. Maybe this should be optionnaly disabled ?

Note for other testers : you ought to create 2 textboxes in mydoc.sla first
AND these have to be named "Text1 and "Text2". (non english langages testers have to rename their boxes OR change the script to fit the names of the boxes)

william

2014-10-17 18:37

reporter   ~0034042

To skip the file dialog, in File -> Preferences -> User Interface -> Start Up, I unchecked "Show Startup Dialog".
Do you want the --no-gui command line option to skip the startup dialog (if the dialog is enabled) or a new option similar to the current --no-splash option?

Kunda

2014-10-17 20:03

updater   ~0034044

Quick concern: By introducing command line flexibility is there a potential we are introducing potential exploits for the different OSs Scribus can run on ?

ale

2014-10-20 09:47

manager   ~0034068

i'm no security expert, but as long as we don't have executable code inside of the .sla file, i think that we are rather on the safe side...

(or, better, all code in a .sla file cannot be harmful without the user doing something... see the render frame...)

adding parameters (files to be exectuted and so on) to the command line, is an explicit choice by the user, and can imo does not lead to an increased risk.

JLuc

2014-10-20 13:09

updater   ~0034069

William : Yes, i would appreciate a --no-gui option, because because there is no use in launching a script with the command line, if one has to launch the GUI first to uncheck that option... at least on a desktop where the usual GUI use of scribus also happens.
This is only my point of view and i'm not a core dev.
You should talk to the real scribus devs so as to get that patch merged or have answers on why it could not be merged. (via newsgroups : news://news.gmane.org:119/gmane.comp.graphics.scribus.devel
or via mail : http://lists.scribus.net/mailman/listinfo/scribus-dev
or possibily via irc in the evening)

william

2014-10-20 18:11

reporter   ~0034071

Last edited: 2014-10-20 18:16

Thanks, do you want the existing --no-gui option to suppress the start up dialog or do you want a new option (maybe --no-startup-dialog) to suppress the start up dialog?

To answer an earlier question, I think that a command line option to run a script does not open any new security issues because 1) you already need command line access to run Scribus with the command line option to run a script, and 2) you can already run scripts from inside Scribus using the "Scripter" tool bar option.

JLuc

2014-10-20 21:38

updater   ~0034076

William: i dont know what the --no-gui command line option does. Where can I find documentation about it ?

IMO when --python-script option is used, scribus should not launch the startup-dialog at all, because ATM this startup dialog blocks the launch of the python script.

william

2014-10-20 22:05

reporter   ~0034077

I think that the documentation is currently in the patches and in the patched version of Scribus if you use the --help command line option.

Juraj's original version of the patch added a single --python-script option to run a script and then exit.

In cleaning up the Scribus initialization, he split the functionality into two options, --python-script to run a script as soon as Scribus finishes initializing itself and --no-gui to exit Scribus before entering the main loop for user input.

If you are running Scribus from a shell script and do not want any user interaction, then use both --python-script and --no-gui.

If you want to write a script that sets up a Scribus session that you can continue interactively, then use --python-script but do not use --no-gui.

The next issue is that Scribus opens the startup dialog during initialization, so if you have not disabled the startup dialog in the preferences, Scribus will open it even if you specify both --python-script and --no-gui. That defeats the purpose of having a method of running Scribus from a shell script with no user interaction. Since users might want the startup dialog when they run Scribus normally, we were suggesting either a --no-startup-dialog option or making --no-gui suppress the startup dialog (if it is enabled in the preferences).

JLuc

2014-10-21 08:27

updater   ~0034082

OK for a --no-startup-dialog option
(if you think scribus should not force-default to no startup-dialog when --python-script is used)

cbradney

2014-10-21 20:20

administrator   ~0034098

I'm committing these as they are.
My opinion: both options should turn off the startup dialog. Feel free to submit a patch to achieve that in a new bug.

JLuc

2014-10-21 20:44

updater   ~0034100

Yeah, party tonight !

Congrats to all :-)

william

2014-10-22 01:09

reporter   ~0034116

Last edited: 2016-05-01 00:45

Thanks!

A patch not to open the startup dialog is at 0012786

JLuc

2014-10-27 15:53

updater   ~0034160

thanks for the no startup dialog william.

Next step could be --to-pdf command line option :
0000238 New CL option: scribus --to-pdf <scribus-file> <output.pdf>

Issue History

Date Modified Username Field Change
2014-07-29 19:44 william New Issue
2014-07-29 19:44 william File Added: scribus-cmd-wb-28jul14.pat
2014-07-30 03:47 Kunda Relationship added related to 0000238
2014-07-30 03:47 Kunda Relationship added related to 0000967
2014-07-30 03:48 Kunda Relationship added related to 0009331
2014-07-31 21:36 william Note Added: 0033119
2014-08-05 00:46 william File Added: Run-python-script-from-CLI-4aug14.patch
2014-08-05 01:00 william Note Added: 0033180
2014-08-05 15:27 ale Note Added: 0033182
2014-08-06 13:35 Kunda Note Added: 0033188
2014-08-06 13:35 Kunda Status new => feedback
2014-08-07 06:16 christoph_s Assigned To => jghali
2014-08-07 06:16 christoph_s OS => Fedora
2014-08-07 06:16 christoph_s Platform => Linux
2014-08-07 06:26 christoph_s Status feedback => new
2014-08-07 06:27 christoph_s Status new => assigned
2014-08-08 04:21 Kunda Relationship added related to 0012594
2014-08-09 20:36 jghali Project Contributor Builds => Scribus
2014-08-20 23:42 william File Added: patch_bundle_v150-20aug14.tar.gz
2014-08-21 00:21 william Note Added: 0033306
2014-08-29 02:31 william File Added: 0013-Do-not-show-messagebox-when-python-script-raise-an-e.patch
2014-08-29 02:45 william File Added: 0014-Test-to-convert-qmessagebox.patch
2014-08-29 02:53 william Note Added: 0033359
2014-08-29 02:59 william Note Added: 0033360
2014-08-29 03:43 william File Added: serverplug.py
2014-08-29 03:48 william Note Added: 0033362
2014-08-29 04:09 Kunda Note Added: 0033363
2014-08-29 04:09 Kunda Category General => -
2014-08-29 04:09 Kunda Additional Information Updated
2014-08-30 01:26 jghali Note Added: 0033372
2014-08-30 01:34 Kunda Note Added: 0033373
2014-08-30 02:01 william Note Added: 0033375
2014-09-02 01:48 william File Added: 0015-All-in-one-Run-python-script-from-CLI_v150.patch
2014-09-02 01:48 william File Added: 0003-errtest.py
2014-09-02 01:55 william Note Added: 0033447
2014-09-02 02:02 william Note Added: 0033448
2014-09-02 06:57 jghali Note Added: 0033450
2014-09-06 04:55 william File Added: 0016-scribus15-20140906-062901-messagebox.pat
2014-09-06 05:22 william Note Added: 0033537
2014-09-13 15:17 Kunda Relationship added related to 0007741
2014-09-15 20:05 cbradney Note Added: 0033703
2014-09-15 21:08 Kunda Note Added: 0033706
2014-09-15 23:41 william Note Added: 0033710
2014-09-16 23:53 Kunda Note Added: 0033717
2014-09-16 23:54 Kunda Note Edited: 0033717
2014-09-17 03:31 william Note Added: 0033721
2014-09-21 17:05 william File Added: monster_beheaded_v150.tar.gz
2014-09-21 17:10 william Note Added: 0033788
2014-09-21 19:43 Kunda Note Added: 0033791
2014-09-23 10:03 ale Note Added: 0033821
2014-10-08 20:09 JLuc Note Added: 0033944
2014-10-08 20:20 Kunda Note Added: 0033945
2014-10-08 20:24 JLuc Note Edited: 0033944
2014-10-08 21:05 william Note Added: 0033948
2014-10-09 00:34 Kunda Note Added: 0033955
2014-10-15 14:13 Kunda Sticky Issue No => Yes
2014-10-15 17:02 Kunda Relationship added related to 0012774
2014-10-17 15:40 Kunda Note Added: 0034035
2014-10-17 18:11 JLuc Note Added: 0034041
2014-10-17 18:15 JLuc Note Edited: 0034041
2014-10-17 18:16 JLuc Note Edited: 0034041
2014-10-17 18:37 william Note Added: 0034042
2014-10-17 20:03 Kunda Note Added: 0034044
2014-10-20 09:47 ale Note Added: 0034068
2014-10-20 13:09 JLuc Note Added: 0034069
2014-10-20 18:11 william Note Added: 0034071
2014-10-20 18:16 william Note Edited: 0034071
2014-10-20 21:38 JLuc Note Added: 0034076
2014-10-20 22:05 william Note Added: 0034077
2014-10-21 08:27 JLuc Note Added: 0034082
2014-10-21 20:20 cbradney Note Added: 0034098
2014-10-21 20:23 cbradney Status assigned => resolved
2014-10-21 20:23 cbradney Fixed in Version => 1.5.0svn
2014-10-21 20:23 cbradney Resolution open => fixed
2014-10-21 20:23 cbradney Assigned To jghali => cbradney
2014-10-21 20:36 Kunda Sticky Issue Yes => No
2014-10-21 20:44 JLuc Note Added: 0034100
2014-10-21 20:48 cbradney Category - => Scripter
2014-10-22 01:09 william Note Added: 0034116
2014-10-22 11:45 Kunda Relationship replaced has duplicate 0009331
2014-10-22 11:55 Kunda Relationship added related to 0008967
2014-10-24 09:34 Kunda Relationship replaced has duplicate 0008967
2014-10-24 22:55 Kunda Patch => Yes
2014-10-27 15:53 JLuc Note Added: 0034160
2015-01-30 22:38 cbradney Status resolved => closed
2016-05-01 00:45 Kunda Note Edited: 0034116
2016-05-01 00:46 Kunda Relationship added related to 0012786