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 platform |
---|
13 | import subprocess |
---|
14 | |
---|
15 | from pprint import pprint |
---|
16 | from backends import Backend, BackendException |
---|
17 | |
---|
18 | class CLIException(BackendException): |
---|
19 | def __str__(self): |
---|
20 | return 'CLIException: ' + BackendException.__str__(self) |
---|
21 | |
---|
22 | |
---|
23 | class CLI(Backend): |
---|
24 | """ |
---|
25 | Backend for the headless OpenOffice.org server |
---|
26 | """ |
---|
27 | def initialize(self): |
---|
28 | self.command = self.config.get(self.section, 'command') |
---|
29 | self.result = self.config.get(self.section, 'result') |
---|
30 | self.default_format = self.config.get(self.section, 'default_format') |
---|
31 | |
---|
32 | def execute(self, command): |
---|
33 | try: |
---|
34 | p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
35 | stdout, stderr = p.communicate() |
---|
36 | except OSError, ex: |
---|
37 | raise CLIException(str(ex)) |
---|
38 | |
---|
39 | return (p.returncode, stdout, stderr) |
---|
40 | |
---|
41 | def process(self, job): |
---|
42 | if job['format'] == '': |
---|
43 | job['format'] = self.default_format |
---|
44 | |
---|
45 | self.job = job |
---|
46 | self.source_path = self.save_document(job) |
---|
47 | |
---|
48 | command = self.parse(self.command) |
---|
49 | result = self.parse(self.result) |
---|
50 | |
---|
51 | if platform.system() == 'Windows': |
---|
52 | # Popen needs a string on Windows |
---|
53 | process = self.execute(command) |
---|
54 | else: |
---|
55 | process = self.execute(command.split()) |
---|
56 | |
---|
57 | if process[0] > 0: |
---|
58 | raise CLIException('The command `%s` returned with a non-zero exit status (%s, %s).' % (command, process[1], process[2]), True) |
---|
59 | |
---|
60 | logging.info('CLI converted %s to %s' % (self.source_path, result)) |
---|
61 | contents = self.load_document(result) |
---|
62 | os.unlink(self.source_path) |
---|
63 | os.unlink(result) |
---|
64 | |
---|
65 | return (job['format'], contents) |
---|
66 | |
---|
67 | def parse(self, string): |
---|
68 | source_dir, source_file = os.path.split(self.source_path) |
---|
69 | source_base, source_ext = os.path.splitext(source_file) |
---|
70 | source_ext = source_ext[1:] |
---|
71 | |
---|
72 | dest_ext = self.job['format'] |
---|
73 | if dest_ext == 'odf': |
---|
74 | dest_ext = self.job['doctype'] |
---|
75 | |
---|
76 | tmp_dir = os.path.normpath(self.config.get('global', 'tmp_files')) |
---|
77 | |
---|
78 | string = string.replace('%source_path', self.source_path) |
---|
79 | string = string.replace('%source_dir', source_dir) |
---|
80 | string = string.replace('%source_file', source_file) |
---|
81 | string = string.replace('%source_base', source_base) |
---|
82 | string = string.replace('%source_ext', source_ext) |
---|
83 | |
---|
84 | string = string.replace('%jobid', self.job['job']) |
---|
85 | string = string.replace('%format', self.job['format']) |
---|
86 | string = string.replace('%dest_ext', dest_ext) |
---|
87 | string = string.replace('%temp_dir', tmp_dir) |
---|
88 | |
---|
89 | return string |
---|
90 | |
---|