You are not logged in Log in Join
You are here: Home » Members » pbrunet » Summary of Using Script(Python) vs External Method

Log in
Name

Password

 

Summary of Using Script(Python) vs External Method

Caveat

This was written by a Zope newbie and is fundamentally a summary of other documentation i've read on 'External Methods' (see links below), but simplified in one document, to assist other Zope newbies.

Please see these links for more detailed information :

http://www.Zope.org/Documentation/How-To/ExternalMethods
http://www.Zope.org/Documentation/ZopeBook/ScriptingZope.stx
http://www.Zope.org/Wikis/DevSite/Projects/PythonMethods/UserGuide
http://www.python.org/doc/current/lib/lib.html
http://aspn.activestate.com/ASPN/Python/Reference/Products/ActivePython/python/index.html

Begin How To...

The following is a summary of 'Script(Python)' vs 'External Method' in Zope. It is a synopsis of what you need to do to get basic 'External Methods' working in Zope, calling other 'External Methods' from an 'External Method' and using 'External Methods' to process form and REQUEST vars.

If you've been using python methods in Zope and but found that modules that are part of the standard python library (http://www.python.org/doc/current/lib/lib.html) fail when using 'Script (Python)', the reason is that the Zope web interface allows you to use only a subset of this library (the modules string, random, math, sequence) when creating scripts through the web interface.

If (as happened to me) you try to import a restricted module (for example regular expressions) and use it's methods, Zope will bring up an authentication window and fail. Frustrating! Because a web interface is public, Zope attempts to impose security by limiting a script's scope (and hence scope to cause havoc).

The way to get around this is to use the object 'External Method'. An 'External Method' is a python script (perl can also be used for scripting but is not covered in this HOW TO) that lives in the /home/Zope/Extensions/ directory (this can vary depending on where you've installed Zope) and is able to access the standard python library without restriction. There are however still restrictions on some functions, ie exec or eval, but you should be able to do most of the scripting you want here. Access to this directory is not web based.

Create Script for External Method

To create and use an unrestricted External Method (using python), create a file in :

/home/Zope/Extensions/

(Note - you can't do this through the Zope management interface, you'll have to login and do it in vi or else in a text editor and upload it to this location.)

For example

/home/Zope/Extensions/miscellaneous.py

Once you've created your script (do this by cutting and pasting the example below into a file called miscellaneous.py and place in directory /home/Zope/Extensions/)...

Example miscellaneous.py
------------------------------------------------------------

## Script (Python) 
## parameters = REQUEST, str, findStr, replaceStr
##


"""import regular expression module
   define method and default args if no args submitted
   use replaceStr to strip findStr from str, return str"""

import re
def searchReplaceStr(str='mydefaultvalue',findStr='value',replaceStr='NEWVALUE'):
 p = re.compile(findStr)
 str = p.sub(replaceStr,str)
 return str



"""return some cgi values as array"""

def requestParams(REQUEST):
 user=REQUEST.AUTHENTICATED_USER
 host=REQUEST.HTTP_HOST
 params=[host,user]
 return params 



"""return form values as array"""

def formParams(firstname,lastname):
 params=[firstname,lastname]
 return params



"""call another method from this method"""

def doubleFormParams(firstname,lastname):
 lastname+=lastname
 firstname+=firstname 
 return formParams(firstname,lastname)
 
------------------------------------------------------------

... you can use the methods you have defined.

Create an association between Zope and the Script's Method

To do this, you need to create an association, between Zope and the script/method which lives outside Zope's object database. You do this by adding the object 'External Method' from the Zope management interface drop down list.

You will be prompted for the values :

Id
Title
Module Name
Function Name

Id - represents the id or name of the method you will call from Zope DTML. This can be any name you like, but it's easier if you give it the same name as the name of the method you want to use in your script. For example searchReplaceStr

Title - is just a title for the method.

Module Name - must correspond to the actual name of your script, less the file suffix. For example our scriptname is miscellaneous.py, hence the Module Name will be miscellaneous

Function Name - must correspond to the name of the method in your script that you want to use. In my script i have four methods, but i want to use the method searchReplaceStr, so Function Name will be searchReplaceStr.

You have now created an association between a method in Zope script /home/Zope/Extensions/miscellaneous.py and an External Method object which can now be called from Zope DTML.

Repeat this process of creating associations for the methods requestParams, formParams and doubleFormParams in the script miscellaneous.py.

Test these methods in a DTML Document

You can now use these methods in a Zope DTML document. (Try cutting and pasting the example below into a DTML document to test these methods.)

Example testExternalMethods
------------------------------------------------------------
<DTML-var standard_html_header>

<h5>Example1.</h5>searchReplaceStr method with no args, uses default args :<br />
<dtml-var searchReplaceStr>

<h5>Example2.</h5>searchReplaceStr method with args str='www.fred.com:8080',findStr=':8080',replaceStr='' :<br />
<dtml-var "searchReplaceStr(str='www.fred.com:8080',findStr=':8080',replaceStr='')">

<h5>Example3.</h5>requestParams method with args REQUEST : <br />
<dtml-call "REQUEST.set('requestedParams',requestParams(REQUEST))">
hostname = <dtml-var "requestedParams[0]"><br />
authenticated user = <dtml-var "requestedParams[1]">

<h5>Example4.</h5>formParams or doubleFormParams method with input as args :<br />
<form action="" method="post"><br />
firstname <input type="text" name="firstname"><br />
lastname <input type="text" name="lastname"><br />
<input type="submit" name="formParams:action" value="return names as array"><br />
<input type="submit" name="doubleFormParams:action" value="return concatenated names as array">
</form>
------------------------------------------------------------

Note - when synching, i would guess that you'd need to tar and scp the corresponding scripts to the machine which is being synched, as these scripts are outside the Zope object database and hence will not be included in the synch of Zope objects.

Hope the above is of use.

Cheers,
Paul B