mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix error if merged annotations contain non-string timestamps
This commit is contained in:
parent
200e9d0602
commit
e176f58ce8
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
from operator import itemgetter
|
|
||||||
|
|
||||||
from calibre.ebooks.epub.cfi.parse import cfi_sort_key
|
from calibre.ebooks.epub.cfi.parse import cfi_sort_key
|
||||||
from polyglot.builtins import itervalues
|
from polyglot.builtins import itervalues
|
||||||
@ -34,7 +33,7 @@ def merge_annots_with_identical_field(a, b, field='title'):
|
|||||||
for x in chain(a, b):
|
for x in chain(a, b):
|
||||||
title_groups[x[field]].append(x)
|
title_groups[x[field]].append(x)
|
||||||
for tg in itervalues(title_groups):
|
for tg in itervalues(title_groups):
|
||||||
tg.sort(key=itemgetter('timestamp'), reverse=True)
|
tg.sort(key=safe_timestamp_sort_key, reverse=True)
|
||||||
seen = set()
|
seen = set()
|
||||||
changed = False
|
changed = False
|
||||||
ans = []
|
ans = []
|
||||||
@ -61,7 +60,7 @@ def merge_annot_lists(a, b, annot_type):
|
|||||||
return list(a)
|
return list(a)
|
||||||
if annot_type == 'last-read':
|
if annot_type == 'last-read':
|
||||||
ans = a + b
|
ans = a + b
|
||||||
ans.sort(key=itemgetter('timestamp'), reverse=True)
|
ans.sort(key=safe_timestamp_sort_key, reverse=True)
|
||||||
return ans
|
return ans
|
||||||
merge_field = merge_field_map.get(annot_type)
|
merge_field = merge_field_map.get(annot_type)
|
||||||
if merge_field is None:
|
if merge_field is None:
|
||||||
@ -72,6 +71,21 @@ def merge_annot_lists(a, b, annot_type):
|
|||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def safe_timestamp_sort_key(x):
|
||||||
|
# ensure we return a string, so python 3 does not barf
|
||||||
|
# also if the timestamp is a datetime instance convert it to
|
||||||
|
# a string, since we expect it to always be a string
|
||||||
|
ans = x.get('timestamp')
|
||||||
|
if hasattr(ans, 'isoformat'):
|
||||||
|
ans = x['timestamp'] = ans.isoformat()
|
||||||
|
if not isinstance(ans, str):
|
||||||
|
try:
|
||||||
|
ans = str(ans)
|
||||||
|
except Exception:
|
||||||
|
ans = 'zzzz'
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def merge_annotations(annots, annots_map, merge_last_read=True):
|
def merge_annotations(annots, annots_map, merge_last_read=True):
|
||||||
# If you make changes to this algorithm also update the
|
# If you make changes to this algorithm also update the
|
||||||
# implementation in read_book.annotations
|
# implementation in read_book.annotations
|
||||||
@ -89,7 +103,7 @@ def merge_annotations(annots, annots_map, merge_last_read=True):
|
|||||||
if existing:
|
if existing:
|
||||||
lr = existing + lr
|
lr = existing + lr
|
||||||
if lr:
|
if lr:
|
||||||
lr.sort(key=itemgetter('timestamp'), reverse=True)
|
lr.sort(key=safe_timestamp_sort_key, reverse=True)
|
||||||
annots_map['last-read'] = [lr[0]]
|
annots_map['last-read'] = [lr[0]]
|
||||||
|
|
||||||
for annot_type, field in merge_field_map.items():
|
for annot_type, field in merge_field_map.items():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user