Beispiel #1
0
    def testUtil(self):
        self.assert_(IC in implementedBy(C))
        self.assert_(I1 in implementedBy(A))
        self.assert_(not I1 in implementedBy(C))
        self.assert_(I2 in implementedBy(B))
        self.assert_(not I2 in implementedBy(C))

        self.assert_(IC in providedBy(C()))
        self.assert_(I1 in providedBy(A()))
        self.assert_(not I1 in providedBy(C()))
        self.assert_(I2 in providedBy(B()))
        self.assert_(not I2 in providedBy(C()))
Beispiel #2
0
    def test_classImplements(self):
        class A(Odd):
            implements(I3)

        class B(Odd):
            implements(I4)

        class C(A, B):
            pass

        classImplements(C, I1, I2)
        self.assertEqual([i.getName() for i in implementedBy(C)],
                         ['I1', 'I2', 'I3', 'I4'])
        classImplements(C, I5)
        self.assertEqual([i.getName() for i in implementedBy(C)],
                         ['I1', 'I2', 'I5', 'I3', 'I4'])
Beispiel #3
0
    def provideAdapter(self, required, provided, name, factory, info=''):
        """Register an adapter

        >>> from zpt._zope.interface import Interface
        >>> registry = GlobalSiteManager()
        >>> class R1(Interface):
        ...     pass
        >>> class R2(R1):
        ...     pass
        >>> class P1(Interface):
        ...     pass
        >>> class P2(P1):
        ...     pass

        >>> registry.provideAdapter((R1, ), P2, 'bob', 'c1', 'd1')
        >>> registry.provideAdapter((R1, ), P2,    '', 'c2', 'd2')
        >>> registry.adapters.lookup((R2, ), P1, '')
        'c2'

        >>> registrations = map(repr, registry.registrations())
        >>> registrations.sort()
        >>> for registration in registrations:
        ...    print registration
        AdapterRegistration(('R1',), 'P2', '', 'c2', 'd2')
        AdapterRegistration(('R1',), 'P2', 'bob', 'c1', 'd1')

        Let's make sure that we can also register regular classes for
        adaptation.

        >>> class O1(object):
        ...     pass
        >>> class O2(object):
        ...     pass
        >>> class O3(object):
        ...     def __init__(self, obj1, obj2=None):
        ...         pass

        >>> registry.provideAdapter((O1, ), R1, '', O3)
        >>> registry.queryAdapter(O1(), R1, '').__class__
        <class 'zpt._zope.component.site.O3'>

        >>> registry.provideAdapter((O1, O2), R1, '', O3)
        >>> registry.queryMultiAdapter((O1(), O2()), R1, '').__class__
        <class 'zpt._zope.component.site.O3'>
        """
        ifaces = []
        for iface in required:
            if not IInterface.providedBy(iface) and iface is not None:
                if not isinstance(iface, (type, types.ClassType)):
                    raise TypeError(iface, IInterface)
                iface = implementedBy(iface)

            ifaces.append(iface)
        required = tuple(ifaces)

        self._registrations[(required, provided, name)] = AdapterRegistration(
            required, provided, name, factory, info)

        self.adapters.register(required, provided, name, factory)
Beispiel #4
0
    def test_implementedBy(self):
        class I2(I1):
            pass

        class C1(Odd):
            implements(I2)

        class C2(C1):
            implements(I3)

        self.assertEqual([i.getName() for i in implementedBy(C2)],
                         ['I3', 'I2'])
Beispiel #5
0
    def test_classImplementsOnly(self):
        class A(Odd):
            implements(I3)

        class B(Odd):
            implements(I4)

        class C(A, B):
            pass

        classImplementsOnly(C, I1, I2)
        self.assertEqual([i.__name__ for i in implementedBy(C)], ['I1', 'I2'])
Beispiel #6
0
    def subscribe(self, required, provided, factory, info=''):
        """Register an subscriptions adapter

        >>> from zpt._zope.interface import Interface
        >>> registry = GlobalSiteManager()
        >>> class R1(Interface):
        ...     pass
        >>> class R2(R1):
        ...     pass
        >>> class P1(Interface):
        ...     pass
        >>> class P2(P1):
        ...     pass

        >>> registry.subscribe((R1, ), P2, 'c1', 'd1')
        >>> registry.subscribe((R1, ), P2, 'c2', 'd2')
        >>> subscriptions = map(str,
        ...                     registry.adapters.subscriptions((R2, ), P1))
        >>> subscriptions.sort()
        >>> subscriptions
        ['c1', 'c2']

        >>> registrations = map(repr, registry.registrations())
        >>> registrations.sort()
        >>> for registration in registrations:
        ...    print registration
        SubscriptionRegistration(('R1',), 'P2', 'c1', 'd1')
        SubscriptionRegistration(('R1',), 'P2', 'c2', 'd2')
        """
        ifaces = []
        for iface in required:
            if not IInterface.providedBy(iface) and \
                   not isinstance(iface, declarations.Implements) and \
                   iface is not None:
                if not isinstance(iface, (type, types.ClassType)):
                    raise TypeError(iface, IInterface)
                iface = implementedBy(iface)

            ifaces.append(iface)
        required = tuple(ifaces)

        registration = SubscriptionRegistration(
            required, provided, factory, info)

        self._registrations[(required, provided)] = (
            self._registrations.get((required, provided), ())
            +
            (registration, )
            )

        self.adapters.subscribe(required, provided, factory)
Beispiel #7
0
def provideSubscriptionAdapter(factory, adapts=None, provides=None):
    if provides is None:
        provides = list(implementedBy(factory))
        if len(provides) == 1:
            provides = provides[0]
        else:
            raise TypeError("Missing 'provides' argument")

    if adapts is None:
        try:
            adapts = factory.__component_adapts__
        except AttributeError:
            raise TypeError("Missing 'adapts' argument")
            
    getGlobalSiteManager().subscribe(adapts, provides, factory)
Beispiel #8
0
 def __provides__(self):
     return providedBy(self.field) + implementedBy(FieldReadAccessor)
Beispiel #9
0
 def getInterfaces(self):
     if self._interfaces is not None:
         spec = Implements(*self._interfaces)
         spec.__name__ = getattr(self._callable, '__name__', '[callable]')
         return spec
     return implementedBy(self._callable)