def load_book_annotations(self, calibre_book_data=None): amap = self.current_book_data['annotations_map'] path = os.path.join(self.current_book_data['base'], 'calibre-book-annotations.json') if os.path.exists(path): with open(path, 'rb') as f: raw = f.read() merge_annotations(parse_annotations(raw), amap) path = os.path.join(annotations_dir, self.current_book_data['annotations_path_key']) if os.path.exists(path): with open(path, 'rb') as f: raw = f.read() merge_annotations(parse_annotations(raw), amap) if calibre_book_data is None: bld = self.current_book_data['book_library_details'] if bld is not None: lib_amap = load_annotations_map_from_library(bld) sau = get_session_pref('sync_annots_user', default='') if sau: other_amap = load_annotations_map_from_library(bld, user_type='web', user=sau) if other_amap: merge_annotations(other_amap, lib_amap) if lib_amap: for annot_type, annots in iteritems(lib_amap): merge_annotations(annots, amap) else: for annot_type, annots in iteritems(calibre_book_data['annotations_map']): merge_annotations(annots, amap)
def calibre_book_data(self, book_id, fmt): from calibre.db.annotations import merge_annotations from calibre.gui2.viewer.config import get_session_pref, vprefs vprefs.refresh() sync_annots_user = get_session_pref('sync_annots_user', default='') db = self.gui.current_db.new_api annotations_map = db.annotations_map_for_book(book_id, fmt) if sync_annots_user: other_annotations_map = db.annotations_map_for_book( book_id, fmt, user_type='web', user=sync_annots_user) if other_annotations_map: merge_annotations(other_annotations_map, annotations_map, merge_last_read=False) return { 'book_id': book_id, 'uuid': db.field_for('uuid', book_id), 'fmt': fmt.upper(), 'annotations_map': annotations_map, 'library_id': getattr(self.gui.current_db.new_api, 'server_library_id', None) }
def save_annotations_list_to_library(book_library_details, alist, sync_annots_user=''): import apsw from calibre.db.backend import save_annotations_for_book, Connection, annotations_for_book from calibre.gui2.viewer.annotations import annotations_as_copied_list from calibre.db.annotations import merge_annotations dbpath = book_library_details['dbpath'] try: conn = apsw.Connection(dbpath, flags=apsw.SQLITE_OPEN_READWRITE) except Exception: return try: conn.setbusytimeout(Connection.BUSY_TIMEOUT) if not database_has_annotations_support(conn.cursor()): return amap = {} with conn: cursor = conn.cursor() for annot in annotations_for_book(cursor, book_library_details['book_id'], book_library_details['fmt']): amap.setdefault(annot['type'], []).append(annot) merge_annotations((x[0] for x in alist), amap) if sync_annots_user: other_amap = {} for annot in annotations_for_book( cursor, book_library_details['book_id'], book_library_details['fmt'], user_type='web', user=sync_annots_user): other_amap.setdefault(annot['type'], []).append(annot) merge_annotations(amap, other_amap) alist = tuple(annotations_as_copied_list(amap)) save_annotations_for_book(cursor, book_library_details['book_id'], book_library_details['fmt'], alist) if sync_annots_user: alist = tuple(annotations_as_copied_list(other_amap)) save_annotations_for_book(cursor, book_library_details['book_id'], book_library_details['fmt'], alist, user_type='web', user=sync_annots_user) finally: conn.close()