py3: Fix error in Kobo driver when sorting collections

Fixes #1098 (py3: Ignore TypeError when sorting device collections for kobo driver)
This commit is contained in:
Kovid Goyal 2020-02-10 07:19:06 +05:30
parent c3e3bbc408
commit e10dc504cc
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 47 deletions

View File

@ -7,16 +7,14 @@ import os, time, sys
from functools import cmp_to_key from functools import cmp_to_key
from calibre.constants import preferred_encoding, DEBUG, ispy3 from calibre.constants import preferred_encoding, DEBUG, ispy3
from calibre import isbytestring, force_unicode from calibre import isbytestring
from calibre.utils.icu import sort_key
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.devices.usbms.books import Book as Book_ from calibre.devices.usbms.books import Book as Book_, CollectionsBookList, none_cmp
from calibre.devices.usbms.books import CollectionsBookList
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs
from calibre.devices.usbms.driver import debug_print from calibre.devices.usbms.driver import debug_print
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from polyglot.builtins import unicode_type, string_or_bytes, iteritems, itervalues, cmp from polyglot.builtins import unicode_type, iteritems, itervalues
class Book(Book_): class Book(Book_):
@ -292,24 +290,6 @@ class KTCollectionsBookList(CollectionsBookList):
# Sort collections # Sort collections
result = {} result = {}
def none_cmp(xx, yy):
x = xx[1]
y = yy[1]
if x is None and y is None:
# No sort_key needed here, because defaults are ascii
return cmp(xx[2], yy[2])
if x is None:
return 1
if y is None:
return -1
if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes):
x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y))
c = cmp(x, y)
if c != 0:
return c
# same as above -- no sort_key needed here
return cmp(xx[2], yy[2])
for category, lpaths in iteritems(collections): for category, lpaths in iteritems(collections):
books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp))
result[category] = [x[0] for x in books] result[category] = [x[0] for x in books]

View File

@ -20,6 +20,31 @@ from calibre.utils.icu import sort_key
from polyglot.builtins import string_or_bytes, iteritems, itervalues, cmp from polyglot.builtins import string_or_bytes, iteritems, itervalues, cmp
def none_cmp(xx, yy):
x = xx[1]
y = yy[1]
if x is None and y is None:
# No sort_key needed here, because defaults are ascii
return cmp(xx[2], yy[2])
if x is None:
return 1
if y is None:
return -1
if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes):
x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y))
try:
c = cmp(x, y)
except TypeError:
c = 0
if c != 0:
return c
# same as above -- no sort_key needed here
try:
return cmp(xx[2], yy[2])
except TypeError:
return 0
class Book(Metadata): class Book(Metadata):
def __init__(self, prefix, lpath, size=None, other=None): def __init__(self, prefix, lpath, size=None, other=None):
@ -280,30 +305,6 @@ class CollectionsBookList(BookList):
# Sort collections # Sort collections
result = {} result = {}
def none_cmp(xx, yy):
x = xx[1]
y = yy[1]
if x is None and y is None:
# No sort_key needed here, because defaults are ascii
return cmp(xx[2], yy[2])
if x is None:
return 1
if y is None:
return -1
if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes):
x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y))
try:
c = cmp(x, y)
except TypeError:
c = 0
if c != 0:
return c
# same as above -- no sort_key needed here
try:
return cmp(xx[2], yy[2])
except TypeError:
return 0
for category, lpaths in iteritems(collections): for category, lpaths in iteritems(collections):
books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp))
result[category] = [x[0] for x in books] result[category] = [x[0] for x in books]