1
1
# -*- coding: utf-8 -*-
2
-
2
+ import codecs
3
3
from multiprocessing import Process , Queue
4
4
import os .path
5
5
import re
22
22
from pylokit import Office
23
23
24
24
import logging
25
+
25
26
log = logging .getLogger (__name__ )
26
27
27
28
__version__ = '0.3.1'
28
29
29
-
30
30
IMAGES_CONTEXT_KEY = '_templated_docs_imgs'
31
31
32
32
@@ -51,6 +51,7 @@ def fix_inline_tags(content):
51
51
broken Django constructs. To remedy that, we find all the Django tags and
52
52
variables and fix entities inside them.
53
53
"""
54
+
54
55
def repl (match ):
55
56
text = match .group (0 )
56
57
text = text .replace ('<text:s/>' , ' ' )
@@ -112,41 +113,50 @@ def fill_template(template_name, context, output_format='odt'):
112
113
113
114
source_file = find_template_file (template_name )
114
115
source_extension = os .path .splitext (source_file )[1 ]
115
- source = zipfile .ZipFile (source_file , 'r' )
116
-
117
116
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 ()
150
160
151
161
if source_extension [1 :] != output_format :
152
162
results = Queue ()
0 commit comments