[Zope3-dev] local-utility location and registration
Dominik Huber
dominik.huber at projekt01.ch
Mon Jul 4 03:27:44 EDT 2005
Garanin Michael wrote:
>
>Hello!
>My app setup code do follow step:
>1) create simple AppSite (as ISite) in top
>2) create local-utility object,
>for example PAU = PluggableAuthentication()
>3) AppSite['PAU'] = PAU
>4) registration the PAU in AppSite/++etc++site/default.
>
>In other words: local-utility object location in top/mysite, but
>registration in top/mysite/++etc++site/default.
>Q: It's work, but is it legal?
>Thanks!
>
>
IMO it makes sense that the default site folder is invoked if a local
utility is added to a site.
We use the following helper function for the local utility additon and
registration:
from zope.app import zapi
from zope.app.container.interfaces import INameChooser
from zope.app.component.interfaces.registration import ActiveStatus
from zope.app.component.interfaces import ISite
from zope.app.utility import UtilityRegistration
def addLocalUtility(site, name, iface, utility, package='default'):
"""Add a utility to a site
The utility is added to the package and activated.
This assumes the site has already a Utility Service.
"""
# preconditions
if not ISite.providedBy(site):
raise TypeError('ISite required.')
# get site manager and site management folder
sitemanager = site.getSiteManager()
default = sitemanager[package]
# add utility to site management folder
chooser = INameChooser(default)
folder_name = chooser.chooseName(utility.__name__, utility)
default[folder_name] = utility
# create service registration
path = zapi.getPath(utility)
registration = UtilityRegistration(name, iface, utility)
key = default.registrationManager.addRegistration(registration)
zapi.traverse(default.registrationManager, key).status = ActiveStatus
return zapi.traverse(sitemanager, path)
Regards,
Dominik
More information about the Zope3-dev
mailing list