Narrative web: fix tar file output and HTML encoding output support.

svn: r21258
This commit is contained in:
Tim G L Lyons 2013-01-30 18:58:44 +00:00
parent ef9464006e
commit adb94b1974
2 changed files with 29 additions and 7 deletions

View File

@ -202,7 +202,7 @@ class Html(list):
if title is not None: if title is not None:
head += (Html('title', title, inline=True, indent=True)) head += (Html('title', title, inline=True, indent=True))
if html5: if html5:
head += Html('meta', charset="utf-8", indent=True) head += Html('meta', charset=encoding, indent=True)
else: else:
meta1 = 'http-equiv="content-type" content="text/html;charset=%s"' meta1 = 'http-equiv="content-type" content="text/html;charset=%s"'
meta2 = 'http-equiv="Content-Style-Type" content="text/css"' meta2 = 'http-equiv="Content-Style-Type" content="text/css"'

View File

@ -82,12 +82,13 @@ import time, datetime
import locale import locale
import shutil import shutil
import io import io
import codecs
import tarfile import tarfile
import tempfile import tempfile
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
from cStringIO import StringIO from cStringIO import StringIO
else: else:
from io import StringIO from io import StringIO, BytesIO, TextIOWrapper, BufferedWriter
from textwrap import TextWrapper from textwrap import TextWrapper
from unicodedata import normalize from unicodedata import normalize
from collections import defaultdict from collections import defaultdict
@ -7828,9 +7829,14 @@ class NavWebReport(Report):
else: else:
self.cur_fname = fname + ext self.cur_fname = fname + ext
if self.archive: if self.archive:
string_io = StringIO() if sys.version_info[0] < 3:
of = io.open(fname, "w", encoding = self.encoding, string_io = StringIO()
errors = 'xmlcharrefreplace') of = codecs.EncodedFile(string_io, 'utf-8', self.encoding,
'xmlcharrefreplace')
else:
string_io = BytesIO()
of = TextIOWrapper(string_io, encoding=self.encoding,
errors='xmlcharrefreplace')
else: else:
string_io = None string_io = None
if subdir: if subdir:
@ -7838,8 +7844,22 @@ class NavWebReport(Report):
if not os.path.isdir(subdir): if not os.path.isdir(subdir):
os.makedirs(subdir) os.makedirs(subdir)
fname = os.path.join(self.html_dir, self.cur_fname) fname = os.path.join(self.html_dir, self.cur_fname)
of = io.open(fname, "w", encoding = self.encoding, if sys.version_info[0] < 3:
errors = 'xmlcharrefreplace') # In python 2.x, the data written by of.write() is genarally of
# type 'str' (i.e. 8-bit strings), except for cases where (at
# least) one of the objects being converted by a '%' operator is
# unicode (e.g. the "Generated by" line or the _META3 line), in
# which case the data being written is of type 'unicode' (See
# http://docs.python.org/2/library/stdtypes.html#string-
# formatting). The data written to the file is encoded according
# to self.encoding
of = codecs.EncodedFile(open(fname, 'w'), 'utf-8',
self.encoding, 'xmlcharrefreplace')
else:
# In python 3, the data that is written by of.write() is always
# of type 'str' (i.e. unicode text).
of = open(fname, 'w', encoding=self.encoding,
errors='xmlcharrefreplace')
return (of, string_io) return (of, string_io)
def close_file(self, of, string_io): def close_file(self, of, string_io):
@ -7848,6 +7868,8 @@ class NavWebReport(Report):
""" """
if self.archive: if self.archive:
if sys.version_info[0] >= 3:
of.flush()
tarinfo = tarfile.TarInfo(self.cur_fname) tarinfo = tarfile.TarInfo(self.cur_fname)
tarinfo.size = len(string_io.getvalue()) tarinfo.size = len(string_io.getvalue())
tarinfo.mtime = time.time() tarinfo.mtime = time.time()