Changeset 81

Show
Ignore:
Timestamp:
07/05/07 19:41:39 (2 years ago)
Author:
jkyllo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/spike-20070613 tubes/tubes.py

    r78 r81  
    2828 
    2929    def __init__(self): 
     30        """Initialize a new Node.""" 
    3031        self.inputs = [] 
    3132        self.outputs = [] 
    3233 
    3334    def getInputs(self): 
    34         """Returns a sequence of inputs available on this Node.""" 
     35        """Return a sequence of inputs available on this Node.""" 
    3536        return self.inputs 
    3637 
    3738    def getOutputs(self): 
    38         """Returns a sequence of outputs available on this Node.""" 
     39        """Return a sequence of outputs available on this Node.""" 
    3940        return self.outputs 
    4041 
     42class 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 
     50class 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 
     72class 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 
    4197class PassthroughNode(Node): 
     98    class PassthroughOutput(Output): 
     99        def __init__(self, input=None): 
     100            if input is not None: 
     101                self.source = input 
     102 
     103        def read(self): 
     104            return self.source 
     105 
    42106    def __init__(self): 
     107        Node.__init__(self) 
     108        i = Input() 
     109        self.inputs.append(i) 
     110        self.outputs.append(PassthroughOutput(i)) 
    43111 
     112 
     113class 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