Python zope.interface.Interface() Examples

The following are 30 code examples of zope.interface.Interface(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zope.interface , or try the search function .
Example #1
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 8 votes vote down vote up
def test_class_has_required_method_derived(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class IBase(Interface):
            def method():
                pass

        class IDerived(IBase):
            pass

        @implementer(IDerived)
        class Current(object):

            def method(self):
                pass

        self._callFUT(IDerived, Current) 
Example #2
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_doesnt_take_required_positional_and_starargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(a, *args):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #3
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asReStructuredText_empty_with_multiline_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n'.join([
            "``IEmpty``",
            "",
            " This is an empty interface.",
            " ",
            ("             It can be used to annotate any class or object, "
                             "because it promises"),
            "             nothing.",
            "",
            " Attributes:",
            "",
            " Methods:",
            "",
            ""
        ])
        class IEmpty(Interface):
            """ This is an empty interface.

            It can be used to annotate any class or object, because it promises
            nothing.
            """
        self.assertEqual(self._callFUT(IEmpty), EXPECTED) 
Example #4
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asStructuredText_with_method_positional_args_no_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasMethod",
            " This interface has a method.",
            " Attributes:",
            " Methods:",
            "  aMethod(first, second) -- no documentation",
            ""
        ])
        class IHasMethod(Interface):
            """ This interface has a method.
            """
            def aMethod(first, second):
                pass

        self.assertEqual(self._callFUT(IHasMethod), EXPECTED) 
Example #5
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asStructuredText_with_method_kwargs_no_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasMethod",
            " This interface has a method.",
            " Attributes:",
            " Methods:",
            "  aMethod(first, second, **kw) -- no documentation",
            ""
        ])
        class IHasMethod(Interface):
            """ This interface has a method.
            """
            def aMethod(first, second, **kw):
                pass

        self.assertEqual(self._callFUT(IHasMethod), EXPECTED) 
Example #6
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asStructuredText_with_method_starargs_no_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasMethod",
            " This interface has a method.",
            " Attributes:",
            " Methods:",
            "  aMethod(first, second, *rest) -- no documentation",
            ""
        ])
        class IHasMethod(Interface):
            """ This interface has a method.
            """
            def aMethod(first, second, *rest):
                pass

        self.assertEqual(self._callFUT(IHasMethod), EXPECTED) 
Example #7
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asStructuredText_with_method_with_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasMethod",
            " This interface has a method.",
            " Attributes:",
            " Methods:",
            "  aMethod() -- This method is documented.",
            ""
        ])
        class IHasMethod(Interface):
            """ This interface has a method.
            """
            def aMethod():
                """This method is documented.
                """

        self.assertEqual(self._callFUT(IHasMethod), EXPECTED) 
Example #8
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_class_doesnt_have_required_method_derived(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenImplementation

        class IBase(Interface):
            def method():
                pass

        class IDerived(IBase):
            pass

        @implementer(IDerived)
        class Current(object):
            pass

        self.assertRaises(BrokenImplementation,
                          self._callFUT, IDerived, Current) 
Example #9
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_not_enough_args(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #10
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_doesnt_take_required_starargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(*args):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #11
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_doesnt_take_required_only_kwargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(**kw):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #12
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_extra_arg(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, b):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #13
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_extra_arg_with_default(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, b=None):
                pass

        self._callFUT(ICurrent, Current) 
Example #14
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_only_kwargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, **kw):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #15
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_extra_starargs(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, *args):
                pass

        self._callFUT(ICurrent, Current) 
Example #16
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_extra_starargs_and_kwargs(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, *args, **kw):
                pass

        self._callFUT(ICurrent, Current) 
Example #17
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_required_positional_and_starargs(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class ICurrent(Interface):

            def method(a, *args):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, *args):
                pass

        self._callFUT(ICurrent, Current) 
Example #18
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_required_kwargs(self):
        from zope.interface import Interface
        from zope.interface import implementer

        class ICurrent(Interface):

            def method(**kwargs):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, **kw):
                pass

        self._callFUT(ICurrent, Current) 
Example #19
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_takes_positional_plus_required_starargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(*args):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a, *args):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #20
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_method_doesnt_take_required_kwargs(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenMethodImplementation

        class ICurrent(Interface):

            def method(**kwargs):
                pass

        @implementer(ICurrent)
        class Current(object):

            def method(self, a):
                pass

        self.assertRaises(BrokenMethodImplementation,
                          self._callFUT, ICurrent, Current) 
Example #21
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_w_callable_non_func_method(self):
        from zope.interface.interface import Method
        from zope.interface import Interface
        from zope.interface import implementer

        class QuasiMethod(Method):
            def __call__(self, *args, **kw):
                pass

        class QuasiCallable(object):
            def __call__(self, *args, **kw):
                pass

        class ICurrent(Interface):
            attr = QuasiMethod('This is callable')

        @implementer(ICurrent)
        class Current:
            attr = QuasiCallable()

        self._callFUT(ICurrent, Current) 
Example #22
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_w_decorated_method(self):
        from zope.interface import Interface
        from zope.interface import implementer

        def decorator(func):
            # this is, in fact, zope.proxy.non_overridable
            return property(lambda self: func.__get__(self))

        class ICurrent(Interface):

            def method(a):
                pass

        @implementer(ICurrent)
        class Current(object):

            @decorator
            def method(self, a):
                pass

        self._callFUT(ICurrent, Current) 
Example #23
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_class_misses_attribute_for_attribute(self):
        # This check *fails* for verifyObject
        from zope.interface import Attribute
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenImplementation

        class ICurrent(Interface):
            attr = Attribute("The foo Attribute")

        @implementer(ICurrent)
        class Current:
            pass

        self.assertRaises(BrokenImplementation,
                          self._callFUT, ICurrent, Current) 
Example #24
Source File: test_adapter.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _makeInterfaces():
    from zope.interface import Interface

    class IB0(Interface): pass
    class IB1(IB0): pass
    class IB2(IB0): pass
    class IB3(IB2, IB1): pass
    class IB4(IB1, IB2): pass

    class IF0(Interface): pass
    class IF1(IF0): pass

    class IR0(Interface): pass
    class IR1(IR0): pass

    return IB0, IB1, IB2, IB3, IB4, IF0, IF1, IR0, IR1 
Example #25
Source File: test_tls.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_wrappedProtocolInterfaces(self):
        """
        L{TLSMemoryBIOProtocol} instances provide the interfaces provided by
        the transport they wrap.
        """
        class ITransport(Interface):
            pass

        class MyTransport(object):
            def write(self, data):
                pass

        clientFactory = ClientFactory()
        contextFactory = ClientTLSContext()
        wrapperFactory = TLSMemoryBIOFactory(
            contextFactory, True, clientFactory)

        transport = MyTransport()
        directlyProvides(transport, ITransport)
        tlsProtocol = TLSMemoryBIOProtocol(wrapperFactory, Protocol())
        tlsProtocol.makeConnection(transport)
        self.assertTrue(ITransport.providedBy(tlsProtocol)) 
Example #26
Source File: test_mail.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_requestAvatar(self):
        """
        L{MaildirDirdbmDomain.requestAvatar} raises L{NotImplementedError}
        unless it is supplied with an L{pop3.IMailbox} interface.
        When called with an L{pop3.IMailbox}, it returns a 3-tuple
        containing L{pop3.IMailbox}, an implementation of that interface
        and a NOOP callable.
        """
        class ISomething(Interface):
            pass

        self.D.addUser('user', 'password')
        self.assertRaises(
            NotImplementedError,
            self.D.requestAvatar, 'user', None, ISomething
        )

        t = self.D.requestAvatar('user', None, pop3.IMailbox)
        self.assertEqual(len(t), 3)
        self.assertTrue(t[0] is pop3.IMailbox)
        self.assertTrue(pop3.IMailbox.providedBy(t[1]))

        t[2]() 
Example #27
Source File: components.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def registerAdapter(adapterFactory, origInterface, *interfaceClasses):
    """Register an adapter class.

    An adapter class is expected to implement the given interface, by
    adapting instances implementing 'origInterface'. An adapter class's
    __init__ method should accept one parameter, an instance implementing
    'origInterface'.
    """
    self = globalRegistry
    assert interfaceClasses, "You need to pass an Interface"
    global ALLOW_DUPLICATES

    # deal with class->interface adapters:
    if not isinstance(origInterface, interface.InterfaceClass):
        origInterface = declarations.implementedBy(origInterface)

    for interfaceClass in interfaceClasses:
        factory = self.registered([origInterface], interfaceClass)
        if factory is not None and not ALLOW_DUPLICATES:
            raise ValueError("an adapter (%s) was already registered." % (factory, ))
    for interfaceClass in interfaceClasses:
        self.register([origInterface], interfaceClass, '', adapterFactory) 
Example #28
Source File: test_twisted.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_zope35(self):
        """
        Version 3.5 of L{zope.interface} has a C{implementer} method which
        cannot be used as a class decorator.
        """
        with SetAsideModule("zope"):
            self.install((3, 5))
            from zope.interface import Interface, implementer
            class IDummy(Interface):
                pass
            try:
                @implementer(IDummy)
                class Dummy(object):
                    pass
            except TypeError as exc:
                self.assertEqual(
                    "Can't use implementer with classes.  "
                    "Use one of the class-declaration functions instead.",
                    str(exc)) 
Example #29
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asStructuredText_empty_with_multiline_docstring(self):
        from zope.interface import Interface
        EXPECTED = '\n'.join([
            "IEmpty",
            "",
            " This is an empty interface.",
            " ",
            ("             It can be used to annotate any class or object, "
                             "because it promises"),
            "             nothing.",
            "",
            " Attributes:",
            "",
            " Methods:",
            "",
            ""
        ])
        class IEmpty(Interface):
            """ This is an empty interface.

            It can be used to annotate any class or object, because it promises
            nothing.
            """
        self.assertEqual(self._callFUT(IEmpty), EXPECTED) 
Example #30
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asStructuredText_with_attribute_no_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasAttribute",
            " This interface has an attribute.",
            " Attributes:",
            "  an_attribute -- no documentation",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)