def test_w_name_wo_ifact_type(self): from zope.interface import Interface from zope.interface.interfaces import IInterface from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() class IFoo(Interface): pass self._callFUT('foo', IFoo) self.assertTrue(IInterface.providedBy(IFoo)) registered = gsm.getUtility(IInterface, name='foo') self.assertTrue(registered is IFoo)
def test_w_class(self): from zope.interface.interfaces import IInterface from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() class IBar(IInterface): pass class Foo(object): pass self._callFUT('', Foo, IBar) self.assertFalse(IBar.providedBy(Foo)) self.assertEqual(len(list(gsm.getUtilitiesFor(IBar))), 0)
def test_named_w_provides(self): from zope.interface import Interface from guillotina.component.globalregistry import getGlobalSiteManager class IFoo(Interface): pass class Foo(object): pass foo = Foo() self._callFUT(foo, IFoo, 'named') gsm = getGlobalSiteManager() self.assertTrue(gsm.getUtility(IFoo, 'named') is foo)
def test_no_search_string_w_base_is_same(self): from zope.interface import Interface from zope.interface.interfaces import IInterface from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() class IFoo(Interface): pass class IBar(Interface): pass gsm.registerUtility(IFoo, IInterface, 'foo') gsm.registerUtility(IBar, IInterface, 'bar') self.assertEqual(self._callFUT(object(), base=IFoo), [('foo', IFoo)])
def test_nested(self): from zope.interface import Interface from zope.interface import implementer from zope.interface.registry import Components from guillotina.component import getGlobalSiteManager from guillotina.component.tests.examples import ConformsToIComponentLookup class IFoo(Interface): pass class IBar(Interface): pass class IBaz(Interface): pass @implementer(IBar) class Bar(object): pass @implementer(IBaz) class Baz(object): pass @implementer(IFoo) class Global(object): def __init__(self, first, second): self.first, self.second = first, second @implementer(IFoo) class Local(object): def __init__(self, first, second): self.first, self.second = first, second class Context(ConformsToIComponentLookup): def __init__(self, sm): self.sitemanager = sm gsm = getGlobalSiteManager() gsm.registerAdapter(Global, (IBar, IBaz), IFoo, '') sm1 = Components('sm1', bases=(gsm, )) sm1.registerAdapter(Local, (IBar, IBaz), IFoo, '') bar = Bar() baz = Baz() adapted = self._callFUT((bar, baz), IFoo, '', context=Context(sm1)) self.assertTrue(adapted.__class__ is Local) self.assertTrue(adapted.first is bar) self.assertTrue(adapted.second is baz)
def test_wo_name_w_iface_type(self): from zope.interface import Interface from zope.interface.interfaces import IInterface from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() class IFoo(Interface): pass class IBar(IInterface): pass self._callFUT('', IFoo, IBar) self.assertTrue(IBar.providedBy(IFoo)) nm = 'guillotina.component.tests.test_interface.IFoo' self.assertTrue(gsm.getUtility(IBar, nm) is IFoo)
def test_w_None(self): from guillotina.component import hooks from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() _SM2 = object() _SITE = object() _HOOK = object() siteinfo = _DummySiteInfo() siteinfo.sm = _SM2 siteinfo.site = _SITE siteinfo.adapterhook = _HOOK with _Monkey(hooks, siteinfo=siteinfo): self._callFUT(None) self.assertTrue(siteinfo.sm is gsm) self.assertTrue(siteinfo.site is None) self.assertFalse('adapter_hook' in siteinfo.__dict__)
def test_anonymous_no_provides(self): from zope.interface import Interface from zope.interface import implementer from guillotina.component.globalregistry import getGlobalSiteManager class IFoo(Interface): pass @implementer(IFoo) class Foo(object): pass foo = Foo() self._callFUT(foo) gsm = getGlobalSiteManager() self.assertTrue(gsm.getUtility(IFoo, '') is foo)
def test_it(self): from zope.interface import Interface from guillotina.component.globalregistry import getGlobalSiteManager from guillotina.component.event import dispatch _adapted = [] def _adapter(context): _adapted.append(context) return object() gsm = getGlobalSiteManager() gsm.registerHandler(_adapter, (Interface, )) del _adapted[:] # clear handler reg event = object() dispatch(event) self.assertEqual(_adapted, [event])
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()) return sm
def searchInterfaceUtilities(context, search_string=None, base=None): gsm = getGlobalSiteManager() iface_utilities = gsm.getUtilitiesFor(IInterface) if search_string: search_string = search_string.lower() iface_utilities = [iface_util for iface_util in iface_utilities if (getInterfaceAllDocs(iface_util[1]).\ find(search_string) >= 0)] if base: res = [ iface_util for iface_util in iface_utilities if iface_util[1].isOrExtends(base) ] else: res = [iface_util for iface_util in iface_utilities] return res
def test_w_adapts(self): from zope.interface import Interface from guillotina.component.globalregistry import getGlobalSiteManager class IFoo(Interface): pass def _handler(context): assert 0, "DON'T GO HERE" self._callFUT(_handler, (IFoo, )) gsm = getGlobalSiteManager() regs = list(gsm.registeredHandlers()) self.assertEqual(len(regs), 1) hr = regs[0] self.assertEqual(list(hr.required), [IFoo]) self.assertEqual(hr.name, '') self.assertTrue(hr.factory is _handler)
def test_w_explicit_context_w_IComponentLookup(self): from zope.interface import Interface from guillotina.component import hooks from guillotina.component.globalregistry import getGlobalSiteManager from guillotina.component.interfaces import IComponentLookup class _Lookup(object): def __init__(self, context): self.context = context gsm = getGlobalSiteManager() gsm.registerAdapter(_Lookup, (Interface,), IComponentLookup, '') _SM2 = object() siteinfo = _DummySiteInfo() siteinfo.sm = _SM2 context = object() with _Monkey(hooks, siteinfo=siteinfo): sm = self._callFUT(context) self.assertTrue(isinstance(sm, _Lookup)) self.assertTrue(sm.context is context)
def test_it(self): from guillotina.component import hooks from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() _SM2 = object() class _Site(object): def getSiteManager(self): return _SM2 _site = _Site() siteinfo = _DummySiteInfo() self.assertTrue(siteinfo.site is None) self.assertTrue(siteinfo.sm is _SM) with _Monkey(hooks, siteinfo=siteinfo): with self._callFUT(_site): self.assertTrue(siteinfo.site is _site) self.assertTrue(siteinfo.sm is _SM2) self.assertTrue(siteinfo.site is None) self.assertTrue(siteinfo.sm is gsm)
def test_hook_raises(self): from zope.interface import Interface from guillotina.component import hooks from guillotina.component.globalregistry import getGlobalSiteManager from guillotina.component.interfaces import ComponentLookupError class IFoo(Interface): pass gsm = getGlobalSiteManager() _DEFAULT = object() _CONTEXT = object() _called = [] def _adapter_hook(interface, object, name, default): _called.append((interface, object, name, default)) raise ComponentLookupError('testing') siteinfo = _DummySiteInfo() siteinfo.adapter_hook = _adapter_hook with _Monkey(hooks, siteinfo=siteinfo): adapter = self._callFUT(IFoo, _CONTEXT, 'bar', _DEFAULT) self.assertTrue(adapter is _DEFAULT) self.assertEqual(_called, [(IFoo, _CONTEXT, 'bar', _DEFAULT)])
def provideInterface(id, interface, iface_type=None, info=''): """ Mark 'interface' as a named utilty providing 'iface_type'. """ if not id: id = "%s.%s" % (interface.__module__, interface.__name__) if not IInterface.providedBy(interface): if not isinstance(interface, CLASS_TYPES): raise TypeError(id, "is not an interface or class") return if iface_type is not None: if not iface_type.extends(IInterface): raise TypeError(iface_type, "is not an interface type") alsoProvides(interface, iface_type) else: iface_type = IInterface gsm = getGlobalSiteManager() gsm.registerUtility(interface, iface_type, id, info)
def setSite(site=None): if site is None: sm = getGlobalSiteManager() else: # 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. # The getSiteManager method is defined by IPossibleSite. sm = site.getSiteManager() siteinfo.site = site siteinfo.sm = sm try: del siteinfo.adapter_hook except AttributeError: pass
def test_hit(self): from zope.interface import Interface from guillotina.component import getGlobalSiteManager class IFoo(Interface): pass class BarAdapter(object): def __init__(self, context): self.context = context class BazAdapter(object): def __init__(self, context): self.context = context gsm = getGlobalSiteManager() gsm.registerSubscriptionAdapter(BarAdapter, (None, ), IFoo) gsm.registerSubscriptionAdapter(BazAdapter, (None, ), IFoo) subscribers = self._callFUT((object(), ), IFoo) self.assertEqual(len(subscribers), 2) names = [(x.__class__.__name__) for x in subscribers] self.assertTrue('BarAdapter' in names) self.assertTrue('BazAdapter' in names)
def test_hit(self): from zope.interface import Interface from guillotina.component import getGlobalSiteManager class IFoo(Interface): pass class BarAdapter(object): def __init__(self, context): self.context = context class BazAdapter(object): def __init__(self, context): self.context = context gsm = getGlobalSiteManager() gsm.registerAdapter(BarAdapter, (None, ), IFoo) gsm.registerAdapter(BazAdapter, (None, ), IFoo, name='bar') tuples = list(self._callFUT((object(), ), IFoo)) self.assertEqual(len(tuples), 2) names = [(x, y.__class__.__name__) for x, y in tuples] self.assertTrue(('', 'BarAdapter') in names) self.assertTrue(('bar', 'BazAdapter') in names)
def test_hit(self): from zope.interface import Interface from guillotina.component import getGlobalSiteManager class IFoo(Interface): pass class IBar(IFoo): pass obj = object() obj1 = object() obj2 = object() getGlobalSiteManager().registerUtility(obj, IFoo) getGlobalSiteManager().registerUtility(obj1, IFoo, name='bar') getGlobalSiteManager().registerUtility(obj2, IBar) uts = list(self._callFUT(IFoo)) self.assertEqual(len(uts), 3) self.assertTrue(obj in uts) self.assertTrue(obj1 in uts) self.assertTrue(obj2 in uts)
def test_initial(self): from guillotina.component.globalregistry import getGlobalSiteManager gsm = getGlobalSiteManager() si = self._makeOne() self.assertEqual(si.site, None) self.assertTrue(si.sm is gsm)
def _callFUT(self): from guillotina.component.globalregistry import getGlobalSiteManager return getGlobalSiteManager()