Python script - problem with try: except:

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Python script - problem with try: except: Sun Jun 09, 2013 9:37 pm • by nlippman
I am trying to use a python script as a match rule. I have a folder where incoming scans are stored, and I name them with abbreviations which are relatively easy to remember. I want to use a "control file" which lists the various abbreviations and lets Hazel detect these files; the control file also provides the data needed for hazel to process the files.

Here is the script that I wrote. (The logic may be a bit hard to follow; most of the text processing is designed to allow the control file to be written as a multimarkdown table so that I can format and print it for reference).

-----------
import os.path
import sys

try:
testfile = sys.argv[1]
testfile_lower = os.path.basename(testfile).lower()
control_file = os.path.join(os.path.dirname(testfile), "control.hazel")
if testfile == control_file: sys.exit(1)

with open(control_file, "r") as f:
data = f.read()
data = data.splitlines()

for d in data:
matchstring = d.strip(" |:")
if len(matchstring) == 0: continue
if matchstring[0] in "#[": continue
matchstring = matchstring.split("|")[0].strip(" :").lower()
if testfile_lower.startswith(matchstring): sys.exit(0)

sys.exit(1) # no match
except:
sys.exit(1)
------------

When I click the "eyeball" icon to see if test files show a match based on this script, none of them do. (I have created a test folder, and in that folder created the appropriate control file and some files which do and some which don't match a rule in the control file.)

If I remove the try and except statements, then everything works just fine. I am stuck on figuring out why the try/except causes the script to fail. Nothing of interest gets logged into the Hazel log either way.

Any suggestions much appreciated. I assume it's something relatively simply, but I am overlooking the obvious....
nlippman
 
Posts: 12
Joined: Sun Jun 09, 2013 9:31 pm

As a quick note: my formatting didn't work out right, but the try and except blocks are properly indented in the actual script.
nlippman
 
Posts: 12
Joined: Sun Jun 09, 2013 9:31 pm

Here's a bit more information....I believe there is a bug somewhere in how Hazel handles Python but I'm not able to figure this out further.

This script:
try:
sys.exit(0)
except:
sys.exit(1)

Should match every file in the folder. However, it does not match any files.

By modifying it to:

try:
sys.exit(0)
except Exception:
sys.exit(1)

it now matches every file in the folder.

Python syntax allows one to declare a generic exception handler using "except:"; one does not need to specify the base Exception class in standard python syntax. This works fine running Python directly, so I'm not sure why it doesn't work under Hazel. I have specified the standard "/usr/bin/python" as the Python for Hazel to use.

Interestingly:

try:
sys.exit(1)
except:
sys.exit(0)
sys.exit(1)

You would expect this to match no files, but it actually matches every file in the folder. The final sys.exit(1) is there to ensure that the script doesn't match any files if it falls through the try:except: block, which it clearly doesn't. Somehow, an exception is being generated by not specifying an Exception.

Any ideas / help?

In the meantime, I can do what I want with my script by just using except Exception, but I would like to understand why the above syntax doesn't work.

Thanks.
nlippman
 
Posts: 12
Joined: Sun Jun 09, 2013 9:31 pm

Re: Python script - problem with try: except: Mon Jun 10, 2013 12:13 pm • by Mr_Noodle
I asked a friend who is more well-versed in python. It seems that "sys.exit()" raises SystemExit which is a subclass of BaseException, not Exception, which is why "except:" will catch "sys.exit()" but "except Exception:" won't.

http://docs.python.org/2/library/exceptions.html#exceptions.SystemExit
http://docs.python.org/2/library/sys.html#sys.exit

So, from what I can tell, doing "except Exception:" is probably the correct thing to do here (unless there are some other subclasses of BaseException you need to catch in which case you'll have to adjust accordingly).
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Python script - problem with try: except: Mon Jun 10, 2013 12:24 pm • by Mr_Noodle
Oh, and in the future, use code tags when posting code as that will preserve the indentation, which is particularly critical in python.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Python script - problem with try: except: Thu Jun 20, 2013 10:13 am • by Joopie
I am using an even smaller script:

Code: Select all
import sys
sys.exit(1)


as a rule. No matter what sytem exit code I am passing the Haxel rule ALWAYS matches it. even trying to throw exception in the script e.g. 5/0 or even something that blatantly violates python syntax matches the hazel rule.

In some way the exit code does not get back to hazel. I can only asume that the rule is being run in a subshell and that the parent process does not return any exit codes to Hazel.

Using 3.1 of Hazel. Have to not tried this before upgrading to latest.
Joopie
 
Posts: 2
Joined: Sat Jun 12, 2010 5:02 pm

Re: Python script - problem with try: except: Thu Jun 20, 2013 12:49 pm • by Mr_Noodle
There is an issue with shell scripts now. I should be releasing a fix for it tomorrow so keep an eye out for that.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Python script - problem with try: except: Sat Oct 26, 2013 6:42 pm • by Joopie
Have updgraded to Mavericks and trying the folling python rule. Have created the following code. Cannot seem to get it to match. What does hazel pass as argv[1]? is it a string with file name including the path or some other structure?

import sys
import os

testfile = sys.argv[1]
testfile_lower = os.path.basename(testfile).lower()
if testfile_lower = ‘test5’:
sys.exit(0)
else:
sys.exit(1)
Joopie
 
Posts: 2
Joined: Sat Jun 12, 2010 5:02 pm

Re: Python script - problem with try: except: Mon Oct 28, 2013 11:18 am • by Mr_Noodle
It passes the full path.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support