py3: misc fixes

This commit is contained in:
Kovid Goyal 2019-05-11 08:12:25 +05:30
parent 9bc4d1d375
commit 2cb8f6b076
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 8 additions and 7 deletions

View File

@ -17,7 +17,7 @@ from calibre.ebooks.mobi.reader.headers import MetadataHeader
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from calibre import prints, fsync from calibre import prints, fsync
from calibre.constants import DEBUG from calibre.constants import DEBUG
from polyglot.builtins import range from polyglot.builtins import range, as_unicode, as_bytes
class APNXBuilder(object): class APNXBuilder(object):
@ -37,10 +37,10 @@ class APNXBuilder(object):
with lopen(mobi_file_path, 'rb') as mf: with lopen(mobi_file_path, 'rb') as mf:
ident = PdbHeaderReader(mf).identity() ident = PdbHeaderReader(mf).identity()
if ident != 'BOOKMOBI': if ident != b'BOOKMOBI':
# Check that this is really a MOBI file. # Check that this is really a MOBI file.
raise Exception(_('Not a valid MOBI file. Reports identity of %s') % ident) raise Exception(_('Not a valid MOBI file. Reports identity of %s') % ident)
apnx_meta['acr'] = str(PdbHeaderReader(mf).name()) apnx_meta['acr'] = as_unicode(PdbHeaderReader(mf).name(), errors='replace')
# We'll need the PDB name, the MOBI version, and some metadata to make FW 3.4 happy with KF8 files... # We'll need the PDB name, the MOBI version, and some metadata to make FW 3.4 happy with KF8 files...
with lopen(mobi_file_path, 'rb') as mf: with lopen(mobi_file_path, 'rb') as mf:
@ -92,7 +92,7 @@ class APNXBuilder(object):
fsync(apnxf) fsync(apnxf)
def generate_apnx(self, pages, apnx_meta): def generate_apnx(self, pages, apnx_meta):
apnx = '' apnx = b''
if DEBUG: if DEBUG:
prints('APNX META: guid:', apnx_meta['guid']) prints('APNX META: guid:', apnx_meta['guid'])
@ -117,12 +117,12 @@ class APNXBuilder(object):
apnx += struct.pack('>I', 65537) apnx += struct.pack('>I', 65537)
apnx += struct.pack('>I', 12 + len(content_header)) apnx += struct.pack('>I', 12 + len(content_header))
apnx += struct.pack('>I', len(content_header)) apnx += struct.pack('>I', len(content_header))
apnx += content_header apnx += as_bytes(content_header)
apnx += struct.pack('>H', 1) apnx += struct.pack('>H', 1)
apnx += struct.pack('>H', len(page_header)) apnx += struct.pack('>H', len(page_header))
apnx += struct.pack('>H', len(pages)) apnx += struct.pack('>H', len(pages))
apnx += struct.pack('>H', 32) apnx += struct.pack('>H', 32)
apnx += page_header apnx += as_bytes(page_header)
# Write page values to APNX. # Write page values to APNX.
for page in pages: for page in pages:

View File

@ -354,6 +354,7 @@ class USBMS(CLI, Device):
debug_print('USBMS: adding metadata for %d books'%(len(metadata))) debug_print('USBMS: adding metadata for %d books'%(len(metadata)))
metadata = iter(metadata) metadata = iter(metadata)
locations = tuple(locations)
for i, location in enumerate(locations): for i, location in enumerate(locations):
self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...')) self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...'))
info = next(metadata) info = next(metadata)

View File

@ -32,7 +32,7 @@ class PdbHeaderReader(object):
def name(self): def name(self):
self.stream.seek(0) self.stream.seek(0)
return re.sub('[^-A-Za-z0-9 ]+', '_', self.stream.read(32).replace('\x00', '')) return re.sub(b'[^-A-Za-z0-9 ]+', b'_', self.stream.read(32).replace(b'\x00', b''))
def full_section_info(self, number): def full_section_info(self, number):
if number not in range(0, self.num_sections): if number not in range(0, self.num_sections):