Ticket #6382: foo.py

File foo.py, 1.3 KB (added by exarkun, 21 months ago)

Some code that might be interesting to people interested in this ticket perhaps

Line 
1from zope.interface import providedBy
2
3from twisted.test.proto_helpers import MemoryReactor
4from twisted.internet.task import Clock
5
6class Composite(object):
7    """
8    A helper to compose other objects based on their declared (zope.interface)
9    interfaces.
10
11    This is used here to create a reactor from separate implementations of
12    different reactor interfaces - for example, from L{Clock} and
13    L{ReactorFDSet} to create a reactor which provides L{IReactorTime} and
14    L{IReactorFDSet}.
15    """
16    def __init__(self, parts):
17        """
18        @param parts: An iterable of the objects to compose.  The methods of
19            these objects which are part of any interface the objects declare
20            they provide will be made methods of C{self}.  (Non-method
21            attributes are not supported.)
22
23        @raise ValueError: If an interface is provided by more than one of the
24            objects in C{parts}.
25        """
26        seen = set()
27        for p in parts:
28            for i in providedBy(p):
29                if i in seen:
30                    raise ValueError("More than one part provides %r" % (i,))
31                seen.add(i)
32                for m in i.names():
33                    setattr(self, m, getattr(p, m))
34
35
36
37reactor = Composite([Clock(), MemoryReactor()])