Topic Options
#55323 - 09/22/17 11:37 AM RunScript Condition Weird Results
Ken Stulce Offline
OL Expert

Registered: 07/06/01
Posts: 451
Loc: St. Louis, MO
Hello All,

I'm getting some odd results when using a RunScript Condition for the "Script.ReturnValue = 1" and wanted to have another set of eyes look at it. I have a dummy test at the very beginning of the script that I copied from the forums to force a True result and it will not return True, only keeps running through the rest of the script. I do get my ShowMessage pop-ups though to tell me of the condition results. Look for the code directly after the line "# Force test for True".

I forgot to mention that I added a secondary RunScript condition, but only containing the dummy test lines and it worked, but I have no idea why it will not work in the full script below. My question is for understanding purposes, as it should return true no matter what scenario. Also, I have not run this live, only through the debugger, if that makes a difference.

The script is coded in Python and it works with the rest of my conditioning, but I'm totally baffled why the dummy True test is not returning. Thoughts?

In a nutshell, the script is looking for mostly empty pages that only contain control characters like "\n", etc., that are the results of a Splitter. I'm still testing this, so it's not complete. Long story based on a recent finding and this script will help me figure out what is really happening on our HUGE workflow.

Code:
import os
import string

# Get PPWatch Job Filename
jfn = Watch.GetJobFileName()
ofn = Watch.GetOriginalFileName()

Watch.ShowMessage("The job filename is: " + jfn)
Watch.ShowMessage("The original filename is: " + ofn)

# Force test for True
everythingOK = True
Watch.ShowMessage("eOK value: \"{0}\".".format(everythingOK))
if (everythingOK):
    Watch.ShowMessage("eOK If Test True!")
    Script.ReturnValue = 1
else:
    Watch.ShowMessage("eOK If Test False!")
    Script.ReturnValue = 0

# Get file size
filsize = os.path.getsize(jfn)
filtype = type(filsize)
Watch.ShowMessage("File size is: \"{0}\".".format(filsize))
Watch.ShowMessage("filsize type = \"{0}\".".format(filtype))

# Check for file sizes greater than 100b
if (filsize < 100):
    Watch.ShowMessage("Filesize is less than 100b: \"{0}\".".format(filsize))
    Watch.Log("LOG: File is less than 100 bytes!  Do not process!", 2)
    Script.ReturnValue = 1
else:
    Watch.ShowMessage("Filesize is greater than 100b: \"{0}\".".format(filsize))

with open(jfn, 'r') as myfile:
    prtdata = myfile.read()
    #Test for printable character (whitespace is considered printable)
    printable_char_test = all(c in string.printable for c in prtdata)
    count = 0
    for char in prtdata:
        count += 1

    #Test for file character length
    if (count <= 10):
        Watch.ShowMessage("Character count is <= 10: \"{0}\".".format(count))
        Watch.Log("LOG: File contains less than 10 characters!  Do not process!", 2)
        Script.ReturnValue = 1
    else:
        Watch.ShowMessage("Character count is > 10: \"{0}\".".format(count))
        Script.ReturnValue = 0



Secondary Questions:
Also, is Python v3 supported with PlanetPress Suite 7? I'm still running v2.7 on our server, but I'm planning a server upgrade next year.

Is Windows Server 2016 supported for PP Suite 7 too? I'll only saw Server 2012 R2 listed on a recent update specs listing, but no mention of Server 2016. Same question for PP Connect? I'll eventually make that leap.

Thank you.


Edited by Ken Stulce (09/22/17 11:51 AM)
_________________________
-------------------
Ken Stulce
Manager of Application Development
Essex Industries, Inc.
-------------------

Top
#55324 - 09/22/17 12:01 PM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 482
Loc: Québec, Canada
I'm not sure but looking at your explanation, you seems to expect the script to stop once you have applied a value to Script.Value. If that is the case, try to see Script.Value as a variable...not an action that stops the script.

The script will go on to the end and will remain with the last value applied to Script.Value. Again, I can be mistaken...

Top
#55326 - 09/22/17 01:06 PM Re: RunScript Condition Weird Results [Re: Jean-Cédric]
Ken Stulce Offline
OL Expert

Registered: 07/06/01
Posts: 451
Loc: St. Louis, MO
Hi Jean-Cédric. You are correct. I was thinking of Script.ReturnValue similarly to a Function-like Return, which would usually stop the script and return the specified value. This helps a lot! Thank you.
_________________________
-------------------
Ken Stulce
Manager of Application Development
Essex Industries, Inc.
-------------------

Top
#55327 - 09/22/17 01:33 PM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Ken Stulce Offline
OL Expert

Registered: 07/06/01
Posts: 451
Loc: St. Louis, MO
Is there a preferred method, or is it even possible, to hard return out of a condition RunScript back to PPWatch with the proper Script.ReturnValue?

From Python, I've tried "return", "quit()", "exit()", "sys.exit()", and a few other methods directly after setting Script.ReturnValue, but they all raise an error (W3602, W3603, W1603) in PPWatch Messages.

Code:

W3602 : Error 0 on line 40, column 0: Python ActiveX Scripting Engine: Traceback (most recent call last):
  File "<Script Block >", line 40, in <module>
    exit(0)
  File "C:\Python27\Lib\site.py", line 360, in __call__
    raise SystemExit(code)
SystemExit: 0
W3603 : Error running script.
Run Script: W1603 : Plugin failed - 12:17:41 (elapsed time: 00:00:03:789)


Thank you.
_________________________
-------------------
Ken Stulce
Manager of Application Development
Essex Industries, Inc.
-------------------

Top
#55337 - 09/25/17 08:45 AM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1931
Loc: Objectif Lune, Montreal, Qc
In Python, you should encapsulate your code inside a main() function. You can then use the return statement to exit from that function at any point. For instance:

Code:
def main():
  for x in range(0,5):
    if x == 3:
      return
    else:
      Watch.Log(x,3)
  Watch.Log("This will never get logged",3)

main()

In this example, we interrupt the loop (and the entire function) after having logged the first 3 entries. Notice how that last Watch.Log() statement at the bottom of the main() function never gets called, because we returned from the function before ever reaching it.
_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#55345 - 09/26/17 10:07 AM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Ken Stulce Offline
OL Expert

Registered: 07/06/01
Posts: 451
Loc: St. Louis, MO
Thank you, Philippe. That makes total sense. I do that same technique with other Python scripts, but I was dabbling here at first and didn't take it to that extent. I will do that now.

I appreciate your feedback.

Best Regards,
_________________________
-------------------
Ken Stulce
Manager of Application Development
Essex Industries, Inc.
-------------------

Top
#55346 - 09/26/17 10:41 AM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Ken Stulce Offline
OL Expert

Registered: 07/06/01
Posts: 451
Loc: St. Louis, MO
Hi Philippe,

One thing that I just discovered is that the normal Python syntax, below, for declaring the main method doesn't work with PlanetPress. Instead, I commented out the "if" statement, and it worked as you suggested. Just wanted to make a note about this for myself and others.

Code:
if __name__ == '__main__':
    Watch.ShowMessage("Start script - ReturnValue = \"{0}\".".format(Script.ReturnValue))
    main()
    Watch.ShowMessage("Finished with script - ReturnValue = \"{0}\".".format(Script.ReturnValue))


Thank you.


Edited by Ken Stulce (09/26/17 10:41 AM)
_________________________
-------------------
Ken Stulce
Manager of Application Development
Essex Industries, Inc.
-------------------

Top
#55352 - 09/26/17 03:32 PM Re: RunScript Condition Weird Results [Re: Ken Stulce]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1931
Loc: Objectif Lune, Montreal, Qc
Hi Ken,

If you print out the value of the __name__ variable, you'll see it contains __ax_main__, which I guess is Windows' way of identifying ActivePython as opposed to regular Python.

I have to admit I didn't know that until I tested it out just now... :P
_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top