View Issue Details

IDProjectCategoryView StatusLast Update
0013351ScribusScripterpublic2015-10-04 23:08
Reporterale Assigned Tocbradney  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Fixed in Version1.5.1svn 
Summary0013351: remove the "ext" parameter from the scripter arguments
Descriptionif i understand it correctly the scripter used to pass to parameters in argv to the script: the script name and "ext" or "sub".

the comment in the code gives some sort of explanation for it:

https://github.com/scribusproject/scribus/blob/master/scribus/plugins/scriptplugin/scriptercore.cpp#L266

does anybody know if there is any script that checks for it?

what it is good for?

personally, i would like to see this parameter disappear so that the argv passed to the script is compatible to what gets passed to a normal script when directly run through python.
TagsNo tags attached.
PatchNo

Activities

ale

2015-09-15 07:09

manager   ~0036243

i've updated the priority to urgent:

if nobody knows what the sub/ext is good for, we should get rid of the extra argument before the new functionality starts to be used.

berteh

2015-09-15 07:32

reporter   ~0036244

Last edited: 2015-09-15 07:32

I agree it would be cleaner to remove it.

from a quick "sub" & "ext" search in current source code it seems to used nowhere but in scriptercore.cpp... maybe ask Craig Ringer?

ale

2015-09-16 08:53

manager   ~0036247

hi berteh,

i think that if don't any feedback from the team in the next few days, we should produce a patch for removing it...

asking craig ringer would be fun, but i'm not sure that he still knows what it's meant to do...
so many years went by...

should i try it?

cbradney

2015-09-16 16:28

administrator   ~0036249

Urgency: dropped to low. Where does having these options actually cause a problem or crash? Nowhere. Just because you want to get rid of something doesn't make it urgent.

Craig Ringer has done nothing with Scribus code for 5+ years, don't bother.

I don't know what it does but if you feel its useless, then submit a patch for code removal plus fixing any sample scripts that might use it if there are any.

ale

2015-09-16 17:53

manager   ~0036250

craig, it's urgent because we have to change it *before* people start writing scripts using the new arguments passing!

if berteh dans not propose a patch, soon, i will do so.

cbradney

2015-09-16 18:56

administrator   ~0036251

Its not urgent... It not in a stable or even a development release. We are not about to release a new version...

berteh

2015-09-17 08:31

reporter   ~0036253

patch is compiling for check, will upload today.

berteh

2015-09-17 09:28

reporter  

remove-extra-args-from-scripter.patch (4,518 bytes)   
From 175f28dbb541296dbede06f02a273260f93bfc5b Mon Sep 17 00:00:00 2001
From: berteh <berteh@gmail.com>
Date: Thu, 17 Sep 2015 11:19:12 +0200
Subject: [PATCH] remove ext/sub extra argument added by Scripter to run
 scripts

---
 scribus/plugins/scriptplugin/samples/3columnA4.py | 20 +++-----------------
 scribus/plugins/scriptplugin/samples/wordcount.py | 11 +++++------
 scribus/plugins/scriptplugin/scriptercore.cpp     | 16 ++--------------
 3 files changed, 10 insertions(+), 37 deletions(-)

diff --git a/scribus/plugins/scriptplugin/samples/3columnA4.py b/scribus/plugins/scriptplugin/samples/3columnA4.py
index 2609c72..23f0816 100644
--- a/scribus/plugins/scriptplugin/samples/3columnA4.py
+++ b/scribus/plugins/scriptplugin/samples/3columnA4.py
@@ -8,23 +8,9 @@
     # Do so _after_ the 'import scribus' and only import the names you need, such
     # as commonly used constants.
     import scribus
-except ImportError,err:
-    print "This Python script is written for the Scribus scripting interface."
-    print "It can only be run from within Scribus."
-    sys.exit(1)
-
-def main(argv):
-    """This is a simple way to demonstrate creating a doc on the fly. """
-
-    pass    # <--- Delete this line
-#########################
-# YOUR IMPORTS GO HERE  #
-#########################
-
-import sys
-
-try:
-    from scribus import *
+    #########################
+    # YOUR IMPORTS GO HERE  #
+    #########################
 except ImportError:
     print "This script only runs from within Scribus."
     sys.exit(1)
diff --git a/scribus/plugins/scriptplugin/samples/wordcount.py b/scribus/plugins/scriptplugin/samples/wordcount.py
index de372d6..26540a4 100644
--- a/scribus/plugins/scriptplugin/samples/wordcount.py
+++ b/scribus/plugins/scriptplugin/samples/wordcount.py
@@ -14,6 +14,7 @@
 import re
 
 TITLE = "Word count"
+
 
 def wordsplit(text):
     word_pattern = "([A-Za-zäöüÄÖÜß]+)"
@@ -35,8 +36,7 @@ def main():
                 text = getText(getSelectedObject(i))
                 words += len(wordsplit(text))
             except WrongFrameTypeError:
-                if sel_count == 1:
-                    # If there's only one object selected, display a message
+                if sel_count == 1: # If there's only one object selected, display a message
                     messageBox(TITLE, "Can't count words in a non-text frame", ICON_INFORMATION);
                     sys.exit(1)
                 else:
@@ -52,10 +52,9 @@ def main():
                     words += len(wordsplit(text))
                 except WrongFrameTypeError:
                     pass # ignore the error, it just wasn't a frame we can count
-
-    if words == 0: words = "No"
-    messageBox(TITLE, "%s words counted in %s" % (words, source),
-               ICON_INFORMATION)
+    if (words == 0):
+        words = "No"
+        messageBox(TITLE, "%s words counted in %s" % (words, source), ICON_INFORMATION)
 
 
 if __name__ == '__main__':
diff --git a/scribus/plugins/scriptplugin/scriptercore.cpp b/scribus/plugins/scriptplugin/scriptercore.cpp
index ad76e03..667b763 100644
--- a/scribus/plugins/scriptplugin/scriptercore.cpp
+++ b/scribus/plugins/scriptplugin/scriptercore.cpp
@@ -262,16 +262,8 @@ void ScripterCore::slotRunScriptFile(QString fileName, QStringList arguments, bo
 
 	// Make sure sys.argv[0] is the path to the script
 	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)
-		arguments.insert(1,QString("ext"));
-	else
-		arguments.insert(1,QString("sub"));
-
 	//convert arguments (QListString) to char** for Python bridge
-	/* typically arguments == ['path/to/script.py','ext','--argument1','valueforarg1','--flag']*/
+	/* typically arguments == ['path/to/script.py','--argument1','valueforarg1','--flag']*/
 	char **comm = new char*[arguments.size()];
 	for (int i = 0; i < arguments.size(); i++)
 	{
@@ -431,11 +423,7 @@ void ScripterCore::slotRunScript(const QString Script)
 	"In file tools/qgarray.cpp, line 147: Out of memory"
 	Anyway - sys.argv is set above
 	char* comm[1];
-	comm[0] = const_cast<char*>("scribus");
-	// the scripter console runs everything in the main interpreter
-	// tell the code it's running there.
-	comm[1] = const_cast<char*>("ext");
-	PySys_SetArgv(2, comm); */
+	comm[0] = const_cast<char*>("scribus"); */
 	// then run the code
 	PyObject* m = PyImport_AddModule((char*)"__main__");
 	if (m == NULL)

berteh

2015-09-17 09:28

reporter   ~0036255

attached patch works on svn1.5.1.svn
tested with most sample scripts.

ale

2015-09-17 09:50

manager   ~0036256

the part on removing the 'ext' looks good!

thanks for uploading the patch!

jurajF

2015-09-22 14:59

reporter   ~0036331

I have created similar patch.

Compared to berteh patch, this one does not change any scripts,
it does remove 'ext' argument from python console too,
and most important it does preserve information about the type of
interpreter script is running in (for this it set
scribus.mainInterpreter variable to True/False)

scribus.mainInterpreter is False if you run script from menu
Scripter -> Execute Script ...

scribus.mainInterpreter is True in all other cases:
1. If script is run from CLI:
   scribus -py script.py

2. In python console
   print scribus.mainInterpreter

3. If script is run from menu Scripter -> Execute Script ...
   _AND_ 'Run as Extension Script' box is checked in dialog.
   (to see this check box you need to go to
   File -> Preferences -> Scripter and check 'Enable Extension Scripts' box

Why should we care about scribus.mainInterpreter?
Because if it is true we can use PyQT for creting dialogs...

jurajF

2015-09-22 14:59

reporter  

0001-Remove-extra-argument-from-python_v15.patch (3,194 bytes)   
From 4c55e03bcf378182ee9da87f478c51a2c0aa0909 Mon Sep 17 00:00:00 2001
From: Juraj Fedel <wtxnh-scribus@yahoo.com.au>
Date: Tue, 22 Sep 2015 08:34:20 +0200
Subject: [PATCH] Remove extra argument from python and preserve information
 about type of interpreter script is running in.

---
 scribus/plugins/scriptplugin/scriptercore.cpp | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/scribus/plugins/scriptplugin/scriptercore.cpp b/scribus/plugins/scriptplugin/scriptercore.cpp
index 751d952..4ec6088 100644
--- a/scribus/plugins/scriptplugin/scriptercore.cpp
+++ b/scribus/plugins/scriptplugin/scriptercore.cpp
@@ -262,16 +262,8 @@ void ScripterCore::slotRunScriptFile(QString fileName, QStringList arguments, bo
 
 	// Make sure sys.argv[0] is the path to the script
 	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)
-		arguments.insert(1,QString("ext"));
-	else
-		arguments.insert(1,QString("sub"));
-
 	//convert arguments (QListString) to char** for Python bridge
-	/* typically arguments == ['path/to/script.py','ext','--argument1','valueforarg1','--flag']*/
+	/* typically arguments == ['path/to/script.py','--argument1','valueforarg1','--flag']*/
 	char **comm = new char*[arguments.size()];
 	for (int i = 0; i < arguments.size(); i++)
 	{
@@ -309,6 +301,12 @@ void ScripterCore::slotRunScriptFile(QString fileName, QStringList arguments, bo
 		// Replace sys.stdin with a dummy StringIO that always returns
 		// "" for read
 		cm        += QString("sys.stdin = cStringIO.StringIO()\n");
+		// tell the script if it's running in the main intepreter or a subinterpreter
+		cm        += QString("import scribus\n");
+		if (inMainInterpreter)
+			cm+= QString("scribus.mainInterpreter = True\n");
+		else
+			cm+= QString("scribus.mainInterpreter = False\n");
 		cm        += QString("try:\n");
 		cm        += QString("    execfile(\"%1\")\n").arg(escapedFileName);
 		cm        += QString("except SystemExit:\n");
@@ -414,7 +412,8 @@ void ScripterCore::slotRunScript(const QString Script)
 				"    scribus._bu = cStringIO.StringIO()\n"
 				"    sys.stdout = scribus._bu\n"
 				"    sys.stderr = scribus._bu\n"
-				"    sys.argv = ['scribus', 'ext']\n" // this is the PySys_SetArgv replacement
+				"    sys.argv = ['scribus']\n" // this is the PySys_SetArgv replacement
+				"    scribus.mainInterpreter = True\n" // the scripter console runs everything in the main interpreter
 				"    for i in scribus.getval().splitlines():\n"
 				"        scribus._ia.push(i)\n"
 				"    scribus.retval(scribus._bu.getvalue())\n"
@@ -434,10 +433,7 @@ void ScripterCore::slotRunScript(const QString Script)
 	Anyway - sys.argv is set above
 	char* comm[1];
 	comm[0] = const_cast<char*>("scribus");
-	// the scripter console runs everything in the main interpreter
-	// tell the code it's running there.
-	comm[1] = const_cast<char*>("ext");
-	PySys_SetArgv(2, comm); */
+	PySys_SetArgv(1, comm); */
 	// then run the code
 	PyObject* m = PyImport_AddModule((char*)"__main__");
 	if (m == NULL)
-- 
2.1.4

Kunda

2015-09-24 13:20

updater   ~0036363

Hi Juraj! thanks for the participation.

berteh

2015-09-25 07:21

reporter   ~0036372

thanks jurajF,

I think your patch (0001-Remove-extra-argument-from-python_v15.patch) is better than my former one, so please apply it to core.

Just a detail I did not understand from your post: what's the difference between a script and an extension script?

My last script uses PyQT for dialogs when called from menu: https://github.com/berteh/ScribusGenerator/ ... and actually not when used from the command line (where it relies on command-line argument parsing & console messages instead).

so from there: why should I care for scribus.mainInterpreter?

thanks!
B.

jurajF

2015-09-25 09:11

reporter   ~0036373

Sribus uses two type of python interpreters.

One is used when script is run from menu (without 'Run as Extension Script' box checked)
In this case scribus.mainInterpreter == False. This interpreted is
created to run only this script and when script finishes interpreter is
destroyed. For another run of script (same script or some other script)
new interpreter is created and destroyed again. So each script run in
its own interpreter.

Second type of interpreter is created when scribus is started and it
exist until scribus is running. In this interpreter
scribus.mainInterpreter == True. All scripts run in this interpreter
share the same interpreter. This mean that all script share same global
variables and if script set a global variable it is not destroyed after
script ends. This variable can be used in second run of script or in
another script altogether. This is way to save or pass information
between scripts. It also mean that script can easily unintentionally
change/destroy the variables used in other script if not used carefully.

The bit about using PyQt only in mainInterpreter I remember reading in
some scribus doc (maybe in scribus/doc/en/scripter-extensions.html).

Juraj

berteh

2015-09-25 21:48

reporter   ~0036384

clear & very useful explanations, thanks!

(now I know I want to do an extension script to be able to store "session" variables that will be preserved across script executions)

B.

cbradney

2015-10-03 20:24

administrator   ~0036492

JurajF's patch now applied in r20440

Kunda

2015-10-04 23:08

updater   ~0036514

Thanks Craig for committing.
Thanks Juraj for the patch.
Thanks berteh for the attempted patch.
Thanks ale for the report.
Closing. Please re-open if there is an issue.

Issue History

Date Modified Username Field Change
2015-09-14 13:47 ale New Issue
2015-09-15 07:09 ale Note Added: 0036243
2015-09-15 07:09 ale Priority normal => urgent
2015-09-15 07:32 berteh Note Added: 0036244
2015-09-15 07:32 berteh Note Edited: 0036244
2015-09-16 08:53 ale Note Added: 0036247
2015-09-16 16:26 cbradney Priority urgent => low
2015-09-16 16:28 cbradney Note Added: 0036249
2015-09-16 17:53 ale Note Added: 0036250
2015-09-16 18:56 cbradney Note Added: 0036251
2015-09-17 06:04 ale Priority low => normal
2015-09-17 08:31 berteh Note Added: 0036253
2015-09-17 09:28 berteh File Added: remove-extra-args-from-scripter.patch
2015-09-17 09:28 berteh Note Added: 0036255
2015-09-17 09:50 ale Note Added: 0036256
2015-09-22 14:59 jurajF Note Added: 0036331
2015-09-22 14:59 jurajF File Added: 0001-Remove-extra-argument-from-python_v15.patch
2015-09-24 13:20 Kunda Note Added: 0036363
2015-09-25 07:21 berteh Note Added: 0036372
2015-09-25 09:11 jurajF Note Added: 0036373
2015-09-25 21:48 berteh Note Added: 0036384
2015-10-03 20:24 cbradney Note Added: 0036492
2015-10-03 20:24 cbradney Status new => resolved
2015-10-03 20:24 cbradney Fixed in Version => 1.5.1svn
2015-10-03 20:24 cbradney Resolution open => fixed
2015-10-03 20:24 cbradney Assigned To => cbradney
2015-10-04 23:08 Kunda Note Added: 0036514
2015-10-04 23:08 Kunda Status resolved => closed