def test_builtins(self):
        # Setup

        intspec = implementedBy(int)
        olddeclared = intspec.declared

        classImplements(int, I1)
        class myint(int):
            implements(I2)

        x = 42
        self.assertEqual([i.getName() for i in providedBy(x)],
                         ['I1'])

        x = myint(42)
        directlyProvides(x, I3)
        self.assertEqual([i.getName() for i in providedBy(x)],
                         ['I3', 'I2', 'I1'])

        # cleanup
        intspec.declared = olddeclared
        classImplements(int)

        x = 42
        self.assertEqual([i.getName() for i in providedBy(x)],
                         [])
    def test_builtins(self):
        # Setup

        intspec = implementedBy(int)
        olddeclared = intspec.declared

        classImplements(int, I1)

        class myint(int):
            implements(I2)

        x = 42
        self.assertEqual([i.getName() for i in providedBy(x)], ['I1'])

        x = myint(42)
        directlyProvides(x, I3)
        self.assertEqual([i.getName() for i in providedBy(x)],
                         ['I3', 'I2', 'I1'])

        # cleanup
        intspec.declared = olddeclared
        classImplements(int)

        x = 42
        self.assertEqual([i.getName() for i in providedBy(x)], [])
Esempio n. 3
0
    def testNotImplemented(self):

        class C(object): pass

        class I(Interface): pass

        self.assertRaises(DoesNotImplement, verifyClass, I, C)

        classImplements(C, I)

        verifyClass(I, C)
    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"])
Esempio n. 5
0
    def test_ObjectSpecification(self):
        c = C()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)],
                         ['I4', 'I31', 'I1', 'I2'])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()],
                         ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface'])
        self.assert_(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.assert_(providedBy(c).extends(I31))
        self.failIf(providedBy(c).extends(I5))

        class COnly(A, B):
            pass

        classImplementsOnly(COnly, I31)

        class D(COnly):
            pass

        classImplements(D, I5)

        classImplements(D, I5)

        c = D()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)],
                         ['I4', 'I5', 'I31'])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()],
                         ['I4', 'I5', 'I31', 'I3', 'Interface'])
        self.failIf(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.failIf(providedBy(c).extends(I1))
        self.assert_(providedBy(c).extends(I31))
        self.assert_(providedBy(c).extends(I5))

        class COnly(A, B):
            __implemented__ = I31

        class D(COnly):
            pass

        classImplements(D, I5)

        classImplements(D, I5)
        c = D()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)],
                         ['I4', 'I5', 'I31'])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()],
                         ['I4', 'I5', 'I31', 'I3', 'Interface'])
        self.failIf(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.failIf(providedBy(c).extends(I1))
        self.assert_(providedBy(c).extends(I31))
        self.assert_(providedBy(c).extends(I5))
    def test_implementedBy(self):
        class I2(I1):
            pass

        class C1(Odd):
            pass

        classImplements(C1, I2)

        class C2(C1):
            pass

        classImplements(C2, I3)

        self.assertEqual([i.getName() for i in implementedBy(C2)], ["I3", "I2"])
Esempio n. 7
0
    def test_implementedBy(self):
        class I2(I1):
            pass

        class C1(Odd):
            pass

        classImplements(C1, I2)

        class C2(C1):
            pass

        classImplements(C2, I3)

        self.assertEqual([i.getName() for i in implementedBy(C2)],
                         ['I3', 'I2'])
Esempio n. 8
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'])
    def test_ObjectSpecification(self):
        c = C()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)], ["I4", "I31", "I1", "I2"])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()], ["I4", "I31", "I3", "I1", "I2", "Interface"])
        self.assert_(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.assert_(providedBy(c).extends(I31))
        self.failIf(providedBy(c).extends(I5))

        class COnly(A, B):
            pass

        classImplementsOnly(COnly, I31)

        class D(COnly):
            pass

        classImplements(D, I5)

        classImplements(D, I5)

        c = D()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)], ["I4", "I5", "I31"])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()], ["I4", "I5", "I31", "I3", "Interface"])
        self.failIf(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.failIf(providedBy(c).extends(I1))
        self.assert_(providedBy(c).extends(I31))
        self.assert_(providedBy(c).extends(I5))

        class COnly(A, B):
            __implemented__ = I31

        class D(COnly):
            pass

        classImplements(D, I5)

        classImplements(D, I5)
        c = D()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)], ["I4", "I5", "I31"])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()], ["I4", "I5", "I31", "I3", "Interface"])
        self.failIf(I1 in providedBy(c))
        self.failIf(I3 in providedBy(c))
        self.assert_(providedBy(c).extends(I3))
        self.failIf(providedBy(c).extends(I1))
        self.assert_(providedBy(c).extends(I31))
        self.assert_(providedBy(c).extends(I5))
    def test_directlyProvides(self):
        class IA1(Interface):
            pass

        class IA2(Interface):
            pass

        class IB(Interface):
            pass

        class IC(Interface):
            pass

        class A(Odd):
            pass

        classImplements(A, IA1, IA2)

        class B(Odd):
            pass

        classImplements(B, IB)

        class C(A, B):
            pass

        classImplements(C, IC)

        ob = C()
        directlyProvides(ob, I1, I2)
        self.assert_(I1 in providedBy(ob))
        self.assert_(I2 in providedBy(ob))
        self.assert_(IA1 in providedBy(ob))
        self.assert_(IA2 in providedBy(ob))
        self.assert_(IB in providedBy(ob))
        self.assert_(IC in providedBy(ob))

        directlyProvides(ob, directlyProvidedBy(ob) - I2)
        self.assert_(I1 in providedBy(ob))
        self.failIf(I2 in providedBy(ob))
        self.failIf(I2 in providedBy(ob))
        directlyProvides(ob, directlyProvidedBy(ob), I2)
        self.assert_(I2 in providedBy(ob))
Esempio n. 11
0
    def test_directlyProvides(self):
        class IA1(Interface):
            pass

        class IA2(Interface):
            pass

        class IB(Interface):
            pass

        class IC(Interface):
            pass

        class A(Odd):
            pass

        classImplements(A, IA1, IA2)

        class B(Odd):
            pass

        classImplements(B, IB)

        class C(A, B):
            pass

        classImplements(C, IC)

        ob = C()
        directlyProvides(ob, I1, I2)
        self.assert_(I1 in providedBy(ob))
        self.assert_(I2 in providedBy(ob))
        self.assert_(IA1 in providedBy(ob))
        self.assert_(IA2 in providedBy(ob))
        self.assert_(IB in providedBy(ob))
        self.assert_(IC in providedBy(ob))

        directlyProvides(ob, directlyProvidedBy(ob) - I2)
        self.assert_(I1 in providedBy(ob))
        self.failIf(I2 in providedBy(ob))
        self.failIf(I2 in providedBy(ob))
        directlyProvides(ob, directlyProvidedBy(ob), I2)
        self.assert_(I2 in providedBy(ob))
Esempio n. 12
0
class IOverflowError(IArithmeticError): pass
class IOverflowWarning(IWarning): pass
class IReferenceError(IStandardError): pass
class IRuntimeWarning(IWarning): pass
class IStopIteration(IException): pass
class ISyntaxWarning(IWarning): pass
class ISystemError(IStandardError): pass
class ISystemExit(IException): pass
class ITabError(IIndentationError): pass
class ITypeError(IStandardError): pass
class IUnboundLocalError(INameError): pass
class IUnicodeError(IValueError): pass
class IUserWarning(IWarning): pass
class IZeroDivisionError(IArithmeticError): pass

classImplements(ArithmeticError, IArithmeticError)
classImplements(AssertionError, IAssertionError)
classImplements(AttributeError, IAttributeError)
classImplements(DeprecationWarning, IDeprecationWarning)
classImplements(EnvironmentError, IEnvironmentError)
classImplements(EOFError, IEOFError)
classImplements(Exception, IException)
classImplements(FloatingPointError, IFloatingPointError)
classImplements(ImportError, IImportError)
classImplements(IndentationError, IIndentationError)
classImplements(IndexError, IIndexError)
classImplements(IOError, IIOError)
classImplements(KeyboardInterrupt, IKeyboardInterrupt)
classImplements(KeyError, IKeyError)
classImplements(LookupError, ILookupError)
classImplements(MemoryError, IMemoryError)
Esempio n. 13
0
    def getTcpNoDelay(self):
        return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY))

    def setTcpNoDelay(self, enabled):
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, enabled)

    def getTcpKeepAlive(self):
        return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
                                                     socket.SO_KEEPALIVE))

    def setTcpKeepAlive(self, enabled):
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, enabled)

if SSL:
    classImplements(Connection, interfaces.ITLSTransport)

class BaseClient(Connection):
    """A base class for client TCP (and similiar) sockets.
    """
    addressFamily = socket.AF_INET
    socketType = socket.SOCK_STREAM

    def _finishInit(self, whenDone, skt, error, reactor):
        """Called by base classes to continue to next stage of initialization."""
        if whenDone:
            Connection.__init__(self, skt, None, reactor)
            self.doWrite = self.doConnect
            self.doRead = self.doConnect
            reactor.callLater(0, whenDone)
        else:
Esempio n. 14
0
    pass


class IUnicodeError(IValueError):
    pass


class IUserWarning(IWarning):
    pass


class IZeroDivisionError(IArithmeticError):
    pass


classImplements(ArithmeticError, IArithmeticError)
classImplements(AssertionError, IAssertionError)
classImplements(AttributeError, IAttributeError)
classImplements(DeprecationWarning, IDeprecationWarning)
classImplements(EnvironmentError, IEnvironmentError)
classImplements(EOFError, IEOFError)
classImplements(Exception, IException)
classImplements(FloatingPointError, IFloatingPointError)
classImplements(ImportError, IImportError)
classImplements(IndentationError, IIndentationError)
classImplements(IndexError, IIndexError)
classImplements(IOError, IIOError)
classImplements(KeyboardInterrupt, IKeyboardInterrupt)
classImplements(KeyError, IKeyError)
classImplements(LookupError, ILookupError)
classImplements(MemoryError, IMemoryError)
Esempio n. 15
0
        return operator.truth(
            self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY))

    def setTcpNoDelay(self, enabled):
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, enabled)

    def getTcpKeepAlive(self):
        return operator.truth(
            self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE))

    def setTcpKeepAlive(self, enabled):
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, enabled)


if SSL:
    classImplements(Connection, interfaces.ITLSTransport)


class BaseClient(Connection):
    """A base class for client TCP (and similiar) sockets.
    """
    addressFamily = socket.AF_INET
    socketType = socket.SOCK_STREAM

    def _finishInit(self, whenDone, skt, error, reactor):
        """Called by base classes to continue to next stage of initialization."""
        if whenDone:
            Connection.__init__(self, skt, None, reactor)
            self.doWrite = self.doConnect
            self.doRead = self.doConnect
            reactor.callLater(0, whenDone)
Esempio n. 16
0
        isn't known, return None. Else the value returned must be a timedelta
        object specifying a whole number of minutes in the range -1439 to 1439
        inclusive (1440 = 24*60; the magnitude of the offset must be less
        than one day).
        """

    def dst(dt):
        """Return the daylight saving time (DST) adjustment, in minutes east
        of UTC, or None if DST information isn't known.
        """

    def tzname(dt):
        """Return the time zone name corresponding to the datetime object as
        a string.
        """

    def fromutc(dt):
        """Return an equivalent datetime in self's local time."""


classImplements(timedelta, ITimeDelta)
classImplements(date, IDate)
classImplements(datetime, IDateTime)
classImplements(time, ITime)
classImplements(tzinfo, ITZInfo)

## directlyProvides(timedelta, ITimeDeltaClass)
## directlyProvides(date, IDateClass)
## directlyProvides(datetime, IDateTimeClass)
## directlyProvides(time, ITimeClass)
Esempio n. 17
0
            self.getThreadPool().callInThread(_callable, *args, **kwargs)

        def suggestThreadPoolSize(self, size):
            """
            See L{twisted.internet.interfaces.IReactorThreads.suggestThreadPoolSize}.
            """
            self.getThreadPool().adjustPoolsize(maxthreads=size)
    else:
        # This is for signal handlers.
        def callFromThread(self, f, *args, **kw):
            assert callable(f), "%s is not callable" % (f,)
            # See comment in the other callFromThread implementation.
            self.threadCallQueue.append((f, args, kw))

if platform.supportsThreads():
    classImplements(ReactorBase, IReactorThreads)


class BaseConnector(styles.Ephemeral):
    """Basic implementation of connector.

    State can be: "connecting", "connected", "disconnected"
    """

    implements(IConnector)

    timeoutID = None
    factoryStarted = 0

    def __init__(self, factory, timeout, reactor):
        self.state = "disconnected"
Esempio n. 18
0
        """
        Remove all readers and writers, and list of removed L{IReadDescriptor}s
        and L{IWriteDescriptor}s.

        Meant for calling from subclasses, to implement removeAll, like::

          def removeAll(self):
              return self._removeAll(self._reads, self._writes)

        where C{self._reads} and C{self._writes} are iterables.
        """
        removedReaders = set(readers) - self._internalReaders
        for reader in removedReaders:
            self.removeReader(reader)

        removedWriters = set(writers)
        for writer in removedWriters:
            self.removeWriter(writer)

        return list(removedReaders | removedWriters)


if sslEnabled:
    classImplements(PosixReactorBase, IReactorSSL)
if unixEnabled:
    classImplements(PosixReactorBase, IReactorUNIX, IReactorUNIXDatagram)
if processEnabled:
    classImplements(PosixReactorBase, IReactorProcess)

__all__ = ["PosixReactorBase"]
Esempio n. 19
0
        isn't known, return None. Else the value returned must be a timedelta
        object specifying a whole number of minutes in the range -1439 to 1439
        inclusive (1440 = 24*60; the magnitude of the offset must be less
        than one day).
        """

    def dst(dt):
        """Return the daylight saving time (DST) adjustment, in minutes east
        of UTC, or None if DST information isn't known.
        """

    def tzname(dt):
        """Return the time zone name corresponding to the datetime object as
        a string.
        """

    def fromutc(dt):
        """Return an equivalent datetime in self's local time."""


classImplements(timedelta, ITimeDelta)
classImplements(date, IDate)
classImplements(datetime, IDateTime)
classImplements(time, ITime)
classImplements(tzinfo, ITZInfo)

## directlyProvides(timedelta, ITimeDeltaClass)
## directlyProvides(date, IDateClass)
## directlyProvides(datetime, IDateTimeClass)
## directlyProvides(time, ITimeClass)
Esempio n. 20
0
        def suggestThreadPoolSize(self, size):
            """
            See L{twisted.internet.interfaces.IReactorThreads.suggestThreadPoolSize}.
            """
            self.getThreadPool().adjustPoolsize(maxthreads=size)
    else:
        # This is for signal handlers.
        def callFromThread(self, f, *args, **kw):
            assert callable(f), "%s is not callable" % (f, )
            # See comment in the other callFromThread implementation.
            self.threadCallQueue.append((f, args, kw))


if platform.supportsThreads():
    classImplements(ReactorBase, IReactorThreads)


class BaseConnector(styles.Ephemeral):
    """Basic implementation of connector.

    State can be: "connecting", "connected", "disconnected"
    """

    implements(IConnector)

    timeoutID = None
    factoryStarted = 0

    def __init__(self, factory, timeout, reactor):
        self.state = "disconnected"
Esempio n. 21
0
class B(Odd):
    __implemented__ = I2


# TODO: We are going to need more magic to make classProvides work with odd
#       classes. This will work in the next iteration. For now, we'll use
#       a different mechanism.

# from reqs.zope.interface import classProvides


class A(Odd):
    pass


classImplements(A, I1)


class C(A, B):
    pass


classImplements(C, I31)


class Test(unittest.TestCase):
    def test_ObjectSpecification(self):
        c = C()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)],
                         ['I4', 'I31', 'I1', 'I2'])
class B(Odd):
    __implemented__ = I2


# TODO: We are going to need more magic to make classProvides work with odd
#       classes. This will work in the next iteration. For now, we'll use
#       a different mechanism.

# from reqs.zope.interface import classProvides


class A(Odd):
    pass


classImplements(A, I1)


class C(A, B):
    pass


classImplements(C, I31)


class Test(unittest.TestCase):
    def test_ObjectSpecification(self):
        c = C()
        directlyProvides(c, I4)
        self.assertEqual([i.getName() for i in providedBy(c)], ["I4", "I31", "I1", "I2"])
        self.assertEqual([i.getName() for i in providedBy(c).flattened()], ["I4", "I31", "I3", "I1", "I2", "Interface"])