Package backends :: Module cli
[hide private]

Source Code for Module backends.cli

 1  # Officeshots.org - Test your office documents in different applications 
 2  # Copyright (C) 2009 Stichting Lone Wolves 
 3  # Written by Sander Marechal <s.marechal@jejik.com> 
 4   
 5  """ 
 6  This module is a back-end for generic commandline conversion such as 
 7  supported by AbiWord. 
 8  """ 
 9   
10  import os 
11  import logging 
12  import subprocess 
13   
14  from pprint import pprint 
15  from backends import Backend, BackendException 
16   
17 -class CLIException(BackendException):
18 - def __str__(self):
19 return 'CLIException: ' + BackendException.__str__(self)
20 21
22 -class CLI(Backend):
23 """ 24 Backend for the headless OpenOffice.org server 25 """
26 - def initialize(self):
27 self.command = self.config.get(self.section, 'command') 28 self.result = self.config.get(self.section, 'result') 29 self.default_format = self.config.get(self.section, 'default_format')
30
31 - def execute(self, command):
32 try: 33 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 34 retcode = p.wait() 35 except OSError, ex: 36 raise CLIException(str(ex)) 37 38 stdout = p.stdout.read() 39 stderr = p.stderr.read() 40 41 return (retcode, stdout, stderr)
42
43 - def process(self, job):
44 if job['format'] == '': 45 job['format'] = self.default_format 46 47 self.job = job 48 self.source_path = self.save_document(job) 49 50 command = self.parse(self.command) 51 result = self.parse(self.result) 52 process = self.execute(command.split()) 53 54 if process[0] > 0: 55 raise CLIException('The command `%s` returned with a non-zero exit status' % command, True) 56 57 logging.info('CLI converted %s to %s' % (self.source_path, result)) 58 contents = self.load_document(result) 59 os.unlink(self.source_path) 60 os.unlink(result) 61 62 return (job['format'], contents)
63
64 - def parse(self, string):
65 source_dir, source_file = os.path.split(self.source_path) 66 source_base, source_ext = os.path.splitext(source_file) 67 source_ext = source_ext[1:] 68 69 dest_ext = self.job['format'] 70 if dest_ext == 'odf': 71 dest_ext = job['doctype'] 72 73 tmp_dir = os.path.normpath(self.config.get('global', 'tmp_files')) 74 75 string = string.replace('%source_path', self.source_path) 76 string = string.replace('%source_dir', source_dir) 77 string = string.replace('%source_file', source_file) 78 string = string.replace('%source_base', source_base) 79 string = string.replace('%source_ext', source_ext) 80 81 string = string.replace('%jobid', self.job['job']) 82 string = string.replace('%format', self.job['format']) 83 string = string.replace('%dest_ext', dest_ext) 84 string = string.replace('%temp_dir', tmp_dir) 85 86 return string
87