| author | gibus |
| Thu, 21 Jul 2011 15:02:50 +0200 | |
| changeset 365 | a478cb9786fd |
| parent 253 | a844469257b0 |
| child 366 | 98af3be91847 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# warning : oo server autolaunch is tricky |
2 |
# make sure .qt .kde .openoffice.org2 should be writable in home directory |
|
3 |
# for instance, if working user is www-data |
|
4 |
# mkdir /var/www/.openoffice.org2 ; chown www-data:www-data /var/www/.openoffice.org2 |
|
5 |
# mkdir /var/www/.qt ; chown www-data:www-data /var/www/.qt |
|
6 |
# mkdir /var/www/.kde ; chown www-data:www-data /var/www/.kde |
|
7 |
||
| 51 | 8 |
UNO_IMPORT = True |
| 0 | 9 |
|
10 |
if UNO_IMPORT: |
|
11 |
import uno |
|
12 |
||
13 |
# old ubuntu bug left for the record |
|
14 |
# print "#### Uno import failed ! #### " |
|
15 |
# print "#### https://bugs.launchpad.net/ubuntu/+source/openoffice.org2/+bug/139077 #### " |
|
16 |
# print "#### launch : sudo ldconfig -v /usr/lib/openoffice/program #### " |
|
17 |
||
18 |
from cm.utils.thread import synchronized, daemonize |
|
19 |
if UNO_IMPORT: |
|
20 |
from com.sun.star.beans import PropertyValue |
|
21 |
from datetime import datetime |
|
22 |
from subprocess import Popen,call |
|
23 |
from tempfile import mkstemp,mkdtemp |
|
24 |
||
25 |
if UNO_IMPORT: |
|
26 |
from unohelper import systemPathToFileUrl, absolutize |
|
27 |
||
28 |
from xml.dom.minidom import parseString |
|
29 |
import cStringIO |
|
30 |
import chardet |
|
31 |
import sys |
|
32 |
import magic |
|
33 |
import os,re |
|
34 |
import random |
|
35 |
import threading |
|
36 |
import time |
|
37 |
import logging |
|
38 |
||
39 |
CONN_STRING = "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" |
|
40 |
||
41 |
KILL = 'killall -KILL xvfb-run ; killall -KILL soffice; killall -KILL soffice.bin; killall -KILL Xvfb' |
|
42 |
RM = 'rm -f /tmp/.X99-lock' |
|
43 |
LAUNCH = 'xvfb-run soffice -headless "-accept=socket,port=2002;urp;"' |
|
44 |
||
45 |
# xvfb-run soffice -headless "-accept=socket,port=2002;urp;"; |
|
46 |
# soffice "-accept=socket,port=2002;urp;"; |
|
47 |
||
48 |
||
49 |
ms = magic.open(magic.MAGIC_NONE) |
|
50 |
ms.load() |
|
51 |
||
52 |
def is_text(buffer): |
|
53 |
type = ms.buffer(buffer) |
|
54 |
return ' text, ' in type |
|
55 |
||
56 |
def fix_text_encoding(buffer, to_encoding = 'utf-8'): |
|
57 |
detected = chardet.detect(buffer) |
|
58 |
encoding = detected['encoding'] |
|
59 |
if encoding != to_encoding: |
|
60 |
return buffer.decode(encoding).encode(to_encoding) |
|
61 |
return buffer |
|
62 |
# $$$ RBE TODO fix_content a call should be made before oo_convert call when importing text file with non utf-8 encoding todo test that to make it crash |
|
63 |
def fix_content(buffer): |
|
64 |
""" |
|
65 |
Fix content fixes : |
|
66 |
- encoding to utf8 to txt files |
|
67 |
""" |
|
68 |
try: |
|
69 |
if is_text(buffer): |
|
70 |
return fix_text_encoding(buffer) |
|
71 |
return buffer |
|
72 |
except: |
|
73 |
return buffer |
|
74 |
||
75 |
processing = 0 |
|
76 |
||
77 |
# timeout : kill oo |
|
78 |
PROCESSING_TIMEOUT = 20.0 |
|
79 |
||
80 |
def oo_process_controller(code): |
|
81 |
""" |
|
82 |
If 'code' process is still active : kill oo |
|
83 |
""" |
|
84 |
global processing |
|
85 |
logging.info('oo_process_controller') |
|
86 |
if processing == code: |
|
87 |
logging.error('--> oo_process_controller : killing !') |
|
88 |
kill_oo() |
|
89 |
||
90 |
def kill_oo(): |
|
91 |
logging.info('killing') |
|
92 |
p = Popen(KILL, shell=True) |
|
93 |
sts = os.waitpid(p.pid, 0) |
|
94 |
p = Popen(RM, shell=True) |
|
95 |
sts = os.waitpid(p.pid, 0) |
|
96 |
||
97 |
def launch_oo(): |
|
98 |
logging.info('launching') |
|
99 |
p = Popen(LAUNCH, shell=True) |
|
100 |
||
101 |
def kill_and_relaunch_oo(): |
|
102 |
kill_oo() |
|
103 |
launch_oo() |
|
104 |
||
105 |
get_connection_lock = threading.RLock() |
|
106 |
||
107 |
def start_processing(): |
|
108 |
global processing |
|
109 |
logging.info('start_processing') |
|
110 |
code = random.random() |
|
111 |
processing = code |
|
112 |
t = threading.Timer(PROCESSING_TIMEOUT, oo_process_controller, args = [code,]) |
|
113 |
t.start() |
|
114 |
||
115 |
def end_processing(): |
|
116 |
logging.info('end_processing') |
|
117 |
global processing |
|
118 |
processing = 0 |
|
119 |
||
120 |
@synchronized(get_connection_lock) |
|
121 |
def get_connection(retry = 2): |
|
122 |
while retry > 0: |
|
123 |
try: |
|
124 |
localContext = uno.getComponentContext() |
|
125 |
||
126 |
resolver = localContext.ServiceManager.createInstanceWithContext( |
|
127 |
"com.sun.star.bridge.UnoUrlResolver", localContext ) |
|
128 |
||
129 |
ctx = resolver.resolve(CONN_STRING) |
|
130 |
return ctx |
|
131 |
except: |
|
132 |
retry -= 1 |
|
133 |
kill_and_relaunch_oo() |
|
134 |
time.sleep(8) |
|
135 |
||
136 |
raise Exception('could not launch oo, please read README.txt section Openoffice for troubleshooting') |
|
137 |
||
138 |
def get_desktop(): |
|
139 |
ctx = get_connection() |
|
140 |
smgr = ctx.ServiceManager |
|
141 |
# get the central desktop object |
|
142 |
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) |
|
143 |
||
144 |
return desktop |
|
145 |
||
146 |
||
147 |
class FmtList: |
|
148 |
def __init__(self): |
|
149 |
self._list = [] |
|
150 |
||
151 |
def add(self, name, extension, summary, filter, export = False, mimetype = None): |
|
152 |
dd = { |
|
153 |
'name' : name, |
|
154 |
'extension' : extension, |
|
155 |
'summary' : summary, |
|
156 |
'filter' : filter, |
|
157 |
'export' : export, |
|
158 |
'mimetype' : mimetype, |
|
159 |
} |
|
160 |
self._list.append(dd) |
|
161 |
||
162 |
def get_filter_by_summary(self, value): |
|
163 |
return self.get_filter_by('summary', value) |
|
164 |
||
165 |
def get_filter_by_name(self, value): |
|
166 |
return self.get_filter_by('name', value) |
|
167 |
||
168 |
def get_filter_by(self, name, value): |
|
169 |
res = self.get_by(name, value) |
|
170 |
if res: |
|
171 |
return res['filter'] |
|
172 |
return None |
|
173 |
||
174 |
def get_by_name(self, value): |
|
175 |
return self.get_by('name', value) |
|
176 |
||
177 |
def get_by(self, name, value): |
|
178 |
for fmt in self._list: |
|
179 |
if fmt[name] == value: |
|
180 |
return fmt |
|
181 |
return None |
|
182 |
||
183 |
def get_export_formats_tuple(self): |
|
184 |
return [(f['summary'],f['name']) for f in self._list if f['export']] |
|
185 |
||
186 |
def ids_by_summary(self): |
|
187 |
return self.ids_by('summary') |
|
188 |
||
189 |
def ids_by(self, name): |
|
190 |
return dict([(r[name],r['name']) for r in self._list]) |
|
191 |
||
192 |
fmts = None |
|
193 |
if UNO_IMPORT: |
|
194 |
fmts = FmtList() |
|
195 |
fmts.add('bib', 'bib', 'BibTeX', 'BibTeX_Writer') |
|
196 |
fmts.add('doc', 'doc', 'Microsoft Word 97/2000/XP', 'MS Word 97', True, 'application/msword') |
|
197 |
fmts.add('doc6', 'doc', 'Microsoft Word 6.0', 'MS WinWord 6.0') |
|
198 |
fmts.add('doc95', 'doc', 'Microsoft Word 95', 'MS Word 95') |
|
199 |
fmts.add('docbook', 'xml', 'DocBook', 'DocBook File') |
|
200 |
fmts.add('html', 'html', 'HTML Document (OpenOffice.org Writer)', 'HTML (StarWriter)') |
|
201 |
fmts.add('odt', 'odt', 'Open Document Text', 'writer8', True, 'application/vnd.oasis.opendocument.text') |
|
202 |
fmts.add('ott', 'ott', 'Open Document Text', 'writer8_template') |
|
203 |
fmts.add('ooxml', 'xml', 'Microsoft Office Open XML', 'MS Word 2003 XML') |
|
204 |
fmts.add('pdb', 'pdb', 'AportisDoc (Palm)', 'AportisDoc Palm DB') |
|
205 |
fmts.add('pdf', 'pdf', 'Portable Document Format', 'writer_pdf_Export', True, 'application/pdf') |
|
206 |
fmts.add('psw', 'psw', 'Pocket Word', 'PocketWord File') |
|
207 |
fmts.add('rtf', 'rtf', 'Rich Text Format', 'Rich Text Format', True, 'application/rtf') |
|
208 |
fmts.add('latex', 'ltx', 'LaTeX 2e', 'LaTeX_Writer') |
|
209 |
fmts.add('sdw', 'sdw', 'StarWriter 5.0', 'StarWriter 5.0') |
|
210 |
fmts.add('sdw4', 'sdw', 'StarWriter 4.0', 'StarWriter 4.0') |
|
211 |
fmts.add('sdw3', 'sdw', 'StarWriter 3.0', 'StarWriter 3.0') |
|
212 |
fmts.add('stw', 'stw', 'Open Office.org 1.0 Text Document Template', 'writer_StarOffice_XML_Writer_Template') |
|
213 |
fmts.add('sxw', 'sxw', 'Open Office.org 1.0 Text Document', 'StarOffice XML (Writer)') |
|
214 |
fmts.add('text', 'txt', 'Text Encoded', 'Text (encoded)', True, 'application/txt') |
|
215 |
fmts.add('txt', 'txt', 'Plain Text', 'Text') |
|
216 |
fmts.add('vor', 'vor', 'StarWriter 5.0 Template', 'StarWriter 5.0 Vorlage/Template') |
|
217 |
fmts.add('vor4', 'vor', 'StarWriter 4.0 Template', 'StarWriter 4.0 Vorlage/Template') |
|
218 |
fmts.add('vor3', 'vor', 'StarWriter 3.0 Template', 'StarWriter 3.0 Vorlage/Template') |
|
219 |
fmts.add('xhtml', 'html', 'XHTML Document', 'XHTML Writer File') |
|
220 |
||
221 |
THE_OUTDIR = "outdir" |
|
222 |
THE_OUTFILE = "outfile" |
|
223 |
||
224 |
THE_INDIR = "indir" |
|
225 |
THE_INFILE = "infile" |
|
226 |
||
227 |
def extract_css_body(xhtml): |
|
228 |
dom = parseString(xhtml.encode('utf8')) |
|
229 |
style = dom.getElementsByTagName("style")[0].toxml() |
|
230 |
body = dom.getElementsByTagName("body")[0].toxml() |
|
231 |
# cleanup initial/final tags |
|
232 |
style_clean = style[style.find('>')+1:style.rfind('</')] |
|
233 |
body_clean = body[body.find('>')+1:body.rfind('</')] |
|
234 |
return style_clean,body_clean |
|
235 |
||
236 |
convert_lock = threading.RLock() |
|
237 |
||
238 |
def combine_css_body(body, css): |
|
239 |
return """ |
|
240 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
241 |
<head> |
|
242 |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
|
243 |
<style type="text/css"> |
|
244 |
%s |
|
245 |
</style> |
|
246 |
</head> |
|
247 |
<body> |
|
248 |
%s |
|
249 |
</body> |
|
250 |
</html> |
|
251 |
""" %(css,body) |
|
252 |
||
253 |
def to_string(input): |
|
254 |
if type(input) == unicode: |
|
255 |
input = input.encode('utf8') |
|
256 |
return input |
|
257 |
||
258 |
@synchronized(convert_lock) |
|
259 |
def convert_html(input, format_name, images = None): |
|
260 |
out_filter = fmts.get_filter_by_name(format_name) |
|
261 |
if not out_filter: |
|
262 |
raise Exception("Unsupported format name %s" %(format_name)) |
|
263 |
infile = None |
|
264 |
outfile = None |
|
265 |
out_f = None |
|
266 |
try: |
|
267 |
desktop = get_desktop() |
|
268 |
||
269 |
start_processing() |
|
270 |
||
271 |
# create in/out files |
|
272 |
temp_dir = mkdtemp(prefix="cm_") |
|
273 |
||
274 |
# in |
|
275 |
indir_name = os.path.join(temp_dir, THE_INDIR) |
|
276 |
os.mkdir(indir_name) |
|
277 |
infile_name = os.path.join(indir_name, THE_INFILE + '.html') |
|
278 |
||
279 |
# out |
|
280 |
outdir_name = os.path.join(temp_dir, THE_OUTDIR) |
|
281 |
os.mkdir(outdir_name) |
|
282 |
outfile_name = os.path.join(outdir_name, THE_OUTFILE) |
|
283 |
||
284 |
# write infile |
|
285 |
infile = open(infile_name,'w') |
|
286 |
input = to_string(input) |
|
287 |
infile.write(input) |
|
288 |
infile.close() |
|
289 |
||
290 |
# fix perms |
|
291 |
# TODO: group permission should suffice |
|
292 |
os.chmod(temp_dir, 0755) # read |
|
293 |
os.chmod(indir_name, 0755) # read |
|
294 |
os.chmod(infile_name, 0755) # read |
|
295 |
os.chmod(outdir_name, 0777) # read / write |
|
296 |
||
297 |
inProps = PropertyValue( "Hidden" , 0 , True, 0 ), |
|
298 |
doc = desktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, inProps ) |
|
299 |
text = doc.Text |
|
300 |
cursor = text.createTextCursor() |
|
301 |
||
302 |
fileUrl = systemPathToFileUrl(infile_name) |
|
303 |
cursor.insertDocumentFromURL(fileUrl, ()) |
|
304 |
||
305 |
properties= (PropertyValue("Hidden", 0, True, 0), PropertyValue("FilterName", 0, out_filter, 0)) |
|
306 |
doc.storeToURL('file://%s' %outfile_name,tuple(properties)) |
|
307 |
||
308 |
out_f = open(outfile_name,'r') |
|
309 |
||
310 |
output = out_f.read() |
|
311 |
return output |
|
312 |
finally: |
|
313 |
end_processing() |
|
314 |
try: |
|
315 |
if out_f: |
|
316 |
out_f.close() |
|
317 |
if infile: |
|
318 |
infile.close() |
|
319 |
top = temp_dir |
|
|
365
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
320 |
#for root, dirs, files in os.walk(top, topdown=False): |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
321 |
# for name in files: |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
322 |
# os.remove(os.path.join(root, name)) |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
323 |
# for name in dirs: |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
324 |
# os.rmdir(os.path.join(root, name)) |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
253
diff
changeset
|
325 |
#os.rmdir(top) |
| 0 | 326 |
except: |
327 |
# TODO : warn |
|
328 |
pass |
|
329 |
||
330 |
@synchronized(convert_lock) |
|
331 |
def convert(input, format_name, unicode = False): |
|
332 |
||
333 |
logging.info('convert') |
|
334 |
out_filter = fmts.get_filter_by_name(format_name) |
|
335 |
if not out_filter: |
|
336 |
raise Exception("Unsupported format name %s" %(format_name)) |
|
337 |
infile = None |
|
338 |
outfile = None |
|
339 |
out_f = None |
|
340 |
try: |
|
341 |
desktop = get_desktop() |
|
342 |
||
343 |
start_processing() |
|
344 |
||
345 |
# create in/out files |
|
346 |
temp_dir = mkdtemp(prefix="cm_") |
|
347 |
||
348 |
# in |
|
349 |
indir_name = os.path.join(temp_dir, THE_INDIR) |
|
350 |
os.mkdir(indir_name) |
|
351 |
infile_name = os.path.join(indir_name, THE_INFILE) |
|
352 |
||
353 |
# out |
|
354 |
outdir_name = os.path.join(temp_dir, THE_OUTDIR) |
|
355 |
os.mkdir(outdir_name) |
|
356 |
outfile_name = os.path.join(outdir_name, THE_OUTFILE) |
|
357 |
||
358 |
# write infile |
|
359 |
infile = open(infile_name,'w') |
|
360 |
input = to_string(input) |
|
361 |
infile.write(input) |
|
362 |
infile.close() |
|
363 |
||
364 |
# fix perms |
|
365 |
# TODO group permission should suffice |
|
366 |
os.chmod(temp_dir, 0755) # read |
|
367 |
os.chmod(indir_name, 0755) # read |
|
368 |
os.chmod(infile_name, 0755) # read |
|
369 |
os.chmod(outdir_name, 0777) # read / write |
|
370 |
||
371 |
properties = PropertyValue("Hidden", 0, True, 0), |
|
372 |
||
373 |
#import pdb;pdb.set_trace() |
|
374 |
doc=desktop.loadComponentFromURL("file://%s" % infile_name, "_blank", 0, properties) |
|
375 |
||
376 |
properties= (PropertyValue("Hidden", 0, True, 0), PropertyValue("FilterName", 0, out_filter, 0)) |
|
377 |
doc.storeToURL('file://%s' %outfile_name,tuple(properties)) |
|
378 |
||
379 |
out_f = open(outfile_name,'r') |
|
380 |
||
381 |
output = out_f.read() |
|
382 |
# load other files (useful only for html) |
|
383 |
image_names = [name for name in os.listdir(outdir_name) if name != THE_OUTFILE] |
|
384 |
img_res = [] |
|
385 |
for image_name in image_names: |
|
386 |
img_res.append(os.path.join(outdir_name, image_name)) |
|
387 |
if unicode: |
|
388 |
output = output.decode('utf8') |
|
389 |
return output,img_res |
|
390 |
finally: |
|
391 |
end_processing() |
|
392 |
try: |
|
393 |
if out_f: |
|
394 |
out_f.close() |
|
395 |
if infile: |
|
396 |
infile.close() |
|
397 |
# Do not remove dir: we only return images path to avoid |
|
398 |
# mem overload |
|
399 |
# top = temp_dir |
|
400 |
# for root, dirs, files in os.walk(top, topdown=False): |
|
401 |
# for name in files: |
|
402 |
# os.remove(os.path.join(root, name)) |
|
403 |
# for name in dirs: |
|
404 |
# os.rmdir(os.path.join(root, name)) |
|
405 |
# os.rmdir(top) |
|
406 |
except: |
|
407 |
# TODO : warn |
|
408 |
pass |