| | 42 | class Input(object): |
|---|
| | 43 | """An Input on a Node that receives incoming data.""" |
|---|
| | 44 | source = None |
|---|
| | 45 | |
|---|
| | 46 | def connect(self, output): |
|---|
| | 47 | """Associate this Input with the given Output so that the Input can pull data from the Output when necessary.""" |
|---|
| | 48 | self.source = output |
|---|
| | 49 | |
|---|
| | 50 | class Output(object): |
|---|
| | 51 | """An Output on a Node that sends data to Inputs.""" |
|---|
| | 52 | signalListeners = [] |
|---|
| | 53 | |
|---|
| | 54 | def __init__(self): |
|---|
| | 55 | """Initialize a new Output.""" |
|---|
| | 56 | self.signalListeners = {} |
|---|
| | 57 | |
|---|
| | 58 | def read(self): |
|---|
| | 59 | """Read data from this Output if available. If none is available then return None. i.e. this is non-blocking.""" |
|---|
| | 60 | pass |
|---|
| | 61 | |
|---|
| | 62 | def addListener(self, listener, signal='all'): |
|---|
| | 63 | self.signalListeners[signal].append(listener) |
|---|
| | 64 | |
|---|
| | 65 | def tripSignal(self, signal): |
|---|
| | 66 | if self.signalListeners.hasKey(signal): |
|---|
| | 67 | listeners = self.signalListeners[signal] |
|---|
| | 68 | for listener in listeners: |
|---|
| | 69 | listener(self, signal) |
|---|
| | 70 | |
|---|
| | 71 | |
|---|
| | 72 | class NotificationCenter(Singleton): |
|---|
| | 73 | def __init__(self): |
|---|
| | 74 | self.listeners = {} |
|---|
| | 75 | |
|---|
| | 76 | def _ensureListenerList(self, source, signal): |
|---|
| | 77 | if not self.listeners.has_key((source, signal)): |
|---|
| | 78 | self.listeners[(source, signal)] = [] |
|---|
| | 79 | |
|---|
| | 80 | def addListener(self, source, signal, callable): |
|---|
| | 81 | self._ensureListenerList(source, signal) |
|---|
| | 82 | self.listeners[(source, signal)].append(callable) |
|---|
| | 83 | |
|---|
| | 84 | def removeListener(self, source, signal, func): |
|---|
| | 85 | key = (source, signal) |
|---|
| | 86 | if self.listeners.has_key(key): |
|---|
| | 87 | f = lambda x: x not func, |
|---|
| | 88 | self.listeners[key] = filter(f, self.listeners[key]) |
|---|
| | 89 | |
|---|
| | 90 | def post(self, source, signal): |
|---|
| | 91 | key = (source, signal) |
|---|
| | 92 | if self.listeners.has_key(key): |
|---|
| | 93 | for f in self.listeners[key]: |
|---|
| | 94 | f(source, signal) |
|---|
| | 95 | |
|---|
| | 96 | |
|---|
| | 112 | |
|---|
| | 113 | class Singleton(object): |
|---|
| | 114 | """An implementation of the Singleton pattern. When a new Singleton is created, it first checks to see if an instance of its particular class exists. If not, then a new instance is created. After the created or stored instance is returned. The Singleton is designed so that subclasses of Singleton inherit this ability thereby leaving only the implmentation work for the subclass. |
|---|
| | 115 | |
|---|
| | 116 | >>> class Test(Singleton): |
|---|
| | 117 | ... pass |
|---|
| | 118 | ... |
|---|
| | 119 | >>> s = Singleton() |
|---|
| | 120 | >>> s2 = Singleton() |
|---|
| | 121 | >>> s == s2 |
|---|
| | 122 | True |
|---|
| | 123 | >>> t = Test() |
|---|
| | 124 | >>> t2 = Test() |
|---|
| | 125 | >>> t == t2 |
|---|
| | 126 | True |
|---|
| | 127 | >>> s == t |
|---|
| | 128 | False |
|---|
| | 129 | """ |
|---|
| | 130 | __singletons = {} |
|---|
| | 131 | |
|---|
| | 132 | def __new__(cls, *args, **kwargs): |
|---|
| | 133 | """Overrides default object creation to ensure that only one instance per class is ever returned. These instances are stored in a dictionary on the Singleton class that is keyed on the type of the object being created.""" |
|---|
| | 134 | if not Singleton.__singletons.has_key(cls): |
|---|
| | 135 | Singleton.__singletons[cls] = None |
|---|
| | 136 | |
|---|
| | 137 | if Singleton.__singletons[cls] is None: |
|---|
| | 138 | Singleton.__singletons[cls] = object.__new__(cls, *args, **kwargs) |
|---|
| | 139 | |
|---|
| | 140 | return Singleton.__singletons[cls] |
|---|
| | 141 | |
|---|