Skip to content

Commit e444ceb

Browse files
Jafneekiawin
authored andcommitted
Allow templateddoc to run LO in the same process, using "separate_process" param.
The change will allow templateddoc to be ran in celery without causing the following error: "daemonic processes are not allowed to have children"
1 parent 2afff83 commit e444ceb

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
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: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 crashing randomly
84+
after conversion, it will not terminate the parent process. This is assisted by inserting
85+
the result into a Queue object.
8686
"""
8787
lo_path = getattr(
8888
settings,
@@ -95,10 +95,14 @@ 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+
if isinstance(result_queue, Queue):
100+
result_queue.put(conv_file.name)
101+
else:
102+
return conv_file.name
99103

100104

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):
102106
"""Fill a document with data and convert it to the requested format.
103107
104108
Returns an absolute path to the generated file.
@@ -115,6 +119,7 @@ def fill_template(template_name, context, output_format='odt', options=None):
115119
:param context: the context to be used to inject content
116120
:param output_format: the output format
117121
:param options: value of filterOptions in libreofficekit
122+
:param separate_process: allow LO to
118123
119124
:return:
120125
"""
@@ -163,10 +168,17 @@ def fill_template(template_name, context, output_format='odt', options=None):
163168
dest.close()
164169

165170
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+
)
171183
else:
172184
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)