[Zope-dev] unregistering persistent utilities
David Glick
davidglick at onenw.org
Tue Jul 28 02:26:07 EDT 2009
Is there a recommended way or example code somewhere for removing a
utility registration from a persistent component registry, when that
utility's module no longer exists?
The use case is in Plone, which has a local
zope.app.cache.ram.IRAMCache utility. Now that Hanno has factored out
the RAM cache into zope.ramcache, we'd like the persistent utility to
reference zope.ramcache, so that we can remove our zope.app.cache
dependency.
I've been trying to make this work using a temporary module alias
(code below), but something strange is going on. In particular,
unghosting the persistent utilities registry is affecting things
somehow...before that happens, doing a repr of IRAMCache gives:
(Pdb) zope.app.cache.interfaces.ram.IRAMCache
<InterfaceClass zope.ramcache.interfaces.ram.IRAMCache>
...(what I'd expect, I guess, given my module alias)...but after the
registry is unghosted, it gives:
(Pdb) zope.app.cache.interfaces.ram.IRAMCache
<InterfaceClass zope.app.cache.interfaces.ram.IRAMCache>
...(which is what I'm actually hoping for, in order to unregister the
old utility)...but then I also get:
(Pdb) zope.ramcache.interfaces.ram.IRAMCache
<InterfaceClass zope.app.cache.interfaces.ram.IRAMCache>
...(e.g., now accessing it via the new module gives a class that
thinks its in the old module)
Sorry for the rather muddled description, but I haven't been able to
really understand what's going on there yet.
So what am I missing or doing wrong? Is there a better way to go
about this?
def relocateRamCacheUtility(portal):
# temporary module alias
try:
import zope.app.cache
old_zope_app_cache = zope.app.cache
except ImportError:
old_zope_app_cache = None
import zope.ramcache
zope.app.cache = sys.modules['zope.app.cache'] = zope.ramcache
sm = getSiteManager(context=portal)
# make sure the persistent utilities registry is unghosted before
we
# reference the old interface name, as it seems to make a
difference in
# whether it can be unregistered (WTF?)
sm.utilities.changed
component = sm.getUtility(zope.app.cache.interfaces.ram.IRAMCache)
sm.unregisterUtility(component=component,
provided=zope.app.cache.interfaces.ram.IRAMCache)
import zope.ramcache
import pdb; pdb.set_trace( )
sm.registerUtility(factory=zope.ramcache.ram.RAMCache,
provided=zope.ramcache.interfaces.ram.IRAMCache)
logger.info('Updated the import location of the local IRAMCache
utility.')
# reset the module alias
zope.app.cache = sys.modules['zope.app.cache'] = old_zope_app_cache
thanks,
David Glick
Web Developer
ONE/Northwest
New tools and strategies for engaging people in protecting the
environment
http://www.onenw.org
davidglick at onenw.org
work: (206) 286-1235 x32
mobile: (206) 679-3833
Subscribe to ONEList, our email newsletter!
Practical advice for effective online engagement
http://www.onenw.org/full_signup
More information about the Zope-Dev
mailing list