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

View File

@ -460,7 +460,10 @@ class MobiWriter(object):
Write the PalmDB header Write the PalmDB header
''' '''
title = ascii_filename(unicode_type(self.oeb.metadata.title[0])).replace( 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))) title = title + (b'\0' * (32 - len(title)))
now = int(time.time()) now = int(time.time())
nrecords = len(self.records) nrecords = len(self.records)

View File

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

View File

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