[Zope] acquisition-based class initialization on Zope startup

Mitchell Model mlm@acm.org
Wed, 31 Oct 2001 19:18:09 -0500


Here's an interesting hole I dug for myself; any ideas on how to get out?  The question comes down to: is there any (reasonable) way to invoke a Zope Python Script automatically every time the server starts up.  I realize this is a little weird, since Python scripts, DTML methods, and the like are for creating client-side pages, but I've found it convenient to also use them for site setup, editing meta-information such as properties for classes, etc.  Is there perhaps something like a product's __init__.py that gets invoked late enough to be able to do Zope.app().unrestrictedTraverse, or perhaps another way to get at the Zope scripts and methods?

Here's what I did that makes me want a solution to this challenge.  I have some classes defined in my product.  Initialization of these classes includes setting up some properties.  Originally, the properties were defined in a file, and my product's __init__.py called a function that read the contents of the file and did Zope API stuff to setup the properties.

Then I decided that the properties should be web-editable, not in a file in the product implementation, so I moved the file contents into Zope DTML methods (by the way, what *is* the preferred Zope type of straight text?), and I wrote a Python Script that initialized the properties for the classes based on the Zope methods.  This way, a site manager can edit the properties, execute that Python script, and immediately have all instances of the class see the changes.

I discovered to my chagrin that when I restart Zope, all the class properties have disappeared (of course), and my __init__.py no longer recreates them.  So I have to add back in to __init__.py a call to something to recreate the properties, but that something has to either call the Zope Python script or get the contents of the DTML methods that define the properties, and during product loadup it's too early to try to navigate the Zope object hierarchy (which I normally do via Zope.app().unrestrictedTraverse, which I can give a path to my Zope Python script, and follow it with () to invoke it, which works in the debugger after Zope has started, but not while my product is being loaded; in fact, Zope doesn't even have an 'app' yet at that point).