From e10dc504cc9390391e4a1254f04e4223c51445c0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 10 Feb 2020 07:19:06 +0530 Subject: [PATCH] py3: Fix error in Kobo driver when sorting collections Fixes #1098 (py3: Ignore TypeError when sorting device collections for kobo driver) --- src/calibre/devices/kobo/books.py | 26 ++-------------- src/calibre/devices/usbms/books.py | 49 +++++++++++++++--------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/calibre/devices/kobo/books.py b/src/calibre/devices/kobo/books.py index f725751d00..89ff9c8411 100644 --- a/src/calibre/devices/kobo/books.py +++ b/src/calibre/devices/kobo/books.py @@ -7,16 +7,14 @@ import os, time, sys from functools import cmp_to_key from calibre.constants import preferred_encoding, DEBUG, ispy3 -from calibre import isbytestring, force_unicode -from calibre.utils.icu import sort_key +from calibre import isbytestring from calibre.ebooks.metadata.book.base import Metadata -from calibre.devices.usbms.books import Book as Book_ -from calibre.devices.usbms.books import CollectionsBookList +from calibre.devices.usbms.books import Book as Book_, CollectionsBookList, none_cmp from calibre.utils.config_base import prefs from calibre.devices.usbms.driver import debug_print 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_): @@ -292,24 +290,6 @@ class KTCollectionsBookList(CollectionsBookList): # Sort collections 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): books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) result[category] = [x[0] for x in books] diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index 4777d86cf4..bbb89e316c 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -20,6 +20,31 @@ from calibre.utils.icu import sort_key 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): def __init__(self, prefix, lpath, size=None, other=None): @@ -280,30 +305,6 @@ class CollectionsBookList(BookList): # Sort collections 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): books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) result[category] = [x[0] for x in books]