py3: misc fixes for conversion pipeline

This commit is contained in:
Kovid Goyal 2019-04-11 12:12:29 +05:30
parent c569f857bb
commit 94d5b27128
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 14 additions and 6 deletions

View File

@ -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:

View File

@ -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 <opf:metadata> 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): # {{{

View File

@ -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

View File

@ -9,6 +9,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
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)

View File

@ -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()