[Zope3-Users] Problems with a catalog

Gary Poster gary at zope.com
Wed Sep 28 08:46:28 EDT 2005


On Sep 27, 2005, at 10:01 PM, Tom Dossis wrote:

> 1. How do I add a catalog to a site ?

Hi Tom.  Looks like you might have already worked yourself out of  
this, but I'll respond for the list, at least.

If you are creating and configuring a site programmatically within a  
single transaction, and adding multiple tools, some of which depend  
on the others, then you typically need to do a setSite dance around  
the whole thing.  For instance (and there are many variations, such  
as creating a site then letting event subscribers respond, but here's  
a simple case):

----8<----

import zope.app.component.hooks # I believe this location is slated  
for improvement
import zope.event
import zope.app.event.objectevent

def makesite(container, name):
     object = MySiteFactory()
     zope.event.notify(zope.app.event.objectevent.ObjectCreatedEvent 
(object))
     container[name] = object
     sm = zope.app.component.site.LocalSiteManager(object)
     zope.event.notify(zope.app.event.objectevent.ObjectCreatedEvent 
(sm))
     object.setSiteManager(sm)
     old_site = zope.app.component.hooks.getSite()
     zope.app.component.hooks.setSite(object)
     try:
         # XXX build your tools in the site manager here, and do  
other configuration
     finally:
         zope.app.component.hooks.setSite(old_site)

----8<----

Moreover, you may also need direction in adding local utilities.  The  
general pattern is this.  This also may be slated for improvement,  
like the location for the getSite/setSite import above.

Given an object that is a site (i.e., has a site manager):
- get the site manager (sm = site.getSiteManager())
- get the package into which you want to add the utility (pkg = sm 
['default'])
- create the utility
- fire off a creation event, as illustrated in code above
- get a name chooser for the package and ask it to choose a name for  
the utility (name_in_package =  
zope.app.container.interfaces.INameChooser(package).chooseName 
(suggested_name, utility))
- add it to the package (pkg[name_in_container] = utility)
- make a utility registration for name and interface you are going to  
look up the utility with--note this is usually *not* the  
name_in_package above (reg = zope.app.utility.UtilityRegistration 
(name, interface, utility))
- add the registration to the package's registration manager  
(pkg.registrationManager.addRegistration(reg))
- make the registration active (reg.status =  
zope.app.component.interfaces.registration.ActiveStatus)

Whew!

Hope that helps

Gary


More information about the Zope3-users mailing list