Skip to content

Commit edc7c6c

Browse files
committed
fix: add flat file formats support
resolves #16 corrections flake8
1 parent c2bd2d2 commit edc7c6c

File tree

3 files changed

+301
-36
lines changed

3 files changed

+301
-36
lines changed

templated_docs/__init__.py

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
2+
import codecs
33
from multiprocessing import Process, Queue
44
import os.path
55
import re
@@ -22,11 +22,11 @@
2222
from pylokit import Office
2323

2424
import logging
25+
2526
log = logging.getLogger(__name__)
2627

2728
__version__ = '0.3.1'
2829

29-
3030
IMAGES_CONTEXT_KEY = '_templated_docs_imgs'
3131

3232

@@ -51,6 +51,7 @@ def fix_inline_tags(content):
5151
broken Django constructs. To remedy that, we find all the Django tags and
5252
variables and fix entities inside them.
5353
"""
54+
5455
def repl(match):
5556
text = match.group(0)
5657
text = text.replace('<text:s/>', ' ')
@@ -112,41 +113,50 @@ def fill_template(template_name, context, output_format='odt'):
112113

113114
source_file = find_template_file(template_name)
114115
source_extension = os.path.splitext(source_file)[1]
115-
source = zipfile.ZipFile(source_file, 'r')
116-
117116
dest_file = NamedTemporaryFile(delete=False, suffix=source_extension)
118-
dest = zipfile.ZipFile(dest_file, 'w')
119-
120-
manifest_data = ''
121-
for name in source.namelist():
122-
data = source.read(name)
123-
if name.endswith('.xml'):
124-
data = smart_str(data)
125-
126-
if any(name.endswith(file) for file in ('content.xml', 'styles.xml')):
127-
template = Template(fix_inline_tags(data))
128-
data = template.render(context)
129-
elif name == 'META-INF/manifest.xml':
130-
manifest_data = data[:-20] # Cut off the closing </manifest> tag
131-
continue # We will append it at the very end
132-
dest.writestr(name, smart_bytes(data))
133-
134-
for _, image in context.dicts[0].get(IMAGES_CONTEXT_KEY, {}).items():
135-
filename = os.path.basename(image.name)
136-
ext = os.path.splitext(filename)[1][1:]
137-
manifest_data += ('<manifest:file-entry '
138-
'manifest:media-type="image/%(ext)s" '
139-
'manifest:full-path="Pictures/%(filename)s"/>\n'
140-
) % locals()
141-
image.open()
142-
dest.writestr('Pictures/%s' % filename, image.read())
143-
image.close()
144-
145-
manifest_data += '</manifest:manifest>'
146-
dest.writestr('META-INF/manifest.xml', manifest_data)
147-
148-
source.close()
149-
dest.close()
117+
118+
if zipfile.is_zipfile(source_file):
119+
with zipfile.ZipFile(source_file, 'r') as source:
120+
with zipfile.ZipFile(dest_file, 'w') as dest:
121+
manifest_data = ''
122+
for name in source.namelist():
123+
data = source.read(name)
124+
if name.endswith('.xml'):
125+
data = smart_str(data)
126+
127+
if any(name.endswith(file) for file in (
128+
'content.xml', 'styles.xml'
129+
)):
130+
template = Template(fix_inline_tags(data))
131+
data = template.render(context)
132+
elif name == 'META-INF/manifest.xml':
133+
# Cut off the closing </manifest> tag
134+
manifest_data = data[:-20]
135+
continue # We will append it at the very end
136+
dest.writestr(name, smart_bytes(data))
137+
138+
for _, image in context.dicts[0].get(
139+
IMAGES_CONTEXT_KEY, {}
140+
).items():
141+
filename = os.path.basename(image.name)
142+
ext = os.path.splitext(filename)[1][1:]
143+
manifest_data += ('<manifest:file-entry '
144+
'manifest:media-type="image/%(ext)s" '
145+
'manifest:full-path="Pictures/'
146+
'%(filename)s"/>\n'
147+
) % locals()
148+
image.open()
149+
dest.writestr('Pictures/%s' % filename, image.read())
150+
image.close()
151+
152+
manifest_data += '</manifest:manifest>'
153+
dest.writestr('META-INF/manifest.xml', manifest_data)
154+
155+
else:
156+
with codecs.open(source_file, 'rb', 'utf-8') as source:
157+
template = Template(source.read())
158+
dest_file.write(smart_bytes(template.render(context)))
159+
dest_file.close()
150160

151161
if source_extension[1:] != output_format:
152162
results = Queue()

0 commit comments

Comments
 (0)