[Zope3-Users] Run starup code with zope 3.

Marius Gedminas marius at gedmin.as
Thu Feb 12 15:05:31 EST 2009


On Thu, Feb 12, 2009 at 07:28:23PM +0100, Sebastian Bartos wrote:
> Was playing with the event system a bit, and ended up with the
> following, which works fine:
> 
> import zope.event
> class MyEvent:
>     pass
> event = MyEvent()
> def f(event):
>     if isinstance(event, zope.app.appsetup.interfaces.ProcessStarting):
>         """Put stuff you want to execute after server startup here. """
>         principalPermissionManager.grantAllPermissionsToPrincipal("use")
> zope.event.subscribers.append(f)
> 
> ...
> 
> Is this a good zopey way?

An emphatic No.  This slows down the processing of every event by
performing an extra function call and an if statement, instead of using
a hash-driven dispatch that zope.component provides for subscribers.

The <subscriber /> ZCML directive is a good Zopey way.  ZCML:

  <subscriber handler=".module.grantStuffOnStartup" />

Code:

  @zope.component.adapts(zope.app.appsetup.interfaces.ProcessStarting)
  def grantStuffOnStartup(event):
      principalPermissionManager.grantAllPermissionsToPrincipal("use")

Also, if you're hardcoding the principal ID here, you might as well just
do it in ZCML:

  <grantAll principal="use" />

and if you're not hardcoding/want to be able to adjust the grant later
through the web, you should really use the IPrincipalRoleMap adapter on
the root folder that will store the grant persistently in the DB rather
than in memory.

Marius Gedminas
-- 
There is an almost obvious extension of interfaces that would allow formal
specification of arguments and return values.  We suspect it leads to the dark
side.
	-- Jim Fulton
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20090212/6c7a5aca/attachment.bin 


More information about the Zope3-users mailing list