[Zope-dev] Stacking zope.component registries

Chris McDonough chrism at plope.com
Thu Apr 8 14:37:33 EDT 2010


Not sure how a new layer setup and teardown is signaled but assuming you can 
hook that, do something like this:

class StackManager(object):
     def __init__(self, default=None):
         self.stack = []
         self.default = default

     def push(self, info):
         self.stack.append(info)

     def pop(self):
         if self.stack:
             return self.stack.pop()

     def get(self):
         try:
             return self.stack[-1]
         except IndexError:
             return self.default()

     def clear(self):
         self.stack[:] = []

from zope.component import getGlobalSiteManager
global_registry = getGlobalSiteManager()

def defaults():
     return {'registry':global_registry}

manager = StackManager(default=defaults)

def get_current_registry(context=None):
     return manager.get()['registry']

from zope.component import getSiteManager
getSiteManager.sethook(get_current_registry)

Then when a layer is pushed:

   from zope.comonent.registry import Components
   manager.push({'registry':Components()})

And popped:

   manager.pop()


On 4/8/10 1:26 PM, Martin Aspeli wrote:
> Hi,
>
> I'd like to come up with a way to set up a test fixture that does the
> component registry equivalent of stackable DemoStorage's: whilst a layer
> is in effect, calls to provideAdapter() and friends (and ZCML execution)
> go into a global registry that is stacked on top of the default global one.
>
> On layer tear-down, the registry is popped, leaving the original (or
> previous) global registry intact.
>
> In zope.component.globalregistry, I see:
>
> def provideUtility(component, provides=None, name=u''):
>       base.registerUtility(component, provides, name, event=False)
>
> base is a module-level variable of type BaseGlobalComponents(). I guess
> there'd be a way to use __bases__ to create this kind of stack, but I'm
> not clear on the details, and in particular whether this would really
> work with provideAdapter() and the rest of the (test-oriented) Python API.
>
> Martin
>
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev at zope.org
> https://mail.zope.org/mailman/listinfo/zope-dev
> **  No cross posts or HTML encoding!  **
> (Related lists -
>   https://mail.zope.org/mailman/listinfo/zope-announce
>   https://mail.zope.org/mailman/listinfo/zope )
>


-- 
Chris McDonough
Agendaless Consulting, Fredericksburg VA
The repoze.bfg Web Application Framework Book: http://bfg.repoze.org/book


More information about the Zope-Dev mailing list