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 crashing randomly
84
+ after conversion, it will not terminate the parent process. This is assisted by inserting
85
+ the result into a Queue object.
86
86
"""
87
87
lo_path = getattr (
88
88
settings ,
@@ -95,10 +95,14 @@ 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
+ if isinstance (result_queue , Queue ):
100
+ result_queue .put (conv_file .name )
101
+ else :
102
+ return conv_file .name
99
103
100
104
101
- def fill_template (template_name , context , output_format = 'odt' , options = None ):
105
+ def fill_template (template_name , context , output_format = 'odt' , options = None , separate_process = True ):
102
106
"""Fill a document with data and convert it to the requested format.
103
107
104
108
Returns an absolute path to the generated file.
@@ -115,6 +119,7 @@ def fill_template(template_name, context, output_format='odt', options=None):
115
119
:param context: the context to be used to inject content
116
120
:param output_format: the output format
117
121
:param options: value of filterOptions in libreofficekit
122
+ :param separate_process: allow LO to
118
123
119
124
:return:
120
125
"""
@@ -163,10 +168,17 @@ def fill_template(template_name, context, output_format='odt', options=None):
163
168
dest .close ()
164
169
165
170
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 ()
171
+ if separate_process :
172
+ results = Queue ()
173
+ converter = Process (target = _convert_file ,
174
+ args = (str (dest_file .name ), output_format , results , options ))
175
+ converter .start ()
176
+ return results .get ()
177
+ else :
178
+ return _convert_file (
179
+ filename = str (dest_file .name ),
180
+ format = output_format ,
181
+ options = options ,
182
+ )
171
183
else :
172
184
return dest_file .name
0 commit comments