Advertisement
Guest User

PipeMonoid

a guest
Jan 12th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. def pipe_example():
  2.     inp = Pipemonoid(["printf", "hello world"]).stdout_to_pipe()
  3.     gzip = Pipemonoid(["gzip", "-c"]).stdout_to_pipe()
  4.     gunzip = Pipemonoid(["gunzip", "-c"]).stdout_to_pipe()
  5.     base32 = Pipemonoid(["base32"])
  6.  
  7.     test_pipe = inp + gzip + gunzip + base32
  8.  
  9.     (stdout, stderr) = test_pipe.stdout_to_pipe().execute()
  10.  
  11.     print stdout
  12.  
  13.  
  14. class Pipemonoid(object):
  15.     def __init__(self, *args, **kwargs):
  16.         self._delegates = [[args, kwargs]]
  17.         self._current = None
  18.         self._temps = []
  19.  
  20.     @property
  21.     def last_process(self):
  22.         """
  23.        Internal use, do not touch
  24.        """
  25.         return self._delegates[-1]
  26.  
  27.     def add_process(self, process):
  28.         """
  29.        Internal use, do not touch
  30.        """
  31.         self._delegates.append(process)
  32.         return self
  33.  
  34.     def set_named_parameter(self, key, val):
  35.         """
  36.        Set a arbitrary named parameter for the
  37.        current top pipe
  38.        """
  39.         last = self.last_process
  40.         xs = last[1]
  41.         xs[key] = val
  42.         last[1] = xs
  43.  
  44.     def bufsize(self, bufsize):
  45.         """
  46.        Set the buffer size
  47.        """
  48.         self.set_named_parameter("bufsize",bufsize)
  49.         return self
  50.  
  51.     def __add__(self, other):
  52.         return self.pipe(other)
  53.  
  54.     def stdout_to_pipe(self):
  55.         """
  56.        Send the stdout down the pipeline
  57.        """
  58.         self.set_named_parameter("stdout", subprocess.PIPE)
  59.         return self
  60.  
  61.     # Realize the last entry, internal usage only!
  62.     def realize(self):
  63.         """
  64.        Realize the last entry of the pipeline
  65.        This will instantiate the pipe, and opens
  66.        a subprocess
  67.        """
  68.         last = self.last_process
  69.         args = last[0]
  70.         kwargs = last[1]
  71.         self._current = subprocess.Popen(*args, **kwargs)
  72.         return self
  73.  
  74.  
  75.     def stderr_to_pipe(self):
  76.         """
  77.        Send stderr to  the next pipe
  78.        """
  79.         self.set_named_parameter("stderr", subprocess.PIPE)
  80.         return self
  81.  
  82.     def pipe(self, nextprocess):
  83.         """
  84.        Concatenate two pipes together. This is
  85.        the binary operation of the pipe monoid.
  86.        """
  87.         self.realize()
  88.         self._delegates.append(nextprocess.last_process)
  89.         self.set_named_parameter("stdin", self._current.stdout)
  90.         return self
  91.  
  92.     def stdout_to_temp(self):
  93.         """
  94.        Redirect stdout to a named tempfile
  95.        returns the name
  96.        """
  97.         tmp = tempfile.NamedTemporaryFile(delete=False)
  98.         self._temps.append(tmp)
  99.         self.set_named_parameter("stdout", tmp)
  100.         return tmp
  101.  
  102.     def stderr_to_temp(self):
  103.         """
  104.        redirect stderr to a named temp file
  105.        returns the name
  106.        """
  107.         tmp = tempfile.NamedTemporaryFile(delete = False)
  108.         self._temps.append(tmp)
  109.         self.set_named_parameter("stderr", tmp)
  110.         return tmp
  111.  
  112.     def close(self):
  113.         """
  114.        Closes all the open file handles
  115.        """
  116.         for tmp in self._temps:
  117.             tmp.close()
  118.             if os.path.exists(tmp.name):
  119.                 os.unlink(tmp.name)
  120.  
  121.     def stdout_to_file(self, fp):
  122.         """
  123.        Redirect stdout to a file
  124.        """
  125.         self.set_named_parameter("stdout", fp)
  126.         return self
  127.  
  128.     def stderr_to_file(self, fp):
  129.         """
  130.        Redirect stderr to a file
  131.        """
  132.         self.set_named_parameter("stderr", fp)
  133.         return self
  134.  
  135.     def stdin_from_file(self, fp):
  136.         """
  137.        Redirect a file to stdin
  138.        """
  139.         self.set_named_parameter("stdin", fp)
  140.         return self
  141.  
  142.  
  143.     def execute(self, stdin = None):
  144.         """
  145.        Execute the build up monoidal action
  146.        """
  147.         self.realize()
  148.         (stdout, stderr) = self._current.communicate(stdin)
  149.         return (stdout, stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement