Skip to content

Commit 2f9b3d4

Browse files
authored
Run LO in the same process (jptd#3)
Run LO in the same process Co-authored-by: Sian Lerk Lau <kiawin@gmail.com>
2 parents 2afff83 + 8732789 commit 2f9b3d4

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.3.2
2+
current_version = 0.3.3
33
commit = True
44
tag = True
55
tag_name = {new_version}

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
requirements = [
1515
'pylokit==0.8.1',
16-
'django>=1.8,<=1.11',
16+
'django>=1.8,<1.12',
1717
]
1818

1919
test_requirements = [
@@ -22,7 +22,7 @@
2222

2323
setup(
2424
name='templateddocs',
25-
version='0.3.2',
25+
version='0.3.3',
2626
description=('Generate PDF, MS Word and Excel documents from templates '
2727
'in Django.'),
2828
long_description=readme + '\n\n' + history,

templated_docs/__init__.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from multiprocessing import Process, Queue
3+
import multiprocessing
44
import os.path
55
import re
66
from tempfile import NamedTemporaryFile
@@ -24,7 +24,7 @@
2424
import logging
2525
log = logging.getLogger(__name__)
2626

27-
__version__ = '0.3.2'
27+
__version__ = '0.3.3'
2828

2929

3030
IMAGES_CONTEXT_KEY = '_templated_docs_imgs'
@@ -77,12 +77,12 @@ def find_template_file(template_name):
7777
raise TemplateDoesNotExist(template_name)
7878

7979

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.
8382
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.
8686
"""
8787
lo_path = getattr(
8888
settings,
@@ -95,26 +95,41 @@ def _convert_subprocess(filename, format, result_queue, options=None):
9595
with lo.documentLoad(filename) as doc:
9696
doc.saveAs(str(conv_file.name), options=options)
9797
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
99105

100106

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+
):
102114
"""Fill a document with data and convert it to the requested format.
103115
104116
Returns an absolute path to the generated file.
105117
106118
Supported output format:
107119
Text documents: doc, docx, fodt, html, odt, ott, pdf, txt, xhtml, png
108120
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
110123
Drawings: fodg, html, odg, pdf, svg, swf, xhtml, png
111124
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
113127
114128
:param template_name: the path to template, in OpenDocument format
115129
:param context: the context to be used to inject content
116130
:param output_format: the output format
117131
:param options: value of filterOptions in libreofficekit
132+
:param separate_process: allow LO to
118133
119134
:return:
120135
"""
@@ -163,10 +178,19 @@ def fill_template(template_name, context, output_format='odt', options=None):
163178
dest.close()
164179

165180
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+
)
171195
else:
172196
return dest_file.name

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,34}-django{18,19,110}, flake8
2+
envlist = py{27,34}-django{18,19,110,111}, flake8
33

44
[testenv:flake8]
55
deps=flake8
@@ -12,5 +12,6 @@ deps =
1212
django18: Django>=1.8,<1.9
1313
django19: Django>=1.9,<1.10
1414
django110: Django>=1.10,<1.11
15+
django111: Django>=1.11,<1.12
1516
commands = python tests/manage.py test
1617
python example/manage.py check

0 commit comments

Comments
 (0)