def test_nested(self): from zope.component import getGlobalSiteManager from zope.interface.interfaces import IComponentLookup from zope.interface.registry import Components gsm = getGlobalSiteManager() gutil = _makeMyUtility('global', gsm) gsm.registerUtility(gutil, IMyUtility, 'myutil') sm1 = Components('sm1', bases=(gsm, )) sm1_1 = Components('sm1_1', bases=(sm1, )) util1 = _makeMyUtility('one', sm1) sm1.registerUtility(util1, IMyUtility, 'myutil') self.assertTrue(IComponentLookup(util1) is sm1) self.assertTrue(self._callFUT(util1, IMyUtility, 'myutil') is gutil) util1_1 = _makeMyUtility('one-one', sm1_1) sm1_1.registerUtility(util1_1, IMyUtility, 'myutil') self.assertTrue(IComponentLookup(util1_1) is sm1_1) self.assertTrue(self._callFUT(util1_1, IMyUtility, 'myutil') is util1)
def setSiteManager(self, sm): if zope.component.interfaces.ISite.providedBy(self): raise TypeError("Already a site") if IComponentLookup.providedBy(sm): self._sm = sm else: raise ValueError('setSiteManager requires an IComponentLookup') zope.interface.directlyProvides( self, zope.component.interfaces.ISite, zope.interface.directlyProvidedBy(self)) zope.event.notify(interfaces.NewLocalSite(sm))
def getSiteManager(context=None): """ See IComponentArchitecture. """ global base if context is None: if base is None: from zope.component.globalregistry import base return base else: # Use the global site manager to adapt context to `IComponentLookup` # to avoid the recursion implied by using a local `getAdapter()` call. try: return IComponentLookup(context) except TypeError as error: raise ComponentLookupError(*error.args)
def getSiteManager(context=None): """A special hook for getting the site manager. Here we take the currently set site into account to find the appropriate site manager. """ if context is None: return siteinfo.sm # We remove the security proxy because there's no way for # untrusted code to get at it without it being proxied again. # We should really look look at this again though, especially # once site managers do less. There's probably no good reason why # they can't be proxied. Well, except maybe for performance. sm = IComponentLookup(context, getGlobalSiteManager()) sm = removeSecurityProxy(sm) return sm
def testSiteManagerSetup(self): clearSite() # The portal should be an ISite self.assertTrue(ISite.providedBy(self.portal)) # There should be a IComponentRegistry comp = IComponentLookup(self.portal) IComponentRegistry.providedBy(comp) # Test if we get the right site managers gsm = getGlobalSiteManager() sm = getSiteManager() # Without setting the site we should get the global site manager self.assertTrue(sm is gsm) # Now we set the site, as it is done in url traversal normally setSite(self.portal) # And should get the local site manager sm = getSiteManager() self.assertTrue(aq_base(sm) is aq_base(comp))
def test_gsm_is_IComponentLookup(self): from zope.component.globalregistry import base from zope.interface.interfaces import IComponentLookup gsm = self._callFUT() self.assertTrue(gsm is base) self.assertTrue(IComponentLookup.providedBy(gsm))
def test_sm_is_IComponentLookup(self): from zope.interface.interfaces import IComponentLookup sm = self._callFUT() self.assertTrue(IComponentLookup.providedBy(sm))