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 collections import deque
from math import ceil, floor from math import ceil, floor
from functools import partial from functools import partial
from polyglot.builtins import string_or_bytes, itervalues
from itertools import chain
try: try:
from PIL import Image as PILImage from PIL import Image as PILImage
@ -258,6 +260,8 @@ class HTMLConverter(object):
src = open(self._override_css, 'rb').read() src = open(self._override_css, 'rb').read()
else: else:
src = self._override_css src = self._override_css
if isinstance(src, bytes):
src = src.decode('utf-8', 'replace')
match = self.PAGE_BREAK_PAT.search(src) match = self.PAGE_BREAK_PAT.search(src)
if match and not re.match('avoid', match.group(1), re.IGNORECASE): if match and not re.match('avoid', match.group(1), re.IGNORECASE):
self.page_break_found = True self.page_break_found = True
@ -1112,6 +1116,8 @@ class HTMLConverter(object):
ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth']))/2.) ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth']))/2.)
for prop in ('topskip', 'footskip', 'sidemargin'): for prop in ('topskip', 'footskip', 'sidemargin'):
if isinstance(ans[prop], string_or_bytes):
ans[prop] = int(ans[prop])
if ans[prop] < 0: if ans[prop] < 0:
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')): 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] path = munge_paths(self.target_prefix, tag['href'])[0]
try: try:
f = open(path, 'rb') with open(path, 'rb') as f:
src = f.read() src = f.read().decode('utf-8', 'replace')
f.close()
match = self.PAGE_BREAK_PAT.search(src) match = self.PAGE_BREAK_PAT.search(src)
if match and not re.match('avoid', match.group(1), re.IGNORECASE): if match and not re.match('avoid', match.group(1), re.IGNORECASE):
self.page_break_found = True self.page_break_found = True
@ -1792,7 +1797,7 @@ class HTMLConverter(object):
self.book.renderLrs(path) if lrs else self.book.renderLrf(path) self.book.renderLrs(path) if lrs else self.book.renderLrf(path)
def cleanup(self): 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__() _file.__del__()

View File

@ -108,11 +108,11 @@ def writeQWord(f, qword):
def writeZeros(f, nZeros): def writeZeros(f, nZeros):
f.write("\x00" * nZeros) f.write(b"\0" * nZeros)
def writeString(f, str): def writeString(f, s):
f.write(str) f.write(s)
def writeIdList(f, idList): def writeIdList(f, idList):
@ -177,7 +177,7 @@ def writeRuledLine(f, lineInfo):
writeColor(f, lineColor) 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 = 48
XOR_KEY = 65024 # that's what lrf2lrs says -- not used, anyway... 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 __appname__, __version__
from calibre import entity_to_unicode 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): class LrsError(Exception):
@ -421,7 +421,7 @@ class Book(Delegator):
LrsObject.nextObjId += 1 LrsObject.nextObjId += 1
styledefault = StyleDefault() styledefault = StyleDefault()
if settings.has_key('setdefault'): # noqa if 'setdefault' in settings:
styledefault = settings.pop('setdefault') styledefault = settings.pop('setdefault')
Delegator.__init__(self, [BookInformation(), Main(), Delegator.__init__(self, [BookInformation(), Main(),
Template(), Style(styledefault), Solos(), Objects()]) Template(), Style(styledefault), Solos(), Objects()])
@ -569,12 +569,12 @@ class Book(Delegator):
text_blocks = list(main.get_all(lambda x: isinstance(x, TextBlock))) text_blocks = list(main.get_all(lambda x: isinstance(x, TextBlock)))
for tb in text_blocks: for tb in text_blocks:
if tb.textSettings.has_key('fontsize'): # noqa if 'fontsize' in tb.textSettings:
tb.textSettings['fontsize'] = rescale(tb.textSettings['fontsize']) tb.textSettings['fontsize'] = rescale(tb.textSettings['fontsize'])
for span in tb.get_all(lambda x: isinstance(x, Span)): 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']) 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']) span.attrs['baselineskip'] = rescale(span.attrs['baselineskip'])
text_styles = set(tb.textStyle for tb in text_blocks) text_styles = set(tb.textStyle for tb in text_blocks)
@ -1835,7 +1835,7 @@ class Span(LrsSimpleChar1, LrsContainer):
oldTextStyle = self.findCurrentTextStyle() oldTextStyle = self.findCurrentTextStyle()
# set the attributes we want changed # 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]: if name in oldTextStyle.attrs and oldTextStyle.attrs[name] == self.attrs[name]:
self.attrs.pop(name) self.attrs.pop(name)
else: else: