On 04/29/2010 10:37 AM, Chris Withers wrote:
> Hi All,
> What's the "correct" way to provide utilities in BFG imperatively?
> My guess would be:
> from repoze.bfg.configuration import Configurator
> from somewhere import ISomething
> from zope.component import provideUtility
> def factory():
>       return ...yada...
> def app(global_config, **settings):
>       config = Configurator(settings=settings)
>       config.begin()
>       ...yada...
>       provideUtility(factory(),ISomething)
>       config.scan()
>       config.end()
>       return config.make_wsgi_app()

You could do:

def app(global_config, **settings):
     config = Configurator(settings=settings)
     config.registry.provideUtility(factory(), ISomething)
     return config.make_wsgi_app()

config.registry isn't really an API, but it's unlikely to change.  It's 
a ZCA registry; you'll have to go find the docs for it wherever they 
are(nt), but usually the method names are the same as the global API 
names (e.g. registry.registerUtility vs. registerUtility).

I won't get boxed in by this though; you get to keep both pieces if this 
eventually breaks.  BFG uses the ZCA.  It doesn't advertise its presence 
as a feature that to be used by app developers.  You don't need to use 
the same registry that BFG uses to use the ZCA in a BFG app; just use a 
different one.

> ...or do I have to do something to make sure I register in the right
> registry?

The right way to use a global (actually threadlocal) API to get the 
registry that BFG uses is:

    from repoze.bfg.threadlocal import get_current_registry
    registry = get_current_registry()

During the configuration stage, the "current" global registry is defined 
as whatever registry is attached to the configurator between the begin() 
and end() calls.

If you want to use the global ZCA api (e.g. getUtility, getAdapter, etc) 
to work against the BFG ZCA registry, you have to use config.hook_zca(), 
which is documented in http://docs.repoze.org/bfg/1.3/narr/zca.html . 
Preferably, don't do that at all, and you'll wind up putting 
registrations into the Zope global registry, which your app can use 

- C
Repoze-dev mailing list

Reply via email to