예제 #1
0
 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
예제 #2
0
파일: bookmarks.py 프로젝트: yzz-00/calibre
    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
예제 #3
0
    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)
예제 #4
0
파일: tests.py 프로젝트: AtulKumar2/calibre
 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)
예제 #5
0
 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)
예제 #6
0
    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)
예제 #7
0
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)
예제 #8
0
파일: bookmarks.py 프로젝트: yzz-00/calibre
 def pos_key(b):
     if b.get('pos_type') == 'epubcfi':
         return cfi_sort_key(b['pos'], only_path=False)
     return defval
예제 #9
0
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
예제 #10
0
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
예제 #11
0
#!/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
예제 #12
0
 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
예제 #13
0
 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
예제 #14
0
 def pos_key(b):
     if b.get('type', None) == 'cfi':
         return b['spine'], cfi_sort_key(b['pos'])
     return (None, None)
예제 #15
0
 def pos_key(b):
     if b.get('type', None) == 'cfi':
         return b['spine'], cfi_sort_key(b['pos'])
     return (None, None)