Tony's Site

Zope Modules

Create modules with functions and classes to use in scripts (Python).

Within scripts (Python) it is possible to define your own fuctions but not classes. It is possible to define these externally but the Zope security starts to intervene. It is not possible to just create a product with modules and classes and just import them.

There are a number of descriptions of how to do this if you Google but they all seem too complicated. The process is described in the Zope Developers Guide. There are two ways of doing this - the hard way and the easy. If you see references to "ModuleSecurityInfo" and "ClassSecurityInfo" that is the hard way and you are on your own. Using that method you can be selective about which functions in a module are available and which methods of a class. You can also link into the Zope security policies.

The easy way allows you to make all functions in a module and all methods in a class available publically. This is done by two functions "allow_module" and allow_class". Assuming you want a product "myproduct" with module "mymod" which has function "myfunc" and class "myclass". In the Products directory create a "myproduct" folder. Within "myproduct" create a file "" with your function and class. Then add a file "" with the following:

from AccessControl import allow_module, allow_class
from Products.myproduct.mymod import myclass

Note that the parameter to "allow_module" is a string (note the quotes) whereas the parameter to "allow_class" is the class itself (no quotes).

The function and class can then be used by:

from Products.myproduct.mymod import myfunc, myclass

Note that when doing this the script (Python) cannot set attributes on instances of the class. If you want to do that you need to create a method for this purpose.

MySQL Exceptions

A special case of this is when you wish to trap MySQL exceptions when using Z SQL Methods. You could just trap generic exceptions but you may wish to be more specific. For example, if you wish to trap a duplicate key try the following.

Within the file of a product (as above) include:

from _mysql_exceptions import IntegrityError

Within the Script(Python), include, for example

from _mysql_exceptions import IntegrityError
except IntegrityError, verror:
    vcode, vdesc = verror
    if vcode == 1062:
        code for duplicate key ....
        code for other error ....