def sort_key(x): x = x['annotation'] atype = x['type'] if atype == 'highlight': cfi = x.get('start_cfi') if cfi: spine_idx = x.get('spine_index', def_spine) cfi = f'/{spine_idx}{cfi}' return cfi_sort_key(cfi) elif atype == 'bookmark': if x.get('pos_type') == 'epubcfi': return cfi_sort_key(x['pos'], only_path=False) return defval
def set_bookmarks(self, bookmarks=()): csb = self.current_sort_by if csb == 'name': sk = lambda x: sort_key(x['title']) elif csb == 'timestamp': sk = itemgetter('timestamp') else: from calibre.ebooks.epub.cfi.parse import cfi_sort_key defval = cfi_sort_key('/99999999') def pos_key(b): if b.get('pos_type') == 'epubcfi': return cfi_sort_key(b['pos'], only_path=False) return defval sk = pos_key bookmarks = sorted(bookmarks, key=sk) current_bookmark_id = self.current_bookmark_id self.bookmarks_list.clear() for bm in bookmarks: i = QListWidgetItem(bm['title']) i.setData(Qt.UserRole, self.bm_to_item(bm)) i.setFlags(i.flags() | Qt.ItemIsEditable) self.bookmarks_list.addItem(i) if bm.get('removed'): i.setHidden(True) for i in range(self.bookmarks_list.count()): item = self.bookmarks_list.item(i) if not item.isHidden(): self.bookmarks_list.setCurrentItem( item, QItemSelectionModel.ClearAndSelect) break if current_bookmark_id is not None: self.current_bookmark_id = current_bookmark_id
def sorted_highlights(self, highlights): defval = 999999999999999, cfi_sort_key('/99999999') def cfi_key(h): cfi = h.get('start_cfi') return (h.get('spine_index') or defval[0], cfi_sort_key(cfi)) if cfi else defval return sorted(highlights, key=cfi_key)
def test_sorting(self): null_offsets = (0, (0, 0), 0) for path, key in [ ('/1/2/3', ((1, 2, 3), null_offsets)), ('/1[id]:34[yyyy]', ((1,), (0, (0, 0), 34))), ('/1@1:2', ((1,), (0, (2, 1), 0))), ('/1~1.2', ((1,), (1.2, (0, 0), 0))), ]: self.assertEqual(cfi_sort_key(path), key)
def test_sorting(self): null_offsets = (0, (0, 0), 0) for path, key in [ ('/1/2/3', ((1, 2, 3), null_offsets)), ('/1[id]:34[yyyy]', ((1, ), (0, (0, 0), 34))), ('/1@1:2', ((1, ), (0, (2, 1), 0))), ('/1~1.2', ((1, ), (1.2, (0, 0), 0))), ]: self.assertEqual(cfi_sort_key(path), key)
def sorted_highlights(self, highlights): def_idx = 999999999999999 defval = def_idx, cfi_sort_key('/99999999') def cfi_key(h): cfi = h.get('start_cfi') si = h.get('spine_index', def_idx) return (si, cfi_sort_key(cfi)) if cfi else defval return sorted(highlights, key=cfi_key)
def sorted_items(items): from calibre.ebooks.epub.cfi.parse import cfi_sort_key def_spine = 999999999 defval = cfi_sort_key(f'/{def_spine}') def sort_key(x): x = x['annotation'] atype = x['type'] if atype == 'highlight': cfi = x.get('start_cfi') if cfi: spine_idx = x.get('spine_index', def_spine) cfi = f'/{spine_idx}{cfi}' return cfi_sort_key(cfi) elif atype == 'bookmark': if x.get('pos_type') == 'epubcfi': return cfi_sort_key(x['pos'], only_path=False) return defval return sorted(items, key=sort_key)
def pos_key(b): if b.get('pos_type') == 'epubcfi': return cfi_sort_key(b['pos'], only_path=False) return defval
def highlight_sort_key(hl): cfi = hl.get('start_cfi') if cfi: return cfi_sort_key(cfi, only_path=False) return no_cfi_sort_key
def bookmark_sort_key(b): if b.get('pos_type') == 'epubcfi': return cfi_sort_key(b['pos'], only_path=False) return no_cfi_sort_key
#!/usr/bin/env python # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> from collections import defaultdict from itertools import chain from operator import itemgetter from calibre.ebooks.epub.cfi.parse import cfi_sort_key from polyglot.builtins import itervalues no_cfi_sort_key = cfi_sort_key('/99999999') def bookmark_sort_key(b): if b.get('pos_type') == 'epubcfi': return cfi_sort_key(b['pos'], only_path=False) return no_cfi_sort_key def highlight_sort_key(hl): cfi = hl.get('start_cfi') if cfi: return cfi_sort_key(cfi, only_path=False) return no_cfi_sort_key def sort_annot_list_by_position_in_book(annots, annot_type): annots.sort(key={ 'bookmark': bookmark_sort_key, 'highlight': highlight_sort_key
def cfi_key(h): cfi = h.get('start_cfi') return (h.get('spine_index') or defval[0], cfi_sort_key(cfi)) if cfi else defval
def cfi_key(h): cfi = h.get('start_cfi') si = h.get('spine_index', def_idx) return (si, cfi_sort_key(cfi)) if cfi else defval
def pos_key(b): if b.get('type', None) == 'cfi': return b['spine'], cfi_sort_key(b['pos']) return (None, None)