class Logger(component): """ This component is used to write messages to file. Upon instantiation, the a backplane is registered with the name LOG_ + logname, so that a log named 'foo.bar' would be registered under 'LOG_foo.bar'. Please note that the Logger will not be shut down automatically. It must be sent a shutdown message via its control box. Typically this component is to be used by a Chassis or some other Parent component to provide a log for its children. """ Inboxes = { 'inbox' : 'Receive a tuple containing the filename and message to log', 'control' : 'Receive shutdown messages',} Outboxes = {'outbox' : 'NOT USED', 'signal' : 'Send shutdown messages',} def __init__(self, logname, wrapper = wrapMessage): """ Initializes a new Logger. -logname - the name of the log to write to -wrapper - a method that takes a message as an argument and returns a formatted string to put in the log. """ super(Logger, self).__init__() self.logname = logname self.bplane = Backplane('LOG_' + logname) self.subscriber = SubscribeTo('LOG_' + logname) self.wrapper = wrapper #add the components as children self.addChildren(self.subscriber, self.bplane) self.link((self.subscriber, 'outbox'), (self, 'inbox')) self.link((self, 'signal'), (self.bplane, 'control')) def main(self): self.bplane.activate() self.subscriber.activate() self.first_run = False not_done = True while not_done: if self.dataReady('inbox'): file = open(self.logname, 'a') while self.dataReady('inbox'): msg = self.recv('inbox') file.write(self.wrapper(msg)) file.close() while self.dataReady('control'): msg = self.recv('control') if isinstance(msg, (shutdownMicroprocess)): not_done = False self.shutdown(msg) if not_done: self.pause() yield 1 def shutdown(self, msg): """ Sends shutdown message to signal box and removes children. """ self.send(msg, 'signal') self.removeChild(self.bplane) self.removeChild(self.subscriber)
n += 1 print "Received: %s" % data yield 1 if self.dataReady("control"): data = self.recv("control") print "Control: %s" % data self.send(producerFinished(self), "signal") return #if not self.anyReady(): # self.pause() yield 1 backplane = Backplane("SAMPLE") backplane.activate() producer = Producer() consumer = Consumer() published = PublishTo("SAMPLE") subscribe = SubscribeTo("SAMPLE") pipe1 = Pipeline( producer, published, ) pipe1.activate() pipe2 = Pipeline( subscribe, consumer