From 94d5b27128cc4aa76f42c8558d9ee9c60313c66e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 11 Apr 2019 12:12:29 +0530 Subject: [PATCH] py3: misc fixes for conversion pipeline --- src/calibre/ebooks/conversion/plugins/html_output.py | 2 +- src/calibre/ebooks/conversion/plugins/oeb_output.py | 4 ++-- src/calibre/ebooks/oeb/base.py | 9 ++++++++- src/calibre/ebooks/oeb/stylizer.py | 3 ++- src/calibre/ebooks/oeb/writer.py | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/html_output.py b/src/calibre/ebooks/conversion/plugins/html_output.py index 15d2f02c07..3caa19ef2f 100644 --- a/src/calibre/ebooks/conversion/plugins/html_output.py +++ b/src/calibre/ebooks/conversion/plugins/html_output.py @@ -151,7 +151,7 @@ class HTMLOutput(OutputFormatPlugin): pass else: with open(path, 'wb') as f: - f.write(str(item)) + f.write(item.bytes_representation) item.unload_data_from_memory(memory=path) for item in oeb_book.spine: diff --git a/src/calibre/ebooks/conversion/plugins/oeb_output.py b/src/calibre/ebooks/conversion/plugins/oeb_output.py index 82ae031c4d..c5146e0001 100644 --- a/src/calibre/ebooks/conversion/plugins/oeb_output.py +++ b/src/calibre/ebooks/conversion/plugins/oeb_output.py @@ -51,7 +51,7 @@ class OEBOutput(OutputFormatPlugin): if key == OPF_MIME: # Needed as I can't get lxml to output opf:role and # not output as well - raw = re.sub(r'(<[/]{0,1})opf:', r'\1', raw) + raw = re.sub(br'(<[/]{0,1})opf:', br'\1', raw) with open(href, 'wb') as f: f.write(raw) @@ -65,7 +65,7 @@ class OEBOutput(OutputFormatPlugin): if not os.path.exists(dir): os.makedirs(dir) with open(path, 'wb') as f: - f.write(str(item)) + f.write(item.bytes_representation) item.unload_data_from_memory(memory=path) def workaround_nook_cover_bug(self, root): # {{{ diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index cf2deb47f2..1019ff582d 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -1095,6 +1095,10 @@ class Manifest(object): return unicode_type(data.cssText, 'utf-8', 'replace') return unicode_type(data) + @property + def bytes_representation(self): + return serialize(self.data, self.media_type, pretty_print=self.oeb.pretty_print) + if ispy3: def __str__(self): return self.unicode_representation @@ -1103,7 +1107,7 @@ class Manifest(object): return self.unicode_representation def __str__(self): - return serialize(self.data, self.media_type, pretty_print=self.oeb.pretty_print) + return self.bytes_representation def __eq__(self, other): return self is other @@ -1111,6 +1115,9 @@ class Manifest(object): def __ne__(self, other): return self is not other + def __hash__(self): + return id(self) + @property def sort_key(self): href = self.href diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 595779ebd8..444156d821 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -9,6 +9,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' import os, re, logging, copy, unicodedata, numbers +from operator import itemgetter from weakref import WeakKeyDictionary from xml.dom import SyntaxErr as CSSSyntaxError from css_parser.css import (CSSStyleRule, CSSPageRule, CSSFontFaceRule, @@ -215,7 +216,7 @@ class Stylizer(object): else: rules.extend(self.flatten_rule(rule, href, index, is_user_agent_sheet=sheet_index==0)) index = index + 1 - rules.sort() + rules.sort(key=itemgetter(0)) # sort by specificity self.rules = rules self._styles = {} pseudo_pat = re.compile(u':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I) diff --git a/src/calibre/ebooks/oeb/writer.py b/src/calibre/ebooks/oeb/writer.py index 450a8f141d..5f82a05011 100644 --- a/src/calibre/ebooks/oeb/writer.py +++ b/src/calibre/ebooks/oeb/writer.py @@ -62,7 +62,7 @@ class OEBWriter(object): os.mkdir(path) output = DirContainer(path, oeb.log) for item in oeb.manifest.values(): - output.write(item.href, str(item)) + output.write(item.href, item.bytes_representation) if version == 1: metadata = oeb.to_opf1()