Refactoring of pack an unpack template and result.'

This commit is contained in:
Christopher Ramírez 2013-07-21 12:45:11 -06:00
parent 68882fbcf6
commit 06769a3a39

View file

@ -36,7 +36,7 @@ import sys
import logging import logging
import zipfile import zipfile
import StringIO import StringIO
import xml.dom.minidom from xml.dom.minidom import parseString
from os.path import isfile from os.path import isfile
from jinja2 import Environment, Undefined from jinja2 import Environment, Undefined
@ -134,19 +134,32 @@ class Render():
# go through the files in source # go through the files in source
for zi in self._unpacked_template.filelist: for zi in self._unpacked_template.filelist:
archive_file = self._unpacked_template.read( zi.filename ) file_contents = self._unpacked_template.read( zi.filename )
if zi.filename == 'content.xml': if zi.filename == 'content.xml':
self._content_file = archive_file self.content = parseString( file_contents )
elif zi.filename == 'styles.xml': elif zi.filename == 'styles.xml':
self._style_file = archive_file self.styles = parseString( file_contents )
elif zi.filename == 'mimetype': elif zi.filename == 'mimetype':
self._mimetype = archive_file self._mimetype = file_contents
# Load content.xml and style.xml file
body = self.content.getElementsByTagName('office:body')
self.content_body = body and body[0]
body = self.styles.getElementsByTagName('office:master-styles')
self.headers = body and body[0]
def pack_document(self): def pack_document(self):
""" """
Make an archive from _unpacked_template Make an archive from _unpacked_template
""" """
# Save rendered content and headers
self._content_file = self.content.toxml().encode('ascii', 'xmlcharrefreplace')
self._style_file = (self.styles.toxml().encode('ascii', 'xmlcharrefreplace'))
self.rendered = StringIO.StringIO() self.rendered = StringIO.StringIO()
self._packed_template = zipfile.ZipFile(self.rendered, 'a') self._packed_template = zipfile.ZipFile(self.rendered, 'a')
@ -180,21 +193,12 @@ class Render():
self.unpack_template() self.unpack_template()
self._template_vars = kwargs self._template_vars = kwargs
# Load content.xml and style.xml file
self.content = xml.dom.minidom.parseString(self._content_file)
body = self.content.getElementsByTagName('office:body')
self.content_body = body and body[0]
self.styles = xml.dom.minidom.parseString(self._style_file)
body = self.styles.getElementsByTagName('office:master-styles')
self.headers = body and body[0]
# Render content.xml # Render content.xml
self.prepare_template_tags(self.content_body) self.prepare_template_tags(self.content_body)
template = self._environment.from_string(self.content.toxml()) template = self._environment.from_string(self.content.toxml())
result = template.render(**kwargs) result = template.render(**kwargs)
result = result.replace('\n', '<text:line-break/>') result = result.replace('\n', '<text:line-break/>')
self.content = xml.dom.minidom.parseString(result.encode('ascii', 'xmlcharrefreplace')) self.content = parseString(result.encode('ascii', 'xmlcharrefreplace'))
self.content_body = self.content.getElementsByTagName('office:body') self.content_body = self.content.getElementsByTagName('office:body')
# Render style.xml # Render style.xml
@ -202,14 +206,9 @@ class Render():
template = self._environment.from_string(self.styles.toxml()) template = self._environment.from_string(self.styles.toxml())
result = template.render(**kwargs) result = template.render(**kwargs)
result = result.replace('\n', '<text:line-break/>') result = result.replace('\n', '<text:line-break/>')
self.styles = xml.dom.minidom.parseString(result.encode('ascii', 'xmlcharrefreplace')) self.styles = parseString(result.encode('ascii', 'xmlcharrefreplace'))
self.headers = None self.headers = None
# Save rendered content and headers
self._content_file = self.content.toxml().encode('ascii', 'xmlcharrefreplace')
self._style_file = (self.styles.toxml().encode('ascii', 'xmlcharrefreplace'))
self.pack_document() self.pack_document()
return self.rendered return self.rendered