py3: Port LRF Output

This commit is contained in:
Kovid Goyal 2019-04-30 16:11:56 +05:30
parent f1b0cb58ac
commit 30232ebd52
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 19 additions and 14 deletions

View File

@ -11,6 +11,8 @@ import os, re, sys, copy, glob, tempfile
from collections import deque
from math import ceil, floor
from functools import partial
from polyglot.builtins import string_or_bytes, itervalues
from itertools import chain
try:
from PIL import Image as PILImage
@ -258,6 +260,8 @@ class HTMLConverter(object):
src = open(self._override_css, 'rb').read()
else:
src = self._override_css
if isinstance(src, bytes):
src = src.decode('utf-8', 'replace')
match = self.PAGE_BREAK_PAT.search(src)
if match and not re.match('avoid', match.group(1), re.IGNORECASE):
self.page_break_found = True
@ -1112,6 +1116,8 @@ class HTMLConverter(object):
ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth']))/2.)
for prop in ('topskip', 'footskip', 'sidemargin'):
if isinstance(ans[prop], string_or_bytes):
ans[prop] = int(ans[prop])
if ans[prop] < 0:
ans[prop] = 0
@ -1520,9 +1526,8 @@ class HTMLConverter(object):
elif (tag.has_attr('type') and tag['type'] in ("text/css", "text/x-oeb1-css") and tag.has_attr('href')):
path = munge_paths(self.target_prefix, tag['href'])[0]
try:
f = open(path, 'rb')
src = f.read()
f.close()
with open(path, 'rb') as f:
src = f.read().decode('utf-8', 'replace')
match = self.PAGE_BREAK_PAT.search(src)
if match and not re.match('avoid', match.group(1), re.IGNORECASE):
self.page_break_found = True
@ -1792,7 +1797,7 @@ class HTMLConverter(object):
self.book.renderLrs(path) if lrs else self.book.renderLrf(path)
def cleanup(self):
for _file in self.scaled_images.values() + self.rotated_images.values():
for _file in chain(itervalues(self.scaled_images), itervalues(self.rotated_images)):
_file.__del__()

View File

@ -108,11 +108,11 @@ def writeQWord(f, qword):
def writeZeros(f, nZeros):
f.write("\x00" * nZeros)
f.write(b"\0" * nZeros)
def writeString(f, str):
f.write(str)
def writeString(f, s):
f.write(s)
def writeIdList(f, idList):
@ -177,7 +177,7 @@ def writeRuledLine(f, lineInfo):
writeColor(f, lineColor)
LRF_SIGNATURE = "L\x00R\x00F\x00\x00\x00"
LRF_SIGNATURE = b"L\x00R\x00F\x00\x00\x00"
# XOR_KEY = 48
XOR_KEY = 65024 # that's what lrf2lrs says -- not used, anyway...

View File

@ -53,7 +53,7 @@ DEFAULT_GENREADING = "fs" # default is yes to both lrf and lrs
from calibre import __appname__, __version__
from calibre import entity_to_unicode
from polyglot.builtins import string_or_bytes, unicode_type
from polyglot.builtins import string_or_bytes, unicode_type, iteritems
class LrsError(Exception):
@ -421,7 +421,7 @@ class Book(Delegator):
LrsObject.nextObjId += 1
styledefault = StyleDefault()
if settings.has_key('setdefault'): # noqa
if 'setdefault' in settings:
styledefault = settings.pop('setdefault')
Delegator.__init__(self, [BookInformation(), Main(),
Template(), Style(styledefault), Solos(), Objects()])
@ -569,12 +569,12 @@ class Book(Delegator):
text_blocks = list(main.get_all(lambda x: isinstance(x, TextBlock)))
for tb in text_blocks:
if tb.textSettings.has_key('fontsize'): # noqa
if 'fontsize' in tb.textSettings:
tb.textSettings['fontsize'] = rescale(tb.textSettings['fontsize'])
for span in tb.get_all(lambda x: isinstance(x, Span)):
if span.attrs.has_key('fontsize'): # noqa
if 'fontsize' in span.attrs:
span.attrs['fontsize'] = rescale(span.attrs['fontsize'])
if span.attrs.has_key('baselineskip'): # noqa
if 'baselineskip' in span.attrs:
span.attrs['baselineskip'] = rescale(span.attrs['baselineskip'])
text_styles = set(tb.textStyle for tb in text_blocks)
@ -1835,7 +1835,7 @@ class Span(LrsSimpleChar1, LrsContainer):
oldTextStyle = self.findCurrentTextStyle()
# set the attributes we want changed
for (name, value) in self.attrs.items():
for (name, value) in tuple(iteritems(self.attrs)):
if name in oldTextStyle.attrs and oldTextStyle.attrs[name] == self.attrs[name]:
self.attrs.pop(name)
else: