Package conduit :: Package utils :: Module CommandLineConverter
[hide private]

Source Code for Module conduit.utils.CommandLineConverter

 1  import re 
 2  import os 
 3  import signal 
 4  import popen2 
 5  import logging 
 6  log = logging.getLogger("Utils") 
 7   
8 -class CommandLineConverter:
9 - def __init__(self):
10 self.percentage_match = re.compile('.*')
11
12 - def _kill(self, process):
13 log.debug("Killing process") 14 os.kill(process.pid, signal.SIGKILL)
15
16 - def build_command(self, command, **params):
17 self.command = command
18
19 - def calculate_percentage(self, val):
20 return float(val)
21
22 - def check_cancelled(self):
23 return False
24
25 - def convert( self, input_filename, output_filename, callback=None,save_output=False):
26 command = self.command % (input_filename, output_filename) 27 log.debug("Executing %s" % command) 28 29 output = "" 30 process = popen2.Popen4(command) 31 stdout = process.fromchild 32 s = stdout.read(80) 33 if save_output: 34 output += s 35 while s: 36 if callback: 37 for i in self.percentage_match.finditer(s): 38 val = self.calculate_percentage(i.group(1).strip()) 39 callback(val) 40 if save_output: 41 output += s 42 if self.check_cancelled(): 43 self._kill(process) 44 s = stdout.read(80) 45 46 ok = process.wait() == 0 47 if save_output: 48 return ok, output 49 else: 50 return ok
51