/tut2/Runner.py
Python | 34 lines | 22 code | 2 blank | 10 comment | 2 complexity | 3aa5146f98b5a5d0aa7775e01aae54bd MD5 | raw file
- from subprocess import call
- class Runner(object):
- """ A class to simplify running external programs. """
- def _run(self, command = [], stdin = None, stdout = None, shell = False):
- """
- Don't call directly! Use kickoff.
- Wrapper around subprocess.call. See
- http://docs.python.org/library/subprocess.html for full details.
- """
- ret_code = call(command, stdin=stdin, stdout=stdout, shell=shell)
- if ret_code:
- self.handle_ret_code(ret_code, command, stdin, stdout, shell)
- def handle_ret_code(self, ret_code, command, stdin, stdout, shell):
- """
- Exception handler in case something breaks running an external command.
- Should be overridden in child classes. Parameters are as in self.run,
- with the exception of the first parameter, which is the actual return code
- from the command execution.
- """
- raise RuntimeError("Command Failed!!!")
- def super_me(self):
- """ A shortcut method to call super on me. """
- return super(type(self), self)
- def kickoff(self, command = [], stdin = None, stdout = None, shell = False):
- """
- Actually kick off a run of the command. Use this for interface
- consistency.
- """
- self._run(command, stdin, stdout, shell)