[Zope] import inside an external method

Passin, Tom tpassin@mitretek.org
Fri, 27 Sep 2002 17:31:50 -0400


[Courtland Idstrom]
>=20
> I'm dealing with a few external methods that I prefer to keep in=20
> separate files. There are a few shared routines that I need to import=20
> from them, but unfortunately I can't find a way to import=20
> things in the=20
> Extensions directory (for example I need to import a few=20
> things from a=20
> files called shared.py). The only way I've successfully=20
> gotten this to=20
> work is by adding the current path to Extensions to sys.path before=20
> 'import shared'. I'd rather not do it this way in case my zope=20
> installation moves paths/servers, etc.
>=20
> Notice that the usual __path__ and __file__ properties from=20
> python seem=20
> to be inaccessible, so I can't add it to sys.path in a way=20
> that's going=20
> to work in the future.
>=20

Although putting an __init__.py onto the extensions  directory might be
made to work, I do not recommend it.  Instead, put the python code
somewhere else on the python path, outside the Zope tree.  That way you
can maintain the code separately, and if you replace your Zope
installation, you do not have to remember to do anything beyond setting
the python path of the new Zope installation.

This approach also keeps the extensions directory uncluttered, which I
think is very desirable.

The best way to put a new directory on the python path, I think, is with
a .pth file.  Put it in the same directory as the python executable that
Zope uses, and put the path to the directory on a single line.  You can
have as many such paths as you want in one .pth file.

The external method should just dispatch to the real code and do nothing
else besides returning results to Zope.

For example, here is my external method for taking a string containing
XML and transforming it with an xslt stylesheet, using 4suite:

from fourxslt.do_xslt import simpleXslt

def xslt_xmlstring(xml,xslt):
    '''Transform an xml string using a stylesheet file.'''
    if not (xml and xslt): return ''
    result=3DsimpleXslt(str(xml),xslt)
    return result

Here, "simpleXslt" is a wrapper function I wrote around the 4xslt code.
The "xml" parameter is supposed to be a string containing xml, and the
"xslt" value is the path to the stylesheet.  The module "fourxslt" is on
my python path, courtesy of a .pth file.

I compute the path in Zope by getting the base directory using another
external  method call, then concatenating it with the right stylesheet
name.  You can set this up in an __init__.py file for a package so that
the basepath gets imported when the package is imported.  This lets you
use only relative paths, which is a very good idea because it lets you
move your code to other locations and other machines without changing
the paths.

One drawback to this approach is that you may have to stop and restart
Zope when you change the code that is outside of the external method.
The best way to handle that is to make the code self-testing without
using Zope at all, although I know that is not always possible.  I think
this tradeoff is worth it.

Cheers,

Tom P