Exemple #1
0
class BaseOutput:
    """ This is the base class for all output types. Please do not use this
        output directly. Use one of L{ValueOutput}, L{StreamOutput} or
        L{FrameOutput} instead. 
        
        All other types are subclassed from this one. But they provide some 
        additional functions or other behavior like this."""
    _d_inputs           = None # List of inputs connected to this output
    _d_event_manager    = None
    _d_logger           = None


    def __init__(self):
        """ Constructor of an output pin. This constructor takes a referece to
            the event-manager. """
        self._d_inputs = list()
        self._d_event_manager = EventManager()
        self._d_logger = logging.getLogger("edef.core")


    def __call__(self, value):
        """ This method will be used to set the value of this output. Simply
            call o_out(value) to set the value """
        self.set_value(value)


    def __rshift__(self, inp):
        """ This operatior is used to add this putput as an event-source to 
            the other input. The usage is ModA.o_out >> ModB.i_in
            inp should be a callable."""
        self.add_input(inp)


    def __iadd__(self, inp):
        """ Does the same like __rshift__() but can be used otherwise:
            ModA.o_out += ModB.i_in """
        self.add_input(inp)
        return self

    
    def __isub__(self, inp):
        """ Removes the given input from this output.
            Usage: ModA.o_out -= ModB.i_in """
        self.rem_input(inp)
        return self


    def __contains__(self, inp):
        """ This method should return true if the give input is connected to 
            this output.
            
            Usage: if ModB.i_in in ModB.o_out: pass
            
            inp should be a callable. """
        return self.has_input(inp)


    def set_value(self, value):
        """ This method will be called by the __call__ special method to set 
            the value of the output. Please use the __call__ method instead of
            calling this method directly. Also the __call__method will provide
            some eye-candy for you. Simple do C{output(new_value)} to set the 
            new value to the output. """
        self._d_logger.debug("Set output %s => %s"%(getattr(self,"__name__",self),value))

        for inp in self._d_inputs:
            self._d_event_manager.add_event(inp, value)


    def add_input(self, callback):
        """ This method will add an input to the output. This input will be
            called if the value of the output changes. Please use the 
            overwritten operator C{+=} or C{>>} to add new inputs to an 
            output instead of calling this method direct. Example:
            C{output += new_input} or C{output >> another_input}. The imput 
            should be a I{callable} that takes only one argument, may be a 
            function or a method of a class. """
        if not callback in self._d_inputs:
            self._d_logger.debug("Adding input %s (%s) to list"%(callback, id(callback)))
            self._d_inputs.append(callback)


    def rem_input(self, callback):
        """ This method will remove the given input from this output. But 
            PLEASE do not call this method direct! Use the overwritten 
            operator C{-=} instead. 
            Example: C{output -= any_input_added_first}
            If the given input was not added to the output first, this method
            will raise an exception. """
        self._d_logger.debug("Remove input %s (%s) from list"%(callback, id(callback)))
        del self._d_inputs[self._d_inputs.index(callback)]


    def has_input(self, callback):
        """ This method will return True if the given input was added to this 
            output and False otherwise. Please do not use this method direct. 
            Use the overwritten operator C{in} instead: 
            C{if any_input in output: do_somethin()} """
        return callback in self._d_inputs