From cc30b3f408e0ef207442b9093f25120f806fce4c Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Fri, 5 Jul 2019 16:31:59 -0400 Subject: [PATCH] py3: port oeb rasterize string types The problem is I'm not sure what manifest's item.data is supposed to be. The docs for calibre.ebooks.oeb.base.Manifest.Item.data say "it should be a str type", but that doesn't seem to make sense for a binary file (at least for python3). More likely it should be a string_or_bytes, and consumers should check whether the item data is an instance of bytes (which it should be for image files that aren't representable as decoded bytes). --- src/calibre/ebooks/oeb/base.py | 4 ++-- src/calibre/ebooks/oeb/transforms/rasterize.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index c536006912..9ec154dec6 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -1024,8 +1024,8 @@ class Manifest(object): - XML content is parsed and returned as an lxml.etree element. - CSS and CSS-variant content is parsed and returned as a css_parser CSS DOM stylesheet. - - All other content is returned as a :class:`str` object with no - special parsing. + - All other content is returned as a :class:`str` or :class:`bytes` + object with no special parsing. """ data = self._data if data is None: diff --git a/src/calibre/ebooks/oeb/transforms/rasterize.py b/src/calibre/ebooks/oeb/transforms/rasterize.py index 35a96df60f..83651b23e5 100644 --- a/src/calibre/ebooks/oeb/transforms/rasterize.py +++ b/src/calibre/ebooks/oeb/transforms/rasterize.py @@ -1,7 +1,7 @@ ''' SVG rasterization transform. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -77,7 +77,7 @@ class SVGRasterizer(object): logger.info('Found SVG image height in %, trying to convert...') try: h = float(image.get('height').replace('%', ''))/100. - image.set('height', str(h*sizes[1])) + image.set('height', unicode_type(h*sizes[1])) except: logger.exception('Failed to convert percentage height:', image.get('height')) @@ -101,7 +101,7 @@ class SVGRasterizer(object): buffer = QBuffer(array) buffer.open(QIODevice.WriteOnly) image.save(buffer, format) - return str(array) + return array.data() def dataize_manifest(self): for item in self.oeb.manifest.values(): @@ -121,10 +121,10 @@ class SVGRasterizer(object): if abshref not in hrefs: continue linkee = hrefs[abshref] - data = str(linkee) + data = unicode_type(linkee) ext = what(None, data) or 'jpg' with PersistentTemporaryFile(suffix='.'+ext) as pt: - pt.write(data) + pt.write(data.encode('utf-8')) self.temp_files.append(pt.name) elem.attrib[XLINK('href')] = pt.name return svg @@ -182,7 +182,7 @@ class SVGRasterizer(object): height = style['height'] width = (width / 72) * self.profile.dpi height = (height / 72) * self.profile.dpi - data = QByteArray(str(svgitem)) + data = QByteArray(unicode_type(svgitem).encode('utf-8')) svg = QSvgRenderer(data) size = svg.defaultSize() size.scale(width, height, Qt.KeepAspectRatio) @@ -202,7 +202,7 @@ class SVGRasterizer(object): buffer = QBuffer(array) buffer.open(QIODevice.WriteOnly) image.save(buffer, 'PNG') - data = str(array) + data = array.data() manifest = self.oeb.manifest href = os.path.splitext(svgitem.href)[0] + '.png' id, href = manifest.generate(svgitem.id, href)