[TransWarp] input-driven application - assembling filters

alexander smishlajev alex at ank-sia.com
Fri May 16 13:49:52 EDT 2003


hello!

the following module makes a pipeline of two dummy filters from my 
yesterday's example 
http://www.eby-sarna.com/pipermail/transwarp/2003-May/000434.html

=== begin cut Echo2.py ===

from peak.api import binding
from peak.running import commands, interfaces

from Echo import Echo

class Echo2(commands.AbstractCommand):

     usage = """Copy standard input to standard output

Usage: peak import:Echo2.Echo2
"""

     filter1 = binding.New(Echo)
     filter2 = binding.New(Echo)

     def pipe(self, instDict, attrName):
         try:
             from ank.tools.NonBlockingPipe import NonBlockingPipe
             _obj = NonBlockingPipe()
             return (_obj, _obj)
         except ImportError:
             self.stderr.write("WARNING: using OS pipe")
             import os
             (_r, _w) = os.pipe()
             return (os.fdopen(_r, "rb"), os.fdopen(_w, "wb"))

     pipe = binding.Once(pipe)

     mainLoop = binding.bindTo(interfaces.IMainLoop)

     def run(self):
         self.uponAssembly()
         self.mainLoop.run()

     def uponAssembly(self):
         super(Echo2, self).uponAssembly()
         _pipe = self.pipe
         self.stderr.write("=== Pipe: %r\n" % (_pipe,))
         self.filter1.stdin = self.stdin
         self.filter2.stdout = _pipe[1]
         self.filter2.stdin = _pipe[0]
         self.filter2.stdout = self.stdout

==== end cut Echo2.py ====


this module works ok, but i think that there may be a better way to 
connect inner filters together.  right?

best wishes,
alex.

ps.  by the way, Phillip, if you think that such NonBlockingPipe could 
be useful addition to the PEAK, i may donate the code to you.  it is 
implemented over pythonic StringIO.  here is the class doc:

     """Pipe emulation with non-blocking read()

     NonBlockingPipe acts like OS pipe: it is written from one end,
     and read from the othe end.  Single instance of this class
     represents both sides of the pipe: all write operations
     come to the input end; all read operations return data
     from the output end.

     seek() and truncate() are disabled.

     tell() returns number of characters read from the pipe so far.

     close() closes the input side of the pipe.  When remaining data
     is read out, the pipe gets closed automagically.
     """





More information about the PEAK mailing list