From edc7c6c03e5999789d349d98d4aa64bf022de9f3 Mon Sep 17 00:00:00 2001 From: jnoortheen Date: Thu, 3 Aug 2017 12:30:27 +0530 Subject: [PATCH] fix: add flat file formats support resolves #16 corrections flake8 --- templated_docs/__init__.py | 82 +++--- .../test_app/templates/correct_template.fodt | 247 ++++++++++++++++++ tests/test_app/tests.py | 8 + 3 files changed, 301 insertions(+), 36 deletions(-) create mode 100644 tests/test_app/templates/correct_template.fodt diff --git a/templated_docs/__init__.py b/templated_docs/__init__.py index 1e646e0..2b12ea7 100644 --- a/templated_docs/__init__.py +++ b/templated_docs/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- - +import codecs from multiprocessing import Process, Queue import os.path import re @@ -22,11 +22,11 @@ from pylokit import Office import logging + log = logging.getLogger(__name__) __version__ = '0.3.1' - IMAGES_CONTEXT_KEY = '_templated_docs_imgs' @@ -51,6 +51,7 @@ def fix_inline_tags(content): broken Django constructs. To remedy that, we find all the Django tags and variables and fix entities inside them. """ + def repl(match): text = match.group(0) text = text.replace('', ' ') @@ -112,41 +113,50 @@ def fill_template(template_name, context, output_format='odt'): source_file = find_template_file(template_name) source_extension = os.path.splitext(source_file)[1] - source = zipfile.ZipFile(source_file, 'r') - dest_file = NamedTemporaryFile(delete=False, suffix=source_extension) - dest = zipfile.ZipFile(dest_file, 'w') - - manifest_data = '' - for name in source.namelist(): - data = source.read(name) - if name.endswith('.xml'): - data = smart_str(data) - - if any(name.endswith(file) for file in ('content.xml', 'styles.xml')): - template = Template(fix_inline_tags(data)) - data = template.render(context) - elif name == 'META-INF/manifest.xml': - manifest_data = data[:-20] # Cut off the closing tag - continue # We will append it at the very end - dest.writestr(name, smart_bytes(data)) - - for _, image in context.dicts[0].get(IMAGES_CONTEXT_KEY, {}).items(): - filename = os.path.basename(image.name) - ext = os.path.splitext(filename)[1][1:] - manifest_data += ('\n' - ) % locals() - image.open() - dest.writestr('Pictures/%s' % filename, image.read()) - image.close() - - manifest_data += '' - dest.writestr('META-INF/manifest.xml', manifest_data) - - source.close() - dest.close() + + if zipfile.is_zipfile(source_file): + with zipfile.ZipFile(source_file, 'r') as source: + with zipfile.ZipFile(dest_file, 'w') as dest: + manifest_data = '' + for name in source.namelist(): + data = source.read(name) + if name.endswith('.xml'): + data = smart_str(data) + + if any(name.endswith(file) for file in ( + 'content.xml', 'styles.xml' + )): + template = Template(fix_inline_tags(data)) + data = template.render(context) + elif name == 'META-INF/manifest.xml': + # Cut off the closing tag + manifest_data = data[:-20] + continue # We will append it at the very end + dest.writestr(name, smart_bytes(data)) + + for _, image in context.dicts[0].get( + IMAGES_CONTEXT_KEY, {} + ).items(): + filename = os.path.basename(image.name) + ext = os.path.splitext(filename)[1][1:] + manifest_data += ('\n' + ) % locals() + image.open() + dest.writestr('Pictures/%s' % filename, image.read()) + image.close() + + manifest_data += '' + dest.writestr('META-INF/manifest.xml', manifest_data) + + else: + with codecs.open(source_file, 'rb', 'utf-8') as source: + template = Template(source.read()) + dest_file.write(smart_bytes(template.render(context))) + dest_file.close() if source_extension[1:] != output_format: results = Queue() diff --git a/tests/test_app/templates/correct_template.fodt b/tests/test_app/templates/correct_template.fodt new file mode 100644 index 0000000..2f0e7ae --- /dev/null +++ b/tests/test_app/templates/correct_template.fodt @@ -0,0 +1,247 @@ + + + + LibreOffice/5.2.7.2$Linux_X86_64 LibreOffice_project/20m0$Build-2Морозов АлександрМорозов Александр2016-07-13T10:10:00Z2016-07-13T10:11:00Z1PT60S + + + 0 + 0 + 36768 + 16850 + true + false + + + view2 + 10883 + 2501 + 0 + 0 + 36767 + 16849 + 0 + 1 + false + 100 + false + + + + + false + + false + false + false + false + false + true + true + false + + true + + false + false + true + false + true + false + 1699386 + false + false + true + false + false + true + true + true + true + true + true + true + false + false + 1 + false + true + false + + false + false + false + + false + false + false + + false + 0 + false + + 0 + true + false + false + false + false + false + false + false + true + 1699386 + false + false + false + high-resolution + true + true + true + false + false + false + false + false + false + false + false + false + true + false + 0 + true + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {{ name }} + + + \ No newline at end of file diff --git a/tests/test_app/tests.py b/tests/test_app/tests.py index feb5e95..36a8b8b 100644 --- a/tests/test_app/tests.py +++ b/tests/test_app/tests.py @@ -16,3 +16,11 @@ def test_fill_correct_template(self): filename = fill_template('correct_template.odt', {'name': 'John'}, output_format='pdf') os.unlink(filename) + + def test_fill_flat_correct_template(self): + """ + test for flat file format fodt + """ + filename = fill_template('correct_template.fodt', {'name': 'John'}, + output_format='pdf') + os.unlink(filename)