1
1
# -*- coding: utf-8 -*-
2
2
3
- from multiprocessing import Process , Queue
3
+ import multiprocessing
4
4
import os .path
5
5
import re
6
6
from tempfile import NamedTemporaryFile
24
24
import logging
25
25
log = logging .getLogger (__name__ )
26
26
27
- __version__ = '0.3.2 '
27
+ __version__ = '0.3.3 '
28
28
29
29
30
30
IMAGES_CONTEXT_KEY = '_templated_docs_imgs'
@@ -77,12 +77,12 @@ def find_template_file(template_name):
77
77
raise TemplateDoesNotExist (template_name )
78
78
79
79
80
- def _convert_subprocess (filename , format , result_queue , options = None ):
81
- """
82
- Subprocess helper to convert a file via LOKit.
80
+ def _convert_file (filename , format , result_queue = None , options = None ):
81
+ """Helper function to convert a file via LOKit.
83
82
84
- We need it until LO doesn't crash randomly after conversion, terminating
85
- the calling process as well.
83
+ This function can be called via subprocess so that in the event of LO
84
+ crashing randomly after conversion, it will not terminate the parent
85
+ process. This is assisted by inserting the result into a Queue object.
86
86
"""
87
87
lo_path = getattr (
88
88
settings ,
@@ -95,26 +95,41 @@ def _convert_subprocess(filename, format, result_queue, options=None):
95
95
with lo .documentLoad (filename ) as doc :
96
96
doc .saveAs (str (conv_file .name ), options = options )
97
97
os .unlink (filename )
98
- result_queue .put (conv_file .name )
98
+
99
+ # type comparison is required instead of isinstance owing to
100
+ # multiprocessing.Queue is a method
101
+ if type (result_queue ) == multiprocessing .queues .Queue :
102
+ result_queue .put (conv_file .name )
103
+ else :
104
+ return conv_file .name
99
105
100
106
101
- def fill_template (template_name , context , output_format = 'odt' , options = None ):
107
+ def fill_template (
108
+ template_name ,
109
+ context ,
110
+ output_format = 'odt' ,
111
+ options = None ,
112
+ separate_process = True ,
113
+ ):
102
114
"""Fill a document with data and convert it to the requested format.
103
115
104
116
Returns an absolute path to the generated file.
105
117
106
118
Supported output format:
107
119
Text documents: doc, docx, fodt, html, odt, ott, pdf, txt, xhtml, png
108
120
Spreadsheets: csv, fods, html, ods, ots, pdf, xhtml, xls, xlsx, png
109
- Presentations: fodp, html, odg, odp, otp, pdf, potm, pot, pptx, pps, ppt, svg, swf, xhtml, png
121
+ Presentations: fodp, html, odg, odp, otp, pdf, potm, pot, pptx, pps,
122
+ ppt, svg, swf, xhtml, png
110
123
Drawings: fodg, html, odg, pdf, svg, swf, xhtml, png
111
124
112
- More on filter options, https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
125
+ More on filter options,
126
+ https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options # noqa: E501
113
127
114
128
:param template_name: the path to template, in OpenDocument format
115
129
:param context: the context to be used to inject content
116
130
:param output_format: the output format
117
131
:param options: value of filterOptions in libreofficekit
132
+ :param separate_process: allow LO to
118
133
119
134
:return:
120
135
"""
@@ -163,10 +178,19 @@ def fill_template(template_name, context, output_format='odt', options=None):
163
178
dest .close ()
164
179
165
180
if source_extension [1 :] != output_format :
166
- results = Queue ()
167
- convertor = Process (target = _convert_subprocess ,
168
- args = (str (dest_file .name ), output_format , results , options ))
169
- convertor .start ()
170
- return results .get ()
181
+ if separate_process :
182
+ results = multiprocessing .Queue ()
183
+ converter = multiprocessing .Process (
184
+ target = _convert_file ,
185
+ args = (str (dest_file .name ), output_format , results , options ),
186
+ )
187
+ converter .start ()
188
+ return results .get ()
189
+ else :
190
+ return _convert_file (
191
+ filename = str (dest_file .name ),
192
+ format = output_format ,
193
+ options = options ,
194
+ )
171
195
else :
172
196
return dest_file .name
0 commit comments