Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graceful handling of Kobo on-device DB unicode issue #2629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/calibre/devices/kobo/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ def get_bookshelvesforbook(connection, ContentID):
return bookshelves

self.debug_index = 0
device_db_unicode_errors = []

with closing(self.device_database_connection(use_row_factory=True)) as connection:
debug_print("KoboTouch:books - reading device database")
Expand Down Expand Up @@ -2088,7 +2089,19 @@ def get_bookshelvesforbook(connection, ContentID):

changed = False
i = 0
for row in cursor:
while True:
try:
row = cursor.fetchone()
except UnicodeDecodeError as e:
device_db_unicode_errors.append(
{
"title": row["Title"],
"attribution": row["Attribution"],
"error": e.__repr__(),
}
)
if not row:
break
i += 1
# self.report_progress((i) / float(books_on_device), _('Getting list of books on device...'))
show_debug = self.is_debugging_title(row['Title'])
Expand Down Expand Up @@ -2127,7 +2140,6 @@ def get_bookshelvesforbook(connection, ContentID):
need_sync = True

cursor.close()

if not prefs['manage_device_metadata'] == 'on_connect':
self.dump_bookshelves(connection)
else:
Expand Down Expand Up @@ -2160,6 +2172,29 @@ def get_bookshelvesforbook(connection, ContentID):

self.report_progress(1.0, _('Getting list of books on device...'))
debug_print("KoboTouch:books - end - oncard='%s'"%oncard)

if device_db_unicode_errors:
from calibre.devices.interface import OpenPopupMessage
issue_message = (
'The problem is in title "{title}" by "{attribution}".\n'
'Exception:\n{error}'
)
details = "\n\n".join(
issue_message.format(**item)
for item in device_db_unicode_errors
)
message = (
'The database on your Kobo device has an inconsistency'
' in the unicode character encoding.\n\n'
f'{details}\n\n'
'Maybe removing and re-adding the title will help.'
)
print(f"ERROR:\n{message}")
self._popup_warning = OpenPopupMessage(
_("Unicode error in devicd database - See details"),
message,
level="warning")

return bl

@classmethod
Expand Down