py3: Fix various call sites of ascii_filename() that assume the result is a bytestring

This commit is contained in:
Kovid Goyal 2019-04-10 15:05:46 +05:30
parent 09ae07631b
commit 46b9a7a325
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 19 additions and 16 deletions

View File

@ -1179,8 +1179,8 @@ class DB(object):
'''
book_id = ' (%d)' % book_id
l = self.PATH_LIMIT - (len(book_id) // 2) - 2
author = ascii_filename(author)[:l].decode('ascii', 'replace')
title = ascii_filename(title.lstrip())[:l].decode('ascii', 'replace').rstrip()
author = ascii_filename(author)[:l]
title = ascii_filename(title.lstrip())[:l].rstrip()
if not title:
title = 'Unknown'[:l]
try:
@ -1189,8 +1189,7 @@ class DB(object):
except IndexError:
author = ''
if not author:
author = ascii_filename(_('Unknown')).decode(
'ascii', 'replace')
author = ascii_filename(_('Unknown'))
if author.upper() in WINDOWS_RESERVED_NAMES:
author += 'w'
return '%s/%s%s' % (author, title, book_id)
@ -1207,15 +1206,15 @@ class DB(object):
l = (self.PATH_LIMIT - (extlen // 2) - 2) if iswindows else ((self.PATH_LIMIT - extlen - 2) // 2)
if l < 5:
raise ValueError('Extension length too long: %d' % extlen)
author = ascii_filename(author)[:l].decode('ascii', 'replace')
title = ascii_filename(title.lstrip())[:l].decode('ascii', 'replace').rstrip()
author = ascii_filename(author)[:l]
title = ascii_filename(title.lstrip())[:l].rstrip()
if not title:
title = 'Unknown'[:l]
name = title + ' - ' + author
while name.endswith('.'):
name = name[:-1]
if not name:
name = ascii_filename(_('Unknown')).decode('ascii', 'replace')
name = ascii_filename(_('Unknown'))
return name
# Database layer API {{{

View File

@ -460,7 +460,10 @@ class MobiWriter(object):
Write the PalmDB header
'''
title = ascii_filename(unicode_type(self.oeb.metadata.title[0])).replace(
' ', '_')[:31]
' ', '_')
if not isinstance(title, bytes):
title = title.encode('ascii')
title = title[:31]
title = title + (b'\0' * (32 - len(title)))
now = int(time.time())
nrecords = len(self.records)

View File

@ -332,8 +332,10 @@ class KF8Book(object):
# Write PalmDB Header
title = ascii_filename(self.full_title.decode('utf-8')).replace(
' ', '_')[:31]
title = ascii_filename(self.full_title.decode('utf-8')).replace(' ', '_')
if not isinstance(title, bytes):
title = title.encode('ascii')
title = title[:31]
title += (b'\0' * (32 - len(title)))
now = int(time.time())
nrecords = len(records)

View File

@ -611,14 +611,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if not authors:
authors = _('Unknown')
author = ascii_filename(authors.split(',')[0].replace('|', ',')
)[:self.PATH_LIMIT].decode('ascii', 'replace')
)[:self.PATH_LIMIT]
title = ascii_filename(self.title(id, index_is_id=True)
)[:self.PATH_LIMIT].decode('ascii', 'replace')
)[:self.PATH_LIMIT]
while author[-1] in (' ', '.'):
author = author[:-1]
if not author:
author = ascii_filename(_('Unknown')).decode(
'ascii', 'replace')
author = ascii_filename(_('Unknown'))
path = author + '/' + title + ' (%d)'%id
return path
@ -630,9 +629,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if not authors:
authors = _('Unknown')
author = ascii_filename(authors.split(',')[0].replace('|', ',')
)[:self.PATH_LIMIT].decode('ascii', 'replace')
)[:self.PATH_LIMIT]
title = ascii_filename(self.title(id, index_is_id=True)
)[:self.PATH_LIMIT].decode('ascii', 'replace')
)[:self.PATH_LIMIT]
name = title + ' - ' + author
while name.endswith('.'):
name = name[:-1]