I have what should be a simple newbie question. I need to validate an email address (just syntax for now). I found the following URL doing a Google search on "zope email": http://zope.nipltd.com/public/lists/zope-archive.nsf/0dec1f578f18f116802568a b003585d2/a1398ea9c177a77d80256b0e00647852?OpenDocument It includes sample Perl and Python code to validate email address syntax so I created the following file in the Extensions directory to be called from an external method and named it email_checker.py:
def valid_email(address, debug=None): import re rega = re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$") #failure a regb = re.compile("^[^0-9a-zA-Z]|[^0-9a-zA-Z]$") #failure b regc = re.compile("([0-9a-zA-Z]{1})\@.") #failure c regd = re.compile(".\@([0-9a-zA-Z]{1})") #failure d rege = re.compile(".\.\-.|.\-\..|.\.\..|.\-\-.") #failure e regf = re.compile(".\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_.") #failure f regg = re.compile(".\.([a-zA-Z]{2,3})$|.\.([a-zA-Z]{2,4})$")#failure g fail = None if rega.search(address) is None: fail = 'a' elif regb.search(address) is not None: fail = 'b' elif regc.search(address) is None: fail = 'c' elif regd.search(address) is None: fail = 'd' elif rege.search(address) is not None: fail = 'e' elif regf.search(address) is not None: fail = 'f' elif regg.search(address) is None: fail = 'g' if fail is not None: if debug: return address,fail,None else: return None if debug: return address,1,1 else: return 1 <<< Then I tried to create an external method from the ZMI with the following definition:
Id: email_checker Title: Email Address Validator Module Name: email_checker Function Name: valid_email <<< When I tried to save it I get the following error:
Zope Error Zope has encountered an error while publishing this resource. Error Type: SyntaxError Error Value: invalid syntax (email_checker.py, line 1) ---------------------------------------------------------------------------- ---- Troubleshooting Suggestions The URL may be incorrect. The parameters passed to this resource may be incorrect. A resource that this resource relies on may be encountering an error. <<< I went through the Zope Book examples, but there is no assistance when errors occur. Just refers you to the Python web site for more details. First, is there a better way to validate email addresses (I am not using CMF)? If not, what am I doing wrong with the external method? Thanks in advance. ___________________ Tim Owen
Tim Owen writes:
... External Method ...
def valid_email(address, debug=None): ... Zope Error Zope has encountered an error while publishing this resource.
Error Type: SyntaxError Error Value: invalid syntax (email_checker.py, line 1) Apparently, you did nothing wrong. At least, it is not obvious.
But the problem must be really easy to fix: Python is unable to parse your code. It find a syntactical error in line 1. I assume the ">>>" is not really in your code. If it were, this would be the problem. Otherwise, Python is very sensitive to indentation. Your code must start in column 1 (when you start counting with 1). Run the Python interpreter on your file: python email_checker.py If it complains about the syntax, you need to find the reason and fix it. If not, there has been a very short lived bug in Zope that let it have problems with "\r\n" line endings (i.e. Windows like line endings). A Zope upgrade may remove the problem in this case. But it is not very likely, that you have precisely this Zope version... Dieter
The >>> and <<< are just separators in the email and not in the code. I tried to run the script in the python interpreter and it complained as well. Will look at the file and position of characters further. Thanks. ----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Tim Owen" <timowen@voyager.net> Cc: <zope@zope.org> Sent: Sunday, January 27, 2002 1:15 PM Subject: Re: [Zope] External Method Question
Tim Owen writes:
... External Method ...
def valid_email(address, debug=None): ... Zope Error Zope has encountered an error while publishing this resource.
Error Type: SyntaxError Error Value: invalid syntax (email_checker.py, line 1) Apparently, you did nothing wrong. At least, it is not obvious.
But the problem must be really easy to fix:
Python is unable to parse your code. It find a syntactical error in line 1.
I assume the ">>>" is not really in your code. If it were, this would be the problem.
Otherwise, Python is very sensitive to indentation. Your code must start in column 1 (when you start counting with 1).
Run the Python interpreter on your file:
python email_checker.py
If it complains about the syntax, you need to find the reason and fix it. If not, there has been a very short lived bug in Zope that let it have problems with "\r\n" line endings (i.e. Windows like line endings). A Zope upgrade may remove the problem in this case. But it is not very likely, that you have precisely this Zope version...
Dieter
Well, it was my bonehead mistake - of course. Turns out that the file was corrupted with binary characters. I had used Wordpad in text mode to edit and save the file and it did not save properly. Found this when I opened it in PythonWin. The interpreter now has no issues with the file. I could create the external method and call it. Thanks for your assistance. ----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Tim Owen" <timowen@voyager.net> Cc: <zope@zope.org> Sent: Sunday, January 27, 2002 1:15 PM Subject: Re: [Zope] External Method Question
Tim Owen writes:
... External Method ...
def valid_email(address, debug=None): ... Zope Error Zope has encountered an error while publishing this resource.
Error Type: SyntaxError Error Value: invalid syntax (email_checker.py, line 1) Apparently, you did nothing wrong. At least, it is not obvious.
But the problem must be really easy to fix:
Python is unable to parse your code. It find a syntactical error in line 1.
I assume the ">>>" is not really in your code. If it were, this would be the problem.
Otherwise, Python is very sensitive to indentation. Your code must start in column 1 (when you start counting with 1).
Run the Python interpreter on your file:
python email_checker.py
If it complains about the syntax, you need to find the reason and fix it. If not, there has been a very short lived bug in Zope that let it have problems with "\r\n" line endings (i.e. Windows like line endings). A Zope upgrade may remove the problem in this case. But it is not very likely, that you have precisely this Zope version...
Dieter
participants (2)
-
Dieter Maurer -
Tim Owen