from subprocess import PIPE
from .process import Process


class Pipe(object):
    process_class = Process

    def __init__(self, commands, data=(), **opts):
        self.commands = commands
        self.data = data
        self.opts = opts

    def spawn_procs(self):
        stdout = PIPE
        for item in self.commands:
            proc = self.process_class(
                command=item,
                stdin=stdout,
                **self.opts
                )
            stdout = proc.process.stdout
            yield proc

    def run(self):
        procs = list(self.spawn_procs())
        procs[0].pipe(self.data)

        history = [p.run() for p in procs]
        r = history.pop()
        r.history = history
        for item in r.history:
            item.stdout.close()
        return r

@@ -1,12 +1,89 @@
+%22%22%22%0A    proclib.pipe%0A    ~~~~~~~~~~~~%0A%0A    Implements the Pipe object.%0A%22%22%22%0A%0A%0A
 from subproc
@@ -149,16 +149,313 @@
 bject):%0A
+    %22%22%22%0A    A Pipe object represents and starts the parallel%0A    execution and piping of multiple Processes.%0A%0A    :param commands: A list of commands.%0A    :param data: Data to be piped in to the first process.%0A    :param opts: Extra options to be passed to every%0A        spawned process.%0A    %22%22%22%0A%0A
     proc
@@ -629,32 +629,162 @@
 wn_procs(self):%0A
+        %22%22%22%0A        Return a list of processes that have had their%0A        file handles configured the correct order.%0A        %22%22%22%0A
         stdout =
@@ -1034,16 +1034,16 @@
 d proc%0A%0A
-
     def 
@@ -1045,32 +1045,325 @@
  def run(self):%0A
+        %22%22%22%0A        Runs the processes. Internally this calls the%0A        %60%60spawn_procs%60%60 method but converts them into%0A        responses via their %60%60run%60%60 method and returns%0A        a Response object. This also closes the stdout%0A        file handles of all but the last response.%0A        %22%22%22%0A
         procs = 

document pipe object